You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2475 lines
70 KiB

  1. #include "Python.h"
  2. #include "osdefs.h" /* DELIM */
  3. #include "pycore_coreconfig.h"
  4. #include "pycore_fileutils.h"
  5. #include "pycore_getopt.h"
  6. #include "pycore_pylifecycle.h"
  7. #include "pycore_pymem.h"
  8. #include "pycore_pathconfig.h"
  9. #include <locale.h> /* setlocale() */
  10. #ifdef HAVE_LANGINFO_H
  11. # include <langinfo.h> /* nl_langinfo(CODESET) */
  12. #endif
  13. #if defined(MS_WINDOWS) || defined(__CYGWIN__)
  14. # include <windows.h> /* GetACP() */
  15. # ifdef HAVE_IO_H
  16. # include <io.h>
  17. # endif
  18. # ifdef HAVE_FCNTL_H
  19. # include <fcntl.h> /* O_BINARY */
  20. # endif
  21. #endif
  22. /* --- Command line options --------------------------------------- */
  23. /* Short usage message (with %s for argv0) */
  24. static const char usage_line[] =
  25. "usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
  26. /* Long usage message, split into parts < 512 bytes */
  27. static const char usage_1[] = "\
  28. Options and arguments (and corresponding environment variables):\n\
  29. -b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
  30. and comparing bytes/bytearray with str. (-bb: issue errors)\n\
  31. -B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
  32. -c cmd : program passed in as string (terminates option list)\n\
  33. -d : debug output from parser; also PYTHONDEBUG=x\n\
  34. -E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
  35. -h : print this help message and exit (also --help)\n\
  36. ";
  37. static const char usage_2[] = "\
  38. -i : inspect interactively after running script; forces a prompt even\n\
  39. if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
  40. -I : isolate Python from the user's environment (implies -E and -s)\n\
  41. -m mod : run library module as a script (terminates option list)\n\
  42. -O : remove assert and __debug__-dependent statements; add .opt-1 before\n\
  43. .pyc extension; also PYTHONOPTIMIZE=x\n\
  44. -OO : do -O changes and also discard docstrings; add .opt-2 before\n\
  45. .pyc extension\n\
  46. -q : don't print version and copyright messages on interactive startup\n\
  47. -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
  48. -S : don't imply 'import site' on initialization\n\
  49. ";
  50. static const char usage_3[] = "\
  51. -u : force the stdout and stderr streams to be unbuffered;\n\
  52. this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
  53. -v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
  54. can be supplied multiple times to increase verbosity\n\
  55. -V : print the Python version number and exit (also --version)\n\
  56. when given twice, print more information about the build\n\
  57. -W arg : warning control; arg is action:message:category:module:lineno\n\
  58. also PYTHONWARNINGS=arg\n\
  59. -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
  60. -X opt : set implementation-specific option\n\
  61. --check-hash-based-pycs always|default|never:\n\
  62. control how Python invalidates hash-based .pyc files\n\
  63. ";
  64. static const char usage_4[] = "\
  65. file : program read from script file\n\
  66. - : program read from stdin (default; interactive mode if a tty)\n\
  67. arg ...: arguments passed to program in sys.argv[1:]\n\n\
  68. Other environment variables:\n\
  69. PYTHONSTARTUP: file executed on interactive startup (no default)\n\
  70. PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\
  71. default module search path. The result is sys.path.\n\
  72. ";
  73. static const char usage_5[] =
  74. "PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
  75. " The default module search path uses %s.\n"
  76. "PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
  77. "PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
  78. "PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
  79. static const char usage_6[] =
  80. "PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
  81. " to seed the hashes of str, bytes and datetime objects. It can also be\n"
  82. " set to an integer in the range [0,4294967295] to get hash values with a\n"
  83. " predictable seed.\n"
  84. "PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
  85. " on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
  86. " hooks.\n"
  87. "PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
  88. " coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
  89. " locale coercion and locale compatibility warnings on stderr.\n"
  90. "PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
  91. " debugger. It can be set to the callable of your debugger of choice.\n"
  92. "PYTHONDEVMODE: enable the development mode.\n"
  93. "PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
  94. #if defined(MS_WINDOWS)
  95. # define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
  96. #else
  97. # define PYTHONHOMEHELP "<prefix>/lib/pythonX.X"
  98. #endif
  99. /* --- Global configuration variables ----------------------------- */
  100. /* UTF-8 mode (PEP 540): if equals to 1, use the UTF-8 encoding, and change
  101. stdin and stdout error handler to "surrogateescape". It is equal to
  102. -1 by default: unknown, will be set by Py_Main() */
  103. int Py_UTF8Mode = -1;
  104. int Py_DebugFlag = 0; /* Needed by parser.c */
  105. int Py_VerboseFlag = 0; /* Needed by import.c */
  106. int Py_QuietFlag = 0; /* Needed by sysmodule.c */
  107. int Py_InteractiveFlag = 0; /* Needed by Py_FdIsInteractive() below */
  108. int Py_InspectFlag = 0; /* Needed to determine whether to exit at SystemExit */
  109. int Py_OptimizeFlag = 0; /* Needed by compile.c */
  110. int Py_NoSiteFlag = 0; /* Suppress 'import site' */
  111. int Py_BytesWarningFlag = 0; /* Warn on str(bytes) and str(buffer) */
  112. int Py_FrozenFlag = 0; /* Needed by getpath.c */
  113. int Py_IgnoreEnvironmentFlag = 0; /* e.g. PYTHONPATH, PYTHONHOME */
  114. int Py_DontWriteBytecodeFlag = 0; /* Suppress writing bytecode files (*.pyc) */
  115. int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
  116. int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
  117. int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
  118. int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */
  119. #ifdef MS_WINDOWS
  120. int Py_LegacyWindowsFSEncodingFlag = 0; /* Uses mbcs instead of utf-8 */
  121. int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */
  122. #endif
  123. PyObject *
  124. _Py_GetGlobalVariablesAsDict(void)
  125. {
  126. PyObject *dict, *obj;
  127. dict = PyDict_New();
  128. if (dict == NULL) {
  129. return NULL;
  130. }
  131. #define SET_ITEM(KEY, EXPR) \
  132. do { \
  133. obj = (EXPR); \
  134. if (obj == NULL) { \
  135. return NULL; \
  136. } \
  137. int res = PyDict_SetItemString(dict, (KEY), obj); \
  138. Py_DECREF(obj); \
  139. if (res < 0) { \
  140. goto fail; \
  141. } \
  142. } while (0)
  143. #define SET_ITEM_INT(VAR) \
  144. SET_ITEM(#VAR, PyLong_FromLong(VAR))
  145. #define FROM_STRING(STR) \
  146. ((STR != NULL) ? \
  147. PyUnicode_FromString(STR) \
  148. : (Py_INCREF(Py_None), Py_None))
  149. #define SET_ITEM_STR(VAR) \
  150. SET_ITEM(#VAR, FROM_STRING(VAR))
  151. SET_ITEM_STR(Py_FileSystemDefaultEncoding);
  152. SET_ITEM_INT(Py_HasFileSystemDefaultEncoding);
  153. SET_ITEM_STR(Py_FileSystemDefaultEncodeErrors);
  154. SET_ITEM_INT(_Py_HasFileSystemDefaultEncodeErrors);
  155. SET_ITEM_INT(Py_UTF8Mode);
  156. SET_ITEM_INT(Py_DebugFlag);
  157. SET_ITEM_INT(Py_VerboseFlag);
  158. SET_ITEM_INT(Py_QuietFlag);
  159. SET_ITEM_INT(Py_InteractiveFlag);
  160. SET_ITEM_INT(Py_InspectFlag);
  161. SET_ITEM_INT(Py_OptimizeFlag);
  162. SET_ITEM_INT(Py_NoSiteFlag);
  163. SET_ITEM_INT(Py_BytesWarningFlag);
  164. SET_ITEM_INT(Py_FrozenFlag);
  165. SET_ITEM_INT(Py_IgnoreEnvironmentFlag);
  166. SET_ITEM_INT(Py_DontWriteBytecodeFlag);
  167. SET_ITEM_INT(Py_NoUserSiteDirectory);
  168. SET_ITEM_INT(Py_UnbufferedStdioFlag);
  169. SET_ITEM_INT(Py_HashRandomizationFlag);
  170. SET_ITEM_INT(Py_IsolatedFlag);
  171. #ifdef MS_WINDOWS
  172. SET_ITEM_INT(Py_LegacyWindowsFSEncodingFlag);
  173. SET_ITEM_INT(Py_LegacyWindowsStdioFlag);
  174. #endif
  175. return dict;
  176. fail:
  177. Py_DECREF(dict);
  178. return NULL;
  179. #undef FROM_STRING
  180. #undef SET_ITEM
  181. #undef SET_ITEM_INT
  182. #undef SET_ITEM_STR
  183. }
  184. /* --- _Py_wstrlist ----------------------------------------------- */
  185. void
  186. _Py_wstrlist_clear(int len, wchar_t **list)
  187. {
  188. for (int i=0; i < len; i++) {
  189. PyMem_RawFree(list[i]);
  190. }
  191. PyMem_RawFree(list);
  192. }
  193. wchar_t**
  194. _Py_wstrlist_copy(int len, wchar_t * const *list)
  195. {
  196. assert((len > 0 && list != NULL) || len == 0);
  197. size_t size = len * sizeof(list[0]);
  198. wchar_t **list_copy = PyMem_RawMalloc(size);
  199. if (list_copy == NULL) {
  200. return NULL;
  201. }
  202. for (int i=0; i < len; i++) {
  203. wchar_t* arg = _PyMem_RawWcsdup(list[i]);
  204. if (arg == NULL) {
  205. _Py_wstrlist_clear(i, list_copy);
  206. return NULL;
  207. }
  208. list_copy[i] = arg;
  209. }
  210. return list_copy;
  211. }
  212. _PyInitError
  213. _Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
  214. {
  215. if (*len == INT_MAX) {
  216. /* len+1 would overflow */
  217. return _Py_INIT_NO_MEMORY();
  218. }
  219. wchar_t *str2 = _PyMem_RawWcsdup(str);
  220. if (str2 == NULL) {
  221. return _Py_INIT_NO_MEMORY();
  222. }
  223. size_t size = (*len + 1) * sizeof(list[0]);
  224. wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
  225. if (list2 == NULL) {
  226. PyMem_RawFree(str2);
  227. return _Py_INIT_NO_MEMORY();
  228. }
  229. list2[*len] = str2;
  230. *list = list2;
  231. (*len)++;
  232. return _Py_INIT_OK();
  233. }
  234. PyObject*
  235. _Py_wstrlist_as_pylist(int len, wchar_t **list)
  236. {
  237. assert(list != NULL || len < 1);
  238. PyObject *pylist = PyList_New(len);
  239. if (pylist == NULL) {
  240. return NULL;
  241. }
  242. for (int i = 0; i < len; i++) {
  243. PyObject *v = PyUnicode_FromWideChar(list[i], -1);
  244. if (v == NULL) {
  245. Py_DECREF(pylist);
  246. return NULL;
  247. }
  248. PyList_SET_ITEM(pylist, i, v);
  249. }
  250. return pylist;
  251. }
  252. /* --- Py_SetStandardStreamEncoding() ----------------------------- */
  253. /* Helper to allow an embedding application to override the normal
  254. * mechanism that attempts to figure out an appropriate IO encoding
  255. */
  256. static char *_Py_StandardStreamEncoding = NULL;
  257. static char *_Py_StandardStreamErrors = NULL;
  258. int
  259. Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
  260. {
  261. if (Py_IsInitialized()) {
  262. /* This is too late to have any effect */
  263. return -1;
  264. }
  265. int res = 0;
  266. /* Py_SetStandardStreamEncoding() can be called before Py_Initialize(),
  267. but Py_Initialize() can change the allocator. Use a known allocator
  268. to be able to release the memory later. */
  269. PyMemAllocatorEx old_alloc;
  270. _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
  271. /* Can't call PyErr_NoMemory() on errors, as Python hasn't been
  272. * initialised yet.
  273. *
  274. * However, the raw memory allocators are initialised appropriately
  275. * as C static variables, so _PyMem_RawStrdup is OK even though
  276. * Py_Initialize hasn't been called yet.
  277. */
  278. if (encoding) {
  279. _Py_StandardStreamEncoding = _PyMem_RawStrdup(encoding);
  280. if (!_Py_StandardStreamEncoding) {
  281. res = -2;
  282. goto done;
  283. }
  284. }
  285. if (errors) {
  286. _Py_StandardStreamErrors = _PyMem_RawStrdup(errors);
  287. if (!_Py_StandardStreamErrors) {
  288. if (_Py_StandardStreamEncoding) {
  289. PyMem_RawFree(_Py_StandardStreamEncoding);
  290. }
  291. res = -3;
  292. goto done;
  293. }
  294. }
  295. #ifdef MS_WINDOWS
  296. if (_Py_StandardStreamEncoding) {
  297. /* Overriding the stream encoding implies legacy streams */
  298. Py_LegacyWindowsStdioFlag = 1;
  299. }
  300. #endif
  301. done:
  302. PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
  303. return res;
  304. }
  305. void
  306. _Py_ClearStandardStreamEncoding(void)
  307. {
  308. /* Use the same allocator than Py_SetStandardStreamEncoding() */
  309. PyMemAllocatorEx old_alloc;
  310. _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
  311. /* We won't need them anymore. */
  312. if (_Py_StandardStreamEncoding) {
  313. PyMem_RawFree(_Py_StandardStreamEncoding);
  314. _Py_StandardStreamEncoding = NULL;
  315. }
  316. if (_Py_StandardStreamErrors) {
  317. PyMem_RawFree(_Py_StandardStreamErrors);
  318. _Py_StandardStreamErrors = NULL;
  319. }
  320. PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
  321. }
  322. /* --- Py_GetArgcArgv() ------------------------------------------- */
  323. /* For Py_GetArgcArgv(); set by _Py_SetArgcArgv() */
  324. static int orig_argc = 0;
  325. static wchar_t **orig_argv = NULL;
  326. void
  327. _Py_ClearArgcArgv(void)
  328. {
  329. PyMemAllocatorEx old_alloc;
  330. _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
  331. _Py_wstrlist_clear(orig_argc, orig_argv);
  332. orig_argc = 0;
  333. orig_argv = NULL;
  334. PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
  335. }
  336. int
  337. _Py_SetArgcArgv(int argc, wchar_t * const *argv)
  338. {
  339. int res;
  340. PyMemAllocatorEx old_alloc;
  341. _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
  342. wchar_t **argv_copy = _Py_wstrlist_copy(argc, argv);
  343. if (argv_copy != NULL) {
  344. _Py_ClearArgcArgv();
  345. orig_argc = argc;
  346. orig_argv = argv_copy;
  347. res = 0;
  348. }
  349. else {
  350. res = -1;
  351. }
  352. PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
  353. return res;
  354. }
  355. /* Make the *original* argc/argv available to other modules.
  356. This is rare, but it is needed by the secureware extension. */
  357. void
  358. Py_GetArgcArgv(int *argc, wchar_t ***argv)
  359. {
  360. *argc = orig_argc;
  361. *argv = orig_argv;
  362. }
  363. /* --- _PyCoreConfig ---------------------------------------------- */
  364. #define DECODE_LOCALE_ERR(NAME, LEN) \
  365. (((LEN) == -2) \
  366. ? _Py_INIT_USER_ERR("cannot decode " NAME) \
  367. : _Py_INIT_NO_MEMORY())
  368. /* Free memory allocated in config, but don't clear all attributes */
  369. void
  370. _PyCoreConfig_Clear(_PyCoreConfig *config)
  371. {
  372. _PyPreConfig_Clear(&config->preconfig);
  373. #define CLEAR(ATTR) \
  374. do { \
  375. PyMem_RawFree(ATTR); \
  376. ATTR = NULL; \
  377. } while (0)
  378. #define CLEAR_WSTRLIST(LEN, LIST) \
  379. do { \
  380. _Py_wstrlist_clear(LEN, LIST); \
  381. LEN = 0; \
  382. LIST = NULL; \
  383. } while (0)
  384. CLEAR(config->pycache_prefix);
  385. CLEAR(config->module_search_path_env);
  386. CLEAR(config->home);
  387. CLEAR(config->program_name);
  388. CLEAR(config->program);
  389. CLEAR_WSTRLIST(config->argc, config->argv);
  390. config->argc = -1;
  391. CLEAR_WSTRLIST(config->nwarnoption, config->warnoptions);
  392. CLEAR_WSTRLIST(config->nxoption, config->xoptions);
  393. CLEAR_WSTRLIST(config->nmodule_search_path, config->module_search_paths);
  394. config->nmodule_search_path = -1;
  395. CLEAR(config->executable);
  396. CLEAR(config->prefix);
  397. CLEAR(config->base_prefix);
  398. CLEAR(config->exec_prefix);
  399. #ifdef MS_WINDOWS
  400. CLEAR(config->dll_path);
  401. #endif
  402. CLEAR(config->base_exec_prefix);
  403. CLEAR(config->filesystem_encoding);
  404. CLEAR(config->filesystem_errors);
  405. CLEAR(config->stdio_encoding);
  406. CLEAR(config->stdio_errors);
  407. #undef CLEAR
  408. #undef CLEAR_WSTRLIST
  409. }
  410. int
  411. _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
  412. {
  413. _PyCoreConfig_Clear(config);
  414. if (_PyPreConfig_Copy(&config->preconfig, &config2->preconfig) < 0) {
  415. return -1;
  416. }
  417. #define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
  418. #define COPY_STR_ATTR(ATTR) \
  419. do { \
  420. if (config2->ATTR != NULL) { \
  421. config->ATTR = _PyMem_RawStrdup(config2->ATTR); \
  422. if (config->ATTR == NULL) { \
  423. return -1; \
  424. } \
  425. } \
  426. } while (0)
  427. #define COPY_WSTR_ATTR(ATTR) \
  428. do { \
  429. if (config2->ATTR != NULL) { \
  430. config->ATTR = _PyMem_RawWcsdup(config2->ATTR); \
  431. if (config->ATTR == NULL) { \
  432. return -1; \
  433. } \
  434. } \
  435. } while (0)
  436. #define COPY_WSTRLIST(LEN, LIST) \
  437. do { \
  438. if (config2->LIST != NULL) { \
  439. config->LIST = _Py_wstrlist_copy(config2->LEN, config2->LIST); \
  440. if (config->LIST == NULL) { \
  441. return -1; \
  442. } \
  443. } \
  444. config->LEN = config2->LEN; \
  445. } while (0)
  446. COPY_ATTR(install_signal_handlers);
  447. COPY_ATTR(use_hash_seed);
  448. COPY_ATTR(hash_seed);
  449. COPY_ATTR(_install_importlib);
  450. COPY_ATTR(allocator);
  451. COPY_ATTR(dev_mode);
  452. COPY_ATTR(faulthandler);
  453. COPY_ATTR(tracemalloc);
  454. COPY_ATTR(import_time);
  455. COPY_ATTR(show_ref_count);
  456. COPY_ATTR(show_alloc_count);
  457. COPY_ATTR(dump_refs);
  458. COPY_ATTR(malloc_stats);
  459. COPY_ATTR(coerce_c_locale);
  460. COPY_ATTR(coerce_c_locale_warn);
  461. COPY_ATTR(utf8_mode);
  462. COPY_WSTR_ATTR(pycache_prefix);
  463. COPY_WSTR_ATTR(module_search_path_env);
  464. COPY_WSTR_ATTR(home);
  465. COPY_WSTR_ATTR(program_name);
  466. COPY_WSTR_ATTR(program);
  467. COPY_WSTRLIST(argc, argv);
  468. COPY_WSTRLIST(nwarnoption, warnoptions);
  469. COPY_WSTRLIST(nxoption, xoptions);
  470. COPY_WSTRLIST(nmodule_search_path, module_search_paths);
  471. COPY_WSTR_ATTR(executable);
  472. COPY_WSTR_ATTR(prefix);
  473. COPY_WSTR_ATTR(base_prefix);
  474. COPY_WSTR_ATTR(exec_prefix);
  475. #ifdef MS_WINDOWS
  476. COPY_WSTR_ATTR(dll_path);
  477. #endif
  478. COPY_WSTR_ATTR(base_exec_prefix);
  479. COPY_ATTR(site_import);
  480. COPY_ATTR(bytes_warning);
  481. COPY_ATTR(inspect);
  482. COPY_ATTR(interactive);
  483. COPY_ATTR(optimization_level);
  484. COPY_ATTR(parser_debug);
  485. COPY_ATTR(write_bytecode);
  486. COPY_ATTR(verbose);
  487. COPY_ATTR(quiet);
  488. COPY_ATTR(user_site_directory);
  489. COPY_ATTR(buffered_stdio);
  490. COPY_STR_ATTR(filesystem_encoding);
  491. COPY_STR_ATTR(filesystem_errors);
  492. COPY_STR_ATTR(stdio_encoding);
  493. COPY_STR_ATTR(stdio_errors);
  494. #ifdef MS_WINDOWS
  495. COPY_ATTR(legacy_windows_fs_encoding);
  496. COPY_ATTR(legacy_windows_stdio);
  497. #endif
  498. COPY_ATTR(skip_source_first_line);
  499. COPY_WSTR_ATTR(run_command);
  500. COPY_WSTR_ATTR(run_module);
  501. COPY_WSTR_ATTR(run_filename);
  502. COPY_ATTR(_check_hash_pycs_mode);
  503. COPY_ATTR(_frozen);
  504. #undef COPY_ATTR
  505. #undef COPY_STR_ATTR
  506. #undef COPY_WSTR_ATTR
  507. #undef COPY_WSTRLIST
  508. return 0;
  509. }
  510. const char*
  511. _PyCoreConfig_GetEnv(const _PyCoreConfig *config, const char *name)
  512. {
  513. assert(config->preconfig.use_environment >= 0);
  514. if (!config->preconfig.use_environment) {
  515. return NULL;
  516. }
  517. const char *var = getenv(name);
  518. if (var && var[0] != '\0') {
  519. return var;
  520. }
  521. else {
  522. return NULL;
  523. }
  524. }
  525. int
  526. _PyCoreConfig_GetEnvDup(const _PyCoreConfig *config,
  527. wchar_t **dest,
  528. wchar_t *wname, char *name)
  529. {
  530. assert(config->preconfig.use_environment >= 0);
  531. if (!config->preconfig.use_environment) {
  532. *dest = NULL;
  533. return 0;
  534. }
  535. #ifdef MS_WINDOWS
  536. const wchar_t *var = _wgetenv(wname);
  537. if (!var || var[0] == '\0') {
  538. *dest = NULL;
  539. return 0;
  540. }
  541. wchar_t *copy = _PyMem_RawWcsdup(var);
  542. if (copy == NULL) {
  543. return -1;
  544. }
  545. *dest = copy;
  546. #else
  547. const char *var = getenv(name);
  548. if (!var || var[0] == '\0') {
  549. *dest = NULL;
  550. return 0;
  551. }
  552. size_t len;
  553. wchar_t *wvar = Py_DecodeLocale(var, &len);
  554. if (!wvar) {
  555. if (len == (size_t)-2) {
  556. return -2;
  557. }
  558. else {
  559. return -1;
  560. }
  561. }
  562. *dest = wvar;
  563. #endif
  564. return 0;
  565. }
  566. void
  567. _PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config)
  568. {
  569. _PyPreConfig_GetGlobalConfig(&config->preconfig);
  570. #define COPY_FLAG(ATTR, VALUE) \
  571. if (config->ATTR == -1) { \
  572. config->ATTR = VALUE; \
  573. }
  574. #define COPY_NOT_FLAG(ATTR, VALUE) \
  575. if (config->ATTR == -1) { \
  576. config->ATTR = !(VALUE); \
  577. }
  578. COPY_FLAG(utf8_mode, Py_UTF8Mode);
  579. COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
  580. COPY_FLAG(inspect, Py_InspectFlag);
  581. COPY_FLAG(interactive, Py_InteractiveFlag);
  582. COPY_FLAG(optimization_level, Py_OptimizeFlag);
  583. COPY_FLAG(parser_debug, Py_DebugFlag);
  584. COPY_FLAG(verbose, Py_VerboseFlag);
  585. COPY_FLAG(quiet, Py_QuietFlag);
  586. #ifdef MS_WINDOWS
  587. COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
  588. COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
  589. #endif
  590. COPY_FLAG(_frozen, Py_FrozenFlag);
  591. COPY_NOT_FLAG(buffered_stdio, Py_UnbufferedStdioFlag);
  592. COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
  593. COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
  594. COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
  595. #undef COPY_FLAG
  596. #undef COPY_NOT_FLAG
  597. }
  598. /* Set Py_xxx global configuration variables from 'config' configuration. */
  599. void
  600. _PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config)
  601. {
  602. _PyPreConfig_SetGlobalConfig(&config->preconfig);
  603. #define COPY_FLAG(ATTR, VAR) \
  604. if (config->ATTR != -1) { \
  605. VAR = config->ATTR; \
  606. }
  607. #define COPY_NOT_FLAG(ATTR, VAR) \
  608. if (config->ATTR != -1) { \
  609. VAR = !config->ATTR; \
  610. }
  611. COPY_FLAG(utf8_mode, Py_UTF8Mode);
  612. COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
  613. COPY_FLAG(inspect, Py_InspectFlag);
  614. COPY_FLAG(interactive, Py_InteractiveFlag);
  615. COPY_FLAG(optimization_level, Py_OptimizeFlag);
  616. COPY_FLAG(parser_debug, Py_DebugFlag);
  617. COPY_FLAG(verbose, Py_VerboseFlag);
  618. COPY_FLAG(quiet, Py_QuietFlag);
  619. #ifdef MS_WINDOWS
  620. COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
  621. COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
  622. #endif
  623. COPY_FLAG(_frozen, Py_FrozenFlag);
  624. COPY_NOT_FLAG(buffered_stdio, Py_UnbufferedStdioFlag);
  625. COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
  626. COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
  627. COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
  628. /* Random or non-zero hash seed */
  629. Py_HashRandomizationFlag = (config->use_hash_seed == 0 ||
  630. config->hash_seed != 0);
  631. #undef COPY_FLAG
  632. #undef COPY_NOT_FLAG
  633. }
  634. /* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
  635. environment variables on macOS if available. */
  636. static _PyInitError
  637. config_init_program_name(_PyCoreConfig *config)
  638. {
  639. assert(config->program_name == NULL);
  640. /* If Py_SetProgramName() was called, use its value */
  641. const wchar_t *program_name = _Py_path_config.program_name;
  642. if (program_name != NULL) {
  643. config->program_name = _PyMem_RawWcsdup(program_name);
  644. if (config->program_name == NULL) {
  645. return _Py_INIT_NO_MEMORY();
  646. }
  647. return _Py_INIT_OK();
  648. }
  649. #ifdef __APPLE__
  650. /* On MacOS X, when the Python interpreter is embedded in an
  651. application bundle, it gets executed by a bootstrapping script
  652. that does os.execve() with an argv[0] that's different from the
  653. actual Python executable. This is needed to keep the Finder happy,
  654. or rather, to work around Apple's overly strict requirements of
  655. the process name. However, we still need a usable sys.executable,
  656. so the actual executable path is passed in an environment variable.
  657. See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
  658. script. */
  659. const char *p = _PyCoreConfig_GetEnv(config, "PYTHONEXECUTABLE");
  660. if (p != NULL) {
  661. size_t len;
  662. wchar_t* program_name = Py_DecodeLocale(p, &len);
  663. if (program_name == NULL) {
  664. return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment "
  665. "variable", (Py_ssize_t)len);
  666. }
  667. config->program_name = program_name;
  668. return _Py_INIT_OK();
  669. }
  670. #ifdef WITH_NEXT_FRAMEWORK
  671. else {
  672. const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
  673. if (pyvenv_launcher && *pyvenv_launcher) {
  674. /* Used by Mac/Tools/pythonw.c to forward
  675. * the argv0 of the stub executable
  676. */
  677. size_t len;
  678. wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len);
  679. if (program_name == NULL) {
  680. return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment "
  681. "variable", (Py_ssize_t)len);
  682. }
  683. config->program_name = program_name;
  684. return _Py_INIT_OK();
  685. }
  686. }
  687. #endif /* WITH_NEXT_FRAMEWORK */
  688. #endif /* __APPLE__ */
  689. /* Use argv[0] by default, if available */
  690. if (config->program != NULL) {
  691. config->program_name = _PyMem_RawWcsdup(config->program);
  692. if (config->program_name == NULL) {
  693. return _Py_INIT_NO_MEMORY();
  694. }
  695. return _Py_INIT_OK();
  696. }
  697. /* Last fall back: hardcoded string */
  698. #ifdef MS_WINDOWS
  699. const wchar_t *default_program_name = L"python";
  700. #else
  701. const wchar_t *default_program_name = L"python3";
  702. #endif
  703. config->program_name = _PyMem_RawWcsdup(default_program_name);
  704. if (config->program_name == NULL) {
  705. return _Py_INIT_NO_MEMORY();
  706. }
  707. return _Py_INIT_OK();
  708. }
  709. static _PyInitError
  710. config_init_executable(_PyCoreConfig *config)
  711. {
  712. assert(config->executable == NULL);
  713. /* If Py_SetProgramFullPath() was called, use its value */
  714. const wchar_t *program_full_path = _Py_path_config.program_full_path;
  715. if (program_full_path != NULL) {
  716. config->executable = _PyMem_RawWcsdup(program_full_path);
  717. if (config->executable == NULL) {
  718. return _Py_INIT_NO_MEMORY();
  719. }
  720. return _Py_INIT_OK();
  721. }
  722. return _Py_INIT_OK();
  723. }
  724. static const wchar_t*
  725. config_get_xoption(const _PyCoreConfig *config, wchar_t *name)
  726. {
  727. int nxoption = config->nxoption;
  728. wchar_t **xoptions = config->xoptions;
  729. for (int i=0; i < nxoption; i++) {
  730. wchar_t *option = xoptions[i];
  731. size_t len;
  732. wchar_t *sep = wcschr(option, L'=');
  733. if (sep != NULL) {
  734. len = (sep - option);
  735. }
  736. else {
  737. len = wcslen(option);
  738. }
  739. if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
  740. return option;
  741. }
  742. }
  743. return NULL;
  744. }
  745. static _PyInitError
  746. config_init_home(_PyCoreConfig *config)
  747. {
  748. wchar_t *home;
  749. /* If Py_SetPythonHome() was called, use its value */
  750. home = _Py_path_config.home;
  751. if (home) {
  752. config->home = _PyMem_RawWcsdup(home);
  753. if (config->home == NULL) {
  754. return _Py_INIT_NO_MEMORY();
  755. }
  756. return _Py_INIT_OK();
  757. }
  758. int res = _PyCoreConfig_GetEnvDup(config, &home,
  759. L"PYTHONHOME", "PYTHONHOME");
  760. if (res < 0) {
  761. return DECODE_LOCALE_ERR("PYTHONHOME", res);
  762. }
  763. config->home = home;
  764. return _Py_INIT_OK();
  765. }
  766. static _PyInitError
  767. config_init_hash_seed(_PyCoreConfig *config)
  768. {
  769. const char *seed_text = _PyCoreConfig_GetEnv(config, "PYTHONHASHSEED");
  770. Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc));
  771. /* Convert a text seed to a numeric one */
  772. if (seed_text && strcmp(seed_text, "random") != 0) {
  773. const char *endptr = seed_text;
  774. unsigned long seed;
  775. errno = 0;
  776. seed = strtoul(seed_text, (char **)&endptr, 10);
  777. if (*endptr != '\0'
  778. || seed > 4294967295UL
  779. || (errno == ERANGE && seed == ULONG_MAX))
  780. {
  781. return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
  782. "or an integer in range [0; 4294967295]");
  783. }
  784. /* Use a specific hash */
  785. config->use_hash_seed = 1;
  786. config->hash_seed = seed;
  787. }
  788. else {
  789. /* Use a random hash */
  790. config->use_hash_seed = 0;
  791. config->hash_seed = 0;
  792. }
  793. return _Py_INIT_OK();
  794. }
  795. static _PyInitError
  796. config_init_utf8_mode(_PyCoreConfig *config)
  797. {
  798. const wchar_t *xopt = config_get_xoption(config, L"utf8");
  799. if (xopt) {
  800. wchar_t *sep = wcschr(xopt, L'=');
  801. if (sep) {
  802. xopt = sep + 1;
  803. if (wcscmp(xopt, L"1") == 0) {
  804. config->utf8_mode = 1;
  805. }
  806. else if (wcscmp(xopt, L"0") == 0) {
  807. config->utf8_mode = 0;
  808. }
  809. else {
  810. return _Py_INIT_USER_ERR("invalid -X utf8 option value");
  811. }
  812. }
  813. else {
  814. config->utf8_mode = 1;
  815. }
  816. return _Py_INIT_OK();
  817. }
  818. const char *opt = _PyCoreConfig_GetEnv(config, "PYTHONUTF8");
  819. if (opt) {
  820. if (strcmp(opt, "1") == 0) {
  821. config->utf8_mode = 1;
  822. }
  823. else if (strcmp(opt, "0") == 0) {
  824. config->utf8_mode = 0;
  825. }
  826. else {
  827. return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
  828. "variable value");
  829. }
  830. return _Py_INIT_OK();
  831. }
  832. return _Py_INIT_OK();
  833. }
  834. static int
  835. config_str_to_int(const char *str, int *result)
  836. {
  837. const char *endptr = str;
  838. errno = 0;
  839. long value = strtol(str, (char **)&endptr, 10);
  840. if (*endptr != '\0' || errno == ERANGE) {
  841. return -1;
  842. }
  843. if (value < INT_MIN || value > INT_MAX) {
  844. return -1;
  845. }
  846. *result = (int)value;
  847. return 0;
  848. }
  849. static int
  850. config_wstr_to_int(const wchar_t *wstr, int *result)
  851. {
  852. const wchar_t *endptr = wstr;
  853. errno = 0;
  854. long value = wcstol(wstr, (wchar_t **)&endptr, 10);
  855. if (*endptr != '\0' || errno == ERANGE) {
  856. return -1;
  857. }
  858. if (value < INT_MIN || value > INT_MAX) {
  859. return -1;
  860. }
  861. *result = (int)value;
  862. return 0;
  863. }
  864. static void
  865. get_env_flag(_PyCoreConfig *config, int *flag, const char *name)
  866. {
  867. const char *var = _PyCoreConfig_GetEnv(config, name);
  868. if (!var) {
  869. return;
  870. }
  871. int value;
  872. if (config_str_to_int(var, &value) < 0 || value < 0) {
  873. /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
  874. value = 1;
  875. }
  876. if (*flag < value) {
  877. *flag = value;
  878. }
  879. }
  880. static _PyInitError
  881. config_read_env_vars(_PyCoreConfig *config)
  882. {
  883. /* Get environment variables */
  884. get_env_flag(config, &config->parser_debug, "PYTHONDEBUG");
  885. get_env_flag(config, &config->verbose, "PYTHONVERBOSE");
  886. get_env_flag(config, &config->optimization_level, "PYTHONOPTIMIZE");
  887. get_env_flag(config, &config->inspect, "PYTHONINSPECT");
  888. int dont_write_bytecode = 0;
  889. get_env_flag(config, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
  890. if (dont_write_bytecode) {
  891. config->write_bytecode = 0;
  892. }
  893. int no_user_site_directory = 0;
  894. get_env_flag(config, &no_user_site_directory, "PYTHONNOUSERSITE");
  895. if (no_user_site_directory) {
  896. config->user_site_directory = 0;
  897. }
  898. int unbuffered_stdio = 0;
  899. get_env_flag(config, &unbuffered_stdio, "PYTHONUNBUFFERED");
  900. if (unbuffered_stdio) {
  901. config->buffered_stdio = 0;
  902. }
  903. #ifdef MS_WINDOWS
  904. get_env_flag(config, &config->legacy_windows_fs_encoding,
  905. "PYTHONLEGACYWINDOWSFSENCODING");
  906. get_env_flag(config, &config->legacy_windows_stdio,
  907. "PYTHONLEGACYWINDOWSSTDIO");
  908. #endif
  909. if (config->allocator == NULL) {
  910. config->allocator = _PyCoreConfig_GetEnv(config, "PYTHONMALLOC");
  911. }
  912. if (_PyCoreConfig_GetEnv(config, "PYTHONDUMPREFS")) {
  913. config->dump_refs = 1;
  914. }
  915. if (_PyCoreConfig_GetEnv(config, "PYTHONMALLOCSTATS")) {
  916. config->malloc_stats = 1;
  917. }
  918. const char *env = _PyCoreConfig_GetEnv(config, "PYTHONCOERCECLOCALE");
  919. if (env) {
  920. if (strcmp(env, "0") == 0) {
  921. if (config->coerce_c_locale < 0) {
  922. config->coerce_c_locale = 0;
  923. }
  924. }
  925. else if (strcmp(env, "warn") == 0) {
  926. config->coerce_c_locale_warn = 1;
  927. }
  928. else {
  929. if (config->coerce_c_locale < 0) {
  930. config->coerce_c_locale = 1;
  931. }
  932. }
  933. }
  934. wchar_t *path;
  935. int res = _PyCoreConfig_GetEnvDup(config, &path,
  936. L"PYTHONPATH", "PYTHONPATH");
  937. if (res < 0) {
  938. return DECODE_LOCALE_ERR("PYTHONPATH", res);
  939. }
  940. config->module_search_path_env = path;
  941. if (config->use_hash_seed < 0) {
  942. _PyInitError err = config_init_hash_seed(config);
  943. if (_Py_INIT_FAILED(err)) {
  944. return err;
  945. }
  946. }
  947. return _Py_INIT_OK();
  948. }
  949. static _PyInitError
  950. config_init_tracemalloc(_PyCoreConfig *config)
  951. {
  952. int nframe;
  953. int valid;
  954. const char *env = _PyCoreConfig_GetEnv(config, "PYTHONTRACEMALLOC");
  955. if (env) {
  956. if (!config_str_to_int(env, &nframe)) {
  957. valid = (nframe >= 0);
  958. }
  959. else {
  960. valid = 0;
  961. }
  962. if (!valid) {
  963. return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number "
  964. "of frames");
  965. }
  966. config->tracemalloc = nframe;
  967. }
  968. const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
  969. if (xoption) {
  970. const wchar_t *sep = wcschr(xoption, L'=');
  971. if (sep) {
  972. if (!config_wstr_to_int(sep + 1, &nframe)) {
  973. valid = (nframe >= 0);
  974. }
  975. else {
  976. valid = 0;
  977. }
  978. if (!valid) {
  979. return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: "
  980. "invalid number of frames");
  981. }
  982. }
  983. else {
  984. /* -X tracemalloc behaves as -X tracemalloc=1 */
  985. nframe = 1;
  986. }
  987. config->tracemalloc = nframe;
  988. }
  989. return _Py_INIT_OK();
  990. }
  991. static _PyInitError
  992. config_init_pycache_prefix(_PyCoreConfig *config)
  993. {
  994. assert(config->pycache_prefix == NULL);
  995. const wchar_t *xoption = config_get_xoption(config, L"pycache_prefix");
  996. if (xoption) {
  997. const wchar_t *sep = wcschr(xoption, L'=');
  998. if (sep && wcslen(sep) > 1) {
  999. config->pycache_prefix = _PyMem_RawWcsdup(sep + 1);
  1000. if (config->pycache_prefix == NULL) {
  1001. return _Py_INIT_NO_MEMORY();
  1002. }
  1003. }
  1004. else {
  1005. // -X pycache_prefix= can cancel the env var
  1006. config->pycache_prefix = NULL;
  1007. }
  1008. }
  1009. else {
  1010. wchar_t *env;
  1011. int res = _PyCoreConfig_GetEnvDup(config, &env,
  1012. L"PYTHONPYCACHEPREFIX",
  1013. "PYTHONPYCACHEPREFIX");
  1014. if (res < 0) {
  1015. return DECODE_LOCALE_ERR("PYTHONPYCACHEPREFIX", res);
  1016. }
  1017. if (env) {
  1018. config->pycache_prefix = env;
  1019. }
  1020. }
  1021. return _Py_INIT_OK();
  1022. }
  1023. static _PyInitError
  1024. config_read_complex_options(_PyCoreConfig *config)
  1025. {
  1026. /* More complex options configured by env var and -X option */
  1027. if (config->faulthandler < 0) {
  1028. if (_PyCoreConfig_GetEnv(config, "PYTHONFAULTHANDLER")
  1029. || config_get_xoption(config, L"faulthandler")) {
  1030. config->faulthandler = 1;
  1031. }
  1032. }
  1033. if (_PyCoreConfig_GetEnv(config, "PYTHONPROFILEIMPORTTIME")
  1034. || config_get_xoption(config, L"importtime")) {
  1035. config->import_time = 1;
  1036. }
  1037. if (config_get_xoption(config, L"dev" ) ||
  1038. _PyCoreConfig_GetEnv(config, "PYTHONDEVMODE"))
  1039. {
  1040. config->dev_mode = 1;
  1041. }
  1042. _PyInitError err;
  1043. if (config->tracemalloc < 0) {
  1044. err = config_init_tracemalloc(config);
  1045. if (_Py_INIT_FAILED(err)) {
  1046. return err;
  1047. }
  1048. }
  1049. if (config->pycache_prefix == NULL) {
  1050. err = config_init_pycache_prefix(config);
  1051. if (_Py_INIT_FAILED(err)) {
  1052. return err;
  1053. }
  1054. }
  1055. return _Py_INIT_OK();
  1056. }
  1057. static void
  1058. config_init_locale(_PyCoreConfig *config)
  1059. {
  1060. /* Test also if coerce_c_locale equals 1: PYTHONCOERCECLOCALE=1 doesn't
  1061. imply that the C locale is always coerced. It is only coerced if
  1062. if the LC_CTYPE locale is "C". */
  1063. if (config->coerce_c_locale != 0) {
  1064. /* The C locale enables the C locale coercion (PEP 538) */
  1065. if (_Py_LegacyLocaleDetected()) {
  1066. config->coerce_c_locale = 1;
  1067. }
  1068. else {
  1069. config->coerce_c_locale = 0;
  1070. }
  1071. }
  1072. #ifndef MS_WINDOWS
  1073. if (config->utf8_mode < 0) {
  1074. /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */
  1075. const char *ctype_loc = setlocale(LC_CTYPE, NULL);
  1076. if (ctype_loc != NULL
  1077. && (strcmp(ctype_loc, "C") == 0
  1078. || strcmp(ctype_loc, "POSIX") == 0))
  1079. {
  1080. config->utf8_mode = 1;
  1081. }
  1082. }
  1083. #endif
  1084. }
  1085. static const char *
  1086. get_stdio_errors(const _PyCoreConfig *config)
  1087. {
  1088. #ifndef MS_WINDOWS
  1089. const char *loc = setlocale(LC_CTYPE, NULL);
  1090. if (loc != NULL) {
  1091. /* surrogateescape is the default in the legacy C and POSIX locales */
  1092. if (strcmp(loc, "C") == 0 || strcmp(loc, "POSIX") == 0) {
  1093. return "surrogateescape";
  1094. }
  1095. #ifdef PY_COERCE_C_LOCALE
  1096. /* surrogateescape is the default in locale coercion target locales */
  1097. if (_Py_IsLocaleCoercionTarget(loc)) {
  1098. return "surrogateescape";
  1099. }
  1100. #endif
  1101. }
  1102. return "strict";
  1103. #else
  1104. /* On Windows, always use surrogateescape by default */
  1105. return "surrogateescape";
  1106. #endif
  1107. }
  1108. static _PyInitError
  1109. get_locale_encoding(char **locale_encoding)
  1110. {
  1111. #ifdef MS_WINDOWS
  1112. char encoding[20];
  1113. PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
  1114. #elif defined(__ANDROID__) || defined(__VXWORKS__)
  1115. const char *encoding = "UTF-8";
  1116. #else
  1117. const char *encoding = nl_langinfo(CODESET);
  1118. if (!encoding || encoding[0] == '\0') {
  1119. return _Py_INIT_USER_ERR("failed to get the locale encoding: "
  1120. "nl_langinfo(CODESET) failed");
  1121. }
  1122. #endif
  1123. *locale_encoding = _PyMem_RawStrdup(encoding);
  1124. if (*locale_encoding == NULL) {
  1125. return _Py_INIT_NO_MEMORY();
  1126. }
  1127. return _Py_INIT_OK();
  1128. }
  1129. static _PyInitError
  1130. config_init_stdio_encoding(_PyCoreConfig *config)
  1131. {
  1132. /* If Py_SetStandardStreamEncoding() have been called, use these
  1133. parameters. */
  1134. if (config->stdio_encoding == NULL && _Py_StandardStreamEncoding != NULL) {
  1135. config->stdio_encoding = _PyMem_RawStrdup(_Py_StandardStreamEncoding);
  1136. if (config->stdio_encoding == NULL) {
  1137. return _Py_INIT_NO_MEMORY();
  1138. }
  1139. }
  1140. if (config->stdio_errors == NULL && _Py_StandardStreamErrors != NULL) {
  1141. config->stdio_errors = _PyMem_RawStrdup(_Py_StandardStreamErrors);
  1142. if (config->stdio_errors == NULL) {
  1143. return _Py_INIT_NO_MEMORY();
  1144. }
  1145. }
  1146. if (config->stdio_encoding != NULL && config->stdio_errors != NULL) {
  1147. return _Py_INIT_OK();
  1148. }
  1149. /* PYTHONIOENCODING environment variable */
  1150. const char *opt = _PyCoreConfig_GetEnv(config, "PYTHONIOENCODING");
  1151. if (opt) {
  1152. char *pythonioencoding = _PyMem_RawStrdup(opt);
  1153. if (pythonioencoding == NULL) {
  1154. return _Py_INIT_NO_MEMORY();
  1155. }
  1156. char *err = strchr(pythonioencoding, ':');
  1157. if (err) {
  1158. *err = '\0';
  1159. err++;
  1160. if (!err[0]) {
  1161. err = NULL;
  1162. }
  1163. }
  1164. /* Does PYTHONIOENCODING contain an encoding? */
  1165. if (pythonioencoding[0]) {
  1166. if (config->stdio_encoding == NULL) {
  1167. config->stdio_encoding = _PyMem_RawStrdup(pythonioencoding);
  1168. if (config->stdio_encoding == NULL) {
  1169. PyMem_RawFree(pythonioencoding);
  1170. return _Py_INIT_NO_MEMORY();
  1171. }
  1172. }
  1173. /* If the encoding is set but not the error handler,
  1174. use "strict" error handler by default.
  1175. PYTHONIOENCODING=latin1 behaves as
  1176. PYTHONIOENCODING=latin1:strict. */
  1177. if (!err) {
  1178. err = "strict";
  1179. }
  1180. }
  1181. if (config->stdio_errors == NULL && err != NULL) {
  1182. config->stdio_errors = _PyMem_RawStrdup(err);
  1183. if (config->stdio_errors == NULL) {
  1184. PyMem_RawFree(pythonioencoding);
  1185. return _Py_INIT_NO_MEMORY();
  1186. }
  1187. }
  1188. PyMem_RawFree(pythonioencoding);
  1189. }
  1190. /* UTF-8 Mode uses UTF-8/surrogateescape */
  1191. if (config->utf8_mode) {
  1192. if (config->stdio_encoding == NULL) {
  1193. config->stdio_encoding = _PyMem_RawStrdup("utf-8");
  1194. if (config->stdio_encoding == NULL) {
  1195. return _Py_INIT_NO_MEMORY();
  1196. }
  1197. }
  1198. if (config->stdio_errors == NULL) {
  1199. config->stdio_errors = _PyMem_RawStrdup("surrogateescape");
  1200. if (config->stdio_errors == NULL) {
  1201. return _Py_INIT_NO_MEMORY();
  1202. }
  1203. }
  1204. }
  1205. /* Choose the default error handler based on the current locale. */
  1206. if (config->stdio_encoding == NULL) {
  1207. _PyInitError err = get_locale_encoding(&config->stdio_encoding);
  1208. if (_Py_INIT_FAILED(err)) {
  1209. return err;
  1210. }
  1211. }
  1212. if (config->stdio_errors == NULL) {
  1213. const char *errors = get_stdio_errors(config);
  1214. config->stdio_errors = _PyMem_RawStrdup(errors);
  1215. if (config->stdio_errors == NULL) {
  1216. return _Py_INIT_NO_MEMORY();
  1217. }
  1218. }
  1219. return _Py_INIT_OK();
  1220. }
  1221. static _PyInitError
  1222. config_init_fs_encoding(_PyCoreConfig *config)
  1223. {
  1224. #ifdef MS_WINDOWS
  1225. if (config->legacy_windows_fs_encoding) {
  1226. /* Legacy Windows filesystem encoding: mbcs/replace */
  1227. if (config->filesystem_encoding == NULL) {
  1228. config->filesystem_encoding = _PyMem_RawStrdup("mbcs");
  1229. if (config->filesystem_encoding == NULL) {
  1230. return _Py_INIT_NO_MEMORY();
  1231. }
  1232. }
  1233. if (config->filesystem_errors == NULL) {
  1234. config->filesystem_errors = _PyMem_RawStrdup("replace");
  1235. if (config->filesystem_errors == NULL) {
  1236. return _Py_INIT_NO_MEMORY();
  1237. }
  1238. }
  1239. }
  1240. /* Windows defaults to utf-8/surrogatepass (PEP 529).
  1241. Note: UTF-8 Mode takes the same code path and the Legacy Windows FS
  1242. encoding has the priortiy over UTF-8 Mode. */
  1243. if (config->filesystem_encoding == NULL) {
  1244. config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
  1245. if (config->filesystem_encoding == NULL) {
  1246. return _Py_INIT_NO_MEMORY();
  1247. }
  1248. }
  1249. if (config->filesystem_errors == NULL) {
  1250. config->filesystem_errors = _PyMem_RawStrdup("surrogatepass");
  1251. if (config->filesystem_errors == NULL) {
  1252. return _Py_INIT_NO_MEMORY();
  1253. }
  1254. }
  1255. #else
  1256. if (config->filesystem_encoding == NULL) {
  1257. if (config->utf8_mode) {
  1258. /* UTF-8 Mode use: utf-8/surrogateescape */
  1259. config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
  1260. /* errors defaults to surrogateescape above */
  1261. }
  1262. else if (_Py_GetForceASCII()) {
  1263. config->filesystem_encoding = _PyMem_RawStrdup("ascii");
  1264. }
  1265. else {
  1266. /* macOS and Android use UTF-8,
  1267. other platforms use the locale encoding. */
  1268. #if defined(__APPLE__) || defined(__ANDROID__)
  1269. config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
  1270. #else
  1271. _PyInitError err = get_locale_encoding(&config->filesystem_encoding);
  1272. if (_Py_INIT_FAILED(err)) {
  1273. return err;
  1274. }
  1275. #endif
  1276. }
  1277. if (config->filesystem_encoding == NULL) {
  1278. return _Py_INIT_NO_MEMORY();
  1279. }
  1280. }
  1281. if (config->filesystem_errors == NULL) {
  1282. /* by default, use the "surrogateescape" error handler */
  1283. config->filesystem_errors = _PyMem_RawStrdup("surrogateescape");
  1284. if (config->filesystem_errors == NULL) {
  1285. return _Py_INIT_NO_MEMORY();
  1286. }
  1287. }
  1288. #endif
  1289. return _Py_INIT_OK();
  1290. }
  1291. static _PyInitError
  1292. _PyCoreConfig_ReadPreConfig(_PyCoreConfig *config)
  1293. {
  1294. _PyInitError err;
  1295. _PyPreConfig local_preconfig = _PyPreConfig_INIT;
  1296. _PyPreConfig_GetGlobalConfig(&local_preconfig);
  1297. if (_PyPreConfig_Copy(&local_preconfig, &config->preconfig) < 0) {
  1298. err = _Py_INIT_NO_MEMORY();
  1299. goto done;
  1300. }
  1301. err = _PyPreConfig_Read(&local_preconfig);
  1302. if (_Py_INIT_FAILED(err)) {
  1303. goto done;
  1304. }
  1305. if (_PyPreConfig_Copy(&config->preconfig, &local_preconfig) < 0) {
  1306. err = _Py_INIT_NO_MEMORY();
  1307. goto done;
  1308. }
  1309. err = _Py_INIT_OK();
  1310. done:
  1311. _PyPreConfig_Clear(&local_preconfig);
  1312. return err;
  1313. }
  1314. /* Read the configuration into _PyCoreConfig and initialize the LC_CTYPE
  1315. locale: enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538).
  1316. Read the configuration from:
  1317. * Environment variables
  1318. * Py_xxx global configuration variables
  1319. See _PyCoreConfig_ReadFromArgv() to parse also command line arguments. */
  1320. _PyInitError
  1321. _PyCoreConfig_Read(_PyCoreConfig *config, const _PyPreConfig *preconfig)
  1322. {
  1323. _PyInitError err;
  1324. _PyCoreConfig_GetGlobalConfig(config);
  1325. if (preconfig != NULL) {
  1326. if (_PyPreConfig_Copy(&config->preconfig, preconfig) < 0) {
  1327. return _Py_INIT_NO_MEMORY();
  1328. }
  1329. }
  1330. else {
  1331. err = _PyCoreConfig_ReadPreConfig(config);
  1332. if (_Py_INIT_FAILED(err)) {
  1333. return err;
  1334. }
  1335. }
  1336. assert(config->preconfig.use_environment >= 0);
  1337. if (config->preconfig.isolated > 0) {
  1338. config->user_site_directory = 0;
  1339. }
  1340. #ifdef MS_WINDOWS
  1341. if (config->legacy_windows_fs_encoding) {
  1342. config->utf8_mode = 0;
  1343. }
  1344. #endif
  1345. if (config->preconfig.use_environment) {
  1346. err = config_read_env_vars(config);
  1347. if (_Py_INIT_FAILED(err)) {
  1348. return err;
  1349. }
  1350. }
  1351. /* -X options */
  1352. if (config_get_xoption(config, L"showrefcount")) {
  1353. config->show_ref_count = 1;
  1354. }
  1355. if (config_get_xoption(config, L"showalloccount")) {
  1356. config->show_alloc_count = 1;
  1357. }
  1358. err = config_read_complex_options(config);
  1359. if (_Py_INIT_FAILED(err)) {
  1360. return err;
  1361. }
  1362. if (config->utf8_mode < 0) {
  1363. err = config_init_utf8_mode(config);
  1364. if (_Py_INIT_FAILED(err)) {
  1365. return err;
  1366. }
  1367. }
  1368. if (config->home == NULL) {
  1369. err = config_init_home(config);
  1370. if (_Py_INIT_FAILED(err)) {
  1371. return err;
  1372. }
  1373. }
  1374. if (config->program_name == NULL) {
  1375. err = config_init_program_name(config);
  1376. if (_Py_INIT_FAILED(err)) {
  1377. return err;
  1378. }
  1379. }
  1380. if (config->executable == NULL) {
  1381. err = config_init_executable(config);
  1382. if (_Py_INIT_FAILED(err)) {
  1383. return err;
  1384. }
  1385. }
  1386. if (config->coerce_c_locale != 0 || config->utf8_mode < 0) {
  1387. config_init_locale(config);
  1388. }
  1389. if (config->_install_importlib) {
  1390. err = _PyCoreConfig_InitPathConfig(config);
  1391. if (_Py_INIT_FAILED(err)) {
  1392. return err;
  1393. }
  1394. }
  1395. /* default values */
  1396. if (config->dev_mode) {
  1397. if (config->faulthandler < 0) {
  1398. config->faulthandler = 1;
  1399. }
  1400. if (config->allocator == NULL) {
  1401. config->allocator = "debug";
  1402. }
  1403. }
  1404. if (config->use_hash_seed < 0) {
  1405. config->use_hash_seed = 0;
  1406. config->hash_seed = 0;
  1407. }
  1408. if (config->faulthandler < 0) {
  1409. config->faulthandler = 0;
  1410. }
  1411. if (config->tracemalloc < 0) {
  1412. config->tracemalloc = 0;
  1413. }
  1414. if (config->coerce_c_locale < 0) {
  1415. config->coerce_c_locale = 0;
  1416. }
  1417. if (config->utf8_mode < 0) {
  1418. config->utf8_mode = 0;
  1419. }
  1420. if (config->argc < 0) {
  1421. config->argc = 0;
  1422. }
  1423. if (config->filesystem_encoding == NULL || config->filesystem_errors == NULL) {
  1424. err = config_init_fs_encoding(config);
  1425. if (_Py_INIT_FAILED(err)) {
  1426. return err;
  1427. }
  1428. }
  1429. err = config_init_stdio_encoding(config);
  1430. if (_Py_INIT_FAILED(err)) {
  1431. return err;
  1432. }
  1433. assert(config->coerce_c_locale >= 0);
  1434. assert(config->preconfig.use_environment >= 0);
  1435. assert(config->filesystem_encoding != NULL);
  1436. assert(config->filesystem_errors != NULL);
  1437. assert(config->stdio_encoding != NULL);
  1438. assert(config->stdio_errors != NULL);
  1439. assert(config->_check_hash_pycs_mode != NULL);
  1440. return _Py_INIT_OK();
  1441. }
  1442. static void
  1443. config_init_stdio(const _PyCoreConfig *config)
  1444. {
  1445. #if defined(MS_WINDOWS) || defined(__CYGWIN__)
  1446. /* don't translate newlines (\r\n <=> \n) */
  1447. _setmode(fileno(stdin), O_BINARY);
  1448. _setmode(fileno(stdout), O_BINARY);
  1449. _setmode(fileno(stderr), O_BINARY);
  1450. #endif
  1451. if (!config->buffered_stdio) {
  1452. #ifdef HAVE_SETVBUF
  1453. setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
  1454. setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
  1455. setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
  1456. #else /* !HAVE_SETVBUF */
  1457. setbuf(stdin, (char *)NULL);
  1458. setbuf(stdout, (char *)NULL);
  1459. setbuf(stderr, (char *)NULL);
  1460. #endif /* !HAVE_SETVBUF */
  1461. }
  1462. else if (config->interactive) {
  1463. #ifdef MS_WINDOWS
  1464. /* Doesn't have to have line-buffered -- use unbuffered */
  1465. /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
  1466. setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
  1467. #else /* !MS_WINDOWS */
  1468. #ifdef HAVE_SETVBUF
  1469. setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
  1470. setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
  1471. #endif /* HAVE_SETVBUF */
  1472. #endif /* !MS_WINDOWS */
  1473. /* Leave stderr alone - it should be unbuffered anyway. */
  1474. }
  1475. }
  1476. /* Write the configuration:
  1477. - coerce the LC_CTYPE locale (PEP 538)
  1478. - UTF-8 mode (PEP 540)
  1479. - set Py_xxx global configuration variables
  1480. - initialize C standard streams (stdin, stdout, stderr) */
  1481. void
  1482. _PyCoreConfig_Write(const _PyCoreConfig *config)
  1483. {
  1484. if (config->coerce_c_locale) {
  1485. _Py_CoerceLegacyLocale(config->coerce_c_locale_warn);
  1486. }
  1487. _PyCoreConfig_SetGlobalConfig(config);
  1488. config_init_stdio(config);
  1489. }
  1490. PyObject *
  1491. _PyCoreConfig_AsDict(const _PyCoreConfig *config)
  1492. {
  1493. PyObject *dict;
  1494. dict = PyDict_New();
  1495. if (dict == NULL) {
  1496. return NULL;
  1497. }
  1498. if (_PyPreConfig_AsDict(&config->preconfig, dict) < 0) {
  1499. Py_DECREF(dict);
  1500. return NULL;
  1501. }
  1502. #define SET_ITEM(KEY, EXPR) \
  1503. do { \
  1504. PyObject *obj = (EXPR); \
  1505. if (obj == NULL) { \
  1506. goto fail; \
  1507. } \
  1508. int res = PyDict_SetItemString(dict, (KEY), obj); \
  1509. Py_DECREF(obj); \
  1510. if (res < 0) { \
  1511. goto fail; \
  1512. } \
  1513. } while (0)
  1514. #define FROM_STRING(STR) \
  1515. ((STR != NULL) ? \
  1516. PyUnicode_FromString(STR) \
  1517. : (Py_INCREF(Py_None), Py_None))
  1518. #define SET_ITEM_INT(ATTR) \
  1519. SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR))
  1520. #define SET_ITEM_UINT(ATTR) \
  1521. SET_ITEM(#ATTR, PyLong_FromUnsignedLong(config->ATTR))
  1522. #define SET_ITEM_STR(ATTR) \
  1523. SET_ITEM(#ATTR, FROM_STRING(config->ATTR))
  1524. #define FROM_WSTRING(STR) \
  1525. ((STR != NULL) ? \
  1526. PyUnicode_FromWideChar(STR, -1) \
  1527. : (Py_INCREF(Py_None), Py_None))
  1528. #define SET_ITEM_WSTR(ATTR) \
  1529. SET_ITEM(#ATTR, FROM_WSTRING(config->ATTR))
  1530. #define SET_ITEM_WSTRLIST(NOPTION, OPTIONS) \
  1531. SET_ITEM(#OPTIONS, _Py_wstrlist_as_pylist(config->NOPTION, config->OPTIONS))
  1532. SET_ITEM_INT(install_signal_handlers);
  1533. SET_ITEM_INT(use_hash_seed);
  1534. SET_ITEM_UINT(hash_seed);
  1535. SET_ITEM_STR(allocator);
  1536. SET_ITEM_INT(dev_mode);
  1537. SET_ITEM_INT(faulthandler);
  1538. SET_ITEM_INT(tracemalloc);
  1539. SET_ITEM_INT(import_time);
  1540. SET_ITEM_INT(show_ref_count);
  1541. SET_ITEM_INT(show_alloc_count);
  1542. SET_ITEM_INT(dump_refs);
  1543. SET_ITEM_INT(malloc_stats);
  1544. SET_ITEM_INT(coerce_c_locale);
  1545. SET_ITEM_INT(coerce_c_locale_warn);
  1546. SET_ITEM_STR(filesystem_encoding);
  1547. SET_ITEM_STR(filesystem_errors);
  1548. SET_ITEM_INT(utf8_mode);
  1549. SET_ITEM_WSTR(pycache_prefix);
  1550. SET_ITEM_WSTR(program_name);
  1551. SET_ITEM_WSTRLIST(argc, argv);
  1552. SET_ITEM_WSTR(program);
  1553. SET_ITEM_WSTRLIST(nxoption, xoptions);
  1554. SET_ITEM_WSTRLIST(nwarnoption, warnoptions);
  1555. SET_ITEM_WSTR(module_search_path_env);
  1556. SET_ITEM_WSTR(home);
  1557. SET_ITEM_WSTRLIST(nmodule_search_path, module_search_paths);
  1558. SET_ITEM_WSTR(executable);
  1559. SET_ITEM_WSTR(prefix);
  1560. SET_ITEM_WSTR(base_prefix);
  1561. SET_ITEM_WSTR(exec_prefix);
  1562. SET_ITEM_WSTR(base_exec_prefix);
  1563. #ifdef MS_WINDOWS
  1564. SET_ITEM_WSTR(dll_path);
  1565. #endif
  1566. SET_ITEM_INT(site_import);
  1567. SET_ITEM_INT(bytes_warning);
  1568. SET_ITEM_INT(inspect);
  1569. SET_ITEM_INT(interactive);
  1570. SET_ITEM_INT(optimization_level);
  1571. SET_ITEM_INT(parser_debug);
  1572. SET_ITEM_INT(write_bytecode);
  1573. SET_ITEM_INT(verbose);
  1574. SET_ITEM_INT(quiet);
  1575. SET_ITEM_INT(user_site_directory);
  1576. SET_ITEM_INT(buffered_stdio);
  1577. SET_ITEM_STR(stdio_encoding);
  1578. SET_ITEM_STR(stdio_errors);
  1579. #ifdef MS_WINDOWS
  1580. SET_ITEM_INT(legacy_windows_fs_encoding);
  1581. SET_ITEM_INT(legacy_windows_stdio);
  1582. #endif
  1583. SET_ITEM_INT(skip_source_first_line);
  1584. SET_ITEM_WSTR(run_command);
  1585. SET_ITEM_WSTR(run_module);
  1586. SET_ITEM_WSTR(run_filename);
  1587. SET_ITEM_INT(_install_importlib);
  1588. SET_ITEM_STR(_check_hash_pycs_mode);
  1589. SET_ITEM_INT(_frozen);
  1590. return dict;
  1591. fail:
  1592. Py_DECREF(dict);
  1593. return NULL;
  1594. #undef FROM_STRING
  1595. #undef FROM_WSTRING
  1596. #undef SET_ITEM
  1597. #undef SET_ITEM_INT
  1598. #undef SET_ITEM_UINT
  1599. #undef SET_ITEM_STR
  1600. #undef SET_ITEM_WSTR
  1601. #undef SET_ITEM_WSTRLIST
  1602. }
  1603. /* --- _PyCmdline ------------------------------------------------- */
  1604. typedef struct {
  1605. const _PyArgv *args;
  1606. int argc;
  1607. wchar_t **argv;
  1608. int nwarnoption; /* Number of -W command line options */
  1609. wchar_t **warnoptions; /* Command line -W options */
  1610. int nenv_warnoption; /* Number of PYTHONWARNINGS environment variables */
  1611. wchar_t **env_warnoptions; /* PYTHONWARNINGS environment variables */
  1612. int print_help; /* -h, -? options */
  1613. int print_version; /* -V option */
  1614. } _PyCmdline;
  1615. static void
  1616. cmdline_clear(_PyCmdline *cmdline)
  1617. {
  1618. _Py_wstrlist_clear(cmdline->nwarnoption, cmdline->warnoptions);
  1619. cmdline->nwarnoption = 0;
  1620. cmdline->warnoptions = NULL;
  1621. _Py_wstrlist_clear(cmdline->nenv_warnoption, cmdline->env_warnoptions);
  1622. cmdline->nenv_warnoption = 0;
  1623. cmdline->env_warnoptions = NULL;
  1624. if (cmdline->args->use_bytes_argv && cmdline->argv != NULL) {
  1625. _Py_wstrlist_clear(cmdline->args->argc, cmdline->argv);
  1626. }
  1627. cmdline->argv = NULL;
  1628. }
  1629. /* --- _PyCoreConfig command line parser -------------------------- */
  1630. /* Parse the command line arguments */
  1631. static _PyInitError
  1632. config_parse_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
  1633. int *need_usage)
  1634. {
  1635. _PyInitError err;
  1636. _PyOS_ResetGetOpt();
  1637. do {
  1638. int longindex = -1;
  1639. int c = _PyOS_GetOpt(cmdline->args->argc, cmdline->argv, &longindex);
  1640. if (c == EOF) {
  1641. break;
  1642. }
  1643. if (c == 'c') {
  1644. /* -c is the last option; following arguments
  1645. that look like options are left for the
  1646. command to interpret. */
  1647. size_t len = wcslen(_PyOS_optarg) + 1 + 1;
  1648. wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
  1649. if (command == NULL) {
  1650. return _Py_INIT_NO_MEMORY();
  1651. }
  1652. memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
  1653. command[len - 2] = '\n';
  1654. command[len - 1] = 0;
  1655. config->run_command = command;
  1656. break;
  1657. }
  1658. if (c == 'm') {
  1659. /* -m is the last option; following arguments
  1660. that look like options are left for the
  1661. module to interpret. */
  1662. config->run_module = _PyMem_RawWcsdup(_PyOS_optarg);
  1663. if (config->run_module == NULL) {
  1664. return _Py_INIT_NO_MEMORY();
  1665. }
  1666. break;
  1667. }
  1668. switch (c) {
  1669. case 0:
  1670. // Handle long option.
  1671. assert(longindex == 0); // Only one long option now.
  1672. if (!wcscmp(_PyOS_optarg, L"always")) {
  1673. config->_check_hash_pycs_mode = "always";
  1674. } else if (!wcscmp(_PyOS_optarg, L"never")) {
  1675. config->_check_hash_pycs_mode = "never";
  1676. } else if (!wcscmp(_PyOS_optarg, L"default")) {
  1677. config->_check_hash_pycs_mode = "default";
  1678. } else {
  1679. fprintf(stderr, "--check-hash-based-pycs must be one of "
  1680. "'default', 'always', or 'never'\n");
  1681. *need_usage = 1;
  1682. return _Py_INIT_OK();
  1683. }
  1684. break;
  1685. case 'b':
  1686. config->bytes_warning++;
  1687. break;
  1688. case 'd':
  1689. config->parser_debug++;
  1690. break;
  1691. case 'i':
  1692. config->inspect++;
  1693. config->interactive++;
  1694. break;
  1695. case 'E':
  1696. case 'I':
  1697. /* option handled by _PyPreConfig_ReadFromArgv() */
  1698. break;
  1699. /* case 'J': reserved for Jython */
  1700. case 'O':
  1701. config->optimization_level++;
  1702. break;
  1703. case 'B':
  1704. config->write_bytecode = 0;
  1705. break;
  1706. case 's':
  1707. config->user_site_directory = 0;
  1708. break;
  1709. case 'S':
  1710. config->site_import = 0;
  1711. break;
  1712. case 't':
  1713. /* ignored for backwards compatibility */
  1714. break;
  1715. case 'u':
  1716. config->buffered_stdio = 0;
  1717. break;
  1718. case 'v':
  1719. config->verbose++;
  1720. break;
  1721. case 'x':
  1722. config->skip_source_first_line = 1;
  1723. break;
  1724. case 'h':
  1725. case '?':
  1726. cmdline->print_help++;
  1727. break;
  1728. case 'V':
  1729. cmdline->print_version++;
  1730. break;
  1731. case 'W':
  1732. err = _Py_wstrlist_append(&cmdline->nwarnoption,
  1733. &cmdline->warnoptions,
  1734. _PyOS_optarg);
  1735. if (_Py_INIT_FAILED(err)) {
  1736. return err;
  1737. }
  1738. break;
  1739. case 'X':
  1740. err = _Py_wstrlist_append(&config->nxoption,
  1741. &config->xoptions,
  1742. _PyOS_optarg);
  1743. if (_Py_INIT_FAILED(err)) {
  1744. return err;
  1745. }
  1746. break;
  1747. case 'q':
  1748. config->quiet++;
  1749. break;
  1750. case 'R':
  1751. config->use_hash_seed = 0;
  1752. break;
  1753. /* This space reserved for other options */
  1754. default:
  1755. /* unknown argument: parsing failed */
  1756. *need_usage = 1;
  1757. return _Py_INIT_OK();
  1758. }
  1759. } while (1);
  1760. if (config->run_command == NULL && config->run_module == NULL
  1761. && _PyOS_optind < cmdline->args->argc
  1762. && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
  1763. {
  1764. config->run_filename = _PyMem_RawWcsdup(cmdline->argv[_PyOS_optind]);
  1765. if (config->run_filename == NULL) {
  1766. return _Py_INIT_NO_MEMORY();
  1767. }
  1768. }
  1769. if (config->run_command != NULL || config->run_module != NULL) {
  1770. /* Backup _PyOS_optind */
  1771. _PyOS_optind--;
  1772. }
  1773. /* -c and -m options are exclusive */
  1774. assert(!(config->run_command != NULL && config->run_module != NULL));
  1775. return _Py_INIT_OK();
  1776. }
  1777. #ifdef MS_WINDOWS
  1778. # define WCSTOK wcstok_s
  1779. #else
  1780. # define WCSTOK wcstok
  1781. #endif
  1782. /* Get warning options from PYTHONWARNINGS environment variable. */
  1783. static _PyInitError
  1784. cmdline_init_env_warnoptions(_PyCmdline *cmdline, const _PyCoreConfig *config)
  1785. {
  1786. wchar_t *env;
  1787. int res = _PyCoreConfig_GetEnvDup(config, &env,
  1788. L"PYTHONWARNINGS", "PYTHONWARNINGS");
  1789. if (res < 0) {
  1790. return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
  1791. }
  1792. if (env == NULL) {
  1793. return _Py_INIT_OK();
  1794. }
  1795. wchar_t *warning, *context = NULL;
  1796. for (warning = WCSTOK(env, L",", &context);
  1797. warning != NULL;
  1798. warning = WCSTOK(NULL, L",", &context))
  1799. {
  1800. _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption,
  1801. &cmdline->env_warnoptions,
  1802. warning);
  1803. if (_Py_INIT_FAILED(err)) {
  1804. PyMem_RawFree(env);
  1805. return err;
  1806. }
  1807. }
  1808. PyMem_RawFree(env);
  1809. return _Py_INIT_OK();
  1810. }
  1811. static _PyInitError
  1812. config_init_program(_PyCoreConfig *config, const _PyCmdline *cmdline)
  1813. {
  1814. wchar_t *program;
  1815. if (cmdline->args->argc >= 1 && cmdline->argv != NULL) {
  1816. program = cmdline->argv[0];
  1817. }
  1818. else {
  1819. program = L"";
  1820. }
  1821. config->program = _PyMem_RawWcsdup(program);
  1822. if (config->program == NULL) {
  1823. return _Py_INIT_NO_MEMORY();
  1824. }
  1825. return _Py_INIT_OK();
  1826. }
  1827. static _PyInitError
  1828. config_add_warnings_optlist(_PyCoreConfig *config,
  1829. int len, wchar_t * const *options)
  1830. {
  1831. for (int i = 0; i < len; i++) {
  1832. _PyInitError err = _Py_wstrlist_append(&config->nwarnoption,
  1833. &config->warnoptions,
  1834. options[i]);
  1835. if (_Py_INIT_FAILED(err)) {
  1836. return err;
  1837. }
  1838. }
  1839. return _Py_INIT_OK();
  1840. }
  1841. static _PyInitError
  1842. config_init_warnoptions(_PyCoreConfig *config, const _PyCmdline *cmdline)
  1843. {
  1844. _PyInitError err;
  1845. assert(config->nwarnoption == 0);
  1846. /* The priority order for warnings configuration is (highest precedence
  1847. * first):
  1848. *
  1849. * - the BytesWarning filter, if needed ('-b', '-bb')
  1850. * - any '-W' command line options; then
  1851. * - the 'PYTHONWARNINGS' environment variable; then
  1852. * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
  1853. * - any implicit filters added by _warnings.c/warnings.py
  1854. *
  1855. * All settings except the last are passed to the warnings module via
  1856. * the `sys.warnoptions` list. Since the warnings module works on the basis
  1857. * of "the most recently added filter will be checked first", we add
  1858. * the lowest precedence entries first so that later entries override them.
  1859. */
  1860. if (config->dev_mode) {
  1861. err = _Py_wstrlist_append(&config->nwarnoption,
  1862. &config->warnoptions,
  1863. L"default");
  1864. if (_Py_INIT_FAILED(err)) {
  1865. return err;
  1866. }
  1867. }
  1868. err = config_add_warnings_optlist(config,
  1869. cmdline->nenv_warnoption,
  1870. cmdline->env_warnoptions);
  1871. if (_Py_INIT_FAILED(err)) {
  1872. return err;
  1873. }
  1874. err = config_add_warnings_optlist(config,
  1875. cmdline->nwarnoption,
  1876. cmdline->warnoptions);
  1877. if (_Py_INIT_FAILED(err)) {
  1878. return err;
  1879. }
  1880. /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
  1881. * don't even try to emit a warning, so we skip setting the filter in that
  1882. * case.
  1883. */
  1884. if (config->bytes_warning) {
  1885. wchar_t *filter;
  1886. if (config->bytes_warning> 1) {
  1887. filter = L"error::BytesWarning";
  1888. }
  1889. else {
  1890. filter = L"default::BytesWarning";
  1891. }
  1892. err = _Py_wstrlist_append(&config->nwarnoption,
  1893. &config->warnoptions,
  1894. filter);
  1895. if (_Py_INIT_FAILED(err)) {
  1896. return err;
  1897. }
  1898. }
  1899. return _Py_INIT_OK();
  1900. }
  1901. static _PyInitError
  1902. config_init_argv(_PyCoreConfig *config, const _PyCmdline *cmdline)
  1903. {
  1904. /* Copy argv to be able to modify it (to force -c/-m) */
  1905. int argc = cmdline->args->argc - _PyOS_optind;
  1906. wchar_t **argv;
  1907. if (argc <= 0 || cmdline->argv == NULL) {
  1908. /* Ensure at least one (empty) argument is seen */
  1909. static wchar_t *empty_argv[1] = {L""};
  1910. argc = 1;
  1911. argv = _Py_wstrlist_copy(1, empty_argv);
  1912. }
  1913. else {
  1914. argv = _Py_wstrlist_copy(argc, &cmdline->argv[_PyOS_optind]);
  1915. }
  1916. if (argv == NULL) {
  1917. return _Py_INIT_NO_MEMORY();
  1918. }
  1919. wchar_t *arg0 = NULL;
  1920. if (config->run_command != NULL) {
  1921. /* Force sys.argv[0] = '-c' */
  1922. arg0 = L"-c";
  1923. }
  1924. else if (config->run_module != NULL) {
  1925. /* Force sys.argv[0] = '-m'*/
  1926. arg0 = L"-m";
  1927. }
  1928. if (arg0 != NULL) {
  1929. arg0 = _PyMem_RawWcsdup(arg0);
  1930. if (arg0 == NULL) {
  1931. _Py_wstrlist_clear(argc, argv);
  1932. return _Py_INIT_NO_MEMORY();
  1933. }
  1934. assert(argc >= 1);
  1935. PyMem_RawFree(argv[0]);
  1936. argv[0] = arg0;
  1937. }
  1938. config->argc = argc;
  1939. config->argv = argv;
  1940. return _Py_INIT_OK();
  1941. }
  1942. static void
  1943. config_usage(int error, const wchar_t* program)
  1944. {
  1945. FILE *f = error ? stderr : stdout;
  1946. fprintf(f, usage_line, program);
  1947. if (error)
  1948. fprintf(f, "Try `python -h' for more information.\n");
  1949. else {
  1950. fputs(usage_1, f);
  1951. fputs(usage_2, f);
  1952. fputs(usage_3, f);
  1953. fprintf(f, usage_4, (wint_t)DELIM);
  1954. fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP);
  1955. fputs(usage_6, f);
  1956. }
  1957. }
  1958. /* Parse command line options and environment variables. */
  1959. static _PyInitError
  1960. config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
  1961. const _PyPreConfig *preconfig)
  1962. {
  1963. int need_usage = 0;
  1964. _PyInitError err;
  1965. err = config_init_program(config, cmdline);
  1966. if (_Py_INIT_FAILED(err)) {
  1967. return err;
  1968. }
  1969. err = config_parse_cmdline(config, cmdline, &need_usage);
  1970. if (_Py_INIT_FAILED(err)) {
  1971. return err;
  1972. }
  1973. if (need_usage) {
  1974. config_usage(1, config->program);
  1975. return _Py_INIT_EXIT(2);
  1976. }
  1977. if (cmdline->print_help) {
  1978. config_usage(0, config->program);
  1979. return _Py_INIT_EXIT(0);
  1980. }
  1981. if (cmdline->print_version) {
  1982. printf("Python %s\n",
  1983. (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
  1984. return _Py_INIT_EXIT(0);
  1985. }
  1986. err = config_init_argv(config, cmdline);
  1987. if (_Py_INIT_FAILED(err)) {
  1988. return err;
  1989. }
  1990. err = _PyCoreConfig_Read(config, preconfig);
  1991. if (_Py_INIT_FAILED(err)) {
  1992. return err;
  1993. }
  1994. if (config->preconfig.use_environment) {
  1995. err = cmdline_init_env_warnoptions(cmdline, config);
  1996. if (_Py_INIT_FAILED(err)) {
  1997. return err;
  1998. }
  1999. }
  2000. err = config_init_warnoptions(config, cmdline);
  2001. if (_Py_INIT_FAILED(err)) {
  2002. return err;
  2003. }
  2004. if (_Py_SetArgcArgv(cmdline->args->argc, cmdline->argv) < 0) {
  2005. return _Py_INIT_NO_MEMORY();
  2006. }
  2007. return _Py_INIT_OK();
  2008. }
  2009. static _PyInitError
  2010. config_read_from_argv_impl(_PyCoreConfig *config, const _PyArgv *args,
  2011. const _PyPreConfig *preconfig)
  2012. {
  2013. _PyInitError err;
  2014. _PyCmdline cmdline;
  2015. memset(&cmdline, 0, sizeof(cmdline));
  2016. cmdline.args = args;
  2017. err = _PyArgv_Decode(cmdline.args, &cmdline.argv);
  2018. if (_Py_INIT_FAILED(err)) {
  2019. goto done;
  2020. }
  2021. err = config_from_cmdline(config, &cmdline, preconfig);
  2022. if (_Py_INIT_FAILED(err)) {
  2023. goto done;
  2024. }
  2025. err = _Py_INIT_OK();
  2026. done:
  2027. cmdline_clear(&cmdline);
  2028. return err;
  2029. }
  2030. /* Read the configuration into _PyCoreConfig and initialize the LC_CTYPE
  2031. locale: enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538).
  2032. Read the configuration from:
  2033. * Command line arguments
  2034. * Environment variables
  2035. * Py_xxx global configuration variables */
  2036. _PyInitError
  2037. _PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args,
  2038. const _PyPreConfig *preconfig)
  2039. {
  2040. _PyInitError err;
  2041. int init_utf8_mode = Py_UTF8Mode;
  2042. #ifdef MS_WINDOWS
  2043. int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
  2044. #endif
  2045. _PyCoreConfig save_config = _PyCoreConfig_INIT;
  2046. int locale_coerced = 0;
  2047. int loops = 0;
  2048. char *init_ctype_locale = NULL;
  2049. /* copy LC_CTYPE locale */
  2050. const char *loc = setlocale(LC_CTYPE, NULL);
  2051. if (loc == NULL) {
  2052. err = _Py_INIT_ERR("failed to LC_CTYPE locale");
  2053. goto done;
  2054. }
  2055. init_ctype_locale = _PyMem_RawStrdup(loc);
  2056. if (init_ctype_locale == NULL) {
  2057. err = _Py_INIT_NO_MEMORY();
  2058. goto done;
  2059. }
  2060. if (_PyCoreConfig_Copy(&save_config, config) < 0) {
  2061. err = _Py_INIT_NO_MEMORY();
  2062. goto done;
  2063. }
  2064. /* Set LC_CTYPE to the user preferred locale */
  2065. _Py_SetLocaleFromEnv(LC_CTYPE);
  2066. while (1) {
  2067. int utf8_mode = config->utf8_mode;
  2068. int encoding_changed = 0;
  2069. /* Watchdog to prevent an infinite loop */
  2070. loops++;
  2071. if (loops == 3) {
  2072. err = _Py_INIT_ERR("Encoding changed twice while "
  2073. "reading the configuration");
  2074. goto done;
  2075. }
  2076. /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
  2077. on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
  2078. Py_UTF8Mode = config->utf8_mode;
  2079. #ifdef MS_WINDOWS
  2080. Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
  2081. #endif
  2082. err = config_read_from_argv_impl(config, args, preconfig);
  2083. if (_Py_INIT_FAILED(err)) {
  2084. goto done;
  2085. }
  2086. if (locale_coerced) {
  2087. config->coerce_c_locale = 1;
  2088. }
  2089. /* The legacy C locale assumes ASCII as the default text encoding, which
  2090. * causes problems not only for the CPython runtime, but also other
  2091. * components like GNU readline.
  2092. *
  2093. * Accordingly, when the CLI detects it, it attempts to coerce it to a
  2094. * more capable UTF-8 based alternative.
  2095. *
  2096. * See the documentation of the PYTHONCOERCECLOCALE setting for more
  2097. * details.
  2098. */
  2099. if (config->coerce_c_locale && !locale_coerced) {
  2100. locale_coerced = 1;
  2101. _Py_CoerceLegacyLocale(0);
  2102. encoding_changed = 1;
  2103. }
  2104. if (utf8_mode == -1) {
  2105. if (config->utf8_mode == 1) {
  2106. /* UTF-8 Mode enabled */
  2107. encoding_changed = 1;
  2108. }
  2109. }
  2110. else {
  2111. if (config->utf8_mode != utf8_mode) {
  2112. encoding_changed = 1;
  2113. }
  2114. }
  2115. if (!encoding_changed) {
  2116. break;
  2117. }
  2118. /* Reset the configuration before reading again the configuration,
  2119. just keep UTF-8 Mode value. */
  2120. int new_utf8_mode = config->utf8_mode;
  2121. int new_coerce_c_locale = config->coerce_c_locale;
  2122. if (_PyCoreConfig_Copy(config, &save_config) < 0) {
  2123. err = _Py_INIT_NO_MEMORY();
  2124. goto done;
  2125. }
  2126. config->utf8_mode = new_utf8_mode;
  2127. config->coerce_c_locale = new_coerce_c_locale;
  2128. /* The encoding changed: read again the configuration
  2129. with the new encoding */
  2130. }
  2131. err = _Py_INIT_OK();
  2132. done:
  2133. if (init_ctype_locale != NULL) {
  2134. setlocale(LC_CTYPE, init_ctype_locale);
  2135. }
  2136. _PyCoreConfig_Clear(&save_config);
  2137. Py_UTF8Mode = init_utf8_mode ;
  2138. #ifdef MS_WINDOWS
  2139. Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;
  2140. #endif
  2141. return err;
  2142. }