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.

1671 lines
46 KiB

  1. /* Python interpreter top-level routines, including init/exit */
  2. #include "Python.h"
  3. #include "Python-ast.h"
  4. #undef Yield /* undefine macro conflicting with winbase.h */
  5. #include "grammar.h"
  6. #include "node.h"
  7. #include "token.h"
  8. #include "parsetok.h"
  9. #include "errcode.h"
  10. #include "code.h"
  11. #include "symtable.h"
  12. #include "ast.h"
  13. #include "marshal.h"
  14. #include "osdefs.h"
  15. #include <locale.h>
  16. #ifdef HAVE_SIGNAL_H
  17. #include <signal.h>
  18. #endif
  19. #ifdef MS_WINDOWS
  20. #include "malloc.h" /* for alloca */
  21. #endif
  22. #ifdef HAVE_LANGINFO_H
  23. #include <langinfo.h>
  24. #endif
  25. #ifdef MS_WINDOWS
  26. #undef BYTE
  27. #include "windows.h"
  28. extern PyTypeObject PyWindowsConsoleIO_Type;
  29. #define PyWindowsConsoleIO_Check(op) (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
  30. #endif
  31. _Py_IDENTIFIER(flush);
  32. _Py_IDENTIFIER(name);
  33. _Py_IDENTIFIER(stdin);
  34. _Py_IDENTIFIER(stdout);
  35. _Py_IDENTIFIER(stderr);
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. extern wchar_t *Py_GetPath(void);
  40. extern grammar _PyParser_Grammar; /* From graminit.c */
  41. /* Forward */
  42. static void initmain(PyInterpreterState *interp);
  43. static int initfsencoding(PyInterpreterState *interp);
  44. static void initsite(void);
  45. static int initstdio(void);
  46. static void initsigs(void);
  47. static void call_py_exitfuncs(void);
  48. static void wait_for_thread_shutdown(void);
  49. static void call_ll_exitfuncs(void);
  50. extern int _PyUnicode_Init(void);
  51. extern int _PyStructSequence_Init(void);
  52. extern void _PyUnicode_Fini(void);
  53. extern int _PyLong_Init(void);
  54. extern void PyLong_Fini(void);
  55. extern int _PyFaulthandler_Init(void);
  56. extern void _PyFaulthandler_Fini(void);
  57. extern void _PyHash_Fini(void);
  58. extern int _PyTraceMalloc_Init(void);
  59. extern int _PyTraceMalloc_Fini(void);
  60. #ifdef WITH_THREAD
  61. extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
  62. extern void _PyGILState_Fini(void);
  63. #endif /* WITH_THREAD */
  64. /* Global configuration variable declarations are in pydebug.h */
  65. /* XXX (ncoghlan): move those declarations to pylifecycle.h? */
  66. int Py_DebugFlag; /* Needed by parser.c */
  67. int Py_VerboseFlag; /* Needed by import.c */
  68. int Py_QuietFlag; /* Needed by sysmodule.c */
  69. int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
  70. int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
  71. int Py_OptimizeFlag = 0; /* Needed by compile.c */
  72. int Py_NoSiteFlag; /* Suppress 'import site' */
  73. int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
  74. int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
  75. int Py_FrozenFlag; /* Needed by getpath.c */
  76. int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
  77. int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.pyc) */
  78. int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
  79. int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
  80. int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
  81. int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */
  82. #ifdef MS_WINDOWS
  83. int Py_LegacyWindowsFSEncodingFlag = 0; /* Uses mbcs instead of utf-8 */
  84. int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */
  85. #endif
  86. PyThreadState *_Py_Finalizing = NULL;
  87. /* Hack to force loading of object files */
  88. int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
  89. PyOS_mystrnicmp; /* Python/pystrcmp.o */
  90. /* PyModule_GetWarningsModule is no longer necessary as of 2.6
  91. since _warnings is builtin. This API should not be used. */
  92. PyObject *
  93. PyModule_GetWarningsModule(void)
  94. {
  95. return PyImport_ImportModule("warnings");
  96. }
  97. static int initialized = 0;
  98. /* API to access the initialized flag -- useful for esoteric use */
  99. int
  100. Py_IsInitialized(void)
  101. {
  102. return initialized;
  103. }
  104. /* Helper to allow an embedding application to override the normal
  105. * mechanism that attempts to figure out an appropriate IO encoding
  106. */
  107. static char *_Py_StandardStreamEncoding = NULL;
  108. static char *_Py_StandardStreamErrors = NULL;
  109. int
  110. Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
  111. {
  112. if (Py_IsInitialized()) {
  113. /* This is too late to have any effect */
  114. return -1;
  115. }
  116. /* Can't call PyErr_NoMemory() on errors, as Python hasn't been
  117. * initialised yet.
  118. *
  119. * However, the raw memory allocators are initialised appropriately
  120. * as C static variables, so _PyMem_RawStrdup is OK even though
  121. * Py_Initialize hasn't been called yet.
  122. */
  123. if (encoding) {
  124. _Py_StandardStreamEncoding = _PyMem_RawStrdup(encoding);
  125. if (!_Py_StandardStreamEncoding) {
  126. return -2;
  127. }
  128. }
  129. if (errors) {
  130. _Py_StandardStreamErrors = _PyMem_RawStrdup(errors);
  131. if (!_Py_StandardStreamErrors) {
  132. if (_Py_StandardStreamEncoding) {
  133. PyMem_RawFree(_Py_StandardStreamEncoding);
  134. }
  135. return -3;
  136. }
  137. }
  138. #ifdef MS_WINDOWS
  139. if (_Py_StandardStreamEncoding) {
  140. /* Overriding the stream encoding implies legacy streams */
  141. Py_LegacyWindowsStdioFlag = 1;
  142. }
  143. #endif
  144. return 0;
  145. }
  146. /* Global initializations. Can be undone by Py_FinalizeEx(). Don't
  147. call this twice without an intervening Py_FinalizeEx() call. When
  148. initializations fail, a fatal error is issued and the function does
  149. not return. On return, the first thread and interpreter state have
  150. been created.
  151. Locking: you must hold the interpreter lock while calling this.
  152. (If the lock has not yet been initialized, that's equivalent to
  153. having the lock, but you cannot use multiple threads.)
  154. */
  155. static int
  156. add_flag(int flag, const char *envs)
  157. {
  158. int env = atoi(envs);
  159. if (flag < env)
  160. flag = env;
  161. if (flag < 1)
  162. flag = 1;
  163. return flag;
  164. }
  165. static char*
  166. get_codec_name(const char *encoding)
  167. {
  168. const char *name_utf8;
  169. char *name_str;
  170. PyObject *codec, *name = NULL;
  171. codec = _PyCodec_Lookup(encoding);
  172. if (!codec)
  173. goto error;
  174. name = _PyObject_GetAttrId(codec, &PyId_name);
  175. Py_CLEAR(codec);
  176. if (!name)
  177. goto error;
  178. name_utf8 = PyUnicode_AsUTF8(name);
  179. if (name_utf8 == NULL)
  180. goto error;
  181. name_str = _PyMem_RawStrdup(name_utf8);
  182. Py_DECREF(name);
  183. if (name_str == NULL) {
  184. PyErr_NoMemory();
  185. return NULL;
  186. }
  187. return name_str;
  188. error:
  189. Py_XDECREF(codec);
  190. Py_XDECREF(name);
  191. return NULL;
  192. }
  193. static char*
  194. get_locale_encoding(void)
  195. {
  196. #ifdef MS_WINDOWS
  197. char codepage[100];
  198. PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
  199. return get_codec_name(codepage);
  200. #elif defined(HAVE_LANGINFO_H) && defined(CODESET)
  201. char* codeset = nl_langinfo(CODESET);
  202. if (!codeset || codeset[0] == '\0') {
  203. PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
  204. return NULL;
  205. }
  206. return get_codec_name(codeset);
  207. #elif defined(__ANDROID__)
  208. return get_codec_name("UTF-8");
  209. #else
  210. PyErr_SetNone(PyExc_NotImplementedError);
  211. return NULL;
  212. #endif
  213. }
  214. static void
  215. import_init(PyInterpreterState *interp, PyObject *sysmod)
  216. {
  217. PyObject *importlib;
  218. PyObject *impmod;
  219. PyObject *sys_modules;
  220. PyObject *value;
  221. /* Import _importlib through its frozen version, _frozen_importlib. */
  222. if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
  223. Py_FatalError("Py_Initialize: can't import _frozen_importlib");
  224. }
  225. else if (Py_VerboseFlag) {
  226. PySys_FormatStderr("import _frozen_importlib # frozen\n");
  227. }
  228. importlib = PyImport_AddModule("_frozen_importlib");
  229. if (importlib == NULL) {
  230. Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from "
  231. "sys.modules");
  232. }
  233. interp->importlib = importlib;
  234. Py_INCREF(interp->importlib);
  235. interp->import_func = PyDict_GetItemString(interp->builtins, "__import__");
  236. if (interp->import_func == NULL)
  237. Py_FatalError("Py_Initialize: __import__ not found");
  238. Py_INCREF(interp->import_func);
  239. /* Import the _imp module */
  240. impmod = PyInit_imp();
  241. if (impmod == NULL) {
  242. Py_FatalError("Py_Initialize: can't import _imp");
  243. }
  244. else if (Py_VerboseFlag) {
  245. PySys_FormatStderr("import _imp # builtin\n");
  246. }
  247. sys_modules = PyImport_GetModuleDict();
  248. if (Py_VerboseFlag) {
  249. PySys_FormatStderr("import sys # builtin\n");
  250. }
  251. if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) {
  252. Py_FatalError("Py_Initialize: can't save _imp to sys.modules");
  253. }
  254. /* Install importlib as the implementation of import */
  255. value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
  256. if (value == NULL) {
  257. PyErr_Print();
  258. Py_FatalError("Py_Initialize: importlib install failed");
  259. }
  260. Py_DECREF(value);
  261. Py_DECREF(impmod);
  262. _PyImportZip_Init();
  263. }
  264. void
  265. _Py_InitializeEx_Private(int install_sigs, int install_importlib)
  266. {
  267. PyInterpreterState *interp;
  268. PyThreadState *tstate;
  269. PyObject *bimod, *sysmod, *pstderr;
  270. char *p;
  271. extern void _Py_ReadyTypes(void);
  272. if (initialized)
  273. return;
  274. initialized = 1;
  275. _Py_Finalizing = NULL;
  276. #ifdef HAVE_SETLOCALE
  277. /* Set up the LC_CTYPE locale, so we can obtain
  278. the locale's charset without having to switch
  279. locales. */
  280. setlocale(LC_CTYPE, "");
  281. #endif
  282. if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
  283. Py_DebugFlag = add_flag(Py_DebugFlag, p);
  284. if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
  285. Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
  286. if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
  287. Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
  288. if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
  289. Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
  290. /* The variable is only tested for existence here; _PyRandom_Init will
  291. check its value further. */
  292. if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
  293. Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
  294. #ifdef MS_WINDOWS
  295. if ((p = Py_GETENV("PYTHONLEGACYWINDOWSFSENCODING")) && *p != '\0')
  296. Py_LegacyWindowsFSEncodingFlag = add_flag(Py_LegacyWindowsFSEncodingFlag, p);
  297. if ((p = Py_GETENV("PYTHONLEGACYWINDOWSSTDIO")) && *p != '\0')
  298. Py_LegacyWindowsStdioFlag = add_flag(Py_LegacyWindowsStdioFlag, p);
  299. #endif
  300. _PyRandom_Init();
  301. interp = PyInterpreterState_New();
  302. if (interp == NULL)
  303. Py_FatalError("Py_Initialize: can't make first interpreter");
  304. tstate = PyThreadState_New(interp);
  305. if (tstate == NULL)
  306. Py_FatalError("Py_Initialize: can't make first thread");
  307. (void) PyThreadState_Swap(tstate);
  308. #ifdef WITH_THREAD
  309. /* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because
  310. destroying the GIL might fail when it is being referenced from
  311. another running thread (see issue #9901).
  312. Instead we destroy the previously created GIL here, which ensures
  313. that we can call Py_Initialize / Py_FinalizeEx multiple times. */
  314. _PyEval_FiniThreads();
  315. /* Auto-thread-state API */
  316. _PyGILState_Init(interp, tstate);
  317. #endif /* WITH_THREAD */
  318. _Py_ReadyTypes();
  319. if (!_PyFrame_Init())
  320. Py_FatalError("Py_Initialize: can't init frames");
  321. if (!_PyLong_Init())
  322. Py_FatalError("Py_Initialize: can't init longs");
  323. if (!PyByteArray_Init())
  324. Py_FatalError("Py_Initialize: can't init bytearray");
  325. if (!_PyFloat_Init())
  326. Py_FatalError("Py_Initialize: can't init float");
  327. interp->modules = PyDict_New();
  328. if (interp->modules == NULL)
  329. Py_FatalError("Py_Initialize: can't make modules dictionary");
  330. /* Init Unicode implementation; relies on the codec registry */
  331. if (_PyUnicode_Init() < 0)
  332. Py_FatalError("Py_Initialize: can't initialize unicode");
  333. if (_PyStructSequence_Init() < 0)
  334. Py_FatalError("Py_Initialize: can't initialize structseq");
  335. bimod = _PyBuiltin_Init();
  336. if (bimod == NULL)
  337. Py_FatalError("Py_Initialize: can't initialize builtins modules");
  338. _PyImport_FixupBuiltin(bimod, "builtins");
  339. interp->builtins = PyModule_GetDict(bimod);
  340. if (interp->builtins == NULL)
  341. Py_FatalError("Py_Initialize: can't initialize builtins dict");
  342. Py_INCREF(interp->builtins);
  343. /* initialize builtin exceptions */
  344. _PyExc_Init(bimod);
  345. sysmod = _PySys_Init();
  346. if (sysmod == NULL)
  347. Py_FatalError("Py_Initialize: can't initialize sys");
  348. interp->sysdict = PyModule_GetDict(sysmod);
  349. if (interp->sysdict == NULL)
  350. Py_FatalError("Py_Initialize: can't initialize sys dict");
  351. Py_INCREF(interp->sysdict);
  352. _PyImport_FixupBuiltin(sysmod, "sys");
  353. PySys_SetPath(Py_GetPath());
  354. PyDict_SetItemString(interp->sysdict, "modules",
  355. interp->modules);
  356. /* Set up a preliminary stderr printer until we have enough
  357. infrastructure for the io module in place. */
  358. pstderr = PyFile_NewStdPrinter(fileno(stderr));
  359. if (pstderr == NULL)
  360. Py_FatalError("Py_Initialize: can't set preliminary stderr");
  361. _PySys_SetObjectId(&PyId_stderr, pstderr);
  362. PySys_SetObject("__stderr__", pstderr);
  363. Py_DECREF(pstderr);
  364. _PyImport_Init();
  365. _PyImportHooks_Init();
  366. /* Initialize _warnings. */
  367. _PyWarnings_Init();
  368. if (!install_importlib)
  369. return;
  370. if (_PyTime_Init() < 0)
  371. Py_FatalError("Py_Initialize: can't initialize time");
  372. import_init(interp, sysmod);
  373. /* initialize the faulthandler module */
  374. if (_PyFaulthandler_Init())
  375. Py_FatalError("Py_Initialize: can't initialize faulthandler");
  376. if (initfsencoding(interp) < 0)
  377. Py_FatalError("Py_Initialize: unable to load the file system codec");
  378. if (install_sigs)
  379. initsigs(); /* Signal handling stuff, including initintr() */
  380. if (_PyTraceMalloc_Init() < 0)
  381. Py_FatalError("Py_Initialize: can't initialize tracemalloc");
  382. initmain(interp); /* Module __main__ */
  383. if (initstdio() < 0)
  384. Py_FatalError(
  385. "Py_Initialize: can't initialize sys standard streams");
  386. /* Initialize warnings. */
  387. if (PySys_HasWarnOptions()) {
  388. PyObject *warnings_module = PyImport_ImportModule("warnings");
  389. if (warnings_module == NULL) {
  390. fprintf(stderr, "'import warnings' failed; traceback:\n");
  391. PyErr_Print();
  392. }
  393. Py_XDECREF(warnings_module);
  394. }
  395. if (!Py_NoSiteFlag)
  396. initsite(); /* Module site */
  397. }
  398. void
  399. Py_InitializeEx(int install_sigs)
  400. {
  401. _Py_InitializeEx_Private(install_sigs, 1);
  402. }
  403. void
  404. Py_Initialize(void)
  405. {
  406. Py_InitializeEx(1);
  407. }
  408. #ifdef COUNT_ALLOCS
  409. extern void dump_counts(FILE*);
  410. #endif
  411. /* Flush stdout and stderr */
  412. static int
  413. file_is_closed(PyObject *fobj)
  414. {
  415. int r;
  416. PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
  417. if (tmp == NULL) {
  418. PyErr_Clear();
  419. return 0;
  420. }
  421. r = PyObject_IsTrue(tmp);
  422. Py_DECREF(tmp);
  423. if (r < 0)
  424. PyErr_Clear();
  425. return r > 0;
  426. }
  427. static int
  428. flush_std_files(void)
  429. {
  430. PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
  431. PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
  432. PyObject *tmp;
  433. int status = 0;
  434. if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
  435. tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
  436. if (tmp == NULL) {
  437. PyErr_WriteUnraisable(fout);
  438. status = -1;
  439. }
  440. else
  441. Py_DECREF(tmp);
  442. }
  443. if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
  444. tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
  445. if (tmp == NULL) {
  446. PyErr_Clear();
  447. status = -1;
  448. }
  449. else
  450. Py_DECREF(tmp);
  451. }
  452. return status;
  453. }
  454. /* Undo the effect of Py_Initialize().
  455. Beware: if multiple interpreter and/or thread states exist, these
  456. are not wiped out; only the current thread and interpreter state
  457. are deleted. But since everything else is deleted, those other
  458. interpreter and thread states should no longer be used.
  459. (XXX We should do better, e.g. wipe out all interpreters and
  460. threads.)
  461. Locking: as above.
  462. */
  463. int
  464. Py_FinalizeEx(void)
  465. {
  466. PyInterpreterState *interp;
  467. PyThreadState *tstate;
  468. int status = 0;
  469. if (!initialized)
  470. return status;
  471. wait_for_thread_shutdown();
  472. /* The interpreter is still entirely intact at this point, and the
  473. * exit funcs may be relying on that. In particular, if some thread
  474. * or exit func is still waiting to do an import, the import machinery
  475. * expects Py_IsInitialized() to return true. So don't say the
  476. * interpreter is uninitialized until after the exit funcs have run.
  477. * Note that Threading.py uses an exit func to do a join on all the
  478. * threads created thru it, so this also protects pending imports in
  479. * the threads created via Threading.
  480. */
  481. call_py_exitfuncs();
  482. /* Get current thread state and interpreter pointer */
  483. tstate = PyThreadState_GET();
  484. interp = tstate->interp;
  485. /* Remaining threads (e.g. daemon threads) will automatically exit
  486. after taking the GIL (in PyEval_RestoreThread()). */
  487. _Py_Finalizing = tstate;
  488. initialized = 0;
  489. /* Flush sys.stdout and sys.stderr */
  490. if (flush_std_files() < 0) {
  491. status = -1;
  492. }
  493. /* Disable signal handling */
  494. PyOS_FiniInterrupts();
  495. /* Collect garbage. This may call finalizers; it's nice to call these
  496. * before all modules are destroyed.
  497. * XXX If a __del__ or weakref callback is triggered here, and tries to
  498. * XXX import a module, bad things can happen, because Python no
  499. * XXX longer believes it's initialized.
  500. * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
  501. * XXX is easy to provoke that way. I've also seen, e.g.,
  502. * XXX Exception exceptions.ImportError: 'No module named sha'
  503. * XXX in <function callback at 0x008F5718> ignored
  504. * XXX but I'm unclear on exactly how that one happens. In any case,
  505. * XXX I haven't seen a real-life report of either of these.
  506. */
  507. _PyGC_CollectIfEnabled();
  508. #ifdef COUNT_ALLOCS
  509. /* With COUNT_ALLOCS, it helps to run GC multiple times:
  510. each collection might release some types from the type
  511. list, so they become garbage. */
  512. while (_PyGC_CollectIfEnabled() > 0)
  513. /* nothing */;
  514. #endif
  515. /* Destroy all modules */
  516. PyImport_Cleanup();
  517. /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
  518. if (flush_std_files() < 0) {
  519. status = -1;
  520. }
  521. /* Collect final garbage. This disposes of cycles created by
  522. * class definitions, for example.
  523. * XXX This is disabled because it caused too many problems. If
  524. * XXX a __del__ or weakref callback triggers here, Python code has
  525. * XXX a hard time running, because even the sys module has been
  526. * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
  527. * XXX One symptom is a sequence of information-free messages
  528. * XXX coming from threads (if a __del__ or callback is invoked,
  529. * XXX other threads can execute too, and any exception they encounter
  530. * XXX triggers a comedy of errors as subsystem after subsystem
  531. * XXX fails to find what it *expects* to find in sys to help report
  532. * XXX the exception and consequent unexpected failures). I've also
  533. * XXX seen segfaults then, after adding print statements to the
  534. * XXX Python code getting called.
  535. */
  536. #if 0
  537. _PyGC_CollectIfEnabled();
  538. #endif
  539. /* Disable tracemalloc after all Python objects have been destroyed,
  540. so it is possible to use tracemalloc in objects destructor. */
  541. _PyTraceMalloc_Fini();
  542. /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
  543. _PyImport_Fini();
  544. /* Cleanup typeobject.c's internal caches. */
  545. _PyType_Fini();
  546. /* unload faulthandler module */
  547. _PyFaulthandler_Fini();
  548. /* Debugging stuff */
  549. #ifdef COUNT_ALLOCS
  550. dump_counts(stderr);
  551. #endif
  552. /* dump hash stats */
  553. _PyHash_Fini();
  554. _PY_DEBUG_PRINT_TOTAL_REFS();
  555. #ifdef Py_TRACE_REFS
  556. /* Display all objects still alive -- this can invoke arbitrary
  557. * __repr__ overrides, so requires a mostly-intact interpreter.
  558. * Alas, a lot of stuff may still be alive now that will be cleaned
  559. * up later.
  560. */
  561. if (Py_GETENV("PYTHONDUMPREFS"))
  562. _Py_PrintReferences(stderr);
  563. #endif /* Py_TRACE_REFS */
  564. /* Clear interpreter state and all thread states. */
  565. PyInterpreterState_Clear(interp);
  566. /* Now we decref the exception classes. After this point nothing
  567. can raise an exception. That's okay, because each Fini() method
  568. below has been checked to make sure no exceptions are ever
  569. raised.
  570. */
  571. _PyExc_Fini();
  572. /* Sundry finalizers */
  573. PyMethod_Fini();
  574. PyFrame_Fini();
  575. PyCFunction_Fini();
  576. PyTuple_Fini();
  577. PyList_Fini();
  578. PySet_Fini();
  579. PyBytes_Fini();
  580. PyByteArray_Fini();
  581. PyLong_Fini();
  582. PyFloat_Fini();
  583. PyDict_Fini();
  584. PySlice_Fini();
  585. _PyGC_Fini();
  586. _PyRandom_Fini();
  587. _PyArg_Fini();
  588. PyAsyncGen_Fini();
  589. /* Cleanup Unicode implementation */
  590. _PyUnicode_Fini();
  591. /* reset file system default encoding */
  592. if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
  593. PyMem_RawFree((char*)Py_FileSystemDefaultEncoding);
  594. Py_FileSystemDefaultEncoding = NULL;
  595. }
  596. /* XXX Still allocated:
  597. - various static ad-hoc pointers to interned strings
  598. - int and float free list blocks
  599. - whatever various modules and libraries allocate
  600. */
  601. PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
  602. /* Cleanup auto-thread-state */
  603. #ifdef WITH_THREAD
  604. _PyGILState_Fini();
  605. #endif /* WITH_THREAD */
  606. /* Delete current thread. After this, many C API calls become crashy. */
  607. PyThreadState_Swap(NULL);
  608. PyInterpreterState_Delete(interp);
  609. #ifdef Py_TRACE_REFS
  610. /* Display addresses (& refcnts) of all objects still alive.
  611. * An address can be used to find the repr of the object, printed
  612. * above by _Py_PrintReferences.
  613. */
  614. if (Py_GETENV("PYTHONDUMPREFS"))
  615. _Py_PrintReferenceAddresses(stderr);
  616. #endif /* Py_TRACE_REFS */
  617. #ifdef WITH_PYMALLOC
  618. if (_PyMem_PymallocEnabled()) {
  619. char *opt = Py_GETENV("PYTHONMALLOCSTATS");
  620. if (opt != NULL && *opt != '\0')
  621. _PyObject_DebugMallocStats(stderr);
  622. }
  623. #endif
  624. call_ll_exitfuncs();
  625. return status;
  626. }
  627. void
  628. Py_Finalize(void)
  629. {
  630. Py_FinalizeEx();
  631. }
  632. /* Create and initialize a new interpreter and thread, and return the
  633. new thread. This requires that Py_Initialize() has been called
  634. first.
  635. Unsuccessful initialization yields a NULL pointer. Note that *no*
  636. exception information is available even in this case -- the
  637. exception information is held in the thread, and there is no
  638. thread.
  639. Locking: as above.
  640. */
  641. PyThreadState *
  642. Py_NewInterpreter(void)
  643. {
  644. PyInterpreterState *interp;
  645. PyThreadState *tstate, *save_tstate;
  646. PyObject *bimod, *sysmod;
  647. if (!initialized)
  648. Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
  649. #ifdef WITH_THREAD
  650. /* Issue #10915, #15751: The GIL API doesn't work with multiple
  651. interpreters: disable PyGILState_Check(). */
  652. _PyGILState_check_enabled = 0;
  653. #endif
  654. interp = PyInterpreterState_New();
  655. if (interp == NULL)
  656. return NULL;
  657. tstate = PyThreadState_New(interp);
  658. if (tstate == NULL) {
  659. PyInterpreterState_Delete(interp);
  660. return NULL;
  661. }
  662. save_tstate = PyThreadState_Swap(tstate);
  663. /* XXX The following is lax in error checking */
  664. interp->modules = PyDict_New();
  665. bimod = _PyImport_FindBuiltin("builtins");
  666. if (bimod != NULL) {
  667. interp->builtins = PyModule_GetDict(bimod);
  668. if (interp->builtins == NULL)
  669. goto handle_error;
  670. Py_INCREF(interp->builtins);
  671. }
  672. /* initialize builtin exceptions */
  673. _PyExc_Init(bimod);
  674. sysmod = _PyImport_FindBuiltin("sys");
  675. if (bimod != NULL && sysmod != NULL) {
  676. PyObject *pstderr;
  677. interp->sysdict = PyModule_GetDict(sysmod);
  678. if (interp->sysdict == NULL)
  679. goto handle_error;
  680. Py_INCREF(interp->sysdict);
  681. PySys_SetPath(Py_GetPath());
  682. PyDict_SetItemString(interp->sysdict, "modules",
  683. interp->modules);
  684. /* Set up a preliminary stderr printer until we have enough
  685. infrastructure for the io module in place. */
  686. pstderr = PyFile_NewStdPrinter(fileno(stderr));
  687. if (pstderr == NULL)
  688. Py_FatalError("Py_Initialize: can't set preliminary stderr");
  689. _PySys_SetObjectId(&PyId_stderr, pstderr);
  690. PySys_SetObject("__stderr__", pstderr);
  691. Py_DECREF(pstderr);
  692. _PyImportHooks_Init();
  693. import_init(interp, sysmod);
  694. if (initfsencoding(interp) < 0)
  695. goto handle_error;
  696. if (initstdio() < 0)
  697. Py_FatalError(
  698. "Py_Initialize: can't initialize sys standard streams");
  699. initmain(interp);
  700. if (!Py_NoSiteFlag)
  701. initsite();
  702. }
  703. if (!PyErr_Occurred())
  704. return tstate;
  705. handle_error:
  706. /* Oops, it didn't work. Undo it all. */
  707. PyErr_PrintEx(0);
  708. PyThreadState_Clear(tstate);
  709. PyThreadState_Swap(save_tstate);
  710. PyThreadState_Delete(tstate);
  711. PyInterpreterState_Delete(interp);
  712. return NULL;
  713. }
  714. /* Delete an interpreter and its last thread. This requires that the
  715. given thread state is current, that the thread has no remaining
  716. frames, and that it is its interpreter's only remaining thread.
  717. It is a fatal error to violate these constraints.
  718. (Py_FinalizeEx() doesn't have these constraints -- it zaps
  719. everything, regardless.)
  720. Locking: as above.
  721. */
  722. void
  723. Py_EndInterpreter(PyThreadState *tstate)
  724. {
  725. PyInterpreterState *interp = tstate->interp;
  726. if (tstate != PyThreadState_GET())
  727. Py_FatalError("Py_EndInterpreter: thread is not current");
  728. if (tstate->frame != NULL)
  729. Py_FatalError("Py_EndInterpreter: thread still has a frame");
  730. wait_for_thread_shutdown();
  731. if (tstate != interp->tstate_head || tstate->next != NULL)
  732. Py_FatalError("Py_EndInterpreter: not the last thread");
  733. PyImport_Cleanup();
  734. PyInterpreterState_Clear(interp);
  735. PyThreadState_Swap(NULL);
  736. PyInterpreterState_Delete(interp);
  737. }
  738. #ifdef MS_WINDOWS
  739. static wchar_t *progname = L"python";
  740. #else
  741. static wchar_t *progname = L"python3";
  742. #endif
  743. void
  744. Py_SetProgramName(wchar_t *pn)
  745. {
  746. if (pn && *pn)
  747. progname = pn;
  748. }
  749. wchar_t *
  750. Py_GetProgramName(void)
  751. {
  752. return progname;
  753. }
  754. static wchar_t *default_home = NULL;
  755. static wchar_t env_home[MAXPATHLEN+1];
  756. void
  757. Py_SetPythonHome(wchar_t *home)
  758. {
  759. default_home = home;
  760. }
  761. wchar_t *
  762. Py_GetPythonHome(void)
  763. {
  764. wchar_t *home = default_home;
  765. if (home == NULL && !Py_IgnoreEnvironmentFlag) {
  766. char* chome = Py_GETENV("PYTHONHOME");
  767. if (chome) {
  768. size_t size = Py_ARRAY_LENGTH(env_home);
  769. size_t r = mbstowcs(env_home, chome, size);
  770. if (r != (size_t)-1 && r < size)
  771. home = env_home;
  772. }
  773. }
  774. return home;
  775. }
  776. /* Create __main__ module */
  777. static void
  778. initmain(PyInterpreterState *interp)
  779. {
  780. PyObject *m, *d, *loader, *ann_dict;
  781. m = PyImport_AddModule("__main__");
  782. if (m == NULL)
  783. Py_FatalError("can't create __main__ module");
  784. d = PyModule_GetDict(m);
  785. ann_dict = PyDict_New();
  786. if ((ann_dict == NULL) ||
  787. (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
  788. Py_FatalError("Failed to initialize __main__.__annotations__");
  789. }
  790. Py_DECREF(ann_dict);
  791. if (PyDict_GetItemString(d, "__builtins__") == NULL) {
  792. PyObject *bimod = PyImport_ImportModule("builtins");
  793. if (bimod == NULL) {
  794. Py_FatalError("Failed to retrieve builtins module");
  795. }
  796. if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
  797. Py_FatalError("Failed to initialize __main__.__builtins__");
  798. }
  799. Py_DECREF(bimod);
  800. }
  801. /* Main is a little special - imp.is_builtin("__main__") will return
  802. * False, but BuiltinImporter is still the most appropriate initial
  803. * setting for its __loader__ attribute. A more suitable value will
  804. * be set if __main__ gets further initialized later in the startup
  805. * process.
  806. */
  807. loader = PyDict_GetItemString(d, "__loader__");
  808. if (loader == NULL || loader == Py_None) {
  809. PyObject *loader = PyObject_GetAttrString(interp->importlib,
  810. "BuiltinImporter");
  811. if (loader == NULL) {
  812. Py_FatalError("Failed to retrieve BuiltinImporter");
  813. }
  814. if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
  815. Py_FatalError("Failed to initialize __main__.__loader__");
  816. }
  817. Py_DECREF(loader);
  818. }
  819. }
  820. static int
  821. initfsencoding(PyInterpreterState *interp)
  822. {
  823. PyObject *codec;
  824. #ifdef MS_WINDOWS
  825. if (Py_LegacyWindowsFSEncodingFlag)
  826. {
  827. Py_FileSystemDefaultEncoding = "mbcs";
  828. Py_FileSystemDefaultEncodeErrors = "replace";
  829. }
  830. else
  831. {
  832. Py_FileSystemDefaultEncoding = "utf-8";
  833. Py_FileSystemDefaultEncodeErrors = "surrogatepass";
  834. }
  835. #else
  836. if (Py_FileSystemDefaultEncoding == NULL)
  837. {
  838. Py_FileSystemDefaultEncoding = get_locale_encoding();
  839. if (Py_FileSystemDefaultEncoding == NULL)
  840. Py_FatalError("Py_Initialize: Unable to get the locale encoding");
  841. Py_HasFileSystemDefaultEncoding = 0;
  842. interp->fscodec_initialized = 1;
  843. return 0;
  844. }
  845. #endif
  846. /* the encoding is mbcs, utf-8 or ascii */
  847. codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
  848. if (!codec) {
  849. /* Such error can only occurs in critical situations: no more
  850. * memory, import a module of the standard library failed,
  851. * etc. */
  852. return -1;
  853. }
  854. Py_DECREF(codec);
  855. interp->fscodec_initialized = 1;
  856. return 0;
  857. }
  858. /* Import the site module (not into __main__ though) */
  859. static void
  860. initsite(void)
  861. {
  862. PyObject *m;
  863. m = PyImport_ImportModule("site");
  864. if (m == NULL) {
  865. fprintf(stderr, "Failed to import the site module\n");
  866. PyErr_Print();
  867. Py_Finalize();
  868. exit(1);
  869. }
  870. else {
  871. Py_DECREF(m);
  872. }
  873. }
  874. /* Check if a file descriptor is valid or not.
  875. Return 0 if the file descriptor is invalid, return non-zero otherwise. */
  876. static int
  877. is_valid_fd(int fd)
  878. {
  879. int fd2;
  880. if (fd < 0)
  881. return 0;
  882. _Py_BEGIN_SUPPRESS_IPH
  883. /* Prefer dup() over fstat(). fstat() can require input/output whereas
  884. dup() doesn't, there is a low risk of EMFILE/ENFILE at Python
  885. startup. */
  886. fd2 = dup(fd);
  887. if (fd2 >= 0)
  888. close(fd2);
  889. _Py_END_SUPPRESS_IPH
  890. return fd2 >= 0;
  891. }
  892. /* returns Py_None if the fd is not valid */
  893. static PyObject*
  894. create_stdio(PyObject* io,
  895. int fd, int write_mode, const char* name,
  896. const char* encoding, const char* errors)
  897. {
  898. PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
  899. const char* mode;
  900. const char* newline;
  901. PyObject *line_buffering;
  902. int buffering, isatty;
  903. _Py_IDENTIFIER(open);
  904. _Py_IDENTIFIER(isatty);
  905. _Py_IDENTIFIER(TextIOWrapper);
  906. _Py_IDENTIFIER(mode);
  907. if (!is_valid_fd(fd))
  908. Py_RETURN_NONE;
  909. /* stdin is always opened in buffered mode, first because it shouldn't
  910. make a difference in common use cases, second because TextIOWrapper
  911. depends on the presence of a read1() method which only exists on
  912. buffered streams.
  913. */
  914. if (Py_UnbufferedStdioFlag && write_mode)
  915. buffering = 0;
  916. else
  917. buffering = -1;
  918. if (write_mode)
  919. mode = "wb";
  920. else
  921. mode = "rb";
  922. buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
  923. fd, mode, buffering,
  924. Py_None, Py_None, /* encoding, errors */
  925. Py_None, 0); /* newline, closefd */
  926. if (buf == NULL)
  927. goto error;
  928. if (buffering) {
  929. _Py_IDENTIFIER(raw);
  930. raw = _PyObject_GetAttrId(buf, &PyId_raw);
  931. if (raw == NULL)
  932. goto error;
  933. }
  934. else {
  935. raw = buf;
  936. Py_INCREF(raw);
  937. }
  938. #ifdef MS_WINDOWS
  939. /* Windows console IO is always UTF-8 encoded */
  940. if (PyWindowsConsoleIO_Check(raw))
  941. encoding = "utf-8";
  942. #endif
  943. text = PyUnicode_FromString(name);
  944. if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
  945. goto error;
  946. res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL);
  947. if (res == NULL)
  948. goto error;
  949. isatty = PyObject_IsTrue(res);
  950. Py_DECREF(res);
  951. if (isatty == -1)
  952. goto error;
  953. if (isatty || Py_UnbufferedStdioFlag)
  954. line_buffering = Py_True;
  955. else
  956. line_buffering = Py_False;
  957. Py_CLEAR(raw);
  958. Py_CLEAR(text);
  959. #ifdef MS_WINDOWS
  960. /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
  961. newlines to "\n".
  962. sys.stdout and sys.stderr: translate "\n" to "\r\n". */
  963. newline = NULL;
  964. #else
  965. /* sys.stdin: split lines at "\n".
  966. sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
  967. newline = "\n";
  968. #endif
  969. stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
  970. buf, encoding, errors,
  971. newline, line_buffering);
  972. Py_CLEAR(buf);
  973. if (stream == NULL)
  974. goto error;
  975. if (write_mode)
  976. mode = "w";
  977. else
  978. mode = "r";
  979. text = PyUnicode_FromString(mode);
  980. if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
  981. goto error;
  982. Py_CLEAR(text);
  983. return stream;
  984. error:
  985. Py_XDECREF(buf);
  986. Py_XDECREF(stream);
  987. Py_XDECREF(text);
  988. Py_XDECREF(raw);
  989. if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) {
  990. /* Issue #24891: the file descriptor was closed after the first
  991. is_valid_fd() check was called. Ignore the OSError and set the
  992. stream to None. */
  993. PyErr_Clear();
  994. Py_RETURN_NONE;
  995. }
  996. return NULL;
  997. }
  998. /* Initialize sys.stdin, stdout, stderr and builtins.open */
  999. static int
  1000. initstdio(void)
  1001. {
  1002. PyObject *iomod = NULL, *wrapper;
  1003. PyObject *bimod = NULL;
  1004. PyObject *m;
  1005. PyObject *std = NULL;
  1006. int status = 0, fd;
  1007. PyObject * encoding_attr;
  1008. char *pythonioencoding = NULL, *encoding, *errors;
  1009. /* Hack to avoid a nasty recursion issue when Python is invoked
  1010. in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
  1011. if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
  1012. goto error;
  1013. }
  1014. Py_DECREF(m);
  1015. if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
  1016. goto error;
  1017. }
  1018. Py_DECREF(m);
  1019. if (!(bimod = PyImport_ImportModule("builtins"))) {
  1020. goto error;
  1021. }
  1022. if (!(iomod = PyImport_ImportModule("io"))) {
  1023. goto error;
  1024. }
  1025. if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
  1026. goto error;
  1027. }
  1028. /* Set builtins.open */
  1029. if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
  1030. Py_DECREF(wrapper);
  1031. goto error;
  1032. }
  1033. Py_DECREF(wrapper);
  1034. encoding = _Py_StandardStreamEncoding;
  1035. errors = _Py_StandardStreamErrors;
  1036. if (!encoding || !errors) {
  1037. pythonioencoding = Py_GETENV("PYTHONIOENCODING");
  1038. if (pythonioencoding) {
  1039. char *err;
  1040. pythonioencoding = _PyMem_Strdup(pythonioencoding);
  1041. if (pythonioencoding == NULL) {
  1042. PyErr_NoMemory();
  1043. goto error;
  1044. }
  1045. err = strchr(pythonioencoding, ':');
  1046. if (err) {
  1047. *err = '\0';
  1048. err++;
  1049. if (*err && !errors) {
  1050. errors = err;
  1051. }
  1052. }
  1053. if (*pythonioencoding && !encoding) {
  1054. encoding = pythonioencoding;
  1055. }
  1056. }
  1057. if (!errors && !(pythonioencoding && *pythonioencoding)) {
  1058. /* When the LC_CTYPE locale is the POSIX locale ("C locale"),
  1059. stdin and stdout use the surrogateescape error handler by
  1060. default, instead of the strict error handler. */
  1061. char *loc = setlocale(LC_CTYPE, NULL);
  1062. if (loc != NULL && strcmp(loc, "C") == 0)
  1063. errors = "surrogateescape";
  1064. }
  1065. }
  1066. /* Set sys.stdin */
  1067. fd = fileno(stdin);
  1068. /* Under some conditions stdin, stdout and stderr may not be connected
  1069. * and fileno() may point to an invalid file descriptor. For example
  1070. * GUI apps don't have valid standard streams by default.
  1071. */
  1072. std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
  1073. if (std == NULL)
  1074. goto error;
  1075. PySys_SetObject("__stdin__", std);
  1076. _PySys_SetObjectId(&PyId_stdin, std);
  1077. Py_DECREF(std);
  1078. /* Set sys.stdout */
  1079. fd = fileno(stdout);
  1080. std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
  1081. if (std == NULL)
  1082. goto error;
  1083. PySys_SetObject("__stdout__", std);
  1084. _PySys_SetObjectId(&PyId_stdout, std);
  1085. Py_DECREF(std);
  1086. #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
  1087. /* Set sys.stderr, replaces the preliminary stderr */
  1088. fd = fileno(stderr);
  1089. std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
  1090. if (std == NULL)
  1091. goto error;
  1092. /* Same as hack above, pre-import stderr's codec to avoid recursion
  1093. when import.c tries to write to stderr in verbose mode. */
  1094. encoding_attr = PyObject_GetAttrString(std, "encoding");
  1095. if (encoding_attr != NULL) {
  1096. const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
  1097. if (std_encoding != NULL) {
  1098. PyObject *codec_info = _PyCodec_Lookup(std_encoding);
  1099. Py_XDECREF(codec_info);
  1100. }
  1101. Py_DECREF(encoding_attr);
  1102. }
  1103. PyErr_Clear(); /* Not a fatal error if codec isn't available */
  1104. if (PySys_SetObject("__stderr__", std) < 0) {
  1105. Py_DECREF(std);
  1106. goto error;
  1107. }
  1108. if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
  1109. Py_DECREF(std);
  1110. goto error;
  1111. }
  1112. Py_DECREF(std);
  1113. #endif
  1114. if (0) {
  1115. error:
  1116. status = -1;
  1117. }
  1118. /* We won't need them anymore. */
  1119. if (_Py_StandardStreamEncoding) {
  1120. PyMem_RawFree(_Py_StandardStreamEncoding);
  1121. _Py_StandardStreamEncoding = NULL;
  1122. }
  1123. if (_Py_StandardStreamErrors) {
  1124. PyMem_RawFree(_Py_StandardStreamErrors);
  1125. _Py_StandardStreamErrors = NULL;
  1126. }
  1127. PyMem_Free(pythonioencoding);
  1128. Py_XDECREF(bimod);
  1129. Py_XDECREF(iomod);
  1130. return status;
  1131. }
  1132. static void
  1133. _Py_FatalError_DumpTracebacks(int fd)
  1134. {
  1135. fputc('\n', stderr);
  1136. fflush(stderr);
  1137. /* display the current Python stack */
  1138. _Py_DumpTracebackThreads(fd, NULL, NULL);
  1139. }
  1140. /* Print the current exception (if an exception is set) with its traceback,
  1141. or display the current Python stack.
  1142. Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
  1143. called on catastrophic cases.
  1144. Return 1 if the traceback was displayed, 0 otherwise. */
  1145. static int
  1146. _Py_FatalError_PrintExc(int fd)
  1147. {
  1148. PyObject *ferr, *res;
  1149. PyObject *exception, *v, *tb;
  1150. int has_tb;
  1151. if (PyThreadState_GET() == NULL) {
  1152. /* The GIL is released: trying to acquire it is likely to deadlock,
  1153. just give up. */
  1154. return 0;
  1155. }
  1156. PyErr_Fetch(&exception, &v, &tb);
  1157. if (exception == NULL) {
  1158. /* No current exception */
  1159. return 0;
  1160. }
  1161. ferr = _PySys_GetObjectId(&PyId_stderr);
  1162. if (ferr == NULL || ferr == Py_None) {
  1163. /* sys.stderr is not set yet or set to None,
  1164. no need to try to display the exception */
  1165. return 0;
  1166. }
  1167. PyErr_NormalizeException(&exception, &v, &tb);
  1168. if (tb == NULL) {
  1169. tb = Py_None;
  1170. Py_INCREF(tb);
  1171. }
  1172. PyException_SetTraceback(v, tb);
  1173. if (exception == NULL) {
  1174. /* PyErr_NormalizeException() failed */
  1175. return 0;
  1176. }
  1177. has_tb = (tb != Py_None);
  1178. PyErr_Display(exception, v, tb);
  1179. Py_XDECREF(exception);
  1180. Py_XDECREF(v);
  1181. Py_XDECREF(tb);
  1182. /* sys.stderr may be buffered: call sys.stderr.flush() */
  1183. res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
  1184. if (res == NULL)
  1185. PyErr_Clear();
  1186. else
  1187. Py_DECREF(res);
  1188. return has_tb;
  1189. }
  1190. /* Print fatal error message and abort */
  1191. void
  1192. Py_FatalError(const char *msg)
  1193. {
  1194. const int fd = fileno(stderr);
  1195. static int reentrant = 0;
  1196. #ifdef MS_WINDOWS
  1197. size_t len;
  1198. WCHAR* buffer;
  1199. size_t i;
  1200. #endif
  1201. if (reentrant) {
  1202. /* Py_FatalError() caused a second fatal error.
  1203. Example: flush_std_files() raises a recursion error. */
  1204. goto exit;
  1205. }
  1206. reentrant = 1;
  1207. fprintf(stderr, "Fatal Python error: %s\n", msg);
  1208. fflush(stderr); /* it helps in Windows debug build */
  1209. /* Print the exception (if an exception is set) with its traceback,
  1210. * or display the current Python stack. */
  1211. if (!_Py_FatalError_PrintExc(fd))
  1212. _Py_FatalError_DumpTracebacks(fd);
  1213. /* The main purpose of faulthandler is to display the traceback. We already
  1214. * did our best to display it. So faulthandler can now be disabled.
  1215. * (Don't trigger it on abort().) */
  1216. _PyFaulthandler_Fini();
  1217. /* Check if the current Python thread hold the GIL */
  1218. if (PyThreadState_GET() != NULL) {
  1219. /* Flush sys.stdout and sys.stderr */
  1220. flush_std_files();
  1221. }
  1222. #ifdef MS_WINDOWS
  1223. len = strlen(msg);
  1224. /* Convert the message to wchar_t. This uses a simple one-to-one
  1225. conversion, assuming that the this error message actually uses ASCII
  1226. only. If this ceases to be true, we will have to convert. */
  1227. buffer = alloca( (len+1) * (sizeof *buffer));
  1228. for( i=0; i<=len; ++i)
  1229. buffer[i] = msg[i];
  1230. OutputDebugStringW(L"Fatal Python error: ");
  1231. OutputDebugStringW(buffer);
  1232. OutputDebugStringW(L"\n");
  1233. #endif /* MS_WINDOWS */
  1234. exit:
  1235. #if defined(MS_WINDOWS) && defined(_DEBUG)
  1236. DebugBreak();
  1237. #endif
  1238. abort();
  1239. }
  1240. /* Clean up and exit */
  1241. #ifdef WITH_THREAD
  1242. # include "pythread.h"
  1243. #endif
  1244. static void (*pyexitfunc)(void) = NULL;
  1245. /* For the atexit module. */
  1246. void _Py_PyAtExit(void (*func)(void))
  1247. {
  1248. pyexitfunc = func;
  1249. }
  1250. static void
  1251. call_py_exitfuncs(void)
  1252. {
  1253. if (pyexitfunc == NULL)
  1254. return;
  1255. (*pyexitfunc)();
  1256. PyErr_Clear();
  1257. }
  1258. /* Wait until threading._shutdown completes, provided
  1259. the threading module was imported in the first place.
  1260. The shutdown routine will wait until all non-daemon
  1261. "threading" threads have completed. */
  1262. static void
  1263. wait_for_thread_shutdown(void)
  1264. {
  1265. #ifdef WITH_THREAD
  1266. _Py_IDENTIFIER(_shutdown);
  1267. PyObject *result;
  1268. PyThreadState *tstate = PyThreadState_GET();
  1269. PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
  1270. "threading");
  1271. if (threading == NULL) {
  1272. /* threading not imported */
  1273. PyErr_Clear();
  1274. return;
  1275. }
  1276. result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL);
  1277. if (result == NULL) {
  1278. PyErr_WriteUnraisable(threading);
  1279. }
  1280. else {
  1281. Py_DECREF(result);
  1282. }
  1283. Py_DECREF(threading);
  1284. #endif
  1285. }
  1286. #define NEXITFUNCS 32
  1287. static void (*exitfuncs[NEXITFUNCS])(void);
  1288. static int nexitfuncs = 0;
  1289. int Py_AtExit(void (*func)(void))
  1290. {
  1291. if (nexitfuncs >= NEXITFUNCS)
  1292. return -1;
  1293. exitfuncs[nexitfuncs++] = func;
  1294. return 0;
  1295. }
  1296. static void
  1297. call_ll_exitfuncs(void)
  1298. {
  1299. while (nexitfuncs > 0)
  1300. (*exitfuncs[--nexitfuncs])();
  1301. fflush(stdout);
  1302. fflush(stderr);
  1303. }
  1304. void
  1305. Py_Exit(int sts)
  1306. {
  1307. if (Py_FinalizeEx() < 0) {
  1308. sts = 120;
  1309. }
  1310. exit(sts);
  1311. }
  1312. static void
  1313. initsigs(void)
  1314. {
  1315. #ifdef SIGPIPE
  1316. PyOS_setsig(SIGPIPE, SIG_IGN);
  1317. #endif
  1318. #ifdef SIGXFZ
  1319. PyOS_setsig(SIGXFZ, SIG_IGN);
  1320. #endif
  1321. #ifdef SIGXFSZ
  1322. PyOS_setsig(SIGXFSZ, SIG_IGN);
  1323. #endif
  1324. PyOS_InitInterrupts(); /* May imply initsignal() */
  1325. if (PyErr_Occurred()) {
  1326. Py_FatalError("Py_Initialize: can't import signal");
  1327. }
  1328. }
  1329. /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
  1330. *
  1331. * All of the code in this function must only use async-signal-safe functions,
  1332. * listed at `man 7 signal` or
  1333. * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
  1334. */
  1335. void
  1336. _Py_RestoreSignals(void)
  1337. {
  1338. #ifdef SIGPIPE
  1339. PyOS_setsig(SIGPIPE, SIG_DFL);
  1340. #endif
  1341. #ifdef SIGXFZ
  1342. PyOS_setsig(SIGXFZ, SIG_DFL);
  1343. #endif
  1344. #ifdef SIGXFSZ
  1345. PyOS_setsig(SIGXFSZ, SIG_DFL);
  1346. #endif
  1347. }
  1348. /*
  1349. * The file descriptor fd is considered ``interactive'' if either
  1350. * a) isatty(fd) is TRUE, or
  1351. * b) the -i flag was given, and the filename associated with
  1352. * the descriptor is NULL or "<stdin>" or "???".
  1353. */
  1354. int
  1355. Py_FdIsInteractive(FILE *fp, const char *filename)
  1356. {
  1357. if (isatty((int)fileno(fp)))
  1358. return 1;
  1359. if (!Py_InteractiveFlag)
  1360. return 0;
  1361. return (filename == NULL) ||
  1362. (strcmp(filename, "<stdin>") == 0) ||
  1363. (strcmp(filename, "???") == 0);
  1364. }
  1365. /* Wrappers around sigaction() or signal(). */
  1366. PyOS_sighandler_t
  1367. PyOS_getsig(int sig)
  1368. {
  1369. #ifdef HAVE_SIGACTION
  1370. struct sigaction context;
  1371. if (sigaction(sig, NULL, &context) == -1)
  1372. return SIG_ERR;
  1373. return context.sa_handler;
  1374. #else
  1375. PyOS_sighandler_t handler;
  1376. /* Special signal handling for the secure CRT in Visual Studio 2005 */
  1377. #if defined(_MSC_VER) && _MSC_VER >= 1400
  1378. switch (sig) {
  1379. /* Only these signals are valid */
  1380. case SIGINT:
  1381. case SIGILL:
  1382. case SIGFPE:
  1383. case SIGSEGV:
  1384. case SIGTERM:
  1385. case SIGBREAK:
  1386. case SIGABRT:
  1387. break;
  1388. /* Don't call signal() with other values or it will assert */
  1389. default:
  1390. return SIG_ERR;
  1391. }
  1392. #endif /* _MSC_VER && _MSC_VER >= 1400 */
  1393. handler = signal(sig, SIG_IGN);
  1394. if (handler != SIG_ERR)
  1395. signal(sig, handler);
  1396. return handler;
  1397. #endif
  1398. }
  1399. /*
  1400. * All of the code in this function must only use async-signal-safe functions,
  1401. * listed at `man 7 signal` or
  1402. * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
  1403. */
  1404. PyOS_sighandler_t
  1405. PyOS_setsig(int sig, PyOS_sighandler_t handler)
  1406. {
  1407. #ifdef HAVE_SIGACTION
  1408. /* Some code in Modules/signalmodule.c depends on sigaction() being
  1409. * used here if HAVE_SIGACTION is defined. Fix that if this code
  1410. * changes to invalidate that assumption.
  1411. */
  1412. struct sigaction context, ocontext;
  1413. context.sa_handler = handler;
  1414. sigemptyset(&context.sa_mask);
  1415. context.sa_flags = 0;
  1416. if (sigaction(sig, &context, &ocontext) == -1)
  1417. return SIG_ERR;
  1418. return ocontext.sa_handler;
  1419. #else
  1420. PyOS_sighandler_t oldhandler;
  1421. oldhandler = signal(sig, handler);
  1422. #ifdef HAVE_SIGINTERRUPT
  1423. siginterrupt(sig, 1);
  1424. #endif
  1425. return oldhandler;
  1426. #endif
  1427. }
  1428. #ifdef __cplusplus
  1429. }
  1430. #endif