Line data Source code
1 :
2 : /* Python interpreter top-level routines, including init/exit */
3 :
4 : #include "Python.h"
5 :
6 : #include "Python-ast.h"
7 : #undef Yield /* undefine macro conflicting with winbase.h */
8 : #include "grammar.h"
9 : #include "node.h"
10 : #include "token.h"
11 : #include "parsetok.h"
12 : #include "errcode.h"
13 : #include "code.h"
14 : #include "symtable.h"
15 : #include "ast.h"
16 : #include "marshal.h"
17 : #include "osdefs.h"
18 :
19 : #ifdef HAVE_SIGNAL_H
20 : #include <signal.h>
21 : #endif
22 :
23 : #ifdef MS_WINDOWS
24 : #include "malloc.h" /* for alloca */
25 : #endif
26 :
27 : #ifdef HAVE_LANGINFO_H
28 : #include <locale.h>
29 : #include <langinfo.h>
30 : #endif
31 :
32 : #ifdef MS_WINDOWS
33 : #undef BYTE
34 : #include "windows.h"
35 : #define PATH_MAX MAXPATHLEN
36 : #endif
37 :
38 : #ifndef Py_REF_DEBUG
39 : #define PRINT_TOTAL_REFS()
40 : #else /* Py_REF_DEBUG */
41 : #define PRINT_TOTAL_REFS() fprintf(stderr, \
42 : "[%" PY_FORMAT_SIZE_T "d refs]\n", \
43 : _Py_GetRefTotal())
44 : #endif
45 :
46 : #ifdef __cplusplus
47 : extern "C" {
48 : #endif
49 :
50 : extern wchar_t *Py_GetPath(void);
51 :
52 : extern grammar _PyParser_Grammar; /* From graminit.c */
53 :
54 : /* Forward */
55 : static void initmain(PyInterpreterState *interp);
56 : static int initfsencoding(PyInterpreterState *interp);
57 : static void initsite(void);
58 : static int initstdio(void);
59 : static void flush_io(void);
60 : static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
61 : PyCompilerFlags *, PyArena *);
62 : static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
63 : PyCompilerFlags *);
64 : static void err_input(perrdetail *);
65 : static void err_free(perrdetail *);
66 : static void initsigs(void);
67 : static void call_py_exitfuncs(void);
68 : static void wait_for_thread_shutdown(void);
69 : static void call_ll_exitfuncs(void);
70 : extern int _PyUnicode_Init(void);
71 : extern void _PyUnicode_Fini(void);
72 : extern int _PyLong_Init(void);
73 : extern void PyLong_Fini(void);
74 : extern int _PyFaulthandler_Init(void);
75 : extern void _PyFaulthandler_Fini(void);
76 :
77 : #ifdef WITH_THREAD
78 : extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
79 : extern void _PyGILState_Fini(void);
80 : #endif /* WITH_THREAD */
81 :
82 : int Py_DebugFlag; /* Needed by parser.c */
83 : int Py_VerboseFlag; /* Needed by import.c */
84 : int Py_QuietFlag; /* Needed by sysmodule.c */
85 : int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
86 : int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
87 : int Py_NoSiteFlag; /* Suppress 'import site' */
88 : int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
89 : int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
90 : int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
91 : int Py_FrozenFlag; /* Needed by getpath.c */
92 : int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
93 : int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
94 : int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
95 : int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
96 :
97 : PyThreadState *_Py_Finalizing = NULL;
98 :
99 : /* PyModule_GetWarningsModule is no longer necessary as of 2.6
100 : since _warnings is builtin. This API should not be used. */
101 : PyObject *
102 0 : PyModule_GetWarningsModule(void)
103 : {
104 0 : return PyImport_ImportModule("warnings");
105 : }
106 :
107 : static int initialized = 0;
108 :
109 : /* API to access the initialized flag -- useful for esoteric use */
110 :
111 : int
112 11 : Py_IsInitialized(void)
113 : {
114 11 : return initialized;
115 : }
116 :
117 : /* Global initializations. Can be undone by Py_Finalize(). Don't
118 : call this twice without an intervening Py_Finalize() call. When
119 : initializations fail, a fatal error is issued and the function does
120 : not return. On return, the first thread and interpreter state have
121 : been created.
122 :
123 : Locking: you must hold the interpreter lock while calling this.
124 : (If the lock has not yet been initialized, that's equivalent to
125 : having the lock, but you cannot use multiple threads.)
126 :
127 : */
128 :
129 : static int
130 0 : add_flag(int flag, const char *envs)
131 : {
132 0 : int env = atoi(envs);
133 0 : if (flag < env)
134 0 : flag = env;
135 0 : if (flag < 1)
136 0 : flag = 1;
137 0 : return flag;
138 : }
139 :
140 : static char*
141 1 : get_codec_name(const char *encoding)
142 : {
143 : char *name_utf8, *name_str;
144 1 : PyObject *codec, *name = NULL;
145 : _Py_IDENTIFIER(name);
146 :
147 1 : codec = _PyCodec_Lookup(encoding);
148 1 : if (!codec)
149 0 : goto error;
150 :
151 1 : name = _PyObject_GetAttrId(codec, &PyId_name);
152 1 : Py_CLEAR(codec);
153 1 : if (!name)
154 0 : goto error;
155 :
156 1 : name_utf8 = _PyUnicode_AsString(name);
157 1 : if (name_utf8 == NULL)
158 0 : goto error;
159 1 : name_str = strdup(name_utf8);
160 1 : Py_DECREF(name);
161 1 : if (name_str == NULL) {
162 0 : PyErr_NoMemory();
163 0 : return NULL;
164 : }
165 1 : return name_str;
166 :
167 : error:
168 0 : Py_XDECREF(codec);
169 0 : Py_XDECREF(name);
170 0 : return NULL;
171 : }
172 :
173 : static char*
174 1 : get_locale_encoding(void)
175 : {
176 : #ifdef MS_WINDOWS
177 : char codepage[100];
178 : PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
179 : return get_codec_name(codepage);
180 : #elif defined(HAVE_LANGINFO_H) && defined(CODESET)
181 1 : char* codeset = nl_langinfo(CODESET);
182 1 : if (!codeset || codeset[0] == '\0') {
183 0 : PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
184 0 : return NULL;
185 : }
186 1 : return get_codec_name(codeset);
187 : #else
188 : PyErr_SetNone(PyExc_NotImplementedError);
189 : return NULL;
190 : #endif
191 : }
192 :
193 : static void
194 1 : import_init(PyInterpreterState *interp, PyObject *sysmod)
195 : {
196 : PyObject *importlib;
197 : PyObject *impmod;
198 : PyObject *sys_modules;
199 : PyObject *value;
200 :
201 : /* Import _importlib through its frozen version, _frozen_importlib. */
202 1 : if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
203 0 : Py_FatalError("Py_Initialize: can't import _frozen_importlib");
204 : }
205 1 : else if (Py_VerboseFlag) {
206 0 : PySys_FormatStderr("import _frozen_importlib # frozen\n");
207 : }
208 1 : importlib = PyImport_AddModule("_frozen_importlib");
209 1 : if (importlib == NULL) {
210 0 : Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from "
211 : "sys.modules");
212 : }
213 1 : interp->importlib = importlib;
214 1 : Py_INCREF(interp->importlib);
215 :
216 : /* Install _importlib as __import__ */
217 1 : impmod = PyInit_imp();
218 1 : if (impmod == NULL) {
219 0 : Py_FatalError("Py_Initialize: can't import imp");
220 : }
221 1 : else if (Py_VerboseFlag) {
222 0 : PySys_FormatStderr("import imp # builtin\n");
223 : }
224 1 : sys_modules = PyImport_GetModuleDict();
225 1 : if (Py_VerboseFlag) {
226 0 : PySys_FormatStderr("import sys # builtin\n");
227 : }
228 1 : if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) {
229 0 : Py_FatalError("Py_Initialize: can't save _imp to sys.modules");
230 : }
231 :
232 1 : value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
233 1 : if (value == NULL) {
234 0 : PyErr_Print();
235 0 : Py_FatalError("Py_Initialize: importlib install failed");
236 : }
237 1 : Py_DECREF(value);
238 1 : Py_DECREF(impmod);
239 :
240 1 : _PyImportZip_Init();
241 1 : }
242 :
243 :
244 : void
245 1 : _Py_InitializeEx_Private(int install_sigs, int install_importlib)
246 : {
247 : PyInterpreterState *interp;
248 : PyThreadState *tstate;
249 : PyObject *bimod, *sysmod, *pstderr;
250 : char *p;
251 : extern void _Py_ReadyTypes(void);
252 :
253 1 : if (initialized)
254 0 : return;
255 1 : initialized = 1;
256 1 : _Py_Finalizing = NULL;
257 :
258 : #if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
259 : /* Set up the LC_CTYPE locale, so we can obtain
260 : the locale's charset without having to switch
261 : locales. */
262 1 : setlocale(LC_CTYPE, "");
263 : #endif
264 :
265 1 : if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
266 0 : Py_DebugFlag = add_flag(Py_DebugFlag, p);
267 1 : if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
268 0 : Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
269 1 : if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
270 0 : Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
271 1 : if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
272 0 : Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
273 : /* The variable is only tested for existence here; _PyRandom_Init will
274 : check its value further. */
275 1 : if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
276 0 : Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
277 :
278 1 : _PyRandom_Init();
279 :
280 1 : interp = PyInterpreterState_New();
281 1 : if (interp == NULL)
282 0 : Py_FatalError("Py_Initialize: can't make first interpreter");
283 :
284 1 : tstate = PyThreadState_New(interp);
285 1 : if (tstate == NULL)
286 0 : Py_FatalError("Py_Initialize: can't make first thread");
287 1 : (void) PyThreadState_Swap(tstate);
288 :
289 : #ifdef WITH_THREAD
290 : /* We can't call _PyEval_FiniThreads() in Py_Finalize because
291 : destroying the GIL might fail when it is being referenced from
292 : another running thread (see issue #9901).
293 : Instead we destroy the previously created GIL here, which ensures
294 : that we can call Py_Initialize / Py_Finalize multiple times. */
295 1 : _PyEval_FiniThreads();
296 :
297 : /* Auto-thread-state API */
298 1 : _PyGILState_Init(interp, tstate);
299 : #endif /* WITH_THREAD */
300 :
301 1 : _Py_ReadyTypes();
302 :
303 1 : if (!_PyFrame_Init())
304 0 : Py_FatalError("Py_Initialize: can't init frames");
305 :
306 1 : if (!_PyLong_Init())
307 0 : Py_FatalError("Py_Initialize: can't init longs");
308 :
309 1 : if (!PyByteArray_Init())
310 0 : Py_FatalError("Py_Initialize: can't init bytearray");
311 :
312 1 : _PyFloat_Init();
313 :
314 1 : interp->modules = PyDict_New();
315 1 : if (interp->modules == NULL)
316 0 : Py_FatalError("Py_Initialize: can't make modules dictionary");
317 :
318 : /* Init Unicode implementation; relies on the codec registry */
319 1 : if (_PyUnicode_Init() < 0)
320 0 : Py_FatalError("Py_Initialize: can't initialize unicode");
321 :
322 1 : bimod = _PyBuiltin_Init();
323 1 : if (bimod == NULL)
324 0 : Py_FatalError("Py_Initialize: can't initialize builtins modules");
325 1 : _PyImport_FixupBuiltin(bimod, "builtins");
326 1 : interp->builtins = PyModule_GetDict(bimod);
327 1 : if (interp->builtins == NULL)
328 0 : Py_FatalError("Py_Initialize: can't initialize builtins dict");
329 1 : Py_INCREF(interp->builtins);
330 :
331 : /* initialize builtin exceptions */
332 1 : _PyExc_Init(bimod);
333 :
334 1 : sysmod = _PySys_Init();
335 1 : if (sysmod == NULL)
336 0 : Py_FatalError("Py_Initialize: can't initialize sys");
337 1 : interp->sysdict = PyModule_GetDict(sysmod);
338 1 : if (interp->sysdict == NULL)
339 0 : Py_FatalError("Py_Initialize: can't initialize sys dict");
340 1 : Py_INCREF(interp->sysdict);
341 1 : _PyImport_FixupBuiltin(sysmod, "sys");
342 1 : PySys_SetPath(Py_GetPath());
343 1 : PyDict_SetItemString(interp->sysdict, "modules",
344 : interp->modules);
345 :
346 : /* Set up a preliminary stderr printer until we have enough
347 : infrastructure for the io module in place. */
348 1 : pstderr = PyFile_NewStdPrinter(fileno(stderr));
349 1 : if (pstderr == NULL)
350 0 : Py_FatalError("Py_Initialize: can't set preliminary stderr");
351 1 : PySys_SetObject("stderr", pstderr);
352 1 : PySys_SetObject("__stderr__", pstderr);
353 1 : Py_DECREF(pstderr);
354 :
355 1 : _PyImport_Init();
356 :
357 1 : _PyImportHooks_Init();
358 :
359 : /* Initialize _warnings. */
360 1 : _PyWarnings_Init();
361 :
362 1 : if (!install_importlib)
363 0 : return;
364 :
365 1 : import_init(interp, sysmod);
366 :
367 : /* initialize the faulthandler module */
368 1 : if (_PyFaulthandler_Init())
369 0 : Py_FatalError("Py_Initialize: can't initialize faulthandler");
370 :
371 1 : _PyTime_Init();
372 :
373 1 : if (initfsencoding(interp) < 0)
374 0 : Py_FatalError("Py_Initialize: unable to load the file system codec");
375 :
376 1 : if (install_sigs)
377 1 : initsigs(); /* Signal handling stuff, including initintr() */
378 :
379 1 : initmain(interp); /* Module __main__ */
380 1 : if (initstdio() < 0)
381 0 : Py_FatalError(
382 : "Py_Initialize: can't initialize sys standard streams");
383 :
384 : /* Initialize warnings. */
385 1 : if (PySys_HasWarnOptions()) {
386 0 : PyObject *warnings_module = PyImport_ImportModule("warnings");
387 0 : if (warnings_module == NULL) {
388 0 : fprintf(stderr, "'import warnings' failed; traceback:\n");
389 0 : PyErr_Print();
390 : }
391 0 : Py_XDECREF(warnings_module);
392 : }
393 :
394 1 : if (!Py_NoSiteFlag)
395 1 : initsite(); /* Module site */
396 : }
397 :
398 : void
399 1 : Py_InitializeEx(int install_sigs)
400 : {
401 1 : _Py_InitializeEx_Private(install_sigs, 1);
402 1 : }
403 :
404 : void
405 1 : Py_Initialize(void)
406 : {
407 1 : Py_InitializeEx(1);
408 1 : }
409 :
410 :
411 : #ifdef COUNT_ALLOCS
412 : extern void dump_counts(FILE*);
413 : #endif
414 :
415 : /* Flush stdout and stderr */
416 :
417 : static int
418 0 : file_is_closed(PyObject *fobj)
419 : {
420 : int r;
421 0 : PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
422 0 : if (tmp == NULL) {
423 0 : PyErr_Clear();
424 0 : return 0;
425 : }
426 0 : r = PyObject_IsTrue(tmp);
427 0 : Py_DECREF(tmp);
428 0 : if (r < 0)
429 0 : PyErr_Clear();
430 0 : return r > 0;
431 : }
432 :
433 : static void
434 0 : flush_std_files(void)
435 : {
436 0 : PyObject *fout = PySys_GetObject("stdout");
437 0 : PyObject *ferr = PySys_GetObject("stderr");
438 : PyObject *tmp;
439 : _Py_IDENTIFIER(flush);
440 :
441 0 : if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
442 0 : tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
443 0 : if (tmp == NULL)
444 0 : PyErr_WriteUnraisable(fout);
445 : else
446 0 : Py_DECREF(tmp);
447 : }
448 :
449 0 : if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
450 0 : tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
451 0 : if (tmp == NULL)
452 0 : PyErr_Clear();
453 : else
454 0 : Py_DECREF(tmp);
455 : }
456 0 : }
457 :
458 : /* Undo the effect of Py_Initialize().
459 :
460 : Beware: if multiple interpreter and/or thread states exist, these
461 : are not wiped out; only the current thread and interpreter state
462 : are deleted. But since everything else is deleted, those other
463 : interpreter and thread states should no longer be used.
464 :
465 : (XXX We should do better, e.g. wipe out all interpreters and
466 : threads.)
467 :
468 : Locking: as above.
469 :
470 : */
471 :
472 : void
473 0 : Py_Finalize(void)
474 : {
475 : PyInterpreterState *interp;
476 : PyThreadState *tstate;
477 :
478 0 : if (!initialized)
479 0 : return;
480 :
481 0 : wait_for_thread_shutdown();
482 :
483 : /* The interpreter is still entirely intact at this point, and the
484 : * exit funcs may be relying on that. In particular, if some thread
485 : * or exit func is still waiting to do an import, the import machinery
486 : * expects Py_IsInitialized() to return true. So don't say the
487 : * interpreter is uninitialized until after the exit funcs have run.
488 : * Note that Threading.py uses an exit func to do a join on all the
489 : * threads created thru it, so this also protects pending imports in
490 : * the threads created via Threading.
491 : */
492 0 : call_py_exitfuncs();
493 :
494 : /* Get current thread state and interpreter pointer */
495 0 : tstate = PyThreadState_GET();
496 0 : interp = tstate->interp;
497 :
498 : /* Remaining threads (e.g. daemon threads) will automatically exit
499 : after taking the GIL (in PyEval_RestoreThread()). */
500 0 : _Py_Finalizing = tstate;
501 0 : initialized = 0;
502 :
503 : /* Flush stdout+stderr */
504 0 : flush_std_files();
505 :
506 : /* Disable signal handling */
507 0 : PyOS_FiniInterrupts();
508 :
509 : /* Clear type lookup cache */
510 0 : PyType_ClearCache();
511 :
512 : /* Collect garbage. This may call finalizers; it's nice to call these
513 : * before all modules are destroyed.
514 : * XXX If a __del__ or weakref callback is triggered here, and tries to
515 : * XXX import a module, bad things can happen, because Python no
516 : * XXX longer believes it's initialized.
517 : * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
518 : * XXX is easy to provoke that way. I've also seen, e.g.,
519 : * XXX Exception exceptions.ImportError: 'No module named sha'
520 : * XXX in <function callback at 0x008F5718> ignored
521 : * XXX but I'm unclear on exactly how that one happens. In any case,
522 : * XXX I haven't seen a real-life report of either of these.
523 : */
524 0 : PyGC_Collect();
525 : #ifdef COUNT_ALLOCS
526 : /* With COUNT_ALLOCS, it helps to run GC multiple times:
527 : each collection might release some types from the type
528 : list, so they become garbage. */
529 : while (PyGC_Collect() > 0)
530 : /* nothing */;
531 : #endif
532 : /* We run this while most interpreter state is still alive, so that
533 : debug information can be printed out */
534 0 : _PyGC_Fini();
535 :
536 : /* Destroy all modules */
537 0 : PyImport_Cleanup();
538 :
539 : /* Flush stdout+stderr (again, in case more was printed) */
540 0 : flush_std_files();
541 :
542 : /* Collect final garbage. This disposes of cycles created by
543 : * class definitions, for example.
544 : * XXX This is disabled because it caused too many problems. If
545 : * XXX a __del__ or weakref callback triggers here, Python code has
546 : * XXX a hard time running, because even the sys module has been
547 : * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
548 : * XXX One symptom is a sequence of information-free messages
549 : * XXX coming from threads (if a __del__ or callback is invoked,
550 : * XXX other threads can execute too, and any exception they encounter
551 : * XXX triggers a comedy of errors as subsystem after subsystem
552 : * XXX fails to find what it *expects* to find in sys to help report
553 : * XXX the exception and consequent unexpected failures). I've also
554 : * XXX seen segfaults then, after adding print statements to the
555 : * XXX Python code getting called.
556 : */
557 : #if 0
558 : PyGC_Collect();
559 : #endif
560 :
561 : /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
562 0 : _PyImport_Fini();
563 :
564 : /* unload faulthandler module */
565 0 : _PyFaulthandler_Fini();
566 :
567 : /* Debugging stuff */
568 : #ifdef COUNT_ALLOCS
569 : dump_counts(stdout);
570 : #endif
571 :
572 : PRINT_TOTAL_REFS();
573 :
574 : #ifdef Py_TRACE_REFS
575 : /* Display all objects still alive -- this can invoke arbitrary
576 : * __repr__ overrides, so requires a mostly-intact interpreter.
577 : * Alas, a lot of stuff may still be alive now that will be cleaned
578 : * up later.
579 : */
580 : if (Py_GETENV("PYTHONDUMPREFS"))
581 : _Py_PrintReferences(stderr);
582 : #endif /* Py_TRACE_REFS */
583 :
584 : /* Clear interpreter state */
585 0 : PyInterpreterState_Clear(interp);
586 :
587 : /* Now we decref the exception classes. After this point nothing
588 : can raise an exception. That's okay, because each Fini() method
589 : below has been checked to make sure no exceptions are ever
590 : raised.
591 : */
592 :
593 0 : _PyExc_Fini();
594 :
595 : /* Cleanup auto-thread-state */
596 : #ifdef WITH_THREAD
597 0 : _PyGILState_Fini();
598 : #endif /* WITH_THREAD */
599 :
600 : /* Delete current thread */
601 0 : PyThreadState_Swap(NULL);
602 0 : PyInterpreterState_Delete(interp);
603 :
604 : /* Sundry finalizers */
605 0 : PyMethod_Fini();
606 0 : PyFrame_Fini();
607 0 : PyCFunction_Fini();
608 0 : PyTuple_Fini();
609 0 : PyList_Fini();
610 0 : PySet_Fini();
611 0 : PyBytes_Fini();
612 0 : PyByteArray_Fini();
613 0 : PyLong_Fini();
614 0 : PyFloat_Fini();
615 0 : PyDict_Fini();
616 0 : PySlice_Fini();
617 :
618 : /* Cleanup Unicode implementation */
619 0 : _PyUnicode_Fini();
620 :
621 : /* reset file system default encoding */
622 0 : if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
623 0 : free((char*)Py_FileSystemDefaultEncoding);
624 0 : Py_FileSystemDefaultEncoding = NULL;
625 : }
626 :
627 : /* XXX Still allocated:
628 : - various static ad-hoc pointers to interned strings
629 : - int and float free list blocks
630 : - whatever various modules and libraries allocate
631 : */
632 :
633 0 : PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
634 :
635 : #ifdef Py_TRACE_REFS
636 : /* Display addresses (& refcnts) of all objects still alive.
637 : * An address can be used to find the repr of the object, printed
638 : * above by _Py_PrintReferences.
639 : */
640 : if (Py_GETENV("PYTHONDUMPREFS"))
641 : _Py_PrintReferenceAddresses(stderr);
642 : #endif /* Py_TRACE_REFS */
643 : #ifdef PYMALLOC_DEBUG
644 : if (Py_GETENV("PYTHONMALLOCSTATS"))
645 : _PyObject_DebugMallocStats(stderr);
646 : #endif
647 :
648 0 : call_ll_exitfuncs();
649 : }
650 :
651 : /* Create and initialize a new interpreter and thread, and return the
652 : new thread. This requires that Py_Initialize() has been called
653 : first.
654 :
655 : Unsuccessful initialization yields a NULL pointer. Note that *no*
656 : exception information is available even in this case -- the
657 : exception information is held in the thread, and there is no
658 : thread.
659 :
660 : Locking: as above.
661 :
662 : */
663 :
664 : PyThreadState *
665 0 : Py_NewInterpreter(void)
666 : {
667 : PyInterpreterState *interp;
668 : PyThreadState *tstate, *save_tstate;
669 : PyObject *bimod, *sysmod;
670 :
671 0 : if (!initialized)
672 0 : Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
673 :
674 0 : interp = PyInterpreterState_New();
675 0 : if (interp == NULL)
676 0 : return NULL;
677 :
678 0 : tstate = PyThreadState_New(interp);
679 0 : if (tstate == NULL) {
680 0 : PyInterpreterState_Delete(interp);
681 0 : return NULL;
682 : }
683 :
684 0 : save_tstate = PyThreadState_Swap(tstate);
685 :
686 : /* XXX The following is lax in error checking */
687 :
688 0 : interp->modules = PyDict_New();
689 :
690 0 : bimod = _PyImport_FindBuiltin("builtins");
691 0 : if (bimod != NULL) {
692 0 : interp->builtins = PyModule_GetDict(bimod);
693 0 : if (interp->builtins == NULL)
694 0 : goto handle_error;
695 0 : Py_INCREF(interp->builtins);
696 : }
697 :
698 : /* initialize builtin exceptions */
699 0 : _PyExc_Init(bimod);
700 :
701 0 : sysmod = _PyImport_FindBuiltin("sys");
702 0 : if (bimod != NULL && sysmod != NULL) {
703 : PyObject *pstderr;
704 :
705 0 : interp->sysdict = PyModule_GetDict(sysmod);
706 0 : if (interp->sysdict == NULL)
707 0 : goto handle_error;
708 0 : Py_INCREF(interp->sysdict);
709 0 : PySys_SetPath(Py_GetPath());
710 0 : PyDict_SetItemString(interp->sysdict, "modules",
711 : interp->modules);
712 : /* Set up a preliminary stderr printer until we have enough
713 : infrastructure for the io module in place. */
714 0 : pstderr = PyFile_NewStdPrinter(fileno(stderr));
715 0 : if (pstderr == NULL)
716 0 : Py_FatalError("Py_Initialize: can't set preliminary stderr");
717 0 : PySys_SetObject("stderr", pstderr);
718 0 : PySys_SetObject("__stderr__", pstderr);
719 0 : Py_DECREF(pstderr);
720 :
721 0 : _PyImportHooks_Init();
722 :
723 0 : import_init(interp, sysmod);
724 :
725 0 : if (initfsencoding(interp) < 0)
726 0 : goto handle_error;
727 :
728 0 : if (initstdio() < 0)
729 0 : Py_FatalError(
730 : "Py_Initialize: can't initialize sys standard streams");
731 0 : initmain(interp);
732 0 : if (!Py_NoSiteFlag)
733 0 : initsite();
734 : }
735 :
736 0 : if (!PyErr_Occurred())
737 0 : return tstate;
738 :
739 : handle_error:
740 : /* Oops, it didn't work. Undo it all. */
741 :
742 0 : PyErr_PrintEx(0);
743 0 : PyThreadState_Clear(tstate);
744 0 : PyThreadState_Swap(save_tstate);
745 0 : PyThreadState_Delete(tstate);
746 0 : PyInterpreterState_Delete(interp);
747 :
748 0 : return NULL;
749 : }
750 :
751 : /* Delete an interpreter and its last thread. This requires that the
752 : given thread state is current, that the thread has no remaining
753 : frames, and that it is its interpreter's only remaining thread.
754 : It is a fatal error to violate these constraints.
755 :
756 : (Py_Finalize() doesn't have these constraints -- it zaps
757 : everything, regardless.)
758 :
759 : Locking: as above.
760 :
761 : */
762 :
763 : void
764 0 : Py_EndInterpreter(PyThreadState *tstate)
765 : {
766 0 : PyInterpreterState *interp = tstate->interp;
767 :
768 0 : if (tstate != PyThreadState_GET())
769 0 : Py_FatalError("Py_EndInterpreter: thread is not current");
770 0 : if (tstate->frame != NULL)
771 0 : Py_FatalError("Py_EndInterpreter: thread still has a frame");
772 0 : if (tstate != interp->tstate_head || tstate->next != NULL)
773 0 : Py_FatalError("Py_EndInterpreter: not the last thread");
774 :
775 0 : PyImport_Cleanup();
776 0 : PyInterpreterState_Clear(interp);
777 0 : PyThreadState_Swap(NULL);
778 0 : PyInterpreterState_Delete(interp);
779 0 : }
780 :
781 : #ifdef MS_WINDOWS
782 : static wchar_t *progname = L"python";
783 : #else
784 : static wchar_t *progname = L"python3";
785 : #endif
786 :
787 : void
788 0 : Py_SetProgramName(wchar_t *pn)
789 : {
790 0 : if (pn && *pn)
791 0 : progname = pn;
792 0 : }
793 :
794 : wchar_t *
795 1 : Py_GetProgramName(void)
796 : {
797 1 : return progname;
798 : }
799 :
800 : static wchar_t *default_home = NULL;
801 : static wchar_t env_home[PATH_MAX+1];
802 :
803 : void
804 1 : Py_SetPythonHome(wchar_t *home)
805 : {
806 1 : default_home = home;
807 1 : }
808 :
809 : wchar_t *
810 1 : Py_GetPythonHome(void)
811 : {
812 1 : wchar_t *home = default_home;
813 1 : if (home == NULL && !Py_IgnoreEnvironmentFlag) {
814 0 : char* chome = Py_GETENV("PYTHONHOME");
815 0 : if (chome) {
816 0 : size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
817 0 : if (r != (size_t)-1 && r <= PATH_MAX)
818 0 : home = env_home;
819 : }
820 :
821 : }
822 1 : return home;
823 : }
824 :
825 : /* Create __main__ module */
826 :
827 : static void
828 1 : initmain(PyInterpreterState *interp)
829 : {
830 : PyObject *m, *d;
831 1 : m = PyImport_AddModule("__main__");
832 1 : if (m == NULL)
833 0 : Py_FatalError("can't create __main__ module");
834 1 : d = PyModule_GetDict(m);
835 1 : if (PyDict_GetItemString(d, "__builtins__") == NULL) {
836 1 : PyObject *bimod = PyImport_ImportModule("builtins");
837 1 : if (bimod == NULL) {
838 0 : Py_FatalError("Failed to retrieve builtins module");
839 : }
840 1 : if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
841 0 : Py_FatalError("Failed to initialize __main__.__builtins__");
842 : }
843 1 : Py_DECREF(bimod);
844 : }
845 : /* Main is a little special - imp.is_builtin("__main__") will return
846 : * False, but BuiltinImporter is still the most appropriate initial
847 : * setting for its __loader__ attribute. A more suitable value will
848 : * be set if __main__ gets further initialized later in the startup
849 : * process.
850 : */
851 1 : if (PyDict_GetItemString(d, "__loader__") == NULL) {
852 1 : PyObject *loader = PyObject_GetAttrString(interp->importlib,
853 : "BuiltinImporter");
854 1 : if (loader == NULL) {
855 0 : Py_FatalError("Failed to retrieve BuiltinImporter");
856 : }
857 1 : if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
858 0 : Py_FatalError("Failed to initialize __main__.__loader__");
859 : }
860 1 : Py_DECREF(loader);
861 : }
862 1 : }
863 :
864 : static int
865 1 : initfsencoding(PyInterpreterState *interp)
866 : {
867 : PyObject *codec;
868 :
869 1 : if (Py_FileSystemDefaultEncoding == NULL)
870 : {
871 1 : Py_FileSystemDefaultEncoding = get_locale_encoding();
872 1 : if (Py_FileSystemDefaultEncoding == NULL)
873 0 : Py_FatalError("Py_Initialize: Unable to get the locale encoding");
874 :
875 1 : Py_HasFileSystemDefaultEncoding = 0;
876 1 : interp->fscodec_initialized = 1;
877 1 : return 0;
878 : }
879 :
880 : /* the encoding is mbcs, utf-8 or ascii */
881 0 : codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
882 0 : if (!codec) {
883 : /* Such error can only occurs in critical situations: no more
884 : * memory, import a module of the standard library failed,
885 : * etc. */
886 0 : return -1;
887 : }
888 0 : Py_DECREF(codec);
889 0 : interp->fscodec_initialized = 1;
890 0 : return 0;
891 : }
892 :
893 : /* Import the site module (not into __main__ though) */
894 :
895 : static void
896 1 : initsite(void)
897 : {
898 : PyObject *m;
899 1 : m = PyImport_ImportModule("site");
900 1 : if (m == NULL) {
901 0 : PyErr_Print();
902 0 : Py_Finalize();
903 0 : exit(1);
904 : }
905 : else {
906 1 : Py_DECREF(m);
907 : }
908 1 : }
909 :
910 : static PyObject*
911 3 : create_stdio(PyObject* io,
912 : int fd, int write_mode, char* name,
913 : char* encoding, char* errors)
914 : {
915 3 : PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
916 : const char* mode;
917 : const char* newline;
918 : PyObject *line_buffering;
919 : int buffering, isatty;
920 : _Py_IDENTIFIER(open);
921 : _Py_IDENTIFIER(isatty);
922 : _Py_IDENTIFIER(TextIOWrapper);
923 : _Py_IDENTIFIER(name);
924 : _Py_IDENTIFIER(mode);
925 :
926 : /* stdin is always opened in buffered mode, first because it shouldn't
927 : make a difference in common use cases, second because TextIOWrapper
928 : depends on the presence of a read1() method which only exists on
929 : buffered streams.
930 : */
931 3 : if (Py_UnbufferedStdioFlag && write_mode)
932 0 : buffering = 0;
933 : else
934 3 : buffering = -1;
935 3 : if (write_mode)
936 2 : mode = "wb";
937 : else
938 1 : mode = "rb";
939 3 : buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
940 : fd, mode, buffering,
941 : Py_None, Py_None, Py_None, 0);
942 3 : if (buf == NULL)
943 0 : goto error;
944 :
945 3 : if (buffering) {
946 : _Py_IDENTIFIER(raw);
947 3 : raw = _PyObject_GetAttrId(buf, &PyId_raw);
948 3 : if (raw == NULL)
949 0 : goto error;
950 : }
951 : else {
952 0 : raw = buf;
953 0 : Py_INCREF(raw);
954 : }
955 :
956 3 : text = PyUnicode_FromString(name);
957 3 : if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
958 : goto error;
959 3 : res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
960 3 : if (res == NULL)
961 0 : goto error;
962 3 : isatty = PyObject_IsTrue(res);
963 3 : Py_DECREF(res);
964 3 : if (isatty == -1)
965 0 : goto error;
966 3 : if (isatty || Py_UnbufferedStdioFlag)
967 0 : line_buffering = Py_True;
968 : else
969 3 : line_buffering = Py_False;
970 :
971 3 : Py_CLEAR(raw);
972 3 : Py_CLEAR(text);
973 :
974 : #ifdef MS_WINDOWS
975 : /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
976 : newlines to "\n".
977 : sys.stdout and sys.stderr: translate "\n" to "\r\n". */
978 : newline = NULL;
979 : #else
980 : /* sys.stdin: split lines at "\n".
981 : sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
982 3 : newline = "\n";
983 : #endif
984 :
985 3 : stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
986 : buf, encoding, errors,
987 : newline, line_buffering);
988 3 : Py_CLEAR(buf);
989 3 : if (stream == NULL)
990 0 : goto error;
991 :
992 3 : if (write_mode)
993 2 : mode = "w";
994 : else
995 1 : mode = "r";
996 3 : text = PyUnicode_FromString(mode);
997 3 : if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
998 : goto error;
999 3 : Py_CLEAR(text);
1000 3 : return stream;
1001 :
1002 : error:
1003 0 : Py_XDECREF(buf);
1004 0 : Py_XDECREF(stream);
1005 0 : Py_XDECREF(text);
1006 0 : Py_XDECREF(raw);
1007 0 : return NULL;
1008 : }
1009 :
1010 : static int
1011 3 : is_valid_fd(int fd)
1012 : {
1013 : int dummy_fd;
1014 3 : if (fd < 0 || !_PyVerify_fd(fd))
1015 0 : return 0;
1016 3 : dummy_fd = dup(fd);
1017 3 : if (dummy_fd < 0)
1018 0 : return 0;
1019 3 : close(dummy_fd);
1020 3 : return 1;
1021 : }
1022 :
1023 : /* Initialize sys.stdin, stdout, stderr and builtins.open */
1024 : static int
1025 1 : initstdio(void)
1026 : {
1027 1 : PyObject *iomod = NULL, *wrapper;
1028 1 : PyObject *bimod = NULL;
1029 : PyObject *m;
1030 1 : PyObject *std = NULL;
1031 1 : int status = 0, fd;
1032 : PyObject * encoding_attr;
1033 1 : char *encoding = NULL, *errors;
1034 :
1035 : /* Hack to avoid a nasty recursion issue when Python is invoked
1036 : in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
1037 1 : if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
1038 0 : goto error;
1039 : }
1040 1 : Py_DECREF(m);
1041 :
1042 1 : if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
1043 0 : goto error;
1044 : }
1045 1 : Py_DECREF(m);
1046 :
1047 1 : if (!(bimod = PyImport_ImportModule("builtins"))) {
1048 0 : goto error;
1049 : }
1050 :
1051 1 : if (!(iomod = PyImport_ImportModule("io"))) {
1052 0 : goto error;
1053 : }
1054 1 : if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
1055 0 : goto error;
1056 : }
1057 :
1058 : /* Set builtins.open */
1059 1 : if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
1060 0 : Py_DECREF(wrapper);
1061 0 : goto error;
1062 : }
1063 1 : Py_DECREF(wrapper);
1064 :
1065 1 : encoding = Py_GETENV("PYTHONIOENCODING");
1066 1 : errors = NULL;
1067 1 : if (encoding) {
1068 0 : encoding = strdup(encoding);
1069 0 : errors = strchr(encoding, ':');
1070 0 : if (errors) {
1071 0 : *errors = '\0';
1072 0 : errors++;
1073 : }
1074 : }
1075 :
1076 : /* Set sys.stdin */
1077 1 : fd = fileno(stdin);
1078 : /* Under some conditions stdin, stdout and stderr may not be connected
1079 : * and fileno() may point to an invalid file descriptor. For example
1080 : * GUI apps don't have valid standard streams by default.
1081 : */
1082 1 : if (!is_valid_fd(fd)) {
1083 0 : std = Py_None;
1084 0 : Py_INCREF(std);
1085 : }
1086 : else {
1087 1 : std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
1088 1 : if (std == NULL)
1089 0 : goto error;
1090 : } /* if (fd < 0) */
1091 1 : PySys_SetObject("__stdin__", std);
1092 1 : PySys_SetObject("stdin", std);
1093 1 : Py_DECREF(std);
1094 :
1095 : /* Set sys.stdout */
1096 1 : fd = fileno(stdout);
1097 1 : if (!is_valid_fd(fd)) {
1098 0 : std = Py_None;
1099 0 : Py_INCREF(std);
1100 : }
1101 : else {
1102 1 : std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
1103 1 : if (std == NULL)
1104 0 : goto error;
1105 : } /* if (fd < 0) */
1106 1 : PySys_SetObject("__stdout__", std);
1107 1 : PySys_SetObject("stdout", std);
1108 1 : Py_DECREF(std);
1109 :
1110 : #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
1111 : /* Set sys.stderr, replaces the preliminary stderr */
1112 1 : fd = fileno(stderr);
1113 1 : if (!is_valid_fd(fd)) {
1114 0 : std = Py_None;
1115 0 : Py_INCREF(std);
1116 : }
1117 : else {
1118 1 : std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1119 1 : if (std == NULL)
1120 0 : goto error;
1121 : } /* if (fd < 0) */
1122 :
1123 : /* Same as hack above, pre-import stderr's codec to avoid recursion
1124 : when import.c tries to write to stderr in verbose mode. */
1125 1 : encoding_attr = PyObject_GetAttrString(std, "encoding");
1126 1 : if (encoding_attr != NULL) {
1127 : const char * encoding;
1128 1 : encoding = _PyUnicode_AsString(encoding_attr);
1129 1 : if (encoding != NULL) {
1130 1 : PyObject *codec_info = _PyCodec_Lookup(encoding);
1131 1 : Py_XDECREF(codec_info);
1132 : }
1133 1 : Py_DECREF(encoding_attr);
1134 : }
1135 1 : PyErr_Clear(); /* Not a fatal error if codec isn't available */
1136 :
1137 1 : PySys_SetObject("__stderr__", std);
1138 1 : PySys_SetObject("stderr", std);
1139 1 : Py_DECREF(std);
1140 : #endif
1141 :
1142 : if (0) {
1143 : error:
1144 0 : status = -1;
1145 : }
1146 :
1147 1 : if (encoding)
1148 0 : free(encoding);
1149 1 : Py_XDECREF(bimod);
1150 1 : Py_XDECREF(iomod);
1151 1 : return status;
1152 : }
1153 :
1154 : /* Parse input from a file and execute it */
1155 :
1156 : int
1157 0 : PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
1158 : PyCompilerFlags *flags)
1159 : {
1160 0 : if (filename == NULL)
1161 0 : filename = "???";
1162 0 : if (Py_FdIsInteractive(fp, filename)) {
1163 0 : int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1164 0 : if (closeit)
1165 0 : fclose(fp);
1166 0 : return err;
1167 : }
1168 : else
1169 0 : return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
1170 : }
1171 :
1172 : int
1173 0 : PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
1174 : {
1175 : PyObject *v;
1176 : int ret;
1177 : PyCompilerFlags local_flags;
1178 :
1179 0 : if (flags == NULL) {
1180 0 : flags = &local_flags;
1181 0 : local_flags.cf_flags = 0;
1182 : }
1183 0 : v = PySys_GetObject("ps1");
1184 0 : if (v == NULL) {
1185 0 : PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1186 0 : Py_XDECREF(v);
1187 : }
1188 0 : v = PySys_GetObject("ps2");
1189 0 : if (v == NULL) {
1190 0 : PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1191 0 : Py_XDECREF(v);
1192 : }
1193 : for (;;) {
1194 0 : ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1195 : PRINT_TOTAL_REFS();
1196 0 : if (ret == E_EOF)
1197 0 : return 0;
1198 : /*
1199 : if (ret == E_NOMEM)
1200 : return -1;
1201 : */
1202 0 : }
1203 : }
1204 :
1205 : /* compute parser flags based on compiler flags */
1206 3 : static int PARSER_FLAGS(PyCompilerFlags *flags)
1207 : {
1208 3 : int parser_flags = 0;
1209 3 : if (!flags)
1210 0 : return 0;
1211 3 : if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1212 0 : parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1213 3 : if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1214 3 : parser_flags |= PyPARSE_IGNORE_COOKIE;
1215 3 : if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1216 0 : parser_flags |= PyPARSE_BARRY_AS_BDFL;
1217 3 : return parser_flags;
1218 : }
1219 :
1220 : #if 0
1221 : /* Keep an example of flags with future keyword support. */
1222 : #define PARSER_FLAGS(flags) \
1223 : ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1224 : PyPARSE_DONT_IMPLY_DEDENT : 0) \
1225 : | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1226 : PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
1227 : #endif
1228 :
1229 : int
1230 0 : PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
1231 : {
1232 0 : PyObject *m, *d, *v, *w, *oenc = NULL;
1233 : mod_ty mod;
1234 : PyArena *arena;
1235 0 : char *ps1 = "", *ps2 = "", *enc = NULL;
1236 0 : int errcode = 0;
1237 : _Py_IDENTIFIER(encoding);
1238 :
1239 0 : if (fp == stdin) {
1240 : /* Fetch encoding from sys.stdin */
1241 0 : v = PySys_GetObject("stdin");
1242 0 : if (v == NULL || v == Py_None)
1243 0 : return -1;
1244 0 : oenc = _PyObject_GetAttrId(v, &PyId_encoding);
1245 0 : if (!oenc)
1246 0 : return -1;
1247 0 : enc = _PyUnicode_AsString(oenc);
1248 0 : if (enc == NULL)
1249 0 : return -1;
1250 : }
1251 0 : v = PySys_GetObject("ps1");
1252 0 : if (v != NULL) {
1253 0 : v = PyObject_Str(v);
1254 0 : if (v == NULL)
1255 0 : PyErr_Clear();
1256 0 : else if (PyUnicode_Check(v)) {
1257 0 : ps1 = _PyUnicode_AsString(v);
1258 0 : if (ps1 == NULL) {
1259 0 : PyErr_Clear();
1260 0 : ps1 = "";
1261 : }
1262 : }
1263 : }
1264 0 : w = PySys_GetObject("ps2");
1265 0 : if (w != NULL) {
1266 0 : w = PyObject_Str(w);
1267 0 : if (w == NULL)
1268 0 : PyErr_Clear();
1269 0 : else if (PyUnicode_Check(w)) {
1270 0 : ps2 = _PyUnicode_AsString(w);
1271 0 : if (ps2 == NULL) {
1272 0 : PyErr_Clear();
1273 0 : ps2 = "";
1274 : }
1275 : }
1276 : }
1277 0 : arena = PyArena_New();
1278 0 : if (arena == NULL) {
1279 0 : Py_XDECREF(v);
1280 0 : Py_XDECREF(w);
1281 0 : Py_XDECREF(oenc);
1282 0 : return -1;
1283 : }
1284 0 : mod = PyParser_ASTFromFile(fp, filename, enc,
1285 : Py_single_input, ps1, ps2,
1286 : flags, &errcode, arena);
1287 0 : Py_XDECREF(v);
1288 0 : Py_XDECREF(w);
1289 0 : Py_XDECREF(oenc);
1290 0 : if (mod == NULL) {
1291 0 : PyArena_Free(arena);
1292 0 : if (errcode == E_EOF) {
1293 0 : PyErr_Clear();
1294 0 : return E_EOF;
1295 : }
1296 0 : PyErr_Print();
1297 0 : return -1;
1298 : }
1299 0 : m = PyImport_AddModule("__main__");
1300 0 : if (m == NULL) {
1301 0 : PyArena_Free(arena);
1302 0 : return -1;
1303 : }
1304 0 : d = PyModule_GetDict(m);
1305 0 : v = run_mod(mod, filename, d, d, flags, arena);
1306 0 : PyArena_Free(arena);
1307 0 : flush_io();
1308 0 : if (v == NULL) {
1309 0 : PyErr_Print();
1310 0 : return -1;
1311 : }
1312 0 : Py_DECREF(v);
1313 0 : return 0;
1314 : }
1315 :
1316 : /* Check whether a file maybe a pyc file: Look at the extension,
1317 : the file type, and, if we may close it, at the first few bytes. */
1318 :
1319 : static int
1320 0 : maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
1321 : {
1322 0 : if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1323 0 : return 1;
1324 :
1325 : /* Only look into the file if we are allowed to close it, since
1326 : it then should also be seekable. */
1327 0 : if (closeit) {
1328 : /* Read only two bytes of the magic. If the file was opened in
1329 : text mode, the bytes 3 and 4 of the magic (\r\n) might not
1330 : be read as they are on disk. */
1331 0 : unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1332 : unsigned char buf[2];
1333 : /* Mess: In case of -x, the stream is NOT at its start now,
1334 : and ungetc() was used to push back the first newline,
1335 : which makes the current stream position formally undefined,
1336 : and a x-platform nightmare.
1337 : Unfortunately, we have no direct way to know whether -x
1338 : was specified. So we use a terrible hack: if the current
1339 : stream position is not 0, we assume -x was specified, and
1340 : give up. Bug 132850 on SourceForge spells out the
1341 : hopelessness of trying anything else (fseek and ftell
1342 : don't work predictably x-platform for text-mode files).
1343 : */
1344 0 : int ispyc = 0;
1345 0 : if (ftell(fp) == 0) {
1346 0 : if (fread(buf, 1, 2, fp) == 2 &&
1347 0 : ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1348 0 : ispyc = 1;
1349 0 : rewind(fp);
1350 : }
1351 0 : return ispyc;
1352 : }
1353 0 : return 0;
1354 : }
1355 :
1356 : int
1357 0 : static set_main_loader(PyObject *d, const char *filename, const char *loader_name)
1358 : {
1359 : PyInterpreterState *interp;
1360 : PyThreadState *tstate;
1361 : PyObject *loader_type, *loader;
1362 0 : int result = 0;
1363 : /* Get current thread state and interpreter pointer */
1364 0 : tstate = PyThreadState_GET();
1365 0 : interp = tstate->interp;
1366 0 : loader_type = PyObject_GetAttrString(interp->importlib, loader_name);
1367 0 : if (loader_type == NULL) {
1368 0 : return -1;
1369 : }
1370 0 : loader = PyObject_CallFunction(loader_type, "ss", "__main__", filename);
1371 0 : Py_DECREF(loader_type);
1372 0 : if (loader == NULL) {
1373 0 : return -1;
1374 : }
1375 0 : if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
1376 0 : result = -1;
1377 : }
1378 0 : Py_DECREF(loader);
1379 0 : return result;
1380 : }
1381 :
1382 : int
1383 0 : PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
1384 : PyCompilerFlags *flags)
1385 : {
1386 : PyObject *m, *d, *v;
1387 : const char *ext;
1388 0 : int set_file_name = 0, ret;
1389 : size_t len;
1390 :
1391 0 : m = PyImport_AddModule("__main__");
1392 0 : if (m == NULL)
1393 0 : return -1;
1394 0 : d = PyModule_GetDict(m);
1395 0 : if (PyDict_GetItemString(d, "__file__") == NULL) {
1396 : PyObject *f;
1397 0 : f = PyUnicode_DecodeFSDefault(filename);
1398 0 : if (f == NULL)
1399 0 : return -1;
1400 0 : if (PyDict_SetItemString(d, "__file__", f) < 0) {
1401 0 : Py_DECREF(f);
1402 0 : return -1;
1403 : }
1404 0 : if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1405 0 : Py_DECREF(f);
1406 0 : return -1;
1407 : }
1408 0 : set_file_name = 1;
1409 0 : Py_DECREF(f);
1410 : }
1411 0 : len = strlen(filename);
1412 0 : ext = filename + len - (len > 4 ? 4 : 0);
1413 0 : if (maybe_pyc_file(fp, filename, ext, closeit)) {
1414 : FILE *pyc_fp;
1415 : /* Try to run a pyc file. First, re-open in binary */
1416 0 : if (closeit)
1417 0 : fclose(fp);
1418 0 : if ((pyc_fp = fopen(filename, "rb")) == NULL) {
1419 0 : fprintf(stderr, "python: Can't reopen .pyc file\n");
1420 0 : ret = -1;
1421 0 : goto done;
1422 : }
1423 : /* Turn on optimization if a .pyo file is given */
1424 0 : if (strcmp(ext, ".pyo") == 0)
1425 0 : Py_OptimizeFlag = 1;
1426 :
1427 0 : if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
1428 0 : fprintf(stderr, "python: failed to set __main__.__loader__\n");
1429 0 : ret = -1;
1430 0 : fclose(pyc_fp);
1431 0 : goto done;
1432 : }
1433 0 : v = run_pyc_file(pyc_fp, filename, d, d, flags);
1434 0 : fclose(pyc_fp);
1435 : } else {
1436 : /* When running from stdin, leave __main__.__loader__ alone */
1437 0 : if (strcmp(filename, "<stdin>") != 0 &&
1438 0 : set_main_loader(d, filename, "SourceFileLoader") < 0) {
1439 0 : fprintf(stderr, "python: failed to set __main__.__loader__\n");
1440 0 : ret = -1;
1441 0 : goto done;
1442 : }
1443 0 : v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1444 : closeit, flags);
1445 : }
1446 0 : flush_io();
1447 0 : if (v == NULL) {
1448 0 : PyErr_Print();
1449 0 : ret = -1;
1450 0 : goto done;
1451 : }
1452 0 : Py_DECREF(v);
1453 0 : ret = 0;
1454 : done:
1455 0 : if (set_file_name && PyDict_DelItemString(d, "__file__"))
1456 0 : PyErr_Clear();
1457 0 : return ret;
1458 : }
1459 :
1460 : int
1461 0 : PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
1462 : {
1463 : PyObject *m, *d, *v;
1464 0 : m = PyImport_AddModule("__main__");
1465 0 : if (m == NULL)
1466 0 : return -1;
1467 0 : d = PyModule_GetDict(m);
1468 0 : v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1469 0 : if (v == NULL) {
1470 0 : PyErr_Print();
1471 0 : return -1;
1472 : }
1473 0 : Py_DECREF(v);
1474 0 : return 0;
1475 : }
1476 :
1477 : static int
1478 0 : parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
1479 : int *lineno, int *offset, const char **text)
1480 : {
1481 : long hold;
1482 : PyObject *v;
1483 : _Py_IDENTIFIER(msg);
1484 : _Py_IDENTIFIER(filename);
1485 : _Py_IDENTIFIER(lineno);
1486 : _Py_IDENTIFIER(offset);
1487 : _Py_IDENTIFIER(text);
1488 :
1489 0 : *message = NULL;
1490 :
1491 : /* new style errors. `err' is an instance */
1492 0 : *message = _PyObject_GetAttrId(err, &PyId_msg);
1493 0 : if (!*message)
1494 0 : goto finally;
1495 :
1496 0 : v = _PyObject_GetAttrId(err, &PyId_filename);
1497 0 : if (!v)
1498 0 : goto finally;
1499 0 : if (v == Py_None) {
1500 0 : Py_DECREF(v);
1501 0 : *filename = NULL;
1502 : }
1503 : else {
1504 0 : *filename = _PyUnicode_AsString(v);
1505 0 : Py_DECREF(v);
1506 0 : if (!*filename)
1507 0 : goto finally;
1508 : }
1509 :
1510 0 : v = _PyObject_GetAttrId(err, &PyId_lineno);
1511 0 : if (!v)
1512 0 : goto finally;
1513 0 : hold = PyLong_AsLong(v);
1514 0 : Py_DECREF(v);
1515 0 : if (hold < 0 && PyErr_Occurred())
1516 0 : goto finally;
1517 0 : *lineno = (int)hold;
1518 :
1519 0 : v = _PyObject_GetAttrId(err, &PyId_offset);
1520 0 : if (!v)
1521 0 : goto finally;
1522 0 : if (v == Py_None) {
1523 0 : *offset = -1;
1524 0 : Py_DECREF(v);
1525 : } else {
1526 0 : hold = PyLong_AsLong(v);
1527 0 : Py_DECREF(v);
1528 0 : if (hold < 0 && PyErr_Occurred())
1529 0 : goto finally;
1530 0 : *offset = (int)hold;
1531 : }
1532 :
1533 0 : v = _PyObject_GetAttrId(err, &PyId_text);
1534 0 : if (!v)
1535 0 : goto finally;
1536 0 : if (v == Py_None) {
1537 0 : Py_DECREF(v);
1538 0 : *text = NULL;
1539 : }
1540 : else {
1541 0 : *text = _PyUnicode_AsString(v);
1542 0 : Py_DECREF(v);
1543 0 : if (!*text)
1544 0 : goto finally;
1545 : }
1546 0 : return 1;
1547 :
1548 : finally:
1549 0 : Py_XDECREF(*message);
1550 0 : return 0;
1551 : }
1552 :
1553 : void
1554 0 : PyErr_Print(void)
1555 : {
1556 0 : PyErr_PrintEx(1);
1557 0 : }
1558 :
1559 : static void
1560 0 : print_error_text(PyObject *f, int offset, const char *text)
1561 : {
1562 : char *nl;
1563 0 : if (offset >= 0) {
1564 0 : if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1565 0 : offset--;
1566 : for (;;) {
1567 0 : nl = strchr(text, '\n');
1568 0 : if (nl == NULL || nl-text >= offset)
1569 : break;
1570 0 : offset -= (int)(nl+1-text);
1571 0 : text = nl+1;
1572 0 : }
1573 0 : while (*text == ' ' || *text == '\t') {
1574 0 : text++;
1575 0 : offset--;
1576 : }
1577 : }
1578 0 : PyFile_WriteString(" ", f);
1579 0 : PyFile_WriteString(text, f);
1580 0 : if (*text == '\0' || text[strlen(text)-1] != '\n')
1581 0 : PyFile_WriteString("\n", f);
1582 0 : if (offset == -1)
1583 0 : return;
1584 0 : PyFile_WriteString(" ", f);
1585 0 : while (--offset > 0)
1586 0 : PyFile_WriteString(" ", f);
1587 0 : PyFile_WriteString("^\n", f);
1588 : }
1589 :
1590 : static void
1591 0 : handle_system_exit(void)
1592 : {
1593 : PyObject *exception, *value, *tb;
1594 0 : int exitcode = 0;
1595 :
1596 0 : if (Py_InspectFlag)
1597 : /* Don't exit if -i flag was given. This flag is set to 0
1598 : * when entering interactive mode for inspecting. */
1599 0 : return;
1600 :
1601 0 : PyErr_Fetch(&exception, &value, &tb);
1602 0 : fflush(stdout);
1603 0 : if (value == NULL || value == Py_None)
1604 : goto done;
1605 0 : if (PyExceptionInstance_Check(value)) {
1606 : /* The error code should be in the `code' attribute. */
1607 : _Py_IDENTIFIER(code);
1608 0 : PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
1609 0 : if (code) {
1610 0 : Py_DECREF(value);
1611 0 : value = code;
1612 0 : if (value == Py_None)
1613 0 : goto done;
1614 : }
1615 : /* If we failed to dig out the 'code' attribute,
1616 : just let the else clause below print the error. */
1617 : }
1618 0 : if (PyLong_Check(value))
1619 0 : exitcode = (int)PyLong_AsLong(value);
1620 : else {
1621 0 : PyObject *sys_stderr = PySys_GetObject("stderr");
1622 0 : if (sys_stderr != NULL && sys_stderr != Py_None) {
1623 0 : PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1624 : } else {
1625 0 : PyObject_Print(value, stderr, Py_PRINT_RAW);
1626 0 : fflush(stderr);
1627 : }
1628 0 : PySys_WriteStderr("\n");
1629 0 : exitcode = 1;
1630 : }
1631 : done:
1632 : /* Restore and clear the exception info, in order to properly decref
1633 : * the exception, value, and traceback. If we just exit instead,
1634 : * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1635 : * some finalizers from running.
1636 : */
1637 0 : PyErr_Restore(exception, value, tb);
1638 0 : PyErr_Clear();
1639 0 : Py_Exit(exitcode);
1640 : /* NOTREACHED */
1641 : }
1642 :
1643 : void
1644 0 : PyErr_PrintEx(int set_sys_last_vars)
1645 : {
1646 : PyObject *exception, *v, *tb, *hook;
1647 :
1648 0 : if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1649 0 : handle_system_exit();
1650 : }
1651 0 : PyErr_Fetch(&exception, &v, &tb);
1652 0 : if (exception == NULL)
1653 : return;
1654 0 : PyErr_NormalizeException(&exception, &v, &tb);
1655 0 : if (tb == NULL) {
1656 0 : tb = Py_None;
1657 0 : Py_INCREF(tb);
1658 : }
1659 0 : PyException_SetTraceback(v, tb);
1660 0 : if (exception == NULL)
1661 : return;
1662 : /* Now we know v != NULL too */
1663 0 : if (set_sys_last_vars) {
1664 0 : PySys_SetObject("last_type", exception);
1665 0 : PySys_SetObject("last_value", v);
1666 0 : PySys_SetObject("last_traceback", tb);
1667 : }
1668 0 : hook = PySys_GetObject("excepthook");
1669 0 : if (hook) {
1670 0 : PyObject *args = PyTuple_Pack(3, exception, v, tb);
1671 0 : PyObject *result = PyEval_CallObject(hook, args);
1672 0 : if (result == NULL) {
1673 : PyObject *exception2, *v2, *tb2;
1674 0 : if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1675 0 : handle_system_exit();
1676 : }
1677 0 : PyErr_Fetch(&exception2, &v2, &tb2);
1678 0 : PyErr_NormalizeException(&exception2, &v2, &tb2);
1679 : /* It should not be possible for exception2 or v2
1680 : to be NULL. However PyErr_Display() can't
1681 : tolerate NULLs, so just be safe. */
1682 0 : if (exception2 == NULL) {
1683 0 : exception2 = Py_None;
1684 0 : Py_INCREF(exception2);
1685 : }
1686 0 : if (v2 == NULL) {
1687 0 : v2 = Py_None;
1688 0 : Py_INCREF(v2);
1689 : }
1690 0 : fflush(stdout);
1691 0 : PySys_WriteStderr("Error in sys.excepthook:\n");
1692 0 : PyErr_Display(exception2, v2, tb2);
1693 0 : PySys_WriteStderr("\nOriginal exception was:\n");
1694 0 : PyErr_Display(exception, v, tb);
1695 0 : Py_DECREF(exception2);
1696 0 : Py_DECREF(v2);
1697 0 : Py_XDECREF(tb2);
1698 : }
1699 0 : Py_XDECREF(result);
1700 0 : Py_XDECREF(args);
1701 : } else {
1702 0 : PySys_WriteStderr("sys.excepthook is missing\n");
1703 0 : PyErr_Display(exception, v, tb);
1704 : }
1705 0 : Py_XDECREF(exception);
1706 0 : Py_XDECREF(v);
1707 0 : Py_XDECREF(tb);
1708 : }
1709 :
1710 : static void
1711 0 : print_exception(PyObject *f, PyObject *value)
1712 : {
1713 0 : int err = 0;
1714 : PyObject *type, *tb;
1715 : _Py_IDENTIFIER(print_file_and_line);
1716 :
1717 0 : if (!PyExceptionInstance_Check(value)) {
1718 0 : PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1719 0 : PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1720 0 : PyFile_WriteString(" found\n", f);
1721 0 : return;
1722 : }
1723 :
1724 0 : Py_INCREF(value);
1725 0 : fflush(stdout);
1726 0 : type = (PyObject *) Py_TYPE(value);
1727 0 : tb = PyException_GetTraceback(value);
1728 0 : if (tb && tb != Py_None)
1729 0 : err = PyTraceBack_Print(tb, f);
1730 0 : if (err == 0 &&
1731 0 : _PyObject_HasAttrId(value, &PyId_print_file_and_line))
1732 : {
1733 : PyObject *message;
1734 : const char *filename, *text;
1735 : int lineno, offset;
1736 0 : if (!parse_syntax_error(value, &message, &filename,
1737 : &lineno, &offset, &text))
1738 0 : PyErr_Clear();
1739 : else {
1740 : char buf[10];
1741 0 : PyFile_WriteString(" File \"", f);
1742 0 : if (filename == NULL)
1743 0 : PyFile_WriteString("<string>", f);
1744 : else
1745 0 : PyFile_WriteString(filename, f);
1746 0 : PyFile_WriteString("\", line ", f);
1747 0 : PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1748 0 : PyFile_WriteString(buf, f);
1749 0 : PyFile_WriteString("\n", f);
1750 0 : if (text != NULL)
1751 0 : print_error_text(f, offset, text);
1752 0 : Py_DECREF(value);
1753 0 : value = message;
1754 : /* Can't be bothered to check all those
1755 : PyFile_WriteString() calls */
1756 0 : if (PyErr_Occurred())
1757 0 : err = -1;
1758 : }
1759 : }
1760 0 : if (err) {
1761 : /* Don't do anything else */
1762 : }
1763 : else {
1764 : PyObject* moduleName;
1765 : char* className;
1766 : _Py_IDENTIFIER(__module__);
1767 : assert(PyExceptionClass_Check(type));
1768 0 : className = PyExceptionClass_Name(type);
1769 0 : if (className != NULL) {
1770 0 : char *dot = strrchr(className, '.');
1771 0 : if (dot != NULL)
1772 0 : className = dot+1;
1773 : }
1774 :
1775 0 : moduleName = _PyObject_GetAttrId(type, &PyId___module__);
1776 0 : if (moduleName == NULL || !PyUnicode_Check(moduleName))
1777 : {
1778 0 : Py_XDECREF(moduleName);
1779 0 : err = PyFile_WriteString("<unknown>", f);
1780 : }
1781 : else {
1782 0 : char* modstr = _PyUnicode_AsString(moduleName);
1783 0 : if (modstr && strcmp(modstr, "builtins"))
1784 : {
1785 0 : err = PyFile_WriteString(modstr, f);
1786 0 : err += PyFile_WriteString(".", f);
1787 : }
1788 0 : Py_DECREF(moduleName);
1789 : }
1790 0 : if (err == 0) {
1791 0 : if (className == NULL)
1792 0 : err = PyFile_WriteString("<unknown>", f);
1793 : else
1794 0 : err = PyFile_WriteString(className, f);
1795 : }
1796 : }
1797 0 : if (err == 0 && (value != Py_None)) {
1798 0 : PyObject *s = PyObject_Str(value);
1799 : /* only print colon if the str() of the
1800 : object is not the empty string
1801 : */
1802 0 : if (s == NULL)
1803 0 : err = -1;
1804 0 : else if (!PyUnicode_Check(s) ||
1805 0 : PyUnicode_GetLength(s) != 0)
1806 0 : err = PyFile_WriteString(": ", f);
1807 0 : if (err == 0)
1808 0 : err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1809 0 : Py_XDECREF(s);
1810 : }
1811 : /* try to write a newline in any case */
1812 0 : err += PyFile_WriteString("\n", f);
1813 0 : Py_XDECREF(tb);
1814 0 : Py_DECREF(value);
1815 : /* If an error happened here, don't show it.
1816 : XXX This is wrong, but too many callers rely on this behavior. */
1817 0 : if (err != 0)
1818 0 : PyErr_Clear();
1819 : }
1820 :
1821 : static const char *cause_message =
1822 : "\nThe above exception was the direct cause "
1823 : "of the following exception:\n\n";
1824 :
1825 : static const char *context_message =
1826 : "\nDuring handling of the above exception, "
1827 : "another exception occurred:\n\n";
1828 :
1829 : static void
1830 0 : print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1831 : {
1832 0 : int err = 0, res;
1833 : PyObject *cause, *context;
1834 :
1835 0 : if (seen != NULL) {
1836 : /* Exception chaining */
1837 0 : if (PySet_Add(seen, value) == -1)
1838 0 : PyErr_Clear();
1839 0 : else if (PyExceptionInstance_Check(value)) {
1840 0 : cause = PyException_GetCause(value);
1841 0 : context = PyException_GetContext(value);
1842 0 : if (cause) {
1843 0 : res = PySet_Contains(seen, cause);
1844 0 : if (res == -1)
1845 0 : PyErr_Clear();
1846 0 : if (res == 0) {
1847 0 : print_exception_recursive(
1848 : f, cause, seen);
1849 0 : err |= PyFile_WriteString(
1850 : cause_message, f);
1851 : }
1852 : }
1853 0 : else if (context &&
1854 0 : !((PyBaseExceptionObject *)value)->suppress_context) {
1855 0 : res = PySet_Contains(seen, context);
1856 0 : if (res == -1)
1857 0 : PyErr_Clear();
1858 0 : if (res == 0) {
1859 0 : print_exception_recursive(
1860 : f, context, seen);
1861 0 : err |= PyFile_WriteString(
1862 : context_message, f);
1863 : }
1864 : }
1865 0 : Py_XDECREF(context);
1866 0 : Py_XDECREF(cause);
1867 : }
1868 : }
1869 0 : print_exception(f, value);
1870 0 : if (err != 0)
1871 0 : PyErr_Clear();
1872 0 : }
1873 :
1874 : void
1875 0 : PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1876 : {
1877 : PyObject *seen;
1878 0 : PyObject *f = PySys_GetObject("stderr");
1879 0 : if (f == Py_None) {
1880 : /* pass */
1881 : }
1882 0 : else if (f == NULL) {
1883 0 : _PyObject_Dump(value);
1884 0 : fprintf(stderr, "lost sys.stderr\n");
1885 : }
1886 : else {
1887 : /* We choose to ignore seen being possibly NULL, and report
1888 : at least the main exception (it could be a MemoryError).
1889 : */
1890 0 : seen = PySet_New(NULL);
1891 0 : if (seen == NULL)
1892 0 : PyErr_Clear();
1893 0 : print_exception_recursive(f, value, seen);
1894 0 : Py_XDECREF(seen);
1895 : }
1896 0 : }
1897 :
1898 : PyObject *
1899 2 : PyRun_StringFlags(const char *str, int start, PyObject *globals,
1900 : PyObject *locals, PyCompilerFlags *flags)
1901 : {
1902 2 : PyObject *ret = NULL;
1903 : mod_ty mod;
1904 2 : PyArena *arena = PyArena_New();
1905 2 : if (arena == NULL)
1906 0 : return NULL;
1907 :
1908 2 : mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1909 2 : if (mod != NULL)
1910 2 : ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1911 2 : PyArena_Free(arena);
1912 2 : return ret;
1913 : }
1914 :
1915 : PyObject *
1916 0 : PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1917 : PyObject *locals, int closeit, PyCompilerFlags *flags)
1918 : {
1919 : PyObject *ret;
1920 : mod_ty mod;
1921 0 : PyArena *arena = PyArena_New();
1922 0 : if (arena == NULL)
1923 0 : return NULL;
1924 :
1925 0 : mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1926 : flags, NULL, arena);
1927 0 : if (closeit)
1928 0 : fclose(fp);
1929 0 : if (mod == NULL) {
1930 0 : PyArena_Free(arena);
1931 0 : return NULL;
1932 : }
1933 0 : ret = run_mod(mod, filename, globals, locals, flags, arena);
1934 0 : PyArena_Free(arena);
1935 0 : return ret;
1936 : }
1937 :
1938 : static void
1939 0 : flush_io(void)
1940 : {
1941 : PyObject *f, *r;
1942 : PyObject *type, *value, *traceback;
1943 : _Py_IDENTIFIER(flush);
1944 :
1945 : /* Save the current exception */
1946 0 : PyErr_Fetch(&type, &value, &traceback);
1947 :
1948 0 : f = PySys_GetObject("stderr");
1949 0 : if (f != NULL) {
1950 0 : r = _PyObject_CallMethodId(f, &PyId_flush, "");
1951 0 : if (r)
1952 0 : Py_DECREF(r);
1953 : else
1954 0 : PyErr_Clear();
1955 : }
1956 0 : f = PySys_GetObject("stdout");
1957 0 : if (f != NULL) {
1958 0 : r = _PyObject_CallMethodId(f, &PyId_flush, "");
1959 0 : if (r)
1960 0 : Py_DECREF(r);
1961 : else
1962 0 : PyErr_Clear();
1963 : }
1964 :
1965 0 : PyErr_Restore(type, value, traceback);
1966 0 : }
1967 :
1968 : static PyObject *
1969 2 : run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1970 : PyCompilerFlags *flags, PyArena *arena)
1971 : {
1972 : PyCodeObject *co;
1973 : PyObject *v;
1974 2 : co = PyAST_Compile(mod, filename, flags, arena);
1975 2 : if (co == NULL)
1976 0 : return NULL;
1977 2 : v = PyEval_EvalCode((PyObject*)co, globals, locals);
1978 2 : Py_DECREF(co);
1979 2 : return v;
1980 : }
1981 :
1982 : static PyObject *
1983 0 : run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1984 : PyObject *locals, PyCompilerFlags *flags)
1985 : {
1986 : PyCodeObject *co;
1987 : PyObject *v;
1988 : long magic;
1989 : long PyImport_GetMagicNumber(void);
1990 :
1991 0 : magic = PyMarshal_ReadLongFromFile(fp);
1992 0 : if (magic != PyImport_GetMagicNumber()) {
1993 0 : PyErr_SetString(PyExc_RuntimeError,
1994 : "Bad magic number in .pyc file");
1995 0 : return NULL;
1996 : }
1997 : /* Skip mtime and size */
1998 0 : (void) PyMarshal_ReadLongFromFile(fp);
1999 0 : (void) PyMarshal_ReadLongFromFile(fp);
2000 0 : v = PyMarshal_ReadLastObjectFromFile(fp);
2001 0 : if (v == NULL || !PyCode_Check(v)) {
2002 0 : Py_XDECREF(v);
2003 0 : PyErr_SetString(PyExc_RuntimeError,
2004 : "Bad code object in .pyc file");
2005 0 : return NULL;
2006 : }
2007 0 : co = (PyCodeObject *)v;
2008 0 : v = PyEval_EvalCode((PyObject*)co, globals, locals);
2009 0 : if (v && flags)
2010 0 : flags->cf_flags |= (co->co_flags & PyCF_MASK);
2011 0 : Py_DECREF(co);
2012 0 : return v;
2013 : }
2014 :
2015 : PyObject *
2016 1 : Py_CompileStringExFlags(const char *str, const char *filename, int start,
2017 : PyCompilerFlags *flags, int optimize)
2018 : {
2019 : PyCodeObject *co;
2020 : mod_ty mod;
2021 1 : PyArena *arena = PyArena_New();
2022 1 : if (arena == NULL)
2023 0 : return NULL;
2024 :
2025 1 : mod = PyParser_ASTFromString(str, filename, start, flags, arena);
2026 1 : if (mod == NULL) {
2027 0 : PyArena_Free(arena);
2028 0 : return NULL;
2029 : }
2030 1 : if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
2031 0 : PyObject *result = PyAST_mod2obj(mod);
2032 0 : PyArena_Free(arena);
2033 0 : return result;
2034 : }
2035 1 : co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
2036 1 : PyArena_Free(arena);
2037 1 : return (PyObject *)co;
2038 : }
2039 :
2040 : /* For use in Py_LIMITED_API */
2041 : #undef Py_CompileString
2042 : PyObject *
2043 0 : PyCompileString(const char *str, const char *filename, int start)
2044 : {
2045 0 : return Py_CompileStringFlags(str, filename, start, NULL);
2046 : }
2047 :
2048 : struct symtable *
2049 0 : Py_SymtableString(const char *str, const char *filename, int start)
2050 : {
2051 : struct symtable *st;
2052 : mod_ty mod;
2053 : PyCompilerFlags flags;
2054 0 : PyArena *arena = PyArena_New();
2055 0 : if (arena == NULL)
2056 0 : return NULL;
2057 :
2058 0 : flags.cf_flags = 0;
2059 0 : mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
2060 0 : if (mod == NULL) {
2061 0 : PyArena_Free(arena);
2062 0 : return NULL;
2063 : }
2064 0 : st = PySymtable_Build(mod, filename, 0);
2065 0 : PyArena_Free(arena);
2066 0 : return st;
2067 : }
2068 :
2069 : /* Preferred access to parser is through AST. */
2070 : mod_ty
2071 3 : PyParser_ASTFromString(const char *s, const char *filename, int start,
2072 : PyCompilerFlags *flags, PyArena *arena)
2073 : {
2074 : mod_ty mod;
2075 : PyCompilerFlags localflags;
2076 : perrdetail err;
2077 3 : int iflags = PARSER_FLAGS(flags);
2078 :
2079 3 : node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
2080 : &_PyParser_Grammar, start, &err,
2081 : &iflags);
2082 3 : if (flags == NULL) {
2083 0 : localflags.cf_flags = 0;
2084 0 : flags = &localflags;
2085 : }
2086 3 : if (n) {
2087 3 : flags->cf_flags |= iflags & PyCF_MASK;
2088 3 : mod = PyAST_FromNode(n, flags, filename, arena);
2089 3 : PyNode_Free(n);
2090 : }
2091 : else {
2092 0 : err_input(&err);
2093 0 : mod = NULL;
2094 : }
2095 3 : err_free(&err);
2096 3 : return mod;
2097 : }
2098 :
2099 : mod_ty
2100 0 : PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
2101 : int start, char *ps1,
2102 : char *ps2, PyCompilerFlags *flags, int *errcode,
2103 : PyArena *arena)
2104 : {
2105 : mod_ty mod;
2106 : PyCompilerFlags localflags;
2107 : perrdetail err;
2108 0 : int iflags = PARSER_FLAGS(flags);
2109 :
2110 0 : node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
2111 : &_PyParser_Grammar,
2112 : start, ps1, ps2, &err, &iflags);
2113 0 : if (flags == NULL) {
2114 0 : localflags.cf_flags = 0;
2115 0 : flags = &localflags;
2116 : }
2117 0 : if (n) {
2118 0 : flags->cf_flags |= iflags & PyCF_MASK;
2119 0 : mod = PyAST_FromNode(n, flags, filename, arena);
2120 0 : PyNode_Free(n);
2121 : }
2122 : else {
2123 0 : err_input(&err);
2124 0 : if (errcode)
2125 0 : *errcode = err.error;
2126 0 : mod = NULL;
2127 : }
2128 0 : err_free(&err);
2129 0 : return mod;
2130 : }
2131 :
2132 : /* Simplified interface to parsefile -- return node or set exception */
2133 :
2134 : node *
2135 0 : PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
2136 : {
2137 : perrdetail err;
2138 0 : node *n = PyParser_ParseFileFlags(fp, filename, NULL,
2139 : &_PyParser_Grammar,
2140 : start, NULL, NULL, &err, flags);
2141 0 : if (n == NULL)
2142 0 : err_input(&err);
2143 0 : err_free(&err);
2144 :
2145 0 : return n;
2146 : }
2147 :
2148 : /* Simplified interface to parsestring -- return node or set exception */
2149 :
2150 : node *
2151 0 : PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
2152 : {
2153 : perrdetail err;
2154 0 : node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
2155 : start, &err, flags);
2156 0 : if (n == NULL)
2157 0 : err_input(&err);
2158 0 : err_free(&err);
2159 0 : return n;
2160 : }
2161 :
2162 : node *
2163 0 : PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
2164 : int start, int flags)
2165 : {
2166 : perrdetail err;
2167 0 : node *n = PyParser_ParseStringFlagsFilename(str, filename,
2168 : &_PyParser_Grammar, start, &err, flags);
2169 0 : if (n == NULL)
2170 0 : err_input(&err);
2171 0 : err_free(&err);
2172 0 : return n;
2173 : }
2174 :
2175 : node *
2176 0 : PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
2177 : {
2178 0 : return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
2179 : }
2180 :
2181 : /* May want to move a more generalized form of this to parsetok.c or
2182 : even parser modules. */
2183 :
2184 : void
2185 0 : PyParser_ClearError(perrdetail *err)
2186 : {
2187 0 : err_free(err);
2188 0 : }
2189 :
2190 : void
2191 0 : PyParser_SetError(perrdetail *err)
2192 : {
2193 0 : err_input(err);
2194 0 : }
2195 :
2196 : static void
2197 3 : err_free(perrdetail *err)
2198 : {
2199 3 : Py_CLEAR(err->filename);
2200 3 : }
2201 :
2202 : /* Set the error appropriate to the given input error code (see errcode.h) */
2203 :
2204 : static void
2205 0 : err_input(perrdetail *err)
2206 : {
2207 : PyObject *v, *w, *errtype, *errtext;
2208 0 : PyObject *msg_obj = NULL;
2209 0 : char *msg = NULL;
2210 :
2211 0 : errtype = PyExc_SyntaxError;
2212 0 : switch (err->error) {
2213 : case E_ERROR:
2214 0 : return;
2215 : case E_SYNTAX:
2216 0 : errtype = PyExc_IndentationError;
2217 0 : if (err->expected == INDENT)
2218 0 : msg = "expected an indented block";
2219 0 : else if (err->token == INDENT)
2220 0 : msg = "unexpected indent";
2221 0 : else if (err->token == DEDENT)
2222 0 : msg = "unexpected unindent";
2223 : else {
2224 0 : errtype = PyExc_SyntaxError;
2225 0 : msg = "invalid syntax";
2226 : }
2227 0 : break;
2228 : case E_TOKEN:
2229 0 : msg = "invalid token";
2230 0 : break;
2231 : case E_EOFS:
2232 0 : msg = "EOF while scanning triple-quoted string literal";
2233 0 : break;
2234 : case E_EOLS:
2235 0 : msg = "EOL while scanning string literal";
2236 0 : break;
2237 : case E_INTR:
2238 0 : if (!PyErr_Occurred())
2239 0 : PyErr_SetNone(PyExc_KeyboardInterrupt);
2240 0 : goto cleanup;
2241 : case E_NOMEM:
2242 0 : PyErr_NoMemory();
2243 0 : goto cleanup;
2244 : case E_EOF:
2245 0 : msg = "unexpected EOF while parsing";
2246 0 : break;
2247 : case E_TABSPACE:
2248 0 : errtype = PyExc_TabError;
2249 0 : msg = "inconsistent use of tabs and spaces in indentation";
2250 0 : break;
2251 : case E_OVERFLOW:
2252 0 : msg = "expression too long";
2253 0 : break;
2254 : case E_DEDENT:
2255 0 : errtype = PyExc_IndentationError;
2256 0 : msg = "unindent does not match any outer indentation level";
2257 0 : break;
2258 : case E_TOODEEP:
2259 0 : errtype = PyExc_IndentationError;
2260 0 : msg = "too many levels of indentation";
2261 0 : break;
2262 : case E_DECODE: {
2263 : PyObject *type, *value, *tb;
2264 0 : PyErr_Fetch(&type, &value, &tb);
2265 0 : msg = "unknown decode error";
2266 0 : if (value != NULL)
2267 0 : msg_obj = PyObject_Str(value);
2268 0 : Py_XDECREF(type);
2269 0 : Py_XDECREF(value);
2270 0 : Py_XDECREF(tb);
2271 : break;
2272 : }
2273 : case E_LINECONT:
2274 0 : msg = "unexpected character after line continuation character";
2275 0 : break;
2276 :
2277 : case E_IDENTIFIER:
2278 0 : msg = "invalid character in identifier";
2279 0 : break;
2280 : case E_BADSINGLE:
2281 0 : msg = "multiple statements found while compiling a single statement";
2282 0 : break;
2283 : default:
2284 0 : fprintf(stderr, "error=%d\n", err->error);
2285 0 : msg = "unknown parsing error";
2286 0 : break;
2287 : }
2288 : /* err->text may not be UTF-8 in case of decoding errors.
2289 : Explicitly convert to an object. */
2290 0 : if (!err->text) {
2291 0 : errtext = Py_None;
2292 0 : Py_INCREF(Py_None);
2293 : } else {
2294 0 : errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2295 : "replace");
2296 : }
2297 0 : v = Py_BuildValue("(OiiN)", err->filename,
2298 : err->lineno, err->offset, errtext);
2299 0 : if (v != NULL) {
2300 0 : if (msg_obj)
2301 0 : w = Py_BuildValue("(OO)", msg_obj, v);
2302 : else
2303 0 : w = Py_BuildValue("(sO)", msg, v);
2304 : } else
2305 0 : w = NULL;
2306 0 : Py_XDECREF(v);
2307 0 : PyErr_SetObject(errtype, w);
2308 0 : Py_XDECREF(w);
2309 : cleanup:
2310 0 : Py_XDECREF(msg_obj);
2311 0 : if (err->text != NULL) {
2312 0 : PyObject_FREE(err->text);
2313 0 : err->text = NULL;
2314 : }
2315 : }
2316 :
2317 : /* Print fatal error message and abort */
2318 :
2319 : void
2320 0 : Py_FatalError(const char *msg)
2321 : {
2322 0 : const int fd = fileno(stderr);
2323 : PyThreadState *tstate;
2324 :
2325 0 : fprintf(stderr, "Fatal Python error: %s\n", msg);
2326 0 : fflush(stderr); /* it helps in Windows debug build */
2327 0 : if (PyErr_Occurred()) {
2328 0 : PyErr_PrintEx(0);
2329 : }
2330 : else {
2331 0 : tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2332 0 : if (tstate != NULL) {
2333 0 : fputc('\n', stderr);
2334 0 : fflush(stderr);
2335 0 : _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
2336 : }
2337 0 : _PyFaulthandler_Fini();
2338 : }
2339 :
2340 : #ifdef MS_WINDOWS
2341 : {
2342 : size_t len = strlen(msg);
2343 : WCHAR* buffer;
2344 : size_t i;
2345 :
2346 : /* Convert the message to wchar_t. This uses a simple one-to-one
2347 : conversion, assuming that the this error message actually uses ASCII
2348 : only. If this ceases to be true, we will have to convert. */
2349 : buffer = alloca( (len+1) * (sizeof *buffer));
2350 : for( i=0; i<=len; ++i)
2351 : buffer[i] = msg[i];
2352 : OutputDebugStringW(L"Fatal Python error: ");
2353 : OutputDebugStringW(buffer);
2354 : OutputDebugStringW(L"\n");
2355 : }
2356 : #ifdef _DEBUG
2357 : DebugBreak();
2358 : #endif
2359 : #endif /* MS_WINDOWS */
2360 0 : abort();
2361 : }
2362 :
2363 : /* Clean up and exit */
2364 :
2365 : #ifdef WITH_THREAD
2366 : #include "pythread.h"
2367 : #endif
2368 :
2369 : static void (*pyexitfunc)(void) = NULL;
2370 : /* For the atexit module. */
2371 0 : void _Py_PyAtExit(void (*func)(void))
2372 : {
2373 0 : pyexitfunc = func;
2374 0 : }
2375 :
2376 : static void
2377 0 : call_py_exitfuncs(void)
2378 : {
2379 0 : if (pyexitfunc == NULL)
2380 0 : return;
2381 :
2382 0 : (*pyexitfunc)();
2383 0 : PyErr_Clear();
2384 : }
2385 :
2386 : /* Wait until threading._shutdown completes, provided
2387 : the threading module was imported in the first place.
2388 : The shutdown routine will wait until all non-daemon
2389 : "threading" threads have completed. */
2390 : static void
2391 0 : wait_for_thread_shutdown(void)
2392 : {
2393 : #ifdef WITH_THREAD
2394 : _Py_IDENTIFIER(_shutdown);
2395 : PyObject *result;
2396 0 : PyThreadState *tstate = PyThreadState_GET();
2397 0 : PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2398 : "threading");
2399 0 : if (threading == NULL) {
2400 : /* threading not imported */
2401 0 : PyErr_Clear();
2402 0 : return;
2403 : }
2404 0 : result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
2405 0 : if (result == NULL) {
2406 0 : PyErr_WriteUnraisable(threading);
2407 : }
2408 : else {
2409 0 : Py_DECREF(result);
2410 : }
2411 0 : Py_DECREF(threading);
2412 : #endif
2413 : }
2414 :
2415 : #define NEXITFUNCS 32
2416 : static void (*exitfuncs[NEXITFUNCS])(void);
2417 : static int nexitfuncs = 0;
2418 :
2419 0 : int Py_AtExit(void (*func)(void))
2420 : {
2421 0 : if (nexitfuncs >= NEXITFUNCS)
2422 0 : return -1;
2423 0 : exitfuncs[nexitfuncs++] = func;
2424 0 : return 0;
2425 : }
2426 :
2427 : static void
2428 0 : call_ll_exitfuncs(void)
2429 : {
2430 0 : while (nexitfuncs > 0)
2431 0 : (*exitfuncs[--nexitfuncs])();
2432 :
2433 0 : fflush(stdout);
2434 0 : fflush(stderr);
2435 0 : }
2436 :
2437 : void
2438 0 : Py_Exit(int sts)
2439 : {
2440 0 : Py_Finalize();
2441 :
2442 0 : exit(sts);
2443 : }
2444 :
2445 : static void
2446 1 : initsigs(void)
2447 : {
2448 : #ifdef SIGPIPE
2449 1 : PyOS_setsig(SIGPIPE, SIG_IGN);
2450 : #endif
2451 : #ifdef SIGXFZ
2452 : PyOS_setsig(SIGXFZ, SIG_IGN);
2453 : #endif
2454 : #ifdef SIGXFSZ
2455 1 : PyOS_setsig(SIGXFSZ, SIG_IGN);
2456 : #endif
2457 1 : PyOS_InitInterrupts(); /* May imply initsignal() */
2458 1 : }
2459 :
2460 :
2461 : /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2462 : *
2463 : * All of the code in this function must only use async-signal-safe functions,
2464 : * listed at `man 7 signal` or
2465 : * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2466 : */
2467 : void
2468 0 : _Py_RestoreSignals(void)
2469 : {
2470 : #ifdef SIGPIPE
2471 0 : PyOS_setsig(SIGPIPE, SIG_DFL);
2472 : #endif
2473 : #ifdef SIGXFZ
2474 : PyOS_setsig(SIGXFZ, SIG_DFL);
2475 : #endif
2476 : #ifdef SIGXFSZ
2477 0 : PyOS_setsig(SIGXFSZ, SIG_DFL);
2478 : #endif
2479 0 : }
2480 :
2481 :
2482 : /*
2483 : * The file descriptor fd is considered ``interactive'' if either
2484 : * a) isatty(fd) is TRUE, or
2485 : * b) the -i flag was given, and the filename associated with
2486 : * the descriptor is NULL or "<stdin>" or "???".
2487 : */
2488 : int
2489 0 : Py_FdIsInteractive(FILE *fp, const char *filename)
2490 : {
2491 0 : if (isatty((int)fileno(fp)))
2492 0 : return 1;
2493 0 : if (!Py_InteractiveFlag)
2494 0 : return 0;
2495 0 : return (filename == NULL) ||
2496 0 : (strcmp(filename, "<stdin>") == 0) ||
2497 0 : (strcmp(filename, "???") == 0);
2498 : }
2499 :
2500 :
2501 : #if defined(USE_STACKCHECK)
2502 : #if defined(WIN32) && defined(_MSC_VER)
2503 :
2504 : /* Stack checking for Microsoft C */
2505 :
2506 : #include <malloc.h>
2507 : #include <excpt.h>
2508 :
2509 : /*
2510 : * Return non-zero when we run out of memory on the stack; zero otherwise.
2511 : */
2512 : int
2513 : PyOS_CheckStack(void)
2514 : {
2515 : __try {
2516 : /* alloca throws a stack overflow exception if there's
2517 : not enough space left on the stack */
2518 : alloca(PYOS_STACK_MARGIN * sizeof(void*));
2519 : return 0;
2520 : } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2521 : EXCEPTION_EXECUTE_HANDLER :
2522 : EXCEPTION_CONTINUE_SEARCH) {
2523 : int errcode = _resetstkoflw();
2524 : if (errcode == 0)
2525 : {
2526 : Py_FatalError("Could not reset the stack!");
2527 : }
2528 : }
2529 : return 1;
2530 : }
2531 :
2532 : #endif /* WIN32 && _MSC_VER */
2533 :
2534 : /* Alternate implementations can be added here... */
2535 :
2536 : #endif /* USE_STACKCHECK */
2537 :
2538 :
2539 : /* Wrappers around sigaction() or signal(). */
2540 :
2541 : PyOS_sighandler_t
2542 64 : PyOS_getsig(int sig)
2543 : {
2544 : #ifdef HAVE_SIGACTION
2545 : struct sigaction context;
2546 64 : if (sigaction(sig, NULL, &context) == -1)
2547 2 : return SIG_ERR;
2548 62 : return context.sa_handler;
2549 : #else
2550 : PyOS_sighandler_t handler;
2551 : /* Special signal handling for the secure CRT in Visual Studio 2005 */
2552 : #if defined(_MSC_VER) && _MSC_VER >= 1400
2553 : switch (sig) {
2554 : /* Only these signals are valid */
2555 : case SIGINT:
2556 : case SIGILL:
2557 : case SIGFPE:
2558 : case SIGSEGV:
2559 : case SIGTERM:
2560 : case SIGBREAK:
2561 : case SIGABRT:
2562 : break;
2563 : /* Don't call signal() with other values or it will assert */
2564 : default:
2565 : return SIG_ERR;
2566 : }
2567 : #endif /* _MSC_VER && _MSC_VER >= 1400 */
2568 : handler = signal(sig, SIG_IGN);
2569 : if (handler != SIG_ERR)
2570 : signal(sig, handler);
2571 : return handler;
2572 : #endif
2573 : }
2574 :
2575 : /*
2576 : * All of the code in this function must only use async-signal-safe functions,
2577 : * listed at `man 7 signal` or
2578 : * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2579 : */
2580 : PyOS_sighandler_t
2581 2 : PyOS_setsig(int sig, PyOS_sighandler_t handler)
2582 : {
2583 : #ifdef HAVE_SIGACTION
2584 : /* Some code in Modules/signalmodule.c depends on sigaction() being
2585 : * used here if HAVE_SIGACTION is defined. Fix that if this code
2586 : * changes to invalidate that assumption.
2587 : */
2588 : struct sigaction context, ocontext;
2589 2 : context.sa_handler = handler;
2590 2 : sigemptyset(&context.sa_mask);
2591 2 : context.sa_flags = 0;
2592 2 : if (sigaction(sig, &context, &ocontext) == -1)
2593 0 : return SIG_ERR;
2594 2 : return ocontext.sa_handler;
2595 : #else
2596 : PyOS_sighandler_t oldhandler;
2597 : oldhandler = signal(sig, handler);
2598 : #ifdef HAVE_SIGINTERRUPT
2599 : siginterrupt(sig, 1);
2600 : #endif
2601 : return oldhandler;
2602 : #endif
2603 : }
2604 :
2605 : /* Deprecated C API functions still provided for binary compatiblity */
2606 :
2607 : #undef PyParser_SimpleParseFile
2608 : PyAPI_FUNC(node *)
2609 0 : PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2610 : {
2611 0 : return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
2612 : }
2613 :
2614 : #undef PyParser_SimpleParseString
2615 : PyAPI_FUNC(node *)
2616 0 : PyParser_SimpleParseString(const char *str, int start)
2617 : {
2618 0 : return PyParser_SimpleParseStringFlags(str, start, 0);
2619 : }
2620 :
2621 : #undef PyRun_AnyFile
2622 : PyAPI_FUNC(int)
2623 0 : PyRun_AnyFile(FILE *fp, const char *name)
2624 : {
2625 0 : return PyRun_AnyFileExFlags(fp, name, 0, NULL);
2626 : }
2627 :
2628 : #undef PyRun_AnyFileEx
2629 : PyAPI_FUNC(int)
2630 0 : PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2631 : {
2632 0 : return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
2633 : }
2634 :
2635 : #undef PyRun_AnyFileFlags
2636 : PyAPI_FUNC(int)
2637 0 : PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2638 : {
2639 0 : return PyRun_AnyFileExFlags(fp, name, 0, flags);
2640 : }
2641 :
2642 : #undef PyRun_File
2643 : PyAPI_FUNC(PyObject *)
2644 0 : PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2645 : {
2646 0 : return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
2647 : }
2648 :
2649 : #undef PyRun_FileEx
2650 : PyAPI_FUNC(PyObject *)
2651 0 : PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2652 : {
2653 0 : return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
2654 : }
2655 :
2656 : #undef PyRun_FileFlags
2657 : PyAPI_FUNC(PyObject *)
2658 0 : PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
2659 : PyCompilerFlags *flags)
2660 : {
2661 0 : return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
2662 : }
2663 :
2664 : #undef PyRun_SimpleFile
2665 : PyAPI_FUNC(int)
2666 0 : PyRun_SimpleFile(FILE *f, const char *p)
2667 : {
2668 0 : return PyRun_SimpleFileExFlags(f, p, 0, NULL);
2669 : }
2670 :
2671 : #undef PyRun_SimpleFileEx
2672 : PyAPI_FUNC(int)
2673 0 : PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2674 : {
2675 0 : return PyRun_SimpleFileExFlags(f, p, c, NULL);
2676 : }
2677 :
2678 :
2679 : #undef PyRun_String
2680 : PyAPI_FUNC(PyObject *)
2681 0 : PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2682 : {
2683 0 : return PyRun_StringFlags(str, s, g, l, NULL);
2684 : }
2685 :
2686 : #undef PyRun_SimpleString
2687 : PyAPI_FUNC(int)
2688 0 : PyRun_SimpleString(const char *s)
2689 : {
2690 0 : return PyRun_SimpleStringFlags(s, NULL);
2691 : }
2692 :
2693 : #undef Py_CompileString
2694 : PyAPI_FUNC(PyObject *)
2695 0 : Py_CompileString(const char *str, const char *p, int s)
2696 : {
2697 0 : return Py_CompileStringExFlags(str, p, s, NULL, -1);
2698 : }
2699 :
2700 : #undef Py_CompileStringFlags
2701 : PyAPI_FUNC(PyObject *)
2702 0 : Py_CompileStringFlags(const char *str, const char *p, int s,
2703 : PyCompilerFlags *flags)
2704 : {
2705 0 : return Py_CompileStringExFlags(str, p, s, flags, -1);
2706 : }
2707 :
2708 : #undef PyRun_InteractiveOne
2709 : PyAPI_FUNC(int)
2710 0 : PyRun_InteractiveOne(FILE *f, const char *p)
2711 : {
2712 0 : return PyRun_InteractiveOneFlags(f, p, NULL);
2713 : }
2714 :
2715 : #undef PyRun_InteractiveLoop
2716 : PyAPI_FUNC(int)
2717 0 : PyRun_InteractiveLoop(FILE *f, const char *p)
2718 : {
2719 0 : return PyRun_InteractiveLoopFlags(f, p, NULL);
2720 : }
2721 :
2722 : #ifdef __cplusplus
2723 : }
2724 : #endif
|