preconfig.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. #include "Python.h"
  2. #include "pycore_fileutils.h" // DECODE_LOCALE_ERR
  3. #include "pycore_getopt.h" // _PyOS_GetOpt()
  4. #include "pycore_initconfig.h" // _PyArgv
  5. #include "pycore_pymem.h" // _PyMem_GetAllocatorName()
  6. #include "pycore_runtime.h" // _PyRuntime_Initialize()
  7. #include <locale.h> // setlocale()
  8. #include <stdlib.h> // getenv()
  9. /* Forward declarations */
  10. static void
  11. preconfig_copy(PyPreConfig *config, const PyPreConfig *config2);
  12. /* --- File system encoding/errors -------------------------------- */
  13. const char *Py_FileSystemDefaultEncoding = NULL;
  14. int Py_HasFileSystemDefaultEncoding = 0;
  15. const char *Py_FileSystemDefaultEncodeErrors = NULL;
  16. int _Py_HasFileSystemDefaultEncodeErrors = 0;
  17. void
  18. _Py_ClearFileSystemEncoding(void)
  19. {
  20. _Py_COMP_DIAG_PUSH
  21. _Py_COMP_DIAG_IGNORE_DEPR_DECLS
  22. if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
  23. PyMem_RawFree((char*)Py_FileSystemDefaultEncoding);
  24. Py_FileSystemDefaultEncoding = NULL;
  25. }
  26. if (!_Py_HasFileSystemDefaultEncodeErrors && Py_FileSystemDefaultEncodeErrors) {
  27. PyMem_RawFree((char*)Py_FileSystemDefaultEncodeErrors);
  28. Py_FileSystemDefaultEncodeErrors = NULL;
  29. }
  30. _Py_COMP_DIAG_POP
  31. }
  32. /* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors
  33. global configuration variables to PyConfig.filesystem_encoding and
  34. PyConfig.filesystem_errors (encoded to UTF-8).
  35. Function called by _PyUnicode_InitEncodings(). */
  36. int
  37. _Py_SetFileSystemEncoding(const char *encoding, const char *errors)
  38. {
  39. char *encoding2 = _PyMem_RawStrdup(encoding);
  40. if (encoding2 == NULL) {
  41. return -1;
  42. }
  43. char *errors2 = _PyMem_RawStrdup(errors);
  44. if (errors2 == NULL) {
  45. PyMem_RawFree(encoding2);
  46. return -1;
  47. }
  48. _Py_ClearFileSystemEncoding();
  49. _Py_COMP_DIAG_PUSH
  50. _Py_COMP_DIAG_IGNORE_DEPR_DECLS
  51. Py_FileSystemDefaultEncoding = encoding2;
  52. Py_HasFileSystemDefaultEncoding = 0;
  53. Py_FileSystemDefaultEncodeErrors = errors2;
  54. _Py_HasFileSystemDefaultEncodeErrors = 0;
  55. _Py_COMP_DIAG_POP
  56. return 0;
  57. }
  58. /* --- _PyArgv ---------------------------------------------------- */
  59. /* Decode bytes_argv using Py_DecodeLocale() */
  60. PyStatus
  61. _PyArgv_AsWstrList(const _PyArgv *args, PyWideStringList *list)
  62. {
  63. PyWideStringList wargv = _PyWideStringList_INIT;
  64. if (args->use_bytes_argv) {
  65. size_t size = sizeof(wchar_t*) * args->argc;
  66. wargv.items = (wchar_t **)PyMem_RawMalloc(size);
  67. if (wargv.items == NULL) {
  68. return _PyStatus_NO_MEMORY();
  69. }
  70. for (Py_ssize_t i = 0; i < args->argc; i++) {
  71. size_t len;
  72. wchar_t *arg = Py_DecodeLocale(args->bytes_argv[i], &len);
  73. if (arg == NULL) {
  74. _PyWideStringList_Clear(&wargv);
  75. return DECODE_LOCALE_ERR("command line arguments", len);
  76. }
  77. wargv.items[i] = arg;
  78. wargv.length++;
  79. }
  80. _PyWideStringList_Clear(list);
  81. *list = wargv;
  82. }
  83. else {
  84. wargv.length = args->argc;
  85. wargv.items = (wchar_t **)args->wchar_argv;
  86. if (_PyWideStringList_Copy(list, &wargv) < 0) {
  87. return _PyStatus_NO_MEMORY();
  88. }
  89. }
  90. return _PyStatus_OK();
  91. }
  92. /* --- _PyPreCmdline ------------------------------------------------- */
  93. void
  94. _PyPreCmdline_Clear(_PyPreCmdline *cmdline)
  95. {
  96. _PyWideStringList_Clear(&cmdline->argv);
  97. _PyWideStringList_Clear(&cmdline->xoptions);
  98. }
  99. PyStatus
  100. _PyPreCmdline_SetArgv(_PyPreCmdline *cmdline, const _PyArgv *args)
  101. {
  102. return _PyArgv_AsWstrList(args, &cmdline->argv);
  103. }
  104. static void
  105. precmdline_get_preconfig(_PyPreCmdline *cmdline, const PyPreConfig *config)
  106. {
  107. #define COPY_ATTR(ATTR) \
  108. if (config->ATTR != -1) { \
  109. cmdline->ATTR = config->ATTR; \
  110. }
  111. COPY_ATTR(isolated);
  112. COPY_ATTR(use_environment);
  113. COPY_ATTR(dev_mode);
  114. #undef COPY_ATTR
  115. }
  116. static void
  117. precmdline_set_preconfig(const _PyPreCmdline *cmdline, PyPreConfig *config)
  118. {
  119. #define COPY_ATTR(ATTR) \
  120. config->ATTR = cmdline->ATTR
  121. COPY_ATTR(isolated);
  122. COPY_ATTR(use_environment);
  123. COPY_ATTR(dev_mode);
  124. #undef COPY_ATTR
  125. }
  126. PyStatus
  127. _PyPreCmdline_SetConfig(const _PyPreCmdline *cmdline, PyConfig *config)
  128. {
  129. #define COPY_ATTR(ATTR) \
  130. config->ATTR = cmdline->ATTR
  131. PyStatus status = _PyWideStringList_Extend(&config->xoptions, &cmdline->xoptions);
  132. if (_PyStatus_EXCEPTION(status)) {
  133. return status;
  134. }
  135. COPY_ATTR(isolated);
  136. COPY_ATTR(use_environment);
  137. COPY_ATTR(dev_mode);
  138. COPY_ATTR(warn_default_encoding);
  139. return _PyStatus_OK();
  140. #undef COPY_ATTR
  141. }
  142. /* Parse the command line arguments */
  143. static PyStatus
  144. precmdline_parse_cmdline(_PyPreCmdline *cmdline)
  145. {
  146. const PyWideStringList *argv = &cmdline->argv;
  147. _PyOS_ResetGetOpt();
  148. /* Don't log parsing errors into stderr here: PyConfig_Read()
  149. is responsible for that */
  150. _PyOS_opterr = 0;
  151. do {
  152. int longindex = -1;
  153. int c = _PyOS_GetOpt(argv->length, argv->items, &longindex);
  154. if (c == EOF || c == 'c' || c == 'm') {
  155. break;
  156. }
  157. switch (c) {
  158. case 'E':
  159. cmdline->use_environment = 0;
  160. break;
  161. case 'I':
  162. cmdline->isolated = 1;
  163. break;
  164. case 'X':
  165. {
  166. PyStatus status = PyWideStringList_Append(&cmdline->xoptions,
  167. _PyOS_optarg);
  168. if (_PyStatus_EXCEPTION(status)) {
  169. return status;
  170. }
  171. break;
  172. }
  173. default:
  174. /* ignore other argument:
  175. handled by PyConfig_Read() */
  176. break;
  177. }
  178. } while (1);
  179. return _PyStatus_OK();
  180. }
  181. PyStatus
  182. _PyPreCmdline_Read(_PyPreCmdline *cmdline, const PyPreConfig *preconfig)
  183. {
  184. precmdline_get_preconfig(cmdline, preconfig);
  185. if (preconfig->parse_argv) {
  186. PyStatus status = precmdline_parse_cmdline(cmdline);
  187. if (_PyStatus_EXCEPTION(status)) {
  188. return status;
  189. }
  190. }
  191. /* isolated, use_environment */
  192. if (cmdline->isolated < 0) {
  193. cmdline->isolated = 0;
  194. }
  195. if (cmdline->isolated > 0) {
  196. cmdline->use_environment = 0;
  197. }
  198. if (cmdline->use_environment < 0) {
  199. cmdline->use_environment = 0;
  200. }
  201. /* dev_mode */
  202. if ((cmdline->dev_mode < 0)
  203. && (_Py_get_xoption(&cmdline->xoptions, L"dev")
  204. || _Py_GetEnv(cmdline->use_environment, "PYTHONDEVMODE")))
  205. {
  206. cmdline->dev_mode = 1;
  207. }
  208. if (cmdline->dev_mode < 0) {
  209. cmdline->dev_mode = 0;
  210. }
  211. // warn_default_encoding
  212. if (_Py_get_xoption(&cmdline->xoptions, L"warn_default_encoding")
  213. || _Py_GetEnv(cmdline->use_environment, "PYTHONWARNDEFAULTENCODING"))
  214. {
  215. cmdline->warn_default_encoding = 1;
  216. }
  217. assert(cmdline->use_environment >= 0);
  218. assert(cmdline->isolated >= 0);
  219. assert(cmdline->dev_mode >= 0);
  220. assert(cmdline->warn_default_encoding >= 0);
  221. return _PyStatus_OK();
  222. }
  223. /* --- PyPreConfig ----------------------------------------------- */
  224. void
  225. _PyPreConfig_InitCompatConfig(PyPreConfig *config)
  226. {
  227. memset(config, 0, sizeof(*config));
  228. config->_config_init = (int)_PyConfig_INIT_COMPAT;
  229. config->parse_argv = 0;
  230. config->isolated = -1;
  231. config->use_environment = -1;
  232. config->configure_locale = 1;
  233. /* bpo-36443: C locale coercion (PEP 538) and UTF-8 Mode (PEP 540)
  234. are disabled by default using the Compat configuration.
  235. Py_UTF8Mode=1 enables the UTF-8 mode. PYTHONUTF8 environment variable
  236. is ignored (even if use_environment=1). */
  237. config->utf8_mode = 0;
  238. config->coerce_c_locale = 0;
  239. config->coerce_c_locale_warn = 0;
  240. config->dev_mode = -1;
  241. config->allocator = PYMEM_ALLOCATOR_NOT_SET;
  242. #ifdef MS_WINDOWS
  243. config->legacy_windows_fs_encoding = -1;
  244. #endif
  245. }
  246. void
  247. PyPreConfig_InitPythonConfig(PyPreConfig *config)
  248. {
  249. _PyPreConfig_InitCompatConfig(config);
  250. config->_config_init = (int)_PyConfig_INIT_PYTHON;
  251. config->isolated = 0;
  252. config->parse_argv = 1;
  253. config->use_environment = 1;
  254. /* Set to -1 to enable C locale coercion (PEP 538) and UTF-8 Mode (PEP 540)
  255. depending on the LC_CTYPE locale, PYTHONUTF8 and PYTHONCOERCECLOCALE
  256. environment variables. */
  257. config->coerce_c_locale = -1;
  258. config->coerce_c_locale_warn = -1;
  259. config->utf8_mode = -1;
  260. #ifdef MS_WINDOWS
  261. config->legacy_windows_fs_encoding = 0;
  262. #endif
  263. }
  264. void
  265. PyPreConfig_InitIsolatedConfig(PyPreConfig *config)
  266. {
  267. _PyPreConfig_InitCompatConfig(config);
  268. config->_config_init = (int)_PyConfig_INIT_ISOLATED;
  269. config->configure_locale = 0;
  270. config->isolated = 1;
  271. config->use_environment = 0;
  272. config->utf8_mode = 0;
  273. config->dev_mode = 0;
  274. #ifdef MS_WINDOWS
  275. config->legacy_windows_fs_encoding = 0;
  276. #endif
  277. }
  278. PyStatus
  279. _PyPreConfig_InitFromPreConfig(PyPreConfig *config,
  280. const PyPreConfig *config2)
  281. {
  282. PyPreConfig_InitPythonConfig(config);
  283. preconfig_copy(config, config2);
  284. return _PyStatus_OK();
  285. }
  286. void
  287. _PyPreConfig_InitFromConfig(PyPreConfig *preconfig, const PyConfig *config)
  288. {
  289. _PyConfigInitEnum config_init = (_PyConfigInitEnum)config->_config_init;
  290. switch (config_init) {
  291. case _PyConfig_INIT_PYTHON:
  292. PyPreConfig_InitPythonConfig(preconfig);
  293. break;
  294. case _PyConfig_INIT_ISOLATED:
  295. PyPreConfig_InitIsolatedConfig(preconfig);
  296. break;
  297. case _PyConfig_INIT_COMPAT:
  298. default:
  299. _PyPreConfig_InitCompatConfig(preconfig);
  300. }
  301. _PyPreConfig_GetConfig(preconfig, config);
  302. }
  303. static void
  304. preconfig_copy(PyPreConfig *config, const PyPreConfig *config2)
  305. {
  306. #define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
  307. COPY_ATTR(_config_init);
  308. COPY_ATTR(parse_argv);
  309. COPY_ATTR(isolated);
  310. COPY_ATTR(use_environment);
  311. COPY_ATTR(configure_locale);
  312. COPY_ATTR(dev_mode);
  313. COPY_ATTR(coerce_c_locale);
  314. COPY_ATTR(coerce_c_locale_warn);
  315. COPY_ATTR(utf8_mode);
  316. COPY_ATTR(allocator);
  317. #ifdef MS_WINDOWS
  318. COPY_ATTR(legacy_windows_fs_encoding);
  319. #endif
  320. #undef COPY_ATTR
  321. }
  322. PyObject*
  323. _PyPreConfig_AsDict(const PyPreConfig *config)
  324. {
  325. PyObject *dict;
  326. dict = PyDict_New();
  327. if (dict == NULL) {
  328. return NULL;
  329. }
  330. #define SET_ITEM_INT(ATTR) \
  331. do { \
  332. PyObject *obj = PyLong_FromLong(config->ATTR); \
  333. if (obj == NULL) { \
  334. goto fail; \
  335. } \
  336. int res = PyDict_SetItemString(dict, #ATTR, obj); \
  337. Py_DECREF(obj); \
  338. if (res < 0) { \
  339. goto fail; \
  340. } \
  341. } while (0)
  342. SET_ITEM_INT(_config_init);
  343. SET_ITEM_INT(parse_argv);
  344. SET_ITEM_INT(isolated);
  345. SET_ITEM_INT(use_environment);
  346. SET_ITEM_INT(configure_locale);
  347. SET_ITEM_INT(coerce_c_locale);
  348. SET_ITEM_INT(coerce_c_locale_warn);
  349. SET_ITEM_INT(utf8_mode);
  350. #ifdef MS_WINDOWS
  351. SET_ITEM_INT(legacy_windows_fs_encoding);
  352. #endif
  353. SET_ITEM_INT(dev_mode);
  354. SET_ITEM_INT(allocator);
  355. return dict;
  356. fail:
  357. Py_DECREF(dict);
  358. return NULL;
  359. #undef SET_ITEM_INT
  360. }
  361. void
  362. _PyPreConfig_GetConfig(PyPreConfig *preconfig, const PyConfig *config)
  363. {
  364. #define COPY_ATTR(ATTR) \
  365. if (config->ATTR != -1) { \
  366. preconfig->ATTR = config->ATTR; \
  367. }
  368. COPY_ATTR(parse_argv);
  369. COPY_ATTR(isolated);
  370. COPY_ATTR(use_environment);
  371. COPY_ATTR(dev_mode);
  372. #undef COPY_ATTR
  373. }
  374. static void
  375. preconfig_get_global_vars(PyPreConfig *config)
  376. {
  377. if (config->_config_init != _PyConfig_INIT_COMPAT) {
  378. /* Python and Isolated configuration ignore global variables */
  379. return;
  380. }
  381. #define COPY_FLAG(ATTR, VALUE) \
  382. if (config->ATTR < 0) { \
  383. config->ATTR = VALUE; \
  384. }
  385. #define COPY_NOT_FLAG(ATTR, VALUE) \
  386. if (config->ATTR < 0) { \
  387. config->ATTR = !(VALUE); \
  388. }
  389. _Py_COMP_DIAG_PUSH
  390. _Py_COMP_DIAG_IGNORE_DEPR_DECLS
  391. COPY_FLAG(isolated, Py_IsolatedFlag);
  392. COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
  393. if (Py_UTF8Mode > 0) {
  394. config->utf8_mode = Py_UTF8Mode;
  395. }
  396. #ifdef MS_WINDOWS
  397. COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
  398. #endif
  399. _Py_COMP_DIAG_POP
  400. #undef COPY_FLAG
  401. #undef COPY_NOT_FLAG
  402. }
  403. static void
  404. preconfig_set_global_vars(const PyPreConfig *config)
  405. {
  406. #define COPY_FLAG(ATTR, VAR) \
  407. if (config->ATTR >= 0) { \
  408. VAR = config->ATTR; \
  409. }
  410. #define COPY_NOT_FLAG(ATTR, VAR) \
  411. if (config->ATTR >= 0) { \
  412. VAR = !config->ATTR; \
  413. }
  414. _Py_COMP_DIAG_PUSH
  415. _Py_COMP_DIAG_IGNORE_DEPR_DECLS
  416. COPY_FLAG(isolated, Py_IsolatedFlag);
  417. COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
  418. #ifdef MS_WINDOWS
  419. COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
  420. #endif
  421. COPY_FLAG(utf8_mode, Py_UTF8Mode);
  422. _Py_COMP_DIAG_POP
  423. #undef COPY_FLAG
  424. #undef COPY_NOT_FLAG
  425. }
  426. const char*
  427. _Py_GetEnv(int use_environment, const char *name)
  428. {
  429. assert(use_environment >= 0);
  430. if (!use_environment) {
  431. return NULL;
  432. }
  433. const char *var = getenv(name);
  434. if (var && var[0] != '\0') {
  435. return var;
  436. }
  437. else {
  438. return NULL;
  439. }
  440. }
  441. int
  442. _Py_str_to_int(const char *str, int *result)
  443. {
  444. const char *endptr = str;
  445. errno = 0;
  446. long value = strtol(str, (char **)&endptr, 10);
  447. if (*endptr != '\0' || errno == ERANGE) {
  448. return -1;
  449. }
  450. if (value < INT_MIN || value > INT_MAX) {
  451. return -1;
  452. }
  453. *result = (int)value;
  454. return 0;
  455. }
  456. void
  457. _Py_get_env_flag(int use_environment, int *flag, const char *name)
  458. {
  459. const char *var = _Py_GetEnv(use_environment, name);
  460. if (!var) {
  461. return;
  462. }
  463. int value;
  464. if (_Py_str_to_int(var, &value) < 0 || value < 0) {
  465. /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
  466. value = 1;
  467. }
  468. if (*flag < value) {
  469. *flag = value;
  470. }
  471. }
  472. const wchar_t*
  473. _Py_get_xoption(const PyWideStringList *xoptions, const wchar_t *name)
  474. {
  475. for (Py_ssize_t i=0; i < xoptions->length; i++) {
  476. const wchar_t *option = xoptions->items[i];
  477. size_t len;
  478. wchar_t *sep = wcschr(option, L'=');
  479. if (sep != NULL) {
  480. len = (sep - option);
  481. }
  482. else {
  483. len = wcslen(option);
  484. }
  485. if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
  486. return option;
  487. }
  488. }
  489. return NULL;
  490. }
  491. static PyStatus
  492. preconfig_init_utf8_mode(PyPreConfig *config, const _PyPreCmdline *cmdline)
  493. {
  494. #ifdef MS_WINDOWS
  495. if (config->legacy_windows_fs_encoding) {
  496. config->utf8_mode = 0;
  497. }
  498. #endif
  499. if (config->utf8_mode >= 0) {
  500. return _PyStatus_OK();
  501. }
  502. const wchar_t *xopt;
  503. xopt = _Py_get_xoption(&cmdline->xoptions, L"utf8");
  504. if (xopt) {
  505. wchar_t *sep = wcschr(xopt, L'=');
  506. if (sep) {
  507. xopt = sep + 1;
  508. if (wcscmp(xopt, L"1") == 0) {
  509. config->utf8_mode = 1;
  510. }
  511. else if (wcscmp(xopt, L"0") == 0) {
  512. config->utf8_mode = 0;
  513. }
  514. else {
  515. return _PyStatus_ERR("invalid -X utf8 option value");
  516. }
  517. }
  518. else {
  519. config->utf8_mode = 1;
  520. }
  521. return _PyStatus_OK();
  522. }
  523. const char *opt = _Py_GetEnv(config->use_environment, "PYTHONUTF8");
  524. if (opt) {
  525. if (strcmp(opt, "1") == 0) {
  526. config->utf8_mode = 1;
  527. }
  528. else if (strcmp(opt, "0") == 0) {
  529. config->utf8_mode = 0;
  530. }
  531. else {
  532. return _PyStatus_ERR("invalid PYTHONUTF8 environment "
  533. "variable value");
  534. }
  535. return _PyStatus_OK();
  536. }
  537. #ifndef MS_WINDOWS
  538. if (config->utf8_mode < 0) {
  539. /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */
  540. const char *ctype_loc = setlocale(LC_CTYPE, NULL);
  541. if (ctype_loc != NULL
  542. && (strcmp(ctype_loc, "C") == 0
  543. || strcmp(ctype_loc, "POSIX") == 0))
  544. {
  545. config->utf8_mode = 1;
  546. }
  547. }
  548. #endif
  549. if (config->utf8_mode < 0) {
  550. config->utf8_mode = 0;
  551. }
  552. return _PyStatus_OK();
  553. }
  554. static void
  555. preconfig_init_coerce_c_locale(PyPreConfig *config)
  556. {
  557. if (!config->configure_locale) {
  558. config->coerce_c_locale = 0;
  559. config->coerce_c_locale_warn = 0;
  560. return;
  561. }
  562. const char *env = _Py_GetEnv(config->use_environment, "PYTHONCOERCECLOCALE");
  563. if (env) {
  564. if (strcmp(env, "0") == 0) {
  565. if (config->coerce_c_locale < 0) {
  566. config->coerce_c_locale = 0;
  567. }
  568. }
  569. else if (strcmp(env, "warn") == 0) {
  570. if (config->coerce_c_locale_warn < 0) {
  571. config->coerce_c_locale_warn = 1;
  572. }
  573. }
  574. else {
  575. if (config->coerce_c_locale < 0) {
  576. config->coerce_c_locale = 1;
  577. }
  578. }
  579. }
  580. /* Test if coerce_c_locale equals to -1 or equals to 1:
  581. PYTHONCOERCECLOCALE=1 doesn't imply that the C locale is always coerced.
  582. It is only coerced if if the LC_CTYPE locale is "C". */
  583. if (config->coerce_c_locale < 0 || config->coerce_c_locale == 1) {
  584. /* The C locale enables the C locale coercion (PEP 538) */
  585. if (_Py_LegacyLocaleDetected(0)) {
  586. config->coerce_c_locale = 2;
  587. }
  588. else {
  589. config->coerce_c_locale = 0;
  590. }
  591. }
  592. if (config->coerce_c_locale_warn < 0) {
  593. config->coerce_c_locale_warn = 0;
  594. }
  595. }
  596. static PyStatus
  597. preconfig_init_allocator(PyPreConfig *config)
  598. {
  599. if (config->allocator == PYMEM_ALLOCATOR_NOT_SET) {
  600. /* bpo-34247. The PYTHONMALLOC environment variable has the priority
  601. over PYTHONDEV env var and "-X dev" command line option.
  602. For example, PYTHONMALLOC=malloc PYTHONDEVMODE=1 sets the memory
  603. allocators to "malloc" (and not to "debug"). */
  604. const char *envvar = _Py_GetEnv(config->use_environment, "PYTHONMALLOC");
  605. if (envvar) {
  606. PyMemAllocatorName name;
  607. if (_PyMem_GetAllocatorName(envvar, &name) < 0) {
  608. return _PyStatus_ERR("PYTHONMALLOC: unknown allocator");
  609. }
  610. config->allocator = (int)name;
  611. }
  612. }
  613. if (config->dev_mode && config->allocator == PYMEM_ALLOCATOR_NOT_SET) {
  614. config->allocator = PYMEM_ALLOCATOR_DEBUG;
  615. }
  616. return _PyStatus_OK();
  617. }
  618. static PyStatus
  619. preconfig_read(PyPreConfig *config, _PyPreCmdline *cmdline)
  620. {
  621. PyStatus status;
  622. status = _PyPreCmdline_Read(cmdline, config);
  623. if (_PyStatus_EXCEPTION(status)) {
  624. return status;
  625. }
  626. precmdline_set_preconfig(cmdline, config);
  627. /* legacy_windows_fs_encoding, coerce_c_locale, utf8_mode */
  628. #ifdef MS_WINDOWS
  629. _Py_get_env_flag(config->use_environment,
  630. &config->legacy_windows_fs_encoding,
  631. "PYTHONLEGACYWINDOWSFSENCODING");
  632. #endif
  633. preconfig_init_coerce_c_locale(config);
  634. status = preconfig_init_utf8_mode(config, cmdline);
  635. if (_PyStatus_EXCEPTION(status)) {
  636. return status;
  637. }
  638. /* allocator */
  639. status = preconfig_init_allocator(config);
  640. if (_PyStatus_EXCEPTION(status)) {
  641. return status;
  642. }
  643. assert(config->coerce_c_locale >= 0);
  644. assert(config->coerce_c_locale_warn >= 0);
  645. #ifdef MS_WINDOWS
  646. assert(config->legacy_windows_fs_encoding >= 0);
  647. #endif
  648. assert(config->utf8_mode >= 0);
  649. assert(config->isolated >= 0);
  650. assert(config->use_environment >= 0);
  651. assert(config->dev_mode >= 0);
  652. return _PyStatus_OK();
  653. }
  654. /* Read the configuration from:
  655. - command line arguments
  656. - environment variables
  657. - Py_xxx global configuration variables
  658. - the LC_CTYPE locale */
  659. PyStatus
  660. _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
  661. {
  662. PyStatus status;
  663. status = _PyRuntime_Initialize();
  664. if (_PyStatus_EXCEPTION(status)) {
  665. return status;
  666. }
  667. preconfig_get_global_vars(config);
  668. /* Copy LC_CTYPE locale, since it's modified later */
  669. const char *loc = setlocale(LC_CTYPE, NULL);
  670. if (loc == NULL) {
  671. return _PyStatus_ERR("failed to LC_CTYPE locale");
  672. }
  673. char *init_ctype_locale = _PyMem_RawStrdup(loc);
  674. if (init_ctype_locale == NULL) {
  675. return _PyStatus_NO_MEMORY();
  676. }
  677. /* Save the config to be able to restore it if encodings change */
  678. PyPreConfig save_config;
  679. status = _PyPreConfig_InitFromPreConfig(&save_config, config);
  680. if (_PyStatus_EXCEPTION(status)) {
  681. return status;
  682. }
  683. /* Set LC_CTYPE to the user preferred locale */
  684. if (config->configure_locale) {
  685. _Py_SetLocaleFromEnv(LC_CTYPE);
  686. }
  687. PyPreConfig save_runtime_config;
  688. preconfig_copy(&save_runtime_config, &_PyRuntime.preconfig);
  689. _PyPreCmdline cmdline = _PyPreCmdline_INIT;
  690. int locale_coerced = 0;
  691. int loops = 0;
  692. while (1) {
  693. int utf8_mode = config->utf8_mode;
  694. /* Watchdog to prevent an infinite loop */
  695. loops++;
  696. if (loops == 3) {
  697. status = _PyStatus_ERR("Encoding changed twice while "
  698. "reading the configuration");
  699. goto done;
  700. }
  701. /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
  702. on the utf8_mode and legacy_windows_fs_encoding members
  703. of _PyRuntime.preconfig. */
  704. preconfig_copy(&_PyRuntime.preconfig, config);
  705. if (args) {
  706. // Set command line arguments at each iteration. If they are bytes
  707. // strings, they are decoded from the new encoding.
  708. status = _PyPreCmdline_SetArgv(&cmdline, args);
  709. if (_PyStatus_EXCEPTION(status)) {
  710. goto done;
  711. }
  712. }
  713. status = preconfig_read(config, &cmdline);
  714. if (_PyStatus_EXCEPTION(status)) {
  715. goto done;
  716. }
  717. /* The legacy C locale assumes ASCII as the default text encoding, which
  718. * causes problems not only for the CPython runtime, but also other
  719. * components like GNU readline.
  720. *
  721. * Accordingly, when the CLI detects it, it attempts to coerce it to a
  722. * more capable UTF-8 based alternative.
  723. *
  724. * See the documentation of the PYTHONCOERCECLOCALE setting for more
  725. * details.
  726. */
  727. int encoding_changed = 0;
  728. if (config->coerce_c_locale && !locale_coerced) {
  729. locale_coerced = 1;
  730. _Py_CoerceLegacyLocale(0);
  731. encoding_changed = 1;
  732. }
  733. if (utf8_mode == -1) {
  734. if (config->utf8_mode == 1) {
  735. /* UTF-8 Mode enabled */
  736. encoding_changed = 1;
  737. }
  738. }
  739. else {
  740. if (config->utf8_mode != utf8_mode) {
  741. encoding_changed = 1;
  742. }
  743. }
  744. if (!encoding_changed) {
  745. break;
  746. }
  747. /* Reset the configuration before reading again the configuration,
  748. just keep UTF-8 Mode and coerce C locale value. */
  749. int new_utf8_mode = config->utf8_mode;
  750. int new_coerce_c_locale = config->coerce_c_locale;
  751. preconfig_copy(config, &save_config);
  752. config->utf8_mode = new_utf8_mode;
  753. config->coerce_c_locale = new_coerce_c_locale;
  754. /* The encoding changed: read again the configuration
  755. with the new encoding */
  756. }
  757. status = _PyStatus_OK();
  758. done:
  759. // Revert side effects
  760. setlocale(LC_CTYPE, init_ctype_locale);
  761. PyMem_RawFree(init_ctype_locale);
  762. preconfig_copy(&_PyRuntime.preconfig, &save_runtime_config);
  763. _PyPreCmdline_Clear(&cmdline);
  764. return status;
  765. }
  766. /* Write the pre-configuration:
  767. - set the memory allocators
  768. - set Py_xxx global configuration variables
  769. - set the LC_CTYPE locale (coerce C locale, PEP 538) and set the UTF-8 mode
  770. (PEP 540)
  771. The applied configuration is written into _PyRuntime.preconfig.
  772. If the C locale cannot be coerced, set coerce_c_locale to 0.
  773. Do nothing if called after Py_Initialize(): ignore the new
  774. pre-configuration. */
  775. PyStatus
  776. _PyPreConfig_Write(const PyPreConfig *src_config)
  777. {
  778. PyPreConfig config;
  779. PyStatus status = _PyPreConfig_InitFromPreConfig(&config, src_config);
  780. if (_PyStatus_EXCEPTION(status)) {
  781. return status;
  782. }
  783. if (_PyRuntime.core_initialized) {
  784. /* bpo-34008: Calling this functions after Py_Initialize() ignores
  785. the new configuration. */
  786. return _PyStatus_OK();
  787. }
  788. PyMemAllocatorName name = (PyMemAllocatorName)config.allocator;
  789. if (name != PYMEM_ALLOCATOR_NOT_SET) {
  790. if (_PyMem_SetupAllocators(name) < 0) {
  791. return _PyStatus_ERR("Unknown PYTHONMALLOC allocator");
  792. }
  793. }
  794. preconfig_set_global_vars(&config);
  795. if (config.configure_locale) {
  796. if (config.coerce_c_locale) {
  797. if (!_Py_CoerceLegacyLocale(config.coerce_c_locale_warn)) {
  798. /* C locale not coerced */
  799. config.coerce_c_locale = 0;
  800. }
  801. }
  802. /* Set LC_CTYPE to the user preferred locale */
  803. _Py_SetLocaleFromEnv(LC_CTYPE);
  804. }
  805. /* Write the new pre-configuration into _PyRuntime */
  806. preconfig_copy(&_PyRuntime.preconfig, &config);
  807. return _PyStatus_OK();
  808. }