]>
Commit | Line | Data |
---|---|---|
c8042e10 DM |
1 | \r |
2 | /* Python interpreter top-level routines, including init/exit */\r | |
3 | \r | |
4 | #include "Python.h"\r | |
5 | \r | |
6 | #include "Python-ast.h"\r | |
7 | #undef Yield /* undefine macro conflicting with winbase.h */\r | |
8 | #include "grammar.h"\r | |
9 | #include "node.h"\r | |
10 | #include "token.h"\r | |
11 | #include "parsetok.h"\r | |
12 | #include "errcode.h"\r | |
13 | #include "code.h"\r | |
14 | #include "compile.h"\r | |
15 | #include "symtable.h"\r | |
16 | #include "pyarena.h"\r | |
17 | #include "ast.h"\r | |
18 | #include "eval.h"\r | |
19 | #include "marshal.h"\r | |
20 | #include "abstract.h"\r | |
21 | \r | |
22 | #ifdef HAVE_SIGNAL_H\r | |
23 | #include <signal.h>\r | |
24 | #endif\r | |
25 | \r | |
26 | #ifdef MS_WINDOWS\r | |
27 | #include "malloc.h" /* for alloca */\r | |
28 | #endif\r | |
29 | \r | |
30 | #ifdef HAVE_LANGINFO_H\r | |
31 | #include <locale.h>\r | |
32 | #include <langinfo.h>\r | |
33 | #endif\r | |
34 | \r | |
35 | #ifdef MS_WINDOWS\r | |
36 | #undef BYTE\r | |
37 | #include "windows.h"\r | |
38 | #endif\r | |
39 | \r | |
40 | #ifndef Py_REF_DEBUG\r | |
41 | #define PRINT_TOTAL_REFS()\r | |
42 | #else /* Py_REF_DEBUG */\r | |
43 | #define PRINT_TOTAL_REFS() fprintf(stderr, \\r | |
44 | "[%" PY_FORMAT_SIZE_T "d refs]\n", \\r | |
45 | _Py_GetRefTotal())\r | |
46 | #endif\r | |
47 | \r | |
48 | #ifdef __cplusplus\r | |
49 | extern "C" {\r | |
50 | #endif\r | |
51 | \r | |
52 | extern char *Py_GetPath(void);\r | |
53 | \r | |
54 | extern grammar _PyParser_Grammar; /* From graminit.c */\r | |
55 | \r | |
56 | /* Forward */\r | |
57 | static void initmain(void);\r | |
58 | static void initsite(void);\r | |
59 | static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,\r | |
60 | PyCompilerFlags *, PyArena *);\r | |
61 | static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,\r | |
62 | PyCompilerFlags *);\r | |
63 | static void err_input(perrdetail *);\r | |
64 | static void initsigs(void);\r | |
65 | static void wait_for_thread_shutdown(void);\r | |
66 | static void call_sys_exitfunc(void);\r | |
67 | static void call_ll_exitfuncs(void);\r | |
68 | extern void _PyUnicode_Init(void);\r | |
69 | extern void _PyUnicode_Fini(void);\r | |
70 | \r | |
71 | #ifdef WITH_THREAD\r | |
72 | extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);\r | |
73 | extern void _PyGILState_Fini(void);\r | |
74 | #endif /* WITH_THREAD */\r | |
75 | \r | |
76 | int Py_DebugFlag; /* Needed by parser.c */\r | |
77 | int Py_VerboseFlag; /* Needed by import.c */\r | |
78 | int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */\r | |
79 | int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */\r | |
80 | int Py_NoSiteFlag; /* Suppress 'import site' */\r | |
81 | int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */\r | |
82 | int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */\r | |
83 | int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */\r | |
84 | int Py_FrozenFlag; /* Needed by getpath.c */\r | |
85 | int Py_UnicodeFlag = 0; /* Needed by compile.c */\r | |
86 | int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */\r | |
87 | /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,\r | |
88 | on the command line, and is used in 2.2 by ceval.c to make all "/" divisions\r | |
89 | true divisions (which they will be in 2.3). */\r | |
90 | int _Py_QnewFlag = 0;\r | |
91 | int Py_NoUserSiteDirectory = 0; /* for -s and site.py */\r | |
92 | int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */\r | |
93 | \r | |
94 | \r | |
95 | /* Hack to force loading of object files */\r | |
96 | int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \\r | |
97 | PyOS_mystrnicmp; /* Python/pystrcmp.o */\r | |
98 | \r | |
99 | /* PyModule_GetWarningsModule is no longer necessary as of 2.6\r | |
100 | since _warnings is builtin. This API should not be used. */\r | |
101 | PyObject *\r | |
102 | PyModule_GetWarningsModule(void)\r | |
103 | {\r | |
104 | return PyImport_ImportModule("warnings");\r | |
105 | }\r | |
106 | \r | |
107 | static int initialized = 0;\r | |
108 | \r | |
109 | /* API to access the initialized flag -- useful for esoteric use */\r | |
110 | \r | |
111 | int\r | |
112 | Py_IsInitialized(void)\r | |
113 | {\r | |
114 | return initialized;\r | |
115 | }\r | |
116 | \r | |
117 | /* Global initializations. Can be undone by Py_Finalize(). Don't\r | |
118 | call this twice without an intervening Py_Finalize() call. When\r | |
119 | initializations fail, a fatal error is issued and the function does\r | |
120 | not return. On return, the first thread and interpreter state have\r | |
121 | been created.\r | |
122 | \r | |
123 | Locking: you must hold the interpreter lock while calling this.\r | |
124 | (If the lock has not yet been initialized, that's equivalent to\r | |
125 | having the lock, but you cannot use multiple threads.)\r | |
126 | \r | |
127 | */\r | |
128 | \r | |
129 | static int\r | |
130 | add_flag(int flag, const char *envs)\r | |
131 | {\r | |
132 | int env = atoi(envs);\r | |
133 | if (flag < env)\r | |
134 | flag = env;\r | |
135 | if (flag < 1)\r | |
136 | flag = 1;\r | |
137 | return flag;\r | |
138 | }\r | |
139 | \r | |
140 | void\r | |
141 | Py_InitializeEx(int install_sigs)\r | |
142 | {\r | |
143 | PyInterpreterState *interp;\r | |
144 | PyThreadState *tstate;\r | |
145 | PyObject *bimod, *sysmod;\r | |
146 | char *p;\r | |
147 | char *icodeset = NULL; /* On Windows, input codeset may theoretically\r | |
148 | differ from output codeset. */\r | |
149 | char *codeset = NULL;\r | |
150 | char *errors = NULL;\r | |
151 | int free_codeset = 0;\r | |
152 | int overridden = 0;\r | |
153 | PyObject *sys_stream, *sys_isatty;\r | |
154 | #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)\r | |
155 | char *saved_locale, *loc_codeset;\r | |
156 | #endif\r | |
157 | #ifdef MS_WINDOWS\r | |
158 | char ibuf[128];\r | |
159 | char buf[128];\r | |
160 | #endif\r | |
161 | extern void _Py_ReadyTypes(void);\r | |
162 | \r | |
163 | if (initialized)\r | |
164 | return;\r | |
165 | initialized = 1;\r | |
166 | \r | |
167 | if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')\r | |
168 | Py_DebugFlag = add_flag(Py_DebugFlag, p);\r | |
169 | if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')\r | |
170 | Py_VerboseFlag = add_flag(Py_VerboseFlag, p);\r | |
171 | if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')\r | |
172 | Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);\r | |
173 | if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')\r | |
174 | Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);\r | |
175 | /* The variable is only tested for existence here; _PyRandom_Init will\r | |
176 | check its value further. */\r | |
177 | if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')\r | |
178 | Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);\r | |
179 | \r | |
180 | _PyRandom_Init();\r | |
181 | \r | |
182 | interp = PyInterpreterState_New();\r | |
183 | if (interp == NULL)\r | |
184 | Py_FatalError("Py_Initialize: can't make first interpreter");\r | |
185 | \r | |
186 | tstate = PyThreadState_New(interp);\r | |
187 | if (tstate == NULL)\r | |
188 | Py_FatalError("Py_Initialize: can't make first thread");\r | |
189 | (void) PyThreadState_Swap(tstate);\r | |
190 | \r | |
191 | _Py_ReadyTypes();\r | |
192 | \r | |
193 | if (!_PyFrame_Init())\r | |
194 | Py_FatalError("Py_Initialize: can't init frames");\r | |
195 | \r | |
196 | if (!_PyInt_Init())\r | |
197 | Py_FatalError("Py_Initialize: can't init ints");\r | |
198 | \r | |
199 | if (!_PyLong_Init())\r | |
200 | Py_FatalError("Py_Initialize: can't init longs");\r | |
201 | \r | |
202 | if (!PyByteArray_Init())\r | |
203 | Py_FatalError("Py_Initialize: can't init bytearray");\r | |
204 | \r | |
205 | _PyFloat_Init();\r | |
206 | \r | |
207 | interp->modules = PyDict_New();\r | |
208 | if (interp->modules == NULL)\r | |
209 | Py_FatalError("Py_Initialize: can't make modules dictionary");\r | |
210 | interp->modules_reloading = PyDict_New();\r | |
211 | if (interp->modules_reloading == NULL)\r | |
212 | Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");\r | |
213 | \r | |
214 | #ifdef Py_USING_UNICODE\r | |
215 | /* Init Unicode implementation; relies on the codec registry */\r | |
216 | _PyUnicode_Init();\r | |
217 | #endif\r | |
218 | \r | |
219 | bimod = _PyBuiltin_Init();\r | |
220 | if (bimod == NULL)\r | |
221 | Py_FatalError("Py_Initialize: can't initialize __builtin__");\r | |
222 | interp->builtins = PyModule_GetDict(bimod);\r | |
223 | if (interp->builtins == NULL)\r | |
224 | Py_FatalError("Py_Initialize: can't initialize builtins dict");\r | |
225 | Py_INCREF(interp->builtins);\r | |
226 | \r | |
227 | sysmod = _PySys_Init();\r | |
228 | if (sysmod == NULL)\r | |
229 | Py_FatalError("Py_Initialize: can't initialize sys");\r | |
230 | interp->sysdict = PyModule_GetDict(sysmod);\r | |
231 | if (interp->sysdict == NULL)\r | |
232 | Py_FatalError("Py_Initialize: can't initialize sys dict");\r | |
233 | Py_INCREF(interp->sysdict);\r | |
234 | _PyImport_FixupExtension("sys", "sys");\r | |
235 | PySys_SetPath(Py_GetPath());\r | |
236 | PyDict_SetItemString(interp->sysdict, "modules",\r | |
237 | interp->modules);\r | |
238 | \r | |
239 | _PyImport_Init();\r | |
240 | \r | |
241 | /* initialize builtin exceptions */\r | |
242 | _PyExc_Init();\r | |
243 | _PyImport_FixupExtension("exceptions", "exceptions");\r | |
244 | \r | |
245 | /* phase 2 of builtins */\r | |
246 | _PyImport_FixupExtension("__builtin__", "__builtin__");\r | |
247 | \r | |
248 | _PyImportHooks_Init();\r | |
249 | \r | |
250 | if (install_sigs)\r | |
251 | initsigs(); /* Signal handling stuff, including initintr() */\r | |
252 | \r | |
253 | /* Initialize warnings. */\r | |
254 | _PyWarnings_Init();\r | |
255 | if (PySys_HasWarnOptions()) {\r | |
256 | PyObject *warnings_module = PyImport_ImportModule("warnings");\r | |
257 | if (!warnings_module)\r | |
258 | PyErr_Clear();\r | |
259 | Py_XDECREF(warnings_module);\r | |
260 | }\r | |
261 | \r | |
262 | initmain(); /* Module __main__ */\r | |
263 | \r | |
264 | /* auto-thread-state API, if available */\r | |
265 | #ifdef WITH_THREAD\r | |
266 | _PyGILState_Init(interp, tstate);\r | |
267 | #endif /* WITH_THREAD */\r | |
268 | \r | |
269 | if (!Py_NoSiteFlag)\r | |
270 | initsite(); /* Module site */\r | |
271 | \r | |
272 | if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {\r | |
273 | p = icodeset = codeset = strdup(p);\r | |
274 | free_codeset = 1;\r | |
275 | errors = strchr(p, ':');\r | |
276 | if (errors) {\r | |
277 | *errors = '\0';\r | |
278 | errors++;\r | |
279 | }\r | |
280 | overridden = 1;\r | |
281 | }\r | |
282 | \r | |
283 | #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)\r | |
284 | /* On Unix, set the file system encoding according to the\r | |
285 | user's preference, if the CODESET names a well-known\r | |
286 | Python codec, and Py_FileSystemDefaultEncoding isn't\r | |
287 | initialized by other means. Also set the encoding of\r | |
288 | stdin and stdout if these are terminals, unless overridden. */\r | |
289 | \r | |
290 | if (!overridden || !Py_FileSystemDefaultEncoding) {\r | |
291 | saved_locale = strdup(setlocale(LC_CTYPE, NULL));\r | |
292 | setlocale(LC_CTYPE, "");\r | |
293 | loc_codeset = nl_langinfo(CODESET);\r | |
294 | if (loc_codeset && *loc_codeset) {\r | |
295 | PyObject *enc = PyCodec_Encoder(loc_codeset);\r | |
296 | if (enc) {\r | |
297 | loc_codeset = strdup(loc_codeset);\r | |
298 | Py_DECREF(enc);\r | |
299 | } else {\r | |
300 | if (PyErr_ExceptionMatches(PyExc_LookupError)) {\r | |
301 | PyErr_Clear();\r | |
302 | loc_codeset = NULL;\r | |
303 | } else {\r | |
304 | PyErr_Print();\r | |
305 | exit(1);\r | |
306 | }\r | |
307 | }\r | |
308 | } else\r | |
309 | loc_codeset = NULL;\r | |
310 | setlocale(LC_CTYPE, saved_locale);\r | |
311 | free(saved_locale);\r | |
312 | \r | |
313 | if (!overridden) {\r | |
314 | codeset = icodeset = loc_codeset;\r | |
315 | free_codeset = 1;\r | |
316 | }\r | |
317 | \r | |
318 | /* Initialize Py_FileSystemDefaultEncoding from\r | |
319 | locale even if PYTHONIOENCODING is set. */\r | |
320 | if (!Py_FileSystemDefaultEncoding) {\r | |
321 | Py_FileSystemDefaultEncoding = loc_codeset;\r | |
322 | if (!overridden)\r | |
323 | free_codeset = 0;\r | |
324 | }\r | |
325 | }\r | |
326 | #endif\r | |
327 | \r | |
328 | #ifdef MS_WINDOWS\r | |
329 | if (!overridden) {\r | |
330 | icodeset = ibuf;\r | |
331 | codeset = buf;\r | |
332 | sprintf(ibuf, "cp%d", GetConsoleCP());\r | |
333 | sprintf(buf, "cp%d", GetConsoleOutputCP());\r | |
334 | }\r | |
335 | #endif\r | |
336 | \r | |
337 | if (codeset) {\r | |
338 | sys_stream = PySys_GetObject("stdin");\r | |
339 | sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");\r | |
340 | if (!sys_isatty)\r | |
341 | PyErr_Clear();\r | |
342 | if ((overridden ||\r | |
343 | (sys_isatty && PyObject_IsTrue(sys_isatty))) &&\r | |
344 | PyFile_Check(sys_stream)) {\r | |
345 | if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))\r | |
346 | Py_FatalError("Cannot set codeset of stdin");\r | |
347 | }\r | |
348 | Py_XDECREF(sys_isatty);\r | |
349 | \r | |
350 | sys_stream = PySys_GetObject("stdout");\r | |
351 | sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");\r | |
352 | if (!sys_isatty)\r | |
353 | PyErr_Clear();\r | |
354 | if ((overridden ||\r | |
355 | (sys_isatty && PyObject_IsTrue(sys_isatty))) &&\r | |
356 | PyFile_Check(sys_stream)) {\r | |
357 | if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))\r | |
358 | Py_FatalError("Cannot set codeset of stdout");\r | |
359 | }\r | |
360 | Py_XDECREF(sys_isatty);\r | |
361 | \r | |
362 | sys_stream = PySys_GetObject("stderr");\r | |
363 | sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");\r | |
364 | if (!sys_isatty)\r | |
365 | PyErr_Clear();\r | |
366 | if((overridden ||\r | |
367 | (sys_isatty && PyObject_IsTrue(sys_isatty))) &&\r | |
368 | PyFile_Check(sys_stream)) {\r | |
369 | if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))\r | |
370 | Py_FatalError("Cannot set codeset of stderr");\r | |
371 | }\r | |
372 | Py_XDECREF(sys_isatty);\r | |
373 | \r | |
374 | if (free_codeset)\r | |
375 | free(codeset);\r | |
376 | }\r | |
377 | }\r | |
378 | \r | |
379 | void\r | |
380 | Py_Initialize(void)\r | |
381 | {\r | |
382 | Py_InitializeEx(1);\r | |
383 | }\r | |
384 | \r | |
385 | \r | |
386 | #ifdef COUNT_ALLOCS\r | |
387 | extern void dump_counts(FILE*);\r | |
388 | #endif\r | |
389 | \r | |
390 | /* Undo the effect of Py_Initialize().\r | |
391 | \r | |
392 | Beware: if multiple interpreter and/or thread states exist, these\r | |
393 | are not wiped out; only the current thread and interpreter state\r | |
394 | are deleted. But since everything else is deleted, those other\r | |
395 | interpreter and thread states should no longer be used.\r | |
396 | \r | |
397 | (XXX We should do better, e.g. wipe out all interpreters and\r | |
398 | threads.)\r | |
399 | \r | |
400 | Locking: as above.\r | |
401 | \r | |
402 | */\r | |
403 | \r | |
404 | void\r | |
405 | Py_Finalize(void)\r | |
406 | {\r | |
407 | PyInterpreterState *interp;\r | |
408 | PyThreadState *tstate;\r | |
409 | \r | |
410 | if (!initialized)\r | |
411 | return;\r | |
412 | \r | |
413 | wait_for_thread_shutdown();\r | |
414 | \r | |
415 | /* The interpreter is still entirely intact at this point, and the\r | |
416 | * exit funcs may be relying on that. In particular, if some thread\r | |
417 | * or exit func is still waiting to do an import, the import machinery\r | |
418 | * expects Py_IsInitialized() to return true. So don't say the\r | |
419 | * interpreter is uninitialized until after the exit funcs have run.\r | |
420 | * Note that Threading.py uses an exit func to do a join on all the\r | |
421 | * threads created thru it, so this also protects pending imports in\r | |
422 | * the threads created via Threading.\r | |
423 | */\r | |
424 | call_sys_exitfunc();\r | |
425 | initialized = 0;\r | |
426 | \r | |
427 | /* Get current thread state and interpreter pointer */\r | |
428 | tstate = PyThreadState_GET();\r | |
429 | interp = tstate->interp;\r | |
430 | \r | |
431 | /* Disable signal handling */\r | |
432 | PyOS_FiniInterrupts();\r | |
433 | \r | |
434 | /* Clear type lookup cache */\r | |
435 | PyType_ClearCache();\r | |
436 | \r | |
437 | /* Collect garbage. This may call finalizers; it's nice to call these\r | |
438 | * before all modules are destroyed.\r | |
439 | * XXX If a __del__ or weakref callback is triggered here, and tries to\r | |
440 | * XXX import a module, bad things can happen, because Python no\r | |
441 | * XXX longer believes it's initialized.\r | |
442 | * XXX Fatal Python error: Interpreter not initialized (version mismatch?)\r | |
443 | * XXX is easy to provoke that way. I've also seen, e.g.,\r | |
444 | * XXX Exception exceptions.ImportError: 'No module named sha'\r | |
445 | * XXX in <function callback at 0x008F5718> ignored\r | |
446 | * XXX but I'm unclear on exactly how that one happens. In any case,\r | |
447 | * XXX I haven't seen a real-life report of either of these.\r | |
448 | */\r | |
449 | PyGC_Collect();\r | |
450 | #ifdef COUNT_ALLOCS\r | |
451 | /* With COUNT_ALLOCS, it helps to run GC multiple times:\r | |
452 | each collection might release some types from the type\r | |
453 | list, so they become garbage. */\r | |
454 | while (PyGC_Collect() > 0)\r | |
455 | /* nothing */;\r | |
456 | #endif\r | |
457 | \r | |
458 | /* Destroy all modules */\r | |
459 | PyImport_Cleanup();\r | |
460 | \r | |
461 | /* Collect final garbage. This disposes of cycles created by\r | |
462 | * new-style class definitions, for example.\r | |
463 | * XXX This is disabled because it caused too many problems. If\r | |
464 | * XXX a __del__ or weakref callback triggers here, Python code has\r | |
465 | * XXX a hard time running, because even the sys module has been\r | |
466 | * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).\r | |
467 | * XXX One symptom is a sequence of information-free messages\r | |
468 | * XXX coming from threads (if a __del__ or callback is invoked,\r | |
469 | * XXX other threads can execute too, and any exception they encounter\r | |
470 | * XXX triggers a comedy of errors as subsystem after subsystem\r | |
471 | * XXX fails to find what it *expects* to find in sys to help report\r | |
472 | * XXX the exception and consequent unexpected failures). I've also\r | |
473 | * XXX seen segfaults then, after adding print statements to the\r | |
474 | * XXX Python code getting called.\r | |
475 | */\r | |
476 | #if 0\r | |
477 | PyGC_Collect();\r | |
478 | #endif\r | |
479 | \r | |
480 | /* Destroy the database used by _PyImport_{Fixup,Find}Extension */\r | |
481 | _PyImport_Fini();\r | |
482 | \r | |
483 | /* Debugging stuff */\r | |
484 | #ifdef COUNT_ALLOCS\r | |
485 | dump_counts(stdout);\r | |
486 | #endif\r | |
487 | \r | |
488 | PRINT_TOTAL_REFS();\r | |
489 | \r | |
490 | #ifdef Py_TRACE_REFS\r | |
491 | /* Display all objects still alive -- this can invoke arbitrary\r | |
492 | * __repr__ overrides, so requires a mostly-intact interpreter.\r | |
493 | * Alas, a lot of stuff may still be alive now that will be cleaned\r | |
494 | * up later.\r | |
495 | */\r | |
496 | if (Py_GETENV("PYTHONDUMPREFS"))\r | |
497 | _Py_PrintReferences(stderr);\r | |
498 | #endif /* Py_TRACE_REFS */\r | |
499 | \r | |
500 | /* Clear interpreter state */\r | |
501 | PyInterpreterState_Clear(interp);\r | |
502 | \r | |
503 | /* Now we decref the exception classes. After this point nothing\r | |
504 | can raise an exception. That's okay, because each Fini() method\r | |
505 | below has been checked to make sure no exceptions are ever\r | |
506 | raised.\r | |
507 | */\r | |
508 | \r | |
509 | _PyExc_Fini();\r | |
510 | \r | |
511 | /* Cleanup auto-thread-state */\r | |
512 | #ifdef WITH_THREAD\r | |
513 | _PyGILState_Fini();\r | |
514 | #endif /* WITH_THREAD */\r | |
515 | \r | |
516 | /* Delete current thread */\r | |
517 | PyThreadState_Swap(NULL);\r | |
518 | PyInterpreterState_Delete(interp);\r | |
519 | \r | |
520 | /* Sundry finalizers */\r | |
521 | PyMethod_Fini();\r | |
522 | PyFrame_Fini();\r | |
523 | PyCFunction_Fini();\r | |
524 | PyTuple_Fini();\r | |
525 | PyList_Fini();\r | |
526 | PySet_Fini();\r | |
527 | PyString_Fini();\r | |
528 | PyByteArray_Fini();\r | |
529 | PyInt_Fini();\r | |
530 | PyFloat_Fini();\r | |
531 | PyDict_Fini();\r | |
532 | _PyRandom_Fini();\r | |
533 | \r | |
534 | #ifdef Py_USING_UNICODE\r | |
535 | /* Cleanup Unicode implementation */\r | |
536 | _PyUnicode_Fini();\r | |
537 | #endif\r | |
538 | \r | |
539 | /* XXX Still allocated:\r | |
540 | - various static ad-hoc pointers to interned strings\r | |
541 | - int and float free list blocks\r | |
542 | - whatever various modules and libraries allocate\r | |
543 | */\r | |
544 | \r | |
545 | PyGrammar_RemoveAccelerators(&_PyParser_Grammar);\r | |
546 | \r | |
547 | #ifdef Py_TRACE_REFS\r | |
548 | /* Display addresses (& refcnts) of all objects still alive.\r | |
549 | * An address can be used to find the repr of the object, printed\r | |
550 | * above by _Py_PrintReferences.\r | |
551 | */\r | |
552 | if (Py_GETENV("PYTHONDUMPREFS"))\r | |
553 | _Py_PrintReferenceAddresses(stderr);\r | |
554 | #endif /* Py_TRACE_REFS */\r | |
555 | #ifdef PYMALLOC_DEBUG\r | |
556 | if (Py_GETENV("PYTHONMALLOCSTATS"))\r | |
557 | _PyObject_DebugMallocStats();\r | |
558 | #endif\r | |
559 | \r | |
560 | call_ll_exitfuncs();\r | |
561 | }\r | |
562 | \r | |
563 | /* Create and initialize a new interpreter and thread, and return the\r | |
564 | new thread. This requires that Py_Initialize() has been called\r | |
565 | first.\r | |
566 | \r | |
567 | Unsuccessful initialization yields a NULL pointer. Note that *no*\r | |
568 | exception information is available even in this case -- the\r | |
569 | exception information is held in the thread, and there is no\r | |
570 | thread.\r | |
571 | \r | |
572 | Locking: as above.\r | |
573 | \r | |
574 | */\r | |
575 | \r | |
576 | PyThreadState *\r | |
577 | Py_NewInterpreter(void)\r | |
578 | {\r | |
579 | PyInterpreterState *interp;\r | |
580 | PyThreadState *tstate, *save_tstate;\r | |
581 | PyObject *bimod, *sysmod;\r | |
582 | \r | |
583 | if (!initialized)\r | |
584 | Py_FatalError("Py_NewInterpreter: call Py_Initialize first");\r | |
585 | \r | |
586 | interp = PyInterpreterState_New();\r | |
587 | if (interp == NULL)\r | |
588 | return NULL;\r | |
589 | \r | |
590 | tstate = PyThreadState_New(interp);\r | |
591 | if (tstate == NULL) {\r | |
592 | PyInterpreterState_Delete(interp);\r | |
593 | return NULL;\r | |
594 | }\r | |
595 | \r | |
596 | save_tstate = PyThreadState_Swap(tstate);\r | |
597 | \r | |
598 | /* XXX The following is lax in error checking */\r | |
599 | \r | |
600 | interp->modules = PyDict_New();\r | |
601 | interp->modules_reloading = PyDict_New();\r | |
602 | \r | |
603 | bimod = _PyImport_FindExtension("__builtin__", "__builtin__");\r | |
604 | if (bimod != NULL) {\r | |
605 | interp->builtins = PyModule_GetDict(bimod);\r | |
606 | if (interp->builtins == NULL)\r | |
607 | goto handle_error;\r | |
608 | Py_INCREF(interp->builtins);\r | |
609 | }\r | |
610 | sysmod = _PyImport_FindExtension("sys", "sys");\r | |
611 | if (bimod != NULL && sysmod != NULL) {\r | |
612 | interp->sysdict = PyModule_GetDict(sysmod);\r | |
613 | if (interp->sysdict == NULL)\r | |
614 | goto handle_error;\r | |
615 | Py_INCREF(interp->sysdict);\r | |
616 | PySys_SetPath(Py_GetPath());\r | |
617 | PyDict_SetItemString(interp->sysdict, "modules",\r | |
618 | interp->modules);\r | |
619 | _PyImportHooks_Init();\r | |
620 | initmain();\r | |
621 | if (!Py_NoSiteFlag)\r | |
622 | initsite();\r | |
623 | }\r | |
624 | \r | |
625 | if (!PyErr_Occurred())\r | |
626 | return tstate;\r | |
627 | \r | |
628 | handle_error:\r | |
629 | /* Oops, it didn't work. Undo it all. */\r | |
630 | \r | |
631 | PyErr_Print();\r | |
632 | PyThreadState_Clear(tstate);\r | |
633 | PyThreadState_Swap(save_tstate);\r | |
634 | PyThreadState_Delete(tstate);\r | |
635 | PyInterpreterState_Delete(interp);\r | |
636 | \r | |
637 | return NULL;\r | |
638 | }\r | |
639 | \r | |
640 | /* Delete an interpreter and its last thread. This requires that the\r | |
641 | given thread state is current, that the thread has no remaining\r | |
642 | frames, and that it is its interpreter's only remaining thread.\r | |
643 | It is a fatal error to violate these constraints.\r | |
644 | \r | |
645 | (Py_Finalize() doesn't have these constraints -- it zaps\r | |
646 | everything, regardless.)\r | |
647 | \r | |
648 | Locking: as above.\r | |
649 | \r | |
650 | */\r | |
651 | \r | |
652 | void\r | |
653 | Py_EndInterpreter(PyThreadState *tstate)\r | |
654 | {\r | |
655 | PyInterpreterState *interp = tstate->interp;\r | |
656 | \r | |
657 | if (tstate != PyThreadState_GET())\r | |
658 | Py_FatalError("Py_EndInterpreter: thread is not current");\r | |
659 | if (tstate->frame != NULL)\r | |
660 | Py_FatalError("Py_EndInterpreter: thread still has a frame");\r | |
661 | if (tstate != interp->tstate_head || tstate->next != NULL)\r | |
662 | Py_FatalError("Py_EndInterpreter: not the last thread");\r | |
663 | \r | |
664 | PyImport_Cleanup();\r | |
665 | PyInterpreterState_Clear(interp);\r | |
666 | PyThreadState_Swap(NULL);\r | |
667 | PyInterpreterState_Delete(interp);\r | |
668 | }\r | |
669 | \r | |
670 | static char *progname = "python";\r | |
671 | \r | |
672 | void\r | |
673 | Py_SetProgramName(char *pn)\r | |
674 | {\r | |
675 | if (pn && *pn)\r | |
676 | progname = pn;\r | |
677 | }\r | |
678 | \r | |
679 | char *\r | |
680 | Py_GetProgramName(void)\r | |
681 | {\r | |
682 | return progname;\r | |
683 | }\r | |
684 | \r | |
685 | static char *default_home = NULL;\r | |
686 | \r | |
687 | void\r | |
688 | Py_SetPythonHome(char *home)\r | |
689 | {\r | |
690 | default_home = home;\r | |
691 | }\r | |
692 | \r | |
693 | char *\r | |
694 | Py_GetPythonHome(void)\r | |
695 | {\r | |
696 | char *home = default_home;\r | |
697 | if (home == NULL && !Py_IgnoreEnvironmentFlag)\r | |
698 | home = Py_GETENV("PYTHONHOME");\r | |
699 | return home;\r | |
700 | }\r | |
701 | \r | |
702 | /* Create __main__ module */\r | |
703 | \r | |
704 | static void\r | |
705 | initmain(void)\r | |
706 | {\r | |
707 | PyObject *m, *d;\r | |
708 | m = PyImport_AddModule("__main__");\r | |
709 | if (m == NULL)\r | |
710 | Py_FatalError("can't create __main__ module");\r | |
711 | d = PyModule_GetDict(m);\r | |
712 | if (PyDict_GetItemString(d, "__builtins__") == NULL) {\r | |
713 | PyObject *bimod = PyImport_ImportModule("__builtin__");\r | |
714 | if (bimod == NULL ||\r | |
715 | PyDict_SetItemString(d, "__builtins__", bimod) != 0)\r | |
716 | Py_FatalError("can't add __builtins__ to __main__");\r | |
717 | Py_XDECREF(bimod);\r | |
718 | }\r | |
719 | }\r | |
720 | \r | |
721 | /* Import the site module (not into __main__ though) */\r | |
722 | \r | |
723 | static void\r | |
724 | initsite(void)\r | |
725 | {\r | |
726 | PyObject *m;\r | |
727 | m = PyImport_ImportModule("site");\r | |
728 | if (m == NULL) {\r | |
729 | PyErr_Print();\r | |
730 | Py_Finalize();\r | |
731 | exit(1);\r | |
732 | }\r | |
733 | else {\r | |
734 | Py_DECREF(m);\r | |
735 | }\r | |
736 | }\r | |
737 | \r | |
738 | /* Parse input from a file and execute it */\r | |
739 | \r | |
740 | int\r | |
741 | PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,\r | |
742 | PyCompilerFlags *flags)\r | |
743 | {\r | |
744 | if (filename == NULL)\r | |
745 | filename = "???";\r | |
746 | if (Py_FdIsInteractive(fp, filename)) {\r | |
747 | int err = PyRun_InteractiveLoopFlags(fp, filename, flags);\r | |
748 | if (closeit)\r | |
749 | fclose(fp);\r | |
750 | return err;\r | |
751 | }\r | |
752 | else\r | |
753 | return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);\r | |
754 | }\r | |
755 | \r | |
756 | int\r | |
757 | PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)\r | |
758 | {\r | |
759 | PyObject *v;\r | |
760 | int ret;\r | |
761 | PyCompilerFlags local_flags;\r | |
762 | \r | |
763 | if (flags == NULL) {\r | |
764 | flags = &local_flags;\r | |
765 | local_flags.cf_flags = 0;\r | |
766 | }\r | |
767 | v = PySys_GetObject("ps1");\r | |
768 | if (v == NULL) {\r | |
769 | PySys_SetObject("ps1", v = PyString_FromString(">>> "));\r | |
770 | Py_XDECREF(v);\r | |
771 | }\r | |
772 | v = PySys_GetObject("ps2");\r | |
773 | if (v == NULL) {\r | |
774 | PySys_SetObject("ps2", v = PyString_FromString("... "));\r | |
775 | Py_XDECREF(v);\r | |
776 | }\r | |
777 | for (;;) {\r | |
778 | ret = PyRun_InteractiveOneFlags(fp, filename, flags);\r | |
779 | PRINT_TOTAL_REFS();\r | |
780 | if (ret == E_EOF)\r | |
781 | return 0;\r | |
782 | /*\r | |
783 | if (ret == E_NOMEM)\r | |
784 | return -1;\r | |
785 | */\r | |
786 | }\r | |
787 | }\r | |
788 | \r | |
789 | #if 0\r | |
790 | /* compute parser flags based on compiler flags */\r | |
791 | #define PARSER_FLAGS(flags) \\r | |
792 | ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \\r | |
793 | PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)\r | |
794 | #endif\r | |
795 | #if 1\r | |
796 | /* Keep an example of flags with future keyword support. */\r | |
797 | #define PARSER_FLAGS(flags) \\r | |
798 | ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \\r | |
799 | PyPARSE_DONT_IMPLY_DEDENT : 0) \\r | |
800 | | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \\r | |
801 | PyPARSE_PRINT_IS_FUNCTION : 0) \\r | |
802 | | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \\r | |
803 | PyPARSE_UNICODE_LITERALS : 0) \\r | |
804 | ) : 0)\r | |
805 | #endif\r | |
806 | \r | |
807 | int\r | |
808 | PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)\r | |
809 | {\r | |
810 | PyObject *m, *d, *v, *w;\r | |
811 | mod_ty mod;\r | |
812 | PyArena *arena;\r | |
813 | char *ps1 = "", *ps2 = "";\r | |
814 | int errcode = 0;\r | |
815 | \r | |
816 | v = PySys_GetObject("ps1");\r | |
817 | if (v != NULL) {\r | |
818 | v = PyObject_Str(v);\r | |
819 | if (v == NULL)\r | |
820 | PyErr_Clear();\r | |
821 | else if (PyString_Check(v))\r | |
822 | ps1 = PyString_AsString(v);\r | |
823 | }\r | |
824 | w = PySys_GetObject("ps2");\r | |
825 | if (w != NULL) {\r | |
826 | w = PyObject_Str(w);\r | |
827 | if (w == NULL)\r | |
828 | PyErr_Clear();\r | |
829 | else if (PyString_Check(w))\r | |
830 | ps2 = PyString_AsString(w);\r | |
831 | }\r | |
832 | arena = PyArena_New();\r | |
833 | if (arena == NULL) {\r | |
834 | Py_XDECREF(v);\r | |
835 | Py_XDECREF(w);\r | |
836 | return -1;\r | |
837 | }\r | |
838 | mod = PyParser_ASTFromFile(fp, filename,\r | |
839 | Py_single_input, ps1, ps2,\r | |
840 | flags, &errcode, arena);\r | |
841 | Py_XDECREF(v);\r | |
842 | Py_XDECREF(w);\r | |
843 | if (mod == NULL) {\r | |
844 | PyArena_Free(arena);\r | |
845 | if (errcode == E_EOF) {\r | |
846 | PyErr_Clear();\r | |
847 | return E_EOF;\r | |
848 | }\r | |
849 | PyErr_Print();\r | |
850 | return -1;\r | |
851 | }\r | |
852 | m = PyImport_AddModule("__main__");\r | |
853 | if (m == NULL) {\r | |
854 | PyArena_Free(arena);\r | |
855 | return -1;\r | |
856 | }\r | |
857 | d = PyModule_GetDict(m);\r | |
858 | v = run_mod(mod, filename, d, d, flags, arena);\r | |
859 | PyArena_Free(arena);\r | |
860 | if (v == NULL) {\r | |
861 | PyErr_Print();\r | |
862 | return -1;\r | |
863 | }\r | |
864 | Py_DECREF(v);\r | |
865 | if (Py_FlushLine())\r | |
866 | PyErr_Clear();\r | |
867 | return 0;\r | |
868 | }\r | |
869 | \r | |
870 | /* Check whether a file maybe a pyc file: Look at the extension,\r | |
871 | the file type, and, if we may close it, at the first few bytes. */\r | |
872 | \r | |
873 | static int\r | |
874 | maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)\r | |
875 | {\r | |
876 | if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)\r | |
877 | return 1;\r | |
878 | \r | |
879 | /* Only look into the file if we are allowed to close it, since\r | |
880 | it then should also be seekable. */\r | |
881 | if (closeit) {\r | |
882 | /* Read only two bytes of the magic. If the file was opened in\r | |
883 | text mode, the bytes 3 and 4 of the magic (\r\n) might not\r | |
884 | be read as they are on disk. */\r | |
885 | unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;\r | |
886 | unsigned char buf[2];\r | |
887 | /* Mess: In case of -x, the stream is NOT at its start now,\r | |
888 | and ungetc() was used to push back the first newline,\r | |
889 | which makes the current stream position formally undefined,\r | |
890 | and a x-platform nightmare.\r | |
891 | Unfortunately, we have no direct way to know whether -x\r | |
892 | was specified. So we use a terrible hack: if the current\r | |
893 | stream position is not 0, we assume -x was specified, and\r | |
894 | give up. Bug 132850 on SourceForge spells out the\r | |
895 | hopelessness of trying anything else (fseek and ftell\r | |
896 | don't work predictably x-platform for text-mode files).\r | |
897 | */\r | |
898 | int ispyc = 0;\r | |
899 | if (ftell(fp) == 0) {\r | |
900 | if (fread(buf, 1, 2, fp) == 2 &&\r | |
901 | ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)\r | |
902 | ispyc = 1;\r | |
903 | rewind(fp);\r | |
904 | }\r | |
905 | return ispyc;\r | |
906 | }\r | |
907 | return 0;\r | |
908 | }\r | |
909 | \r | |
910 | int\r | |
911 | PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,\r | |
912 | PyCompilerFlags *flags)\r | |
913 | {\r | |
914 | PyObject *m, *d, *v;\r | |
915 | const char *ext;\r | |
916 | int set_file_name = 0, len, ret = -1;\r | |
917 | \r | |
918 | m = PyImport_AddModule("__main__");\r | |
919 | if (m == NULL)\r | |
920 | return -1;\r | |
921 | Py_INCREF(m);\r | |
922 | d = PyModule_GetDict(m);\r | |
923 | if (PyDict_GetItemString(d, "__file__") == NULL) {\r | |
924 | PyObject *f = PyString_FromString(filename);\r | |
925 | if (f == NULL)\r | |
926 | goto done;\r | |
927 | if (PyDict_SetItemString(d, "__file__", f) < 0) {\r | |
928 | Py_DECREF(f);\r | |
929 | goto done;\r | |
930 | }\r | |
931 | set_file_name = 1;\r | |
932 | Py_DECREF(f);\r | |
933 | }\r | |
934 | len = strlen(filename);\r | |
935 | ext = filename + len - (len > 4 ? 4 : 0);\r | |
936 | if (maybe_pyc_file(fp, filename, ext, closeit)) {\r | |
937 | /* Try to run a pyc file. First, re-open in binary */\r | |
938 | if (closeit)\r | |
939 | fclose(fp);\r | |
940 | if ((fp = fopen(filename, "rb")) == NULL) {\r | |
941 | fprintf(stderr, "python: Can't reopen .pyc file\n");\r | |
942 | goto done;\r | |
943 | }\r | |
944 | /* Turn on optimization if a .pyo file is given */\r | |
945 | if (strcmp(ext, ".pyo") == 0)\r | |
946 | Py_OptimizeFlag = 1;\r | |
947 | v = run_pyc_file(fp, filename, d, d, flags);\r | |
948 | } else {\r | |
949 | v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,\r | |
950 | closeit, flags);\r | |
951 | }\r | |
952 | if (v == NULL) {\r | |
953 | PyErr_Print();\r | |
954 | goto done;\r | |
955 | }\r | |
956 | Py_DECREF(v);\r | |
957 | if (Py_FlushLine())\r | |
958 | PyErr_Clear();\r | |
959 | ret = 0;\r | |
960 | done:\r | |
961 | if (set_file_name && PyDict_DelItemString(d, "__file__"))\r | |
962 | PyErr_Clear();\r | |
963 | Py_DECREF(m);\r | |
964 | return ret;\r | |
965 | }\r | |
966 | \r | |
967 | int\r | |
968 | PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)\r | |
969 | {\r | |
970 | PyObject *m, *d, *v;\r | |
971 | m = PyImport_AddModule("__main__");\r | |
972 | if (m == NULL)\r | |
973 | return -1;\r | |
974 | d = PyModule_GetDict(m);\r | |
975 | v = PyRun_StringFlags(command, Py_file_input, d, d, flags);\r | |
976 | if (v == NULL) {\r | |
977 | PyErr_Print();\r | |
978 | return -1;\r | |
979 | }\r | |
980 | Py_DECREF(v);\r | |
981 | if (Py_FlushLine())\r | |
982 | PyErr_Clear();\r | |
983 | return 0;\r | |
984 | }\r | |
985 | \r | |
986 | static int\r | |
987 | parse_syntax_error(PyObject *err, PyObject **message, const char **filename,\r | |
988 | int *lineno, int *offset, const char **text)\r | |
989 | {\r | |
990 | long hold;\r | |
991 | PyObject *v;\r | |
992 | \r | |
993 | /* old style errors */\r | |
994 | if (PyTuple_Check(err))\r | |
995 | return PyArg_ParseTuple(err, "O(ziiz)", message, filename,\r | |
996 | lineno, offset, text);\r | |
997 | \r | |
998 | *message = NULL;\r | |
999 | \r | |
1000 | /* new style errors. `err' is an instance */\r | |
1001 | *message = PyObject_GetAttrString(err, "msg");\r | |
1002 | if (!*message)\r | |
1003 | goto finally;\r | |
1004 | \r | |
1005 | v = PyObject_GetAttrString(err, "filename");\r | |
1006 | if (!v)\r | |
1007 | goto finally;\r | |
1008 | if (v == Py_None) {\r | |
1009 | Py_DECREF(v);\r | |
1010 | *filename = NULL;\r | |
1011 | }\r | |
1012 | else {\r | |
1013 | *filename = PyString_AsString(v);\r | |
1014 | Py_DECREF(v);\r | |
1015 | if (!*filename)\r | |
1016 | goto finally;\r | |
1017 | }\r | |
1018 | \r | |
1019 | v = PyObject_GetAttrString(err, "lineno");\r | |
1020 | if (!v)\r | |
1021 | goto finally;\r | |
1022 | hold = PyInt_AsLong(v);\r | |
1023 | Py_DECREF(v);\r | |
1024 | if (hold < 0 && PyErr_Occurred())\r | |
1025 | goto finally;\r | |
1026 | *lineno = (int)hold;\r | |
1027 | \r | |
1028 | v = PyObject_GetAttrString(err, "offset");\r | |
1029 | if (!v)\r | |
1030 | goto finally;\r | |
1031 | if (v == Py_None) {\r | |
1032 | *offset = -1;\r | |
1033 | Py_DECREF(v);\r | |
1034 | } else {\r | |
1035 | hold = PyInt_AsLong(v);\r | |
1036 | Py_DECREF(v);\r | |
1037 | if (hold < 0 && PyErr_Occurred())\r | |
1038 | goto finally;\r | |
1039 | *offset = (int)hold;\r | |
1040 | }\r | |
1041 | \r | |
1042 | v = PyObject_GetAttrString(err, "text");\r | |
1043 | if (!v)\r | |
1044 | goto finally;\r | |
1045 | if (v == Py_None) {\r | |
1046 | Py_DECREF(v);\r | |
1047 | *text = NULL;\r | |
1048 | }\r | |
1049 | else {\r | |
1050 | *text = PyString_AsString(v);\r | |
1051 | Py_DECREF(v);\r | |
1052 | if (!*text)\r | |
1053 | goto finally;\r | |
1054 | }\r | |
1055 | return 1;\r | |
1056 | \r | |
1057 | finally:\r | |
1058 | Py_XDECREF(*message);\r | |
1059 | return 0;\r | |
1060 | }\r | |
1061 | \r | |
1062 | void\r | |
1063 | PyErr_Print(void)\r | |
1064 | {\r | |
1065 | PyErr_PrintEx(1);\r | |
1066 | }\r | |
1067 | \r | |
1068 | static void\r | |
1069 | print_error_text(PyObject *f, int offset, const char *text)\r | |
1070 | {\r | |
1071 | char *nl;\r | |
1072 | if (offset >= 0) {\r | |
1073 | if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')\r | |
1074 | offset--;\r | |
1075 | for (;;) {\r | |
1076 | nl = strchr(text, '\n');\r | |
1077 | if (nl == NULL || nl-text >= offset)\r | |
1078 | break;\r | |
1079 | offset -= (int)(nl+1-text);\r | |
1080 | text = nl+1;\r | |
1081 | }\r | |
1082 | while (*text == ' ' || *text == '\t') {\r | |
1083 | text++;\r | |
1084 | offset--;\r | |
1085 | }\r | |
1086 | }\r | |
1087 | PyFile_WriteString(" ", f);\r | |
1088 | PyFile_WriteString(text, f);\r | |
1089 | if (*text == '\0' || text[strlen(text)-1] != '\n')\r | |
1090 | PyFile_WriteString("\n", f);\r | |
1091 | if (offset == -1)\r | |
1092 | return;\r | |
1093 | PyFile_WriteString(" ", f);\r | |
1094 | offset--;\r | |
1095 | while (offset > 0) {\r | |
1096 | PyFile_WriteString(" ", f);\r | |
1097 | offset--;\r | |
1098 | }\r | |
1099 | PyFile_WriteString("^\n", f);\r | |
1100 | }\r | |
1101 | \r | |
1102 | static void\r | |
1103 | handle_system_exit(void)\r | |
1104 | {\r | |
1105 | PyObject *exception, *value, *tb;\r | |
1106 | int exitcode = 0;\r | |
1107 | \r | |
1108 | if (Py_InspectFlag)\r | |
1109 | /* Don't exit if -i flag was given. This flag is set to 0\r | |
1110 | * when entering interactive mode for inspecting. */\r | |
1111 | return;\r | |
1112 | \r | |
1113 | PyErr_Fetch(&exception, &value, &tb);\r | |
1114 | if (Py_FlushLine())\r | |
1115 | PyErr_Clear();\r | |
1116 | fflush(stdout);\r | |
1117 | if (value == NULL || value == Py_None)\r | |
1118 | goto done;\r | |
1119 | if (PyExceptionInstance_Check(value)) {\r | |
1120 | /* The error code should be in the `code' attribute. */\r | |
1121 | PyObject *code = PyObject_GetAttrString(value, "code");\r | |
1122 | if (code) {\r | |
1123 | Py_DECREF(value);\r | |
1124 | value = code;\r | |
1125 | if (value == Py_None)\r | |
1126 | goto done;\r | |
1127 | }\r | |
1128 | /* If we failed to dig out the 'code' attribute,\r | |
1129 | just let the else clause below print the error. */\r | |
1130 | }\r | |
1131 | if (PyInt_Check(value))\r | |
1132 | exitcode = (int)PyInt_AsLong(value);\r | |
1133 | else {\r | |
1134 | PyObject *sys_stderr = PySys_GetObject("stderr");\r | |
1135 | if (sys_stderr != NULL && sys_stderr != Py_None) {\r | |
1136 | PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);\r | |
1137 | } else {\r | |
1138 | PyObject_Print(value, stderr, Py_PRINT_RAW);\r | |
1139 | fflush(stderr);\r | |
1140 | }\r | |
1141 | PySys_WriteStderr("\n");\r | |
1142 | exitcode = 1;\r | |
1143 | }\r | |
1144 | done:\r | |
1145 | /* Restore and clear the exception info, in order to properly decref\r | |
1146 | * the exception, value, and traceback. If we just exit instead,\r | |
1147 | * these leak, which confuses PYTHONDUMPREFS output, and may prevent\r | |
1148 | * some finalizers from running.\r | |
1149 | */\r | |
1150 | PyErr_Restore(exception, value, tb);\r | |
1151 | PyErr_Clear();\r | |
1152 | Py_Exit(exitcode);\r | |
1153 | /* NOTREACHED */\r | |
1154 | }\r | |
1155 | \r | |
1156 | void\r | |
1157 | PyErr_PrintEx(int set_sys_last_vars)\r | |
1158 | {\r | |
1159 | PyObject *exception, *v, *tb, *hook;\r | |
1160 | \r | |
1161 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) {\r | |
1162 | handle_system_exit();\r | |
1163 | }\r | |
1164 | PyErr_Fetch(&exception, &v, &tb);\r | |
1165 | if (exception == NULL)\r | |
1166 | return;\r | |
1167 | PyErr_NormalizeException(&exception, &v, &tb);\r | |
1168 | if (exception == NULL)\r | |
1169 | return;\r | |
1170 | /* Now we know v != NULL too */\r | |
1171 | if (set_sys_last_vars) {\r | |
1172 | PySys_SetObject("last_type", exception);\r | |
1173 | PySys_SetObject("last_value", v);\r | |
1174 | PySys_SetObject("last_traceback", tb);\r | |
1175 | }\r | |
1176 | hook = PySys_GetObject("excepthook");\r | |
1177 | if (hook && hook != Py_None) {\r | |
1178 | PyObject *args = PyTuple_Pack(3,\r | |
1179 | exception, v, tb ? tb : Py_None);\r | |
1180 | PyObject *result = PyEval_CallObject(hook, args);\r | |
1181 | if (result == NULL) {\r | |
1182 | PyObject *exception2, *v2, *tb2;\r | |
1183 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) {\r | |
1184 | handle_system_exit();\r | |
1185 | }\r | |
1186 | PyErr_Fetch(&exception2, &v2, &tb2);\r | |
1187 | PyErr_NormalizeException(&exception2, &v2, &tb2);\r | |
1188 | /* It should not be possible for exception2 or v2\r | |
1189 | to be NULL. However PyErr_Display() can't\r | |
1190 | tolerate NULLs, so just be safe. */\r | |
1191 | if (exception2 == NULL) {\r | |
1192 | exception2 = Py_None;\r | |
1193 | Py_INCREF(exception2);\r | |
1194 | }\r | |
1195 | if (v2 == NULL) {\r | |
1196 | v2 = Py_None;\r | |
1197 | Py_INCREF(v2);\r | |
1198 | }\r | |
1199 | if (Py_FlushLine())\r | |
1200 | PyErr_Clear();\r | |
1201 | fflush(stdout);\r | |
1202 | PySys_WriteStderr("Error in sys.excepthook:\n");\r | |
1203 | PyErr_Display(exception2, v2, tb2);\r | |
1204 | PySys_WriteStderr("\nOriginal exception was:\n");\r | |
1205 | PyErr_Display(exception, v, tb);\r | |
1206 | Py_DECREF(exception2);\r | |
1207 | Py_DECREF(v2);\r | |
1208 | Py_XDECREF(tb2);\r | |
1209 | }\r | |
1210 | Py_XDECREF(result);\r | |
1211 | Py_XDECREF(args);\r | |
1212 | } else {\r | |
1213 | PySys_WriteStderr("sys.excepthook is missing\n");\r | |
1214 | PyErr_Display(exception, v, tb);\r | |
1215 | }\r | |
1216 | Py_XDECREF(exception);\r | |
1217 | Py_XDECREF(v);\r | |
1218 | Py_XDECREF(tb);\r | |
1219 | }\r | |
1220 | \r | |
1221 | void\r | |
1222 | PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)\r | |
1223 | {\r | |
1224 | int err = 0;\r | |
1225 | PyObject *f = PySys_GetObject("stderr");\r | |
1226 | Py_INCREF(value);\r | |
1227 | if (f == NULL || f == Py_None)\r | |
1228 | fprintf(stderr, "lost sys.stderr\n");\r | |
1229 | else {\r | |
1230 | if (Py_FlushLine())\r | |
1231 | PyErr_Clear();\r | |
1232 | fflush(stdout);\r | |
1233 | if (tb && tb != Py_None)\r | |
1234 | err = PyTraceBack_Print(tb, f);\r | |
1235 | if (err == 0 &&\r | |
1236 | PyObject_HasAttrString(value, "print_file_and_line"))\r | |
1237 | {\r | |
1238 | PyObject *message;\r | |
1239 | const char *filename, *text;\r | |
1240 | int lineno, offset;\r | |
1241 | if (!parse_syntax_error(value, &message, &filename,\r | |
1242 | &lineno, &offset, &text))\r | |
1243 | PyErr_Clear();\r | |
1244 | else {\r | |
1245 | char buf[10];\r | |
1246 | PyFile_WriteString(" File \"", f);\r | |
1247 | if (filename == NULL)\r | |
1248 | PyFile_WriteString("<string>", f);\r | |
1249 | else\r | |
1250 | PyFile_WriteString(filename, f);\r | |
1251 | PyFile_WriteString("\", line ", f);\r | |
1252 | PyOS_snprintf(buf, sizeof(buf), "%d", lineno);\r | |
1253 | PyFile_WriteString(buf, f);\r | |
1254 | PyFile_WriteString("\n", f);\r | |
1255 | if (text != NULL)\r | |
1256 | print_error_text(f, offset, text);\r | |
1257 | Py_DECREF(value);\r | |
1258 | value = message;\r | |
1259 | /* Can't be bothered to check all those\r | |
1260 | PyFile_WriteString() calls */\r | |
1261 | if (PyErr_Occurred())\r | |
1262 | err = -1;\r | |
1263 | }\r | |
1264 | }\r | |
1265 | if (err) {\r | |
1266 | /* Don't do anything else */\r | |
1267 | }\r | |
1268 | else if (PyExceptionClass_Check(exception)) {\r | |
1269 | PyObject* moduleName;\r | |
1270 | char* className = PyExceptionClass_Name(exception);\r | |
1271 | if (className != NULL) {\r | |
1272 | char *dot = strrchr(className, '.');\r | |
1273 | if (dot != NULL)\r | |
1274 | className = dot+1;\r | |
1275 | }\r | |
1276 | \r | |
1277 | moduleName = PyObject_GetAttrString(exception, "__module__");\r | |
1278 | if (moduleName == NULL)\r | |
1279 | err = PyFile_WriteString("<unknown>", f);\r | |
1280 | else {\r | |
1281 | char* modstr = PyString_AsString(moduleName);\r | |
1282 | if (modstr && strcmp(modstr, "exceptions"))\r | |
1283 | {\r | |
1284 | err = PyFile_WriteString(modstr, f);\r | |
1285 | err += PyFile_WriteString(".", f);\r | |
1286 | }\r | |
1287 | Py_DECREF(moduleName);\r | |
1288 | }\r | |
1289 | if (err == 0) {\r | |
1290 | if (className == NULL)\r | |
1291 | err = PyFile_WriteString("<unknown>", f);\r | |
1292 | else\r | |
1293 | err = PyFile_WriteString(className, f);\r | |
1294 | }\r | |
1295 | }\r | |
1296 | else\r | |
1297 | err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);\r | |
1298 | if (err == 0 && (value != Py_None)) {\r | |
1299 | PyObject *s = PyObject_Str(value);\r | |
1300 | /* only print colon if the str() of the\r | |
1301 | object is not the empty string\r | |
1302 | */\r | |
1303 | if (s == NULL)\r | |
1304 | err = -1;\r | |
1305 | else if (!PyString_Check(s) ||\r | |
1306 | PyString_GET_SIZE(s) != 0)\r | |
1307 | err = PyFile_WriteString(": ", f);\r | |
1308 | if (err == 0)\r | |
1309 | err = PyFile_WriteObject(s, f, Py_PRINT_RAW);\r | |
1310 | Py_XDECREF(s);\r | |
1311 | }\r | |
1312 | /* try to write a newline in any case */\r | |
1313 | err += PyFile_WriteString("\n", f);\r | |
1314 | }\r | |
1315 | Py_DECREF(value);\r | |
1316 | /* If an error happened here, don't show it.\r | |
1317 | XXX This is wrong, but too many callers rely on this behavior. */\r | |
1318 | if (err != 0)\r | |
1319 | PyErr_Clear();\r | |
1320 | }\r | |
1321 | \r | |
1322 | PyObject *\r | |
1323 | PyRun_StringFlags(const char *str, int start, PyObject *globals,\r | |
1324 | PyObject *locals, PyCompilerFlags *flags)\r | |
1325 | {\r | |
1326 | PyObject *ret = NULL;\r | |
1327 | mod_ty mod;\r | |
1328 | PyArena *arena = PyArena_New();\r | |
1329 | if (arena == NULL)\r | |
1330 | return NULL;\r | |
1331 | \r | |
1332 | mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);\r | |
1333 | if (mod != NULL)\r | |
1334 | ret = run_mod(mod, "<string>", globals, locals, flags, arena);\r | |
1335 | PyArena_Free(arena);\r | |
1336 | return ret;\r | |
1337 | }\r | |
1338 | \r | |
1339 | PyObject *\r | |
1340 | PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,\r | |
1341 | PyObject *locals, int closeit, PyCompilerFlags *flags)\r | |
1342 | {\r | |
1343 | PyObject *ret;\r | |
1344 | mod_ty mod;\r | |
1345 | PyArena *arena = PyArena_New();\r | |
1346 | if (arena == NULL)\r | |
1347 | return NULL;\r | |
1348 | \r | |
1349 | mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,\r | |
1350 | flags, NULL, arena);\r | |
1351 | if (closeit)\r | |
1352 | fclose(fp);\r | |
1353 | if (mod == NULL) {\r | |
1354 | PyArena_Free(arena);\r | |
1355 | return NULL;\r | |
1356 | }\r | |
1357 | ret = run_mod(mod, filename, globals, locals, flags, arena);\r | |
1358 | PyArena_Free(arena);\r | |
1359 | return ret;\r | |
1360 | }\r | |
1361 | \r | |
1362 | static PyObject *\r | |
1363 | run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,\r | |
1364 | PyCompilerFlags *flags, PyArena *arena)\r | |
1365 | {\r | |
1366 | PyCodeObject *co;\r | |
1367 | PyObject *v;\r | |
1368 | co = PyAST_Compile(mod, filename, flags, arena);\r | |
1369 | if (co == NULL)\r | |
1370 | return NULL;\r | |
1371 | v = PyEval_EvalCode(co, globals, locals);\r | |
1372 | Py_DECREF(co);\r | |
1373 | return v;\r | |
1374 | }\r | |
1375 | \r | |
1376 | static PyObject *\r | |
1377 | run_pyc_file(FILE *fp, const char *filename, PyObject *globals,\r | |
1378 | PyObject *locals, PyCompilerFlags *flags)\r | |
1379 | {\r | |
1380 | PyCodeObject *co;\r | |
1381 | PyObject *v;\r | |
1382 | long magic;\r | |
1383 | long PyImport_GetMagicNumber(void);\r | |
1384 | \r | |
1385 | magic = PyMarshal_ReadLongFromFile(fp);\r | |
1386 | if (magic != PyImport_GetMagicNumber()) {\r | |
1387 | PyErr_SetString(PyExc_RuntimeError,\r | |
1388 | "Bad magic number in .pyc file");\r | |
1389 | return NULL;\r | |
1390 | }\r | |
1391 | (void) PyMarshal_ReadLongFromFile(fp);\r | |
1392 | v = PyMarshal_ReadLastObjectFromFile(fp);\r | |
1393 | fclose(fp);\r | |
1394 | if (v == NULL || !PyCode_Check(v)) {\r | |
1395 | Py_XDECREF(v);\r | |
1396 | PyErr_SetString(PyExc_RuntimeError,\r | |
1397 | "Bad code object in .pyc file");\r | |
1398 | return NULL;\r | |
1399 | }\r | |
1400 | co = (PyCodeObject *)v;\r | |
1401 | v = PyEval_EvalCode(co, globals, locals);\r | |
1402 | if (v && flags)\r | |
1403 | flags->cf_flags |= (co->co_flags & PyCF_MASK);\r | |
1404 | Py_DECREF(co);\r | |
1405 | return v;\r | |
1406 | }\r | |
1407 | \r | |
1408 | PyObject *\r | |
1409 | Py_CompileStringFlags(const char *str, const char *filename, int start,\r | |
1410 | PyCompilerFlags *flags)\r | |
1411 | {\r | |
1412 | PyCodeObject *co;\r | |
1413 | mod_ty mod;\r | |
1414 | PyArena *arena = PyArena_New();\r | |
1415 | if (arena == NULL)\r | |
1416 | return NULL;\r | |
1417 | \r | |
1418 | mod = PyParser_ASTFromString(str, filename, start, flags, arena);\r | |
1419 | if (mod == NULL) {\r | |
1420 | PyArena_Free(arena);\r | |
1421 | return NULL;\r | |
1422 | }\r | |
1423 | if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {\r | |
1424 | PyObject *result = PyAST_mod2obj(mod);\r | |
1425 | PyArena_Free(arena);\r | |
1426 | return result;\r | |
1427 | }\r | |
1428 | co = PyAST_Compile(mod, filename, flags, arena);\r | |
1429 | PyArena_Free(arena);\r | |
1430 | return (PyObject *)co;\r | |
1431 | }\r | |
1432 | \r | |
1433 | struct symtable *\r | |
1434 | Py_SymtableString(const char *str, const char *filename, int start)\r | |
1435 | {\r | |
1436 | struct symtable *st;\r | |
1437 | mod_ty mod;\r | |
1438 | PyCompilerFlags flags;\r | |
1439 | PyArena *arena = PyArena_New();\r | |
1440 | if (arena == NULL)\r | |
1441 | return NULL;\r | |
1442 | \r | |
1443 | flags.cf_flags = 0;\r | |
1444 | \r | |
1445 | mod = PyParser_ASTFromString(str, filename, start, &flags, arena);\r | |
1446 | if (mod == NULL) {\r | |
1447 | PyArena_Free(arena);\r | |
1448 | return NULL;\r | |
1449 | }\r | |
1450 | st = PySymtable_Build(mod, filename, 0);\r | |
1451 | PyArena_Free(arena);\r | |
1452 | return st;\r | |
1453 | }\r | |
1454 | \r | |
1455 | /* Preferred access to parser is through AST. */\r | |
1456 | mod_ty\r | |
1457 | PyParser_ASTFromString(const char *s, const char *filename, int start,\r | |
1458 | PyCompilerFlags *flags, PyArena *arena)\r | |
1459 | {\r | |
1460 | mod_ty mod;\r | |
1461 | PyCompilerFlags localflags;\r | |
1462 | perrdetail err;\r | |
1463 | int iflags = PARSER_FLAGS(flags);\r | |
1464 | \r | |
1465 | node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,\r | |
1466 | &_PyParser_Grammar, start, &err,\r | |
1467 | &iflags);\r | |
1468 | if (flags == NULL) {\r | |
1469 | localflags.cf_flags = 0;\r | |
1470 | flags = &localflags;\r | |
1471 | }\r | |
1472 | if (n) {\r | |
1473 | flags->cf_flags |= iflags & PyCF_MASK;\r | |
1474 | mod = PyAST_FromNode(n, flags, filename, arena);\r | |
1475 | PyNode_Free(n);\r | |
1476 | return mod;\r | |
1477 | }\r | |
1478 | else {\r | |
1479 | err_input(&err);\r | |
1480 | return NULL;\r | |
1481 | }\r | |
1482 | }\r | |
1483 | \r | |
1484 | mod_ty\r | |
1485 | PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,\r | |
1486 | char *ps2, PyCompilerFlags *flags, int *errcode,\r | |
1487 | PyArena *arena)\r | |
1488 | {\r | |
1489 | mod_ty mod;\r | |
1490 | PyCompilerFlags localflags;\r | |
1491 | perrdetail err;\r | |
1492 | int iflags = PARSER_FLAGS(flags);\r | |
1493 | \r | |
1494 | node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,\r | |
1495 | start, ps1, ps2, &err, &iflags);\r | |
1496 | if (flags == NULL) {\r | |
1497 | localflags.cf_flags = 0;\r | |
1498 | flags = &localflags;\r | |
1499 | }\r | |
1500 | if (n) {\r | |
1501 | flags->cf_flags |= iflags & PyCF_MASK;\r | |
1502 | mod = PyAST_FromNode(n, flags, filename, arena);\r | |
1503 | PyNode_Free(n);\r | |
1504 | return mod;\r | |
1505 | }\r | |
1506 | else {\r | |
1507 | err_input(&err);\r | |
1508 | if (errcode)\r | |
1509 | *errcode = err.error;\r | |
1510 | return NULL;\r | |
1511 | }\r | |
1512 | }\r | |
1513 | \r | |
1514 | /* Simplified interface to parsefile -- return node or set exception */\r | |
1515 | \r | |
1516 | node *\r | |
1517 | PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)\r | |
1518 | {\r | |
1519 | perrdetail err;\r | |
1520 | node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,\r | |
1521 | start, NULL, NULL, &err, flags);\r | |
1522 | if (n == NULL)\r | |
1523 | err_input(&err);\r | |
1524 | \r | |
1525 | return n;\r | |
1526 | }\r | |
1527 | \r | |
1528 | /* Simplified interface to parsestring -- return node or set exception */\r | |
1529 | \r | |
1530 | node *\r | |
1531 | PyParser_SimpleParseStringFlags(const char *str, int start, int flags)\r | |
1532 | {\r | |
1533 | perrdetail err;\r | |
1534 | node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,\r | |
1535 | start, &err, flags);\r | |
1536 | if (n == NULL)\r | |
1537 | err_input(&err);\r | |
1538 | return n;\r | |
1539 | }\r | |
1540 | \r | |
1541 | node *\r | |
1542 | PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,\r | |
1543 | int start, int flags)\r | |
1544 | {\r | |
1545 | perrdetail err;\r | |
1546 | node *n = PyParser_ParseStringFlagsFilename(str, filename,\r | |
1547 | &_PyParser_Grammar, start, &err, flags);\r | |
1548 | if (n == NULL)\r | |
1549 | err_input(&err);\r | |
1550 | return n;\r | |
1551 | }\r | |
1552 | \r | |
1553 | node *\r | |
1554 | PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)\r | |
1555 | {\r | |
1556 | return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);\r | |
1557 | }\r | |
1558 | \r | |
1559 | /* May want to move a more generalized form of this to parsetok.c or\r | |
1560 | even parser modules. */\r | |
1561 | \r | |
1562 | void\r | |
1563 | PyParser_SetError(perrdetail *err)\r | |
1564 | {\r | |
1565 | err_input(err);\r | |
1566 | }\r | |
1567 | \r | |
1568 | /* Set the error appropriate to the given input error code (see errcode.h) */\r | |
1569 | \r | |
1570 | static void\r | |
1571 | err_input(perrdetail *err)\r | |
1572 | {\r | |
1573 | PyObject *v, *w, *errtype;\r | |
1574 | PyObject* u = NULL;\r | |
1575 | char *msg = NULL;\r | |
1576 | errtype = PyExc_SyntaxError;\r | |
1577 | switch (err->error) {\r | |
1578 | case E_ERROR:\r | |
1579 | return;\r | |
1580 | case E_SYNTAX:\r | |
1581 | errtype = PyExc_IndentationError;\r | |
1582 | if (err->expected == INDENT)\r | |
1583 | msg = "expected an indented block";\r | |
1584 | else if (err->token == INDENT)\r | |
1585 | msg = "unexpected indent";\r | |
1586 | else if (err->token == DEDENT)\r | |
1587 | msg = "unexpected unindent";\r | |
1588 | else {\r | |
1589 | errtype = PyExc_SyntaxError;\r | |
1590 | msg = "invalid syntax";\r | |
1591 | }\r | |
1592 | break;\r | |
1593 | case E_TOKEN:\r | |
1594 | msg = "invalid token";\r | |
1595 | break;\r | |
1596 | case E_EOFS:\r | |
1597 | msg = "EOF while scanning triple-quoted string literal";\r | |
1598 | break;\r | |
1599 | case E_EOLS:\r | |
1600 | msg = "EOL while scanning string literal";\r | |
1601 | break;\r | |
1602 | case E_INTR:\r | |
1603 | if (!PyErr_Occurred())\r | |
1604 | PyErr_SetNone(PyExc_KeyboardInterrupt);\r | |
1605 | goto cleanup;\r | |
1606 | case E_NOMEM:\r | |
1607 | PyErr_NoMemory();\r | |
1608 | goto cleanup;\r | |
1609 | case E_EOF:\r | |
1610 | msg = "unexpected EOF while parsing";\r | |
1611 | break;\r | |
1612 | case E_TABSPACE:\r | |
1613 | errtype = PyExc_TabError;\r | |
1614 | msg = "inconsistent use of tabs and spaces in indentation";\r | |
1615 | break;\r | |
1616 | case E_OVERFLOW:\r | |
1617 | msg = "expression too long";\r | |
1618 | break;\r | |
1619 | case E_DEDENT:\r | |
1620 | errtype = PyExc_IndentationError;\r | |
1621 | msg = "unindent does not match any outer indentation level";\r | |
1622 | break;\r | |
1623 | case E_TOODEEP:\r | |
1624 | errtype = PyExc_IndentationError;\r | |
1625 | msg = "too many levels of indentation";\r | |
1626 | break;\r | |
1627 | case E_DECODE: {\r | |
1628 | PyObject *type, *value, *tb;\r | |
1629 | PyErr_Fetch(&type, &value, &tb);\r | |
1630 | if (value != NULL) {\r | |
1631 | u = PyObject_Str(value);\r | |
1632 | if (u != NULL) {\r | |
1633 | msg = PyString_AsString(u);\r | |
1634 | }\r | |
1635 | }\r | |
1636 | if (msg == NULL)\r | |
1637 | msg = "unknown decode error";\r | |
1638 | Py_XDECREF(type);\r | |
1639 | Py_XDECREF(value);\r | |
1640 | Py_XDECREF(tb);\r | |
1641 | break;\r | |
1642 | }\r | |
1643 | case E_LINECONT:\r | |
1644 | msg = "unexpected character after line continuation character";\r | |
1645 | break;\r | |
1646 | default:\r | |
1647 | fprintf(stderr, "error=%d\n", err->error);\r | |
1648 | msg = "unknown parsing error";\r | |
1649 | break;\r | |
1650 | }\r | |
1651 | v = Py_BuildValue("(ziiz)", err->filename,\r | |
1652 | err->lineno, err->offset, err->text);\r | |
1653 | w = NULL;\r | |
1654 | if (v != NULL)\r | |
1655 | w = Py_BuildValue("(sO)", msg, v);\r | |
1656 | Py_XDECREF(u);\r | |
1657 | Py_XDECREF(v);\r | |
1658 | PyErr_SetObject(errtype, w);\r | |
1659 | Py_XDECREF(w);\r | |
1660 | cleanup:\r | |
1661 | if (err->text != NULL) {\r | |
1662 | PyObject_FREE(err->text);\r | |
1663 | err->text = NULL;\r | |
1664 | }\r | |
1665 | }\r | |
1666 | \r | |
1667 | /* Print fatal error message and abort */\r | |
1668 | \r | |
1669 | void\r | |
1670 | Py_FatalError(const char *msg)\r | |
1671 | {\r | |
1672 | fprintf(stderr, "Fatal Python error: %s\n", msg);\r | |
1673 | fflush(stderr); /* it helps in Windows debug build */\r | |
1674 | \r | |
1675 | #ifdef MS_WINDOWS\r | |
1676 | {\r | |
1677 | size_t len = strlen(msg);\r | |
1678 | WCHAR* buffer;\r | |
1679 | size_t i;\r | |
1680 | \r | |
1681 | /* Convert the message to wchar_t. This uses a simple one-to-one\r | |
1682 | conversion, assuming that the this error message actually uses ASCII\r | |
1683 | only. If this ceases to be true, we will have to convert. */\r | |
1684 | buffer = alloca( (len+1) * (sizeof *buffer));\r | |
1685 | for( i=0; i<=len; ++i)\r | |
1686 | buffer[i] = msg[i];\r | |
1687 | OutputDebugStringW(L"Fatal Python error: ");\r | |
1688 | OutputDebugStringW(buffer);\r | |
1689 | OutputDebugStringW(L"\n");\r | |
1690 | }\r | |
1691 | #ifdef _DEBUG\r | |
1692 | DebugBreak();\r | |
1693 | #endif\r | |
1694 | #endif /* MS_WINDOWS */\r | |
1695 | abort();\r | |
1696 | }\r | |
1697 | \r | |
1698 | /* Clean up and exit */\r | |
1699 | \r | |
1700 | #ifdef WITH_THREAD\r | |
1701 | #include "pythread.h"\r | |
1702 | #endif\r | |
1703 | \r | |
1704 | /* Wait until threading._shutdown completes, provided\r | |
1705 | the threading module was imported in the first place.\r | |
1706 | The shutdown routine will wait until all non-daemon\r | |
1707 | "threading" threads have completed. */\r | |
1708 | static void\r | |
1709 | wait_for_thread_shutdown(void)\r | |
1710 | {\r | |
1711 | #ifdef WITH_THREAD\r | |
1712 | PyObject *result;\r | |
1713 | PyThreadState *tstate = PyThreadState_GET();\r | |
1714 | PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,\r | |
1715 | "threading");\r | |
1716 | if (threading == NULL) {\r | |
1717 | /* threading not imported */\r | |
1718 | PyErr_Clear();\r | |
1719 | return;\r | |
1720 | }\r | |
1721 | result = PyObject_CallMethod(threading, "_shutdown", "");\r | |
1722 | if (result == NULL)\r | |
1723 | PyErr_WriteUnraisable(threading);\r | |
1724 | else\r | |
1725 | Py_DECREF(result);\r | |
1726 | Py_DECREF(threading);\r | |
1727 | #endif\r | |
1728 | }\r | |
1729 | \r | |
1730 | #define NEXITFUNCS 32\r | |
1731 | static void (*exitfuncs[NEXITFUNCS])(void);\r | |
1732 | static int nexitfuncs = 0;\r | |
1733 | \r | |
1734 | int Py_AtExit(void (*func)(void))\r | |
1735 | {\r | |
1736 | if (nexitfuncs >= NEXITFUNCS)\r | |
1737 | return -1;\r | |
1738 | exitfuncs[nexitfuncs++] = func;\r | |
1739 | return 0;\r | |
1740 | }\r | |
1741 | \r | |
1742 | static void\r | |
1743 | call_sys_exitfunc(void)\r | |
1744 | {\r | |
1745 | PyObject *exitfunc = PySys_GetObject("exitfunc");\r | |
1746 | \r | |
1747 | if (exitfunc) {\r | |
1748 | PyObject *res;\r | |
1749 | Py_INCREF(exitfunc);\r | |
1750 | PySys_SetObject("exitfunc", (PyObject *)NULL);\r | |
1751 | res = PyEval_CallObject(exitfunc, (PyObject *)NULL);\r | |
1752 | if (res == NULL) {\r | |
1753 | if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {\r | |
1754 | PySys_WriteStderr("Error in sys.exitfunc:\n");\r | |
1755 | }\r | |
1756 | PyErr_Print();\r | |
1757 | }\r | |
1758 | Py_DECREF(exitfunc);\r | |
1759 | }\r | |
1760 | \r | |
1761 | if (Py_FlushLine())\r | |
1762 | PyErr_Clear();\r | |
1763 | }\r | |
1764 | \r | |
1765 | static void\r | |
1766 | call_ll_exitfuncs(void)\r | |
1767 | {\r | |
1768 | while (nexitfuncs > 0)\r | |
1769 | (*exitfuncs[--nexitfuncs])();\r | |
1770 | \r | |
1771 | fflush(stdout);\r | |
1772 | fflush(stderr);\r | |
1773 | }\r | |
1774 | \r | |
1775 | void\r | |
1776 | Py_Exit(int sts)\r | |
1777 | {\r | |
1778 | Py_Finalize();\r | |
1779 | \r | |
1780 | exit(sts);\r | |
1781 | }\r | |
1782 | \r | |
1783 | static void\r | |
1784 | initsigs(void)\r | |
1785 | {\r | |
1786 | #ifdef SIGPIPE\r | |
1787 | PyOS_setsig(SIGPIPE, SIG_IGN);\r | |
1788 | #endif\r | |
1789 | #ifdef SIGXFZ\r | |
1790 | PyOS_setsig(SIGXFZ, SIG_IGN);\r | |
1791 | #endif\r | |
1792 | #ifdef SIGXFSZ\r | |
1793 | PyOS_setsig(SIGXFSZ, SIG_IGN);\r | |
1794 | #endif\r | |
1795 | PyOS_InitInterrupts(); /* May imply initsignal() */\r | |
1796 | }\r | |
1797 | \r | |
1798 | \r | |
1799 | /*\r | |
1800 | * The file descriptor fd is considered ``interactive'' if either\r | |
1801 | * a) isatty(fd) is TRUE, or\r | |
1802 | * b) the -i flag was given, and the filename associated with\r | |
1803 | * the descriptor is NULL or "<stdin>" or "???".\r | |
1804 | */\r | |
1805 | int\r | |
1806 | Py_FdIsInteractive(FILE *fp, const char *filename)\r | |
1807 | {\r | |
1808 | if (isatty((int)fileno(fp)))\r | |
1809 | return 1;\r | |
1810 | if (!Py_InteractiveFlag)\r | |
1811 | return 0;\r | |
1812 | return (filename == NULL) ||\r | |
1813 | (strcmp(filename, "<stdin>") == 0) ||\r | |
1814 | (strcmp(filename, "???") == 0);\r | |
1815 | }\r | |
1816 | \r | |
1817 | \r | |
1818 | #if defined(USE_STACKCHECK)\r | |
1819 | #if defined(WIN32) && defined(_MSC_VER)\r | |
1820 | \r | |
1821 | /* Stack checking for Microsoft C */\r | |
1822 | \r | |
1823 | #include <malloc.h>\r | |
1824 | #include <excpt.h>\r | |
1825 | \r | |
1826 | /*\r | |
1827 | * Return non-zero when we run out of memory on the stack; zero otherwise.\r | |
1828 | */\r | |
1829 | int\r | |
1830 | PyOS_CheckStack(void)\r | |
1831 | {\r | |
1832 | __try {\r | |
1833 | /* alloca throws a stack overflow exception if there's\r | |
1834 | not enough space left on the stack */\r | |
1835 | alloca(PYOS_STACK_MARGIN * sizeof(void*));\r | |
1836 | return 0;\r | |
1837 | } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?\r | |
1838 | EXCEPTION_EXECUTE_HANDLER :\r | |
1839 | EXCEPTION_CONTINUE_SEARCH) {\r | |
1840 | int errcode = _resetstkoflw();\r | |
1841 | if (errcode == 0)\r | |
1842 | {\r | |
1843 | Py_FatalError("Could not reset the stack!");\r | |
1844 | }\r | |
1845 | }\r | |
1846 | return 1;\r | |
1847 | }\r | |
1848 | \r | |
1849 | #endif /* WIN32 && _MSC_VER */\r | |
1850 | \r | |
1851 | /* Alternate implementations can be added here... */\r | |
1852 | \r | |
1853 | #endif /* USE_STACKCHECK */\r | |
1854 | \r | |
1855 | \r | |
1856 | /* Wrappers around sigaction() or signal(). */\r | |
1857 | \r | |
1858 | PyOS_sighandler_t\r | |
1859 | PyOS_getsig(int sig)\r | |
1860 | {\r | |
1861 | #ifdef HAVE_SIGACTION\r | |
1862 | struct sigaction context;\r | |
1863 | if (sigaction(sig, NULL, &context) == -1)\r | |
1864 | return SIG_ERR;\r | |
1865 | return context.sa_handler;\r | |
1866 | #else\r | |
1867 | PyOS_sighandler_t handler;\r | |
1868 | /* Special signal handling for the secure CRT in Visual Studio 2005 */\r | |
1869 | #if defined(_MSC_VER) && _MSC_VER >= 1400\r | |
1870 | switch (sig) {\r | |
1871 | /* Only these signals are valid */\r | |
1872 | case SIGINT:\r | |
1873 | case SIGILL:\r | |
1874 | case SIGFPE:\r | |
1875 | case SIGSEGV:\r | |
1876 | case SIGTERM:\r | |
1877 | case SIGBREAK:\r | |
1878 | case SIGABRT:\r | |
1879 | break;\r | |
1880 | /* Don't call signal() with other values or it will assert */\r | |
1881 | default:\r | |
1882 | return SIG_ERR;\r | |
1883 | }\r | |
1884 | #endif /* _MSC_VER && _MSC_VER >= 1400 */\r | |
1885 | handler = signal(sig, SIG_IGN);\r | |
1886 | if (handler != SIG_ERR)\r | |
1887 | signal(sig, handler);\r | |
1888 | return handler;\r | |
1889 | #endif\r | |
1890 | }\r | |
1891 | \r | |
1892 | PyOS_sighandler_t\r | |
1893 | PyOS_setsig(int sig, PyOS_sighandler_t handler)\r | |
1894 | {\r | |
1895 | #ifdef HAVE_SIGACTION\r | |
1896 | /* Some code in Modules/signalmodule.c depends on sigaction() being\r | |
1897 | * used here if HAVE_SIGACTION is defined. Fix that if this code\r | |
1898 | * changes to invalidate that assumption.\r | |
1899 | */\r | |
1900 | struct sigaction context, ocontext;\r | |
1901 | context.sa_handler = handler;\r | |
1902 | sigemptyset(&context.sa_mask);\r | |
1903 | context.sa_flags = 0;\r | |
1904 | if (sigaction(sig, &context, &ocontext) == -1)\r | |
1905 | return SIG_ERR;\r | |
1906 | return ocontext.sa_handler;\r | |
1907 | #else\r | |
1908 | PyOS_sighandler_t oldhandler;\r | |
1909 | oldhandler = signal(sig, handler);\r | |
1910 | #ifdef HAVE_SIGINTERRUPT\r | |
1911 | siginterrupt(sig, 1);\r | |
1912 | #endif\r | |
1913 | return oldhandler;\r | |
1914 | #endif\r | |
1915 | }\r | |
1916 | \r | |
1917 | /* Deprecated C API functions still provided for binary compatiblity */\r | |
1918 | \r | |
1919 | #undef PyParser_SimpleParseFile\r | |
1920 | PyAPI_FUNC(node *)\r | |
1921 | PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)\r | |
1922 | {\r | |
1923 | return PyParser_SimpleParseFileFlags(fp, filename, start, 0);\r | |
1924 | }\r | |
1925 | \r | |
1926 | #undef PyParser_SimpleParseString\r | |
1927 | PyAPI_FUNC(node *)\r | |
1928 | PyParser_SimpleParseString(const char *str, int start)\r | |
1929 | {\r | |
1930 | return PyParser_SimpleParseStringFlags(str, start, 0);\r | |
1931 | }\r | |
1932 | \r | |
1933 | #undef PyRun_AnyFile\r | |
1934 | PyAPI_FUNC(int)\r | |
1935 | PyRun_AnyFile(FILE *fp, const char *name)\r | |
1936 | {\r | |
1937 | return PyRun_AnyFileExFlags(fp, name, 0, NULL);\r | |
1938 | }\r | |
1939 | \r | |
1940 | #undef PyRun_AnyFileEx\r | |
1941 | PyAPI_FUNC(int)\r | |
1942 | PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)\r | |
1943 | {\r | |
1944 | return PyRun_AnyFileExFlags(fp, name, closeit, NULL);\r | |
1945 | }\r | |
1946 | \r | |
1947 | #undef PyRun_AnyFileFlags\r | |
1948 | PyAPI_FUNC(int)\r | |
1949 | PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)\r | |
1950 | {\r | |
1951 | return PyRun_AnyFileExFlags(fp, name, 0, flags);\r | |
1952 | }\r | |
1953 | \r | |
1954 | #undef PyRun_File\r | |
1955 | PyAPI_FUNC(PyObject *)\r | |
1956 | PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)\r | |
1957 | {\r | |
1958 | return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);\r | |
1959 | }\r | |
1960 | \r | |
1961 | #undef PyRun_FileEx\r | |
1962 | PyAPI_FUNC(PyObject *)\r | |
1963 | PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)\r | |
1964 | {\r | |
1965 | return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);\r | |
1966 | }\r | |
1967 | \r | |
1968 | #undef PyRun_FileFlags\r | |
1969 | PyAPI_FUNC(PyObject *)\r | |
1970 | PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,\r | |
1971 | PyCompilerFlags *flags)\r | |
1972 | {\r | |
1973 | return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);\r | |
1974 | }\r | |
1975 | \r | |
1976 | #undef PyRun_SimpleFile\r | |
1977 | PyAPI_FUNC(int)\r | |
1978 | PyRun_SimpleFile(FILE *f, const char *p)\r | |
1979 | {\r | |
1980 | return PyRun_SimpleFileExFlags(f, p, 0, NULL);\r | |
1981 | }\r | |
1982 | \r | |
1983 | #undef PyRun_SimpleFileEx\r | |
1984 | PyAPI_FUNC(int)\r | |
1985 | PyRun_SimpleFileEx(FILE *f, const char *p, int c)\r | |
1986 | {\r | |
1987 | return PyRun_SimpleFileExFlags(f, p, c, NULL);\r | |
1988 | }\r | |
1989 | \r | |
1990 | \r | |
1991 | #undef PyRun_String\r | |
1992 | PyAPI_FUNC(PyObject *)\r | |
1993 | PyRun_String(const char *str, int s, PyObject *g, PyObject *l)\r | |
1994 | {\r | |
1995 | return PyRun_StringFlags(str, s, g, l, NULL);\r | |
1996 | }\r | |
1997 | \r | |
1998 | #undef PyRun_SimpleString\r | |
1999 | PyAPI_FUNC(int)\r | |
2000 | PyRun_SimpleString(const char *s)\r | |
2001 | {\r | |
2002 | return PyRun_SimpleStringFlags(s, NULL);\r | |
2003 | }\r | |
2004 | \r | |
2005 | #undef Py_CompileString\r | |
2006 | PyAPI_FUNC(PyObject *)\r | |
2007 | Py_CompileString(const char *str, const char *p, int s)\r | |
2008 | {\r | |
2009 | return Py_CompileStringFlags(str, p, s, NULL);\r | |
2010 | }\r | |
2011 | \r | |
2012 | #undef PyRun_InteractiveOne\r | |
2013 | PyAPI_FUNC(int)\r | |
2014 | PyRun_InteractiveOne(FILE *f, const char *p)\r | |
2015 | {\r | |
2016 | return PyRun_InteractiveOneFlags(f, p, NULL);\r | |
2017 | }\r | |
2018 | \r | |
2019 | #undef PyRun_InteractiveLoop\r | |
2020 | PyAPI_FUNC(int)\r | |
2021 | PyRun_InteractiveLoop(FILE *f, const char *p)\r | |
2022 | {\r | |
2023 | return PyRun_InteractiveLoopFlags(f, p, NULL);\r | |
2024 | }\r | |
2025 | \r | |
2026 | #ifdef __cplusplus\r | |
2027 | }\r | |
2028 | #endif\r | |
2029 | \r |