Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(16289)

Delta Between Two Patch Sets: Python/pythonrun.c

Issue 13390: Hunt memory allocations in addition to reference leaks
Left Patch Set: Created 7 years, 10 months ago
Right Patch Set: Created 6 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Objects/obmalloc.c ('k') | Python/sysmodule.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 1
2 /* Python interpreter top-level routines, including init/exit */ 2 /* Python interpreter top-level routines, including init/exit */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 5
6 #include "Python-ast.h" 6 #include "Python-ast.h"
7 #undef Yield /* undefine macro conflicting with winbase.h */ 7 #undef Yield /* undefine macro conflicting with winbase.h */
8 #include "grammar.h" 8 #include "grammar.h"
9 #include "node.h" 9 #include "node.h"
10 #include "token.h" 10 #include "token.h"
(...skipping 23 matching lines...) Expand all
34 #include "windows.h" 34 #include "windows.h"
35 #define PATH_MAX MAXPATHLEN 35 #define PATH_MAX MAXPATHLEN
36 #endif 36 #endif
37 37
38 #ifndef Py_REF_DEBUG 38 #ifndef Py_REF_DEBUG
39 #define PRINT_TOTAL_REFS() 39 #define PRINT_TOTAL_REFS()
40 #else /* Py_REF_DEBUG */ 40 #else /* Py_REF_DEBUG */
41 #define PRINT_TOTAL_REFS() fprintf(stderr, \ 41 #define PRINT_TOTAL_REFS() fprintf(stderr, \
42 "[%" PY_FORMAT_SIZE_T "d refs, " \ 42 "[%" PY_FORMAT_SIZE_T "d refs, " \
43 "%" PY_FORMAT_SIZE_T "d blocks]\n", \ 43 "%" PY_FORMAT_SIZE_T "d blocks]\n", \
44 _Py_GetRefTotal(), _Py_AllocedBlocks) 44 _Py_GetRefTotal(), _Py_GetAllocedBlocks())
45 #endif 45 #endif
46 46
47 #ifdef __cplusplus 47 #ifdef __cplusplus
48 extern "C" { 48 extern "C" {
49 #endif 49 #endif
50 50
51 extern wchar_t *Py_GetPath(void); 51 extern wchar_t *Py_GetPath(void);
52 52
53 extern grammar _PyParser_Grammar; /* From graminit.c */ 53 extern grammar _PyParser_Grammar; /* From graminit.c */
54 54
55 /* Forward */ 55 /* Forward */
56 static void initmain(void); 56 static void initmain(PyInterpreterState *interp);
57 static int initfsencoding(PyInterpreterState *interp); 57 static int initfsencoding(PyInterpreterState *interp);
58 static void initsite(void); 58 static void initsite(void);
59 static int initstdio(void); 59 static int initstdio(void);
60 static void flush_io(void); 60 static void flush_io(void);
61 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *, 61 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
62 PyCompilerFlags *, PyArena *); 62 PyCompilerFlags *, PyArena *);
63 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *, 63 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
64 PyCompilerFlags *); 64 PyCompilerFlags *);
65 static void err_input(perrdetail *); 65 static void err_input(perrdetail *);
66 static void err_free(perrdetail *); 66 static void err_free(perrdetail *);
(...skipping 19 matching lines...) Expand all
86 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ 86 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
87 int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */ 87 int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
88 int Py_NoSiteFlag; /* Suppress 'import site' */ 88 int Py_NoSiteFlag; /* Suppress 'import site' */
89 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */ 89 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
90 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */ 90 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
91 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */ 91 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
92 int Py_FrozenFlag; /* Needed by getpath.c */ 92 int Py_FrozenFlag; /* Needed by getpath.c */
93 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */ 93 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
94 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ 94 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
95 int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */ 95 int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
96 int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
96 97
97 PyThreadState *_Py_Finalizing = NULL; 98 PyThreadState *_Py_Finalizing = NULL;
98 99
99 /* PyModule_GetWarningsModule is no longer necessary as of 2.6 100 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
100 since _warnings is builtin. This API should not be used. */ 101 since _warnings is builtin. This API should not be used. */
101 PyObject * 102 PyObject *
102 PyModule_GetWarningsModule(void) 103 PyModule_GetWarningsModule(void)
103 { 104 {
104 return PyImport_ImportModule("warnings"); 105 return PyImport_ImportModule("warnings");
105 } 106 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty"); 184 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
184 return NULL; 185 return NULL;
185 } 186 }
186 return get_codec_name(codeset); 187 return get_codec_name(codeset);
187 #else 188 #else
188 PyErr_SetNone(PyExc_NotImplementedError); 189 PyErr_SetNone(PyExc_NotImplementedError);
189 return NULL; 190 return NULL;
190 #endif 191 #endif
191 } 192 }
192 193
194 static void
195 import_init(PyInterpreterState *interp, PyObject *sysmod)
196 {
197 PyObject *importlib;
198 PyObject *impmod;
199 PyObject *sys_modules;
200 PyObject *value;
201
202 /* Import _importlib through its frozen version, _frozen_importlib. */
203 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
204 Py_FatalError("Py_Initialize: can't import _frozen_importlib");
205 }
206 else if (Py_VerboseFlag) {
207 PySys_FormatStderr("import _frozen_importlib # frozen\n");
208 }
209 importlib = PyImport_AddModule("_frozen_importlib");
210 if (importlib == NULL) {
211 Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from "
212 "sys.modules");
213 }
214 interp->importlib = importlib;
215 Py_INCREF(interp->importlib);
216
217 /* Install _importlib as __import__ */
218 impmod = PyInit_imp();
219 if (impmod == NULL) {
220 Py_FatalError("Py_Initialize: can't import imp");
221 }
222 else if (Py_VerboseFlag) {
223 PySys_FormatStderr("import imp # builtin\n");
224 }
225 sys_modules = PyImport_GetModuleDict();
226 if (Py_VerboseFlag) {
227 PySys_FormatStderr("import sys # builtin\n");
228 }
229 if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) {
230 Py_FatalError("Py_Initialize: can't save _imp to sys.modules");
231 }
232
233 value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
234 if (value == NULL) {
235 PyErr_Print();
236 Py_FatalError("Py_Initialize: importlib install failed");
237 }
238 Py_DECREF(value);
239 Py_DECREF(impmod);
240
241 _PyImportZip_Init();
242 }
243
244
193 void 245 void
194 Py_InitializeEx(int install_sigs) 246 _Py_InitializeEx_Private(int install_sigs, int install_importlib)
195 { 247 {
196 PyInterpreterState *interp; 248 PyInterpreterState *interp;
197 PyThreadState *tstate; 249 PyThreadState *tstate;
198 PyObject *bimod, *sysmod, *pstderr; 250 PyObject *bimod, *sysmod, *pstderr;
199 char *p; 251 char *p;
200 extern void _Py_ReadyTypes(void); 252 extern void _Py_ReadyTypes(void);
201 253
202 if (initialized) 254 if (initialized)
203 return; 255 return;
204 initialized = 1; 256 initialized = 1;
205 _Py_Finalizing = NULL; 257 _Py_Finalizing = NULL;
206 258
207 #if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE) 259 #if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
208 /* Set up the LC_CTYPE locale, so we can obtain 260 /* Set up the LC_CTYPE locale, so we can obtain
209 the locale's charset without having to switch 261 the locale's charset without having to switch
210 locales. */ 262 locales. */
211 setlocale(LC_CTYPE, ""); 263 setlocale(LC_CTYPE, "");
212 #endif 264 #endif
213 265
214 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0') 266 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
215 Py_DebugFlag = add_flag(Py_DebugFlag, p); 267 Py_DebugFlag = add_flag(Py_DebugFlag, p);
216 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0') 268 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
217 Py_VerboseFlag = add_flag(Py_VerboseFlag, p); 269 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
218 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0') 270 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
219 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p); 271 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
220 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0') 272 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
221 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p); 273 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
274 /* The variable is only tested for existence here; _PyRandom_Init will
275 check its value further. */
276 if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
277 Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
278
279 _PyRandom_Init();
222 280
223 interp = PyInterpreterState_New(); 281 interp = PyInterpreterState_New();
224 if (interp == NULL) 282 if (interp == NULL)
225 Py_FatalError("Py_Initialize: can't make first interpreter"); 283 Py_FatalError("Py_Initialize: can't make first interpreter");
226 284
227 tstate = PyThreadState_New(interp); 285 tstate = PyThreadState_New(interp);
228 if (tstate == NULL) 286 if (tstate == NULL)
229 Py_FatalError("Py_Initialize: can't make first thread"); 287 Py_FatalError("Py_Initialize: can't make first thread");
230 (void) PyThreadState_Swap(tstate); 288 (void) PyThreadState_Swap(tstate);
231 289
(...skipping 18 matching lines...) Expand all
250 Py_FatalError("Py_Initialize: can't init longs"); 308 Py_FatalError("Py_Initialize: can't init longs");
251 309
252 if (!PyByteArray_Init()) 310 if (!PyByteArray_Init())
253 Py_FatalError("Py_Initialize: can't init bytearray"); 311 Py_FatalError("Py_Initialize: can't init bytearray");
254 312
255 _PyFloat_Init(); 313 _PyFloat_Init();
256 314
257 interp->modules = PyDict_New(); 315 interp->modules = PyDict_New();
258 if (interp->modules == NULL) 316 if (interp->modules == NULL)
259 Py_FatalError("Py_Initialize: can't make modules dictionary"); 317 Py_FatalError("Py_Initialize: can't make modules dictionary");
260 interp->modules_reloading = PyDict_New();
261 if (interp->modules_reloading == NULL)
262 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
263 318
264 /* Init Unicode implementation; relies on the codec registry */ 319 /* Init Unicode implementation; relies on the codec registry */
265 if (_PyUnicode_Init() < 0) 320 if (_PyUnicode_Init() < 0)
266 Py_FatalError("Py_Initialize: can't initialize unicode"); 321 Py_FatalError("Py_Initialize: can't initialize unicode");
267 322
268 bimod = _PyBuiltin_Init(); 323 bimod = _PyBuiltin_Init();
269 if (bimod == NULL) 324 if (bimod == NULL)
270 Py_FatalError("Py_Initialize: can't initialize builtins modules"); 325 Py_FatalError("Py_Initialize: can't initialize builtins modules");
271 _PyImport_FixupBuiltin(bimod, "builtins"); 326 _PyImport_FixupBuiltin(bimod, "builtins");
272 interp->builtins = PyModule_GetDict(bimod); 327 interp->builtins = PyModule_GetDict(bimod);
273 if (interp->builtins == NULL) 328 if (interp->builtins == NULL)
274 Py_FatalError("Py_Initialize: can't initialize builtins dict"); 329 Py_FatalError("Py_Initialize: can't initialize builtins dict");
275 Py_INCREF(interp->builtins); 330 Py_INCREF(interp->builtins);
276 331
277 /* initialize builtin exceptions */ 332 /* initialize builtin exceptions */
278 _PyExc_Init(); 333 _PyExc_Init(bimod);
279 334
280 sysmod = _PySys_Init(); 335 sysmod = _PySys_Init();
281 if (sysmod == NULL) 336 if (sysmod == NULL)
282 Py_FatalError("Py_Initialize: can't initialize sys"); 337 Py_FatalError("Py_Initialize: can't initialize sys");
283 interp->sysdict = PyModule_GetDict(sysmod); 338 interp->sysdict = PyModule_GetDict(sysmod);
284 if (interp->sysdict == NULL) 339 if (interp->sysdict == NULL)
285 Py_FatalError("Py_Initialize: can't initialize sys dict"); 340 Py_FatalError("Py_Initialize: can't initialize sys dict");
286 Py_INCREF(interp->sysdict); 341 Py_INCREF(interp->sysdict);
287 _PyImport_FixupBuiltin(sysmod, "sys"); 342 _PyImport_FixupBuiltin(sysmod, "sys");
288 PySys_SetPath(Py_GetPath()); 343 PySys_SetPath(Py_GetPath());
289 PyDict_SetItemString(interp->sysdict, "modules", 344 PyDict_SetItemString(interp->sysdict, "modules",
290 interp->modules); 345 interp->modules);
291 346
292 /* Set up a preliminary stderr printer until we have enough 347 /* Set up a preliminary stderr printer until we have enough
293 infrastructure for the io module in place. */ 348 infrastructure for the io module in place. */
294 pstderr = PyFile_NewStdPrinter(fileno(stderr)); 349 pstderr = PyFile_NewStdPrinter(fileno(stderr));
295 if (pstderr == NULL) 350 if (pstderr == NULL)
296 Py_FatalError("Py_Initialize: can't set preliminary stderr"); 351 Py_FatalError("Py_Initialize: can't set preliminary stderr");
297 PySys_SetObject("stderr", pstderr); 352 PySys_SetObject("stderr", pstderr);
298 PySys_SetObject("__stderr__", pstderr); 353 PySys_SetObject("__stderr__", pstderr);
299 Py_DECREF(pstderr); 354 Py_DECREF(pstderr);
300 355
301 _PyImport_Init(); 356 _PyImport_Init();
302 357
303 _PyImportHooks_Init(); 358 _PyImportHooks_Init();
304 359
360 /* Initialize _warnings. */
361 _PyWarnings_Init();
362
363 if (!install_importlib)
364 return;
365
366 import_init(interp, sysmod);
367
305 /* initialize the faulthandler module */ 368 /* initialize the faulthandler module */
306 if (_PyFaulthandler_Init()) 369 if (_PyFaulthandler_Init())
307 Py_FatalError("Py_Initialize: can't initialize faulthandler"); 370 Py_FatalError("Py_Initialize: can't initialize faulthandler");
308 371
309 /* Initialize _warnings. */
310 _PyWarnings_Init();
311
312 _PyTime_Init(); 372 _PyTime_Init();
313 373
314 if (initfsencoding(interp) < 0) 374 if (initfsencoding(interp) < 0)
315 Py_FatalError("Py_Initialize: unable to load the file system codec"); 375 Py_FatalError("Py_Initialize: unable to load the file system codec");
316 376
317 if (install_sigs) 377 if (install_sigs)
318 initsigs(); /* Signal handling stuff, including initintr() */ 378 initsigs(); /* Signal handling stuff, including initintr() */
319 379
320 initmain(); /* Module __main__ */ 380 initmain(interp); /* Module __main__ */
321 if (initstdio() < 0) 381 if (initstdio() < 0)
322 Py_FatalError( 382 Py_FatalError(
323 "Py_Initialize: can't initialize sys standard streams"); 383 "Py_Initialize: can't initialize sys standard streams");
324 384
325 /* Initialize warnings. */ 385 /* Initialize warnings. */
326 if (PySys_HasWarnOptions()) { 386 if (PySys_HasWarnOptions()) {
327 PyObject *warnings_module = PyImport_ImportModule("warnings"); 387 PyObject *warnings_module = PyImport_ImportModule("warnings");
328 if (warnings_module == NULL) { 388 if (warnings_module == NULL) {
329 fprintf(stderr, "'import warnings' failed; traceback:\n"); 389 fprintf(stderr, "'import warnings' failed; traceback:\n");
330 PyErr_Print(); 390 PyErr_Print();
331 } 391 }
332 Py_XDECREF(warnings_module); 392 Py_XDECREF(warnings_module);
333 } 393 }
334 394
335 if (!Py_NoSiteFlag) 395 if (!Py_NoSiteFlag)
336 initsite(); /* Module site */ 396 initsite(); /* Module site */
337 } 397 }
338 398
339 void 399 void
400 Py_InitializeEx(int install_sigs)
401 {
402 _Py_InitializeEx_Private(install_sigs, 1);
403 }
404
405 void
340 Py_Initialize(void) 406 Py_Initialize(void)
341 { 407 {
342 Py_InitializeEx(1); 408 Py_InitializeEx(1);
343 } 409 }
344 410
345 411
346 #ifdef COUNT_ALLOCS 412 #ifdef COUNT_ALLOCS
347 extern void dump_counts(FILE*); 413 extern void dump_counts(FILE*);
348 #endif 414 #endif
349 415
350 /* Flush stdout and stderr */ 416 /* Flush stdout and stderr */
417
418 static int
419 file_is_closed(PyObject *fobj)
420 {
421 int r;
422 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
423 if (tmp == NULL) {
424 PyErr_Clear();
425 return 0;
426 }
427 r = PyObject_IsTrue(tmp);
428 Py_DECREF(tmp);
429 if (r < 0)
430 PyErr_Clear();
431 return r > 0;
432 }
351 433
352 static void 434 static void
353 flush_std_files(void) 435 flush_std_files(void)
354 { 436 {
355 PyObject *fout = PySys_GetObject("stdout"); 437 PyObject *fout = PySys_GetObject("stdout");
356 PyObject *ferr = PySys_GetObject("stderr"); 438 PyObject *ferr = PySys_GetObject("stderr");
357 PyObject *tmp; 439 PyObject *tmp;
358 _Py_IDENTIFIER(flush); 440 _Py_IDENTIFIER(flush);
359 441
360 if (fout != NULL && fout != Py_None) { 442 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
361 tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); 443 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
362 if (tmp == NULL) 444 if (tmp == NULL)
363 PyErr_WriteUnraisable(fout); 445 PyErr_WriteUnraisable(fout);
364 else 446 else
365 Py_DECREF(tmp); 447 Py_DECREF(tmp);
366 } 448 }
367 449
368 if (ferr != NULL && ferr != Py_None) { 450 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
369 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, ""); 451 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
370 if (tmp == NULL) 452 if (tmp == NULL)
371 PyErr_Clear(); 453 PyErr_Clear();
372 else 454 else
373 Py_DECREF(tmp); 455 Py_DECREF(tmp);
374 } 456 }
375 } 457 }
376 458
377 /* Undo the effect of Py_Initialize(). 459 /* Undo the effect of Py_Initialize().
378 460
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 debug information can be printed out */ 534 debug information can be printed out */
453 _PyGC_Fini(); 535 _PyGC_Fini();
454 536
455 /* Destroy all modules */ 537 /* Destroy all modules */
456 PyImport_Cleanup(); 538 PyImport_Cleanup();
457 539
458 /* Flush stdout+stderr (again, in case more was printed) */ 540 /* Flush stdout+stderr (again, in case more was printed) */
459 flush_std_files(); 541 flush_std_files();
460 542
461 /* Collect final garbage. This disposes of cycles created by 543 /* Collect final garbage. This disposes of cycles created by
462 * new-style class definitions, for example. 544 * class definitions, for example.
463 * XXX This is disabled because it caused too many problems. If 545 * XXX This is disabled because it caused too many problems. If
464 * XXX a __del__ or weakref callback triggers here, Python code has 546 * XXX a __del__ or weakref callback triggers here, Python code has
465 * XXX a hard time running, because even the sys module has been 547 * XXX a hard time running, because even the sys module has been
466 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). 548 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
467 * XXX One symptom is a sequence of information-free messages 549 * XXX One symptom is a sequence of information-free messages
468 * XXX coming from threads (if a __del__ or callback is invoked, 550 * XXX coming from threads (if a __del__ or callback is invoked,
469 * XXX other threads can execute too, and any exception they encounter 551 * XXX other threads can execute too, and any exception they encounter
470 * XXX triggers a comedy of errors as subsystem after subsystem 552 * XXX triggers a comedy of errors as subsystem after subsystem
471 * XXX fails to find what it *expects* to find in sys to help report 553 * XXX fails to find what it *expects* to find in sys to help report
472 * XXX the exception and consequent unexpected failures). I've also 554 * XXX the exception and consequent unexpected failures). I've also
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 PyFrame_Fini(); 607 PyFrame_Fini();
526 PyCFunction_Fini(); 608 PyCFunction_Fini();
527 PyTuple_Fini(); 609 PyTuple_Fini();
528 PyList_Fini(); 610 PyList_Fini();
529 PySet_Fini(); 611 PySet_Fini();
530 PyBytes_Fini(); 612 PyBytes_Fini();
531 PyByteArray_Fini(); 613 PyByteArray_Fini();
532 PyLong_Fini(); 614 PyLong_Fini();
533 PyFloat_Fini(); 615 PyFloat_Fini();
534 PyDict_Fini(); 616 PyDict_Fini();
617 PySlice_Fini();
535 618
536 /* Cleanup Unicode implementation */ 619 /* Cleanup Unicode implementation */
537 _PyUnicode_Fini(); 620 _PyUnicode_Fini();
538 621
539 /* reset file system default encoding */ 622 /* reset file system default encoding */
540 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) { 623 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
541 free((char*)Py_FileSystemDefaultEncoding); 624 free((char*)Py_FileSystemDefaultEncoding);
542 Py_FileSystemDefaultEncoding = NULL; 625 Py_FileSystemDefaultEncoding = NULL;
543 } 626 }
544 627
545 /* XXX Still allocated: 628 /* XXX Still allocated:
546 - various static ad-hoc pointers to interned strings 629 - various static ad-hoc pointers to interned strings
547 - int and float free list blocks 630 - int and float free list blocks
548 - whatever various modules and libraries allocate 631 - whatever various modules and libraries allocate
549 */ 632 */
550 633
551 PyGrammar_RemoveAccelerators(&_PyParser_Grammar); 634 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
552 635
553 #ifdef Py_TRACE_REFS 636 #ifdef Py_TRACE_REFS
554 /* Display addresses (& refcnts) of all objects still alive. 637 /* Display addresses (& refcnts) of all objects still alive.
555 * An address can be used to find the repr of the object, printed 638 * An address can be used to find the repr of the object, printed
556 * above by _Py_PrintReferences. 639 * above by _Py_PrintReferences.
557 */ 640 */
558 if (Py_GETENV("PYTHONDUMPREFS")) 641 if (Py_GETENV("PYTHONDUMPREFS"))
559 _Py_PrintReferenceAddresses(stderr); 642 _Py_PrintReferenceAddresses(stderr);
560 #endif /* Py_TRACE_REFS */ 643 #endif /* Py_TRACE_REFS */
561 #ifdef PYMALLOC_DEBUG 644 #ifdef PYMALLOC_DEBUG
562 if (Py_GETENV("PYTHONMALLOCSTATS")) 645 if (Py_GETENV("PYTHONMALLOCSTATS"))
563 _PyObject_DebugMallocStats(); 646 _PyObject_DebugMallocStats(stderr);
564 #endif 647 #endif
565 648
566 call_ll_exitfuncs(); 649 call_ll_exitfuncs();
567 } 650 }
568 651
569 /* Create and initialize a new interpreter and thread, and return the 652 /* Create and initialize a new interpreter and thread, and return the
570 new thread. This requires that Py_Initialize() has been called 653 new thread. This requires that Py_Initialize() has been called
571 first. 654 first.
572 655
573 Unsuccessful initialization yields a NULL pointer. Note that *no* 656 Unsuccessful initialization yields a NULL pointer. Note that *no*
(...skipping 23 matching lines...) Expand all
597 if (tstate == NULL) { 680 if (tstate == NULL) {
598 PyInterpreterState_Delete(interp); 681 PyInterpreterState_Delete(interp);
599 return NULL; 682 return NULL;
600 } 683 }
601 684
602 save_tstate = PyThreadState_Swap(tstate); 685 save_tstate = PyThreadState_Swap(tstate);
603 686
604 /* XXX The following is lax in error checking */ 687 /* XXX The following is lax in error checking */
605 688
606 interp->modules = PyDict_New(); 689 interp->modules = PyDict_New();
607 interp->modules_reloading = PyDict_New();
608 690
609 bimod = _PyImport_FindBuiltin("builtins"); 691 bimod = _PyImport_FindBuiltin("builtins");
610 if (bimod != NULL) { 692 if (bimod != NULL) {
611 interp->builtins = PyModule_GetDict(bimod); 693 interp->builtins = PyModule_GetDict(bimod);
612 if (interp->builtins == NULL) 694 if (interp->builtins == NULL)
613 goto handle_error; 695 goto handle_error;
614 Py_INCREF(interp->builtins); 696 Py_INCREF(interp->builtins);
615 } 697 }
616 698
617 /* initialize builtin exceptions */ 699 /* initialize builtin exceptions */
618 _PyExc_Init(); 700 _PyExc_Init(bimod);
619 701
620 sysmod = _PyImport_FindBuiltin("sys"); 702 sysmod = _PyImport_FindBuiltin("sys");
621 if (bimod != NULL && sysmod != NULL) { 703 if (bimod != NULL && sysmod != NULL) {
622 PyObject *pstderr; 704 PyObject *pstderr;
705
623 interp->sysdict = PyModule_GetDict(sysmod); 706 interp->sysdict = PyModule_GetDict(sysmod);
624 if (interp->sysdict == NULL) 707 if (interp->sysdict == NULL)
625 goto handle_error; 708 goto handle_error;
626 Py_INCREF(interp->sysdict); 709 Py_INCREF(interp->sysdict);
627 PySys_SetPath(Py_GetPath()); 710 PySys_SetPath(Py_GetPath());
628 PyDict_SetItemString(interp->sysdict, "modules", 711 PyDict_SetItemString(interp->sysdict, "modules",
629 interp->modules); 712 interp->modules);
630 /* Set up a preliminary stderr printer until we have enough 713 /* Set up a preliminary stderr printer until we have enough
631 infrastructure for the io module in place. */ 714 infrastructure for the io module in place. */
632 pstderr = PyFile_NewStdPrinter(fileno(stderr)); 715 pstderr = PyFile_NewStdPrinter(fileno(stderr));
633 if (pstderr == NULL) 716 if (pstderr == NULL)
634 Py_FatalError("Py_Initialize: can't set preliminary stderr"); 717 Py_FatalError("Py_Initialize: can't set preliminary stderr");
635 PySys_SetObject("stderr", pstderr); 718 PySys_SetObject("stderr", pstderr);
636 PySys_SetObject("__stderr__", pstderr); 719 PySys_SetObject("__stderr__", pstderr);
637 Py_DECREF(pstderr); 720 Py_DECREF(pstderr);
638 721
639 _PyImportHooks_Init(); 722 _PyImportHooks_Init();
640 723
724 import_init(interp, sysmod);
725
641 if (initfsencoding(interp) < 0) 726 if (initfsencoding(interp) < 0)
642 goto handle_error; 727 goto handle_error;
643 728
644 if (initstdio() < 0) 729 if (initstdio() < 0)
645 Py_FatalError( 730 Py_FatalError(
646 "Py_Initialize: can't initialize sys standard streams"); 731 "Py_Initialize: can't initialize sys standard streams");
647 initmain(); 732 initmain(interp);
648 if (!Py_NoSiteFlag) 733 if (!Py_NoSiteFlag)
649 initsite(); 734 initsite();
650 } 735 }
651 736
652 if (!PyErr_Occurred()) 737 if (!PyErr_Occurred())
653 return tstate; 738 return tstate;
654 739
655 handle_error: 740 handle_error:
656 /* Oops, it didn't work. Undo it all. */ 741 /* Oops, it didn't work. Undo it all. */
657 742
(...skipping 29 matching lines...) Expand all
687 Py_FatalError("Py_EndInterpreter: thread still has a frame"); 772 Py_FatalError("Py_EndInterpreter: thread still has a frame");
688 if (tstate != interp->tstate_head || tstate->next != NULL) 773 if (tstate != interp->tstate_head || tstate->next != NULL)
689 Py_FatalError("Py_EndInterpreter: not the last thread"); 774 Py_FatalError("Py_EndInterpreter: not the last thread");
690 775
691 PyImport_Cleanup(); 776 PyImport_Cleanup();
692 PyInterpreterState_Clear(interp); 777 PyInterpreterState_Clear(interp);
693 PyThreadState_Swap(NULL); 778 PyThreadState_Swap(NULL);
694 PyInterpreterState_Delete(interp); 779 PyInterpreterState_Delete(interp);
695 } 780 }
696 781
782 #ifdef MS_WINDOWS
697 static wchar_t *progname = L"python"; 783 static wchar_t *progname = L"python";
784 #else
785 static wchar_t *progname = L"python3";
786 #endif
698 787
699 void 788 void
700 Py_SetProgramName(wchar_t *pn) 789 Py_SetProgramName(wchar_t *pn)
701 { 790 {
702 if (pn && *pn) 791 if (pn && *pn)
703 progname = pn; 792 progname = pn;
704 } 793 }
705 794
706 wchar_t * 795 wchar_t *
707 Py_GetProgramName(void) 796 Py_GetProgramName(void)
(...skipping 22 matching lines...) Expand all
730 home = env_home; 819 home = env_home;
731 } 820 }
732 821
733 } 822 }
734 return home; 823 return home;
735 } 824 }
736 825
737 /* Create __main__ module */ 826 /* Create __main__ module */
738 827
739 static void 828 static void
740 initmain(void) 829 initmain(PyInterpreterState *interp)
741 { 830 {
742 PyObject *m, *d; 831 PyObject *m, *d;
743 m = PyImport_AddModule("__main__"); 832 m = PyImport_AddModule("__main__");
744 if (m == NULL) 833 if (m == NULL)
745 Py_FatalError("can't create __main__ module"); 834 Py_FatalError("can't create __main__ module");
746 d = PyModule_GetDict(m); 835 d = PyModule_GetDict(m);
747 if (PyDict_GetItemString(d, "__builtins__") == NULL) { 836 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
748 PyObject *bimod = PyImport_ImportModule("builtins"); 837 PyObject *bimod = PyImport_ImportModule("builtins");
749 if (bimod == NULL || 838 if (bimod == NULL) {
750 PyDict_SetItemString(d, "__builtins__", bimod) != 0) 839 Py_FatalError("Failed to retrieve builtins module");
751 Py_FatalError("can't add __builtins__ to __main__"); 840 }
841 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
842 Py_FatalError("Failed to initialize __main__.__builtins__");
843 }
752 Py_DECREF(bimod); 844 Py_DECREF(bimod);
845 }
846 /* Main is a little special - imp.is_builtin("__main__") will return
847 * False, but BuiltinImporter is still the most appropriate initial
848 * setting for its __loader__ attribute. A more suitable value will
849 * be set if __main__ gets further initialized later in the startup
850 * process.
851 */
852 if (PyDict_GetItemString(d, "__loader__") == NULL) {
853 PyObject *loader = PyObject_GetAttrString(interp->importlib,
854 "BuiltinImporter");
855 if (loader == NULL) {
856 Py_FatalError("Failed to retrieve BuiltinImporter");
857 }
858 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
859 Py_FatalError("Failed to initialize __main__.__loader__");
860 }
861 Py_DECREF(loader);
753 } 862 }
754 } 863 }
755 864
756 static int 865 static int
757 initfsencoding(PyInterpreterState *interp) 866 initfsencoding(PyInterpreterState *interp)
758 { 867 {
759 PyObject *codec; 868 PyObject *codec;
760 869
761 if (Py_FileSystemDefaultEncoding == NULL) 870 if (Py_FileSystemDefaultEncoding == NULL)
762 { 871 {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
856 if (isatty == -1) 965 if (isatty == -1)
857 goto error; 966 goto error;
858 if (isatty || Py_UnbufferedStdioFlag) 967 if (isatty || Py_UnbufferedStdioFlag)
859 line_buffering = Py_True; 968 line_buffering = Py_True;
860 else 969 else
861 line_buffering = Py_False; 970 line_buffering = Py_False;
862 971
863 Py_CLEAR(raw); 972 Py_CLEAR(raw);
864 Py_CLEAR(text); 973 Py_CLEAR(text);
865 974
975 #ifdef MS_WINDOWS
976 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
977 newlines to "\n".
978 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
979 newline = NULL;
980 #else
981 /* sys.stdin: split lines at "\n".
982 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
866 newline = "\n"; 983 newline = "\n";
867 #ifdef MS_WINDOWS
868 if (!write_mode) {
869 /* translate \r\n to \n for sys.stdin on Windows */
870 newline = NULL;
871 }
872 #endif 984 #endif
873 985
874 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO", 986 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
875 buf, encoding, errors, 987 buf, encoding, errors,
876 newline, line_buffering); 988 newline, line_buffering);
877 Py_CLEAR(buf); 989 Py_CLEAR(buf);
878 if (stream == NULL) 990 if (stream == NULL)
879 goto error; 991 goto error;
880 992
881 if (write_mode) 993 if (write_mode)
882 mode = "w"; 994 mode = "w";
883 else 995 else
884 mode = "r"; 996 mode = "r";
885 text = PyUnicode_FromString(mode); 997 text = PyUnicode_FromString(mode);
886 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) 998 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
887 goto error; 999 goto error;
888 Py_CLEAR(text); 1000 Py_CLEAR(text);
889 return stream; 1001 return stream;
890 1002
891 error: 1003 error:
892 Py_XDECREF(buf); 1004 Py_XDECREF(buf);
893 Py_XDECREF(stream); 1005 Py_XDECREF(stream);
894 Py_XDECREF(text); 1006 Py_XDECREF(text);
895 Py_XDECREF(raw); 1007 Py_XDECREF(raw);
896 return NULL; 1008 return NULL;
897 } 1009 }
898 1010
1011 static int
1012 is_valid_fd(int fd)
1013 {
1014 int dummy_fd;
1015 if (fd < 0 || !_PyVerify_fd(fd))
1016 return 0;
1017 dummy_fd = dup(fd);
1018 if (dummy_fd < 0)
1019 return 0;
1020 close(dummy_fd);
1021 return 1;
1022 }
1023
899 /* Initialize sys.stdin, stdout, stderr and builtins.open */ 1024 /* Initialize sys.stdin, stdout, stderr and builtins.open */
900 static int 1025 static int
901 initstdio(void) 1026 initstdio(void)
902 { 1027 {
903 PyObject *iomod = NULL, *wrapper; 1028 PyObject *iomod = NULL, *wrapper;
904 PyObject *bimod = NULL; 1029 PyObject *bimod = NULL;
905 PyObject *m; 1030 PyObject *m;
906 PyObject *std = NULL; 1031 PyObject *std = NULL;
907 int status = 0, fd; 1032 int status = 0, fd;
908 PyObject * encoding_attr; 1033 PyObject * encoding_attr;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
948 errors++; 1073 errors++;
949 } 1074 }
950 } 1075 }
951 1076
952 /* Set sys.stdin */ 1077 /* Set sys.stdin */
953 fd = fileno(stdin); 1078 fd = fileno(stdin);
954 /* Under some conditions stdin, stdout and stderr may not be connected 1079 /* Under some conditions stdin, stdout and stderr may not be connected
955 * and fileno() may point to an invalid file descriptor. For example 1080 * and fileno() may point to an invalid file descriptor. For example
956 * GUI apps don't have valid standard streams by default. 1081 * GUI apps don't have valid standard streams by default.
957 */ 1082 */
958 if (fd < 0) { 1083 if (!is_valid_fd(fd)) {
959 #ifdef MS_WINDOWS
960 std = Py_None; 1084 std = Py_None;
961 Py_INCREF(std); 1085 Py_INCREF(std);
962 #else
963 goto error;
964 #endif
965 } 1086 }
966 else { 1087 else {
967 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); 1088 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
968 if (std == NULL) 1089 if (std == NULL)
969 goto error; 1090 goto error;
970 } /* if (fd < 0) */ 1091 } /* if (fd < 0) */
971 PySys_SetObject("__stdin__", std); 1092 PySys_SetObject("__stdin__", std);
972 PySys_SetObject("stdin", std); 1093 PySys_SetObject("stdin", std);
973 Py_DECREF(std); 1094 Py_DECREF(std);
974 1095
975 /* Set sys.stdout */ 1096 /* Set sys.stdout */
976 fd = fileno(stdout); 1097 fd = fileno(stdout);
977 if (fd < 0) { 1098 if (!is_valid_fd(fd)) {
978 #ifdef MS_WINDOWS
979 std = Py_None; 1099 std = Py_None;
980 Py_INCREF(std); 1100 Py_INCREF(std);
981 #else
982 goto error;
983 #endif
984 } 1101 }
985 else { 1102 else {
986 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); 1103 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
987 if (std == NULL) 1104 if (std == NULL)
988 goto error; 1105 goto error;
989 } /* if (fd < 0) */ 1106 } /* if (fd < 0) */
990 PySys_SetObject("__stdout__", std); 1107 PySys_SetObject("__stdout__", std);
991 PySys_SetObject("stdout", std); 1108 PySys_SetObject("stdout", std);
992 Py_DECREF(std); 1109 Py_DECREF(std);
993 1110
994 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ 1111 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
995 /* Set sys.stderr, replaces the preliminary stderr */ 1112 /* Set sys.stderr, replaces the preliminary stderr */
996 fd = fileno(stderr); 1113 fd = fileno(stderr);
997 if (fd < 0) { 1114 if (!is_valid_fd(fd)) {
998 #ifdef MS_WINDOWS
999 std = Py_None; 1115 std = Py_None;
1000 Py_INCREF(std); 1116 Py_INCREF(std);
1001 #else
1002 goto error;
1003 #endif
1004 } 1117 }
1005 else { 1118 else {
1006 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace "); 1119 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace ");
1007 if (std == NULL) 1120 if (std == NULL)
1008 goto error; 1121 goto error;
1009 } /* if (fd < 0) */ 1122 } /* if (fd < 0) */
1010 1123
1011 /* Same as hack above, pre-import stderr's codec to avoid recursion 1124 /* Same as hack above, pre-import stderr's codec to avoid recursion
1012 when import.c tries to write to stderr in verbose mode. */ 1125 when import.c tries to write to stderr in verbose mode. */
1013 encoding_attr = PyObject_GetAttrString(std, "encoding"); 1126 encoding_attr = PyObject_GetAttrString(std, "encoding");
1014 if (encoding_attr != NULL) { 1127 if (encoding_attr != NULL) {
1015 const char * encoding; 1128 const char * encoding;
1016 encoding = _PyUnicode_AsString(encoding_attr); 1129 encoding = _PyUnicode_AsString(encoding_attr);
1017 if (encoding != NULL) { 1130 if (encoding != NULL) {
1018 _PyCodec_Lookup(encoding); 1131 PyObject *codec_info = _PyCodec_Lookup(encoding);
1132 Py_XDECREF(codec_info);
1019 } 1133 }
1020 Py_DECREF(encoding_attr); 1134 Py_DECREF(encoding_attr);
1021 } 1135 }
1022 PyErr_Clear(); /* Not a fatal error if codec isn't available */ 1136 PyErr_Clear(); /* Not a fatal error if codec isn't available */
1023 1137
1024 PySys_SetObject("__stderr__", std); 1138 PySys_SetObject("__stderr__", std);
1025 PySys_SetObject("stderr", std); 1139 PySys_SetObject("stderr", std);
1026 Py_DECREF(std); 1140 Py_DECREF(std);
1027 #endif 1141 #endif
1028 1142
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
1234 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic) 1348 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1235 ispyc = 1; 1349 ispyc = 1;
1236 rewind(fp); 1350 rewind(fp);
1237 } 1351 }
1238 return ispyc; 1352 return ispyc;
1239 } 1353 }
1240 return 0; 1354 return 0;
1241 } 1355 }
1242 1356
1243 int 1357 int
1358 static set_main_loader(PyObject *d, const char *filename, const char *loader_nam e)
1359 {
1360 PyInterpreterState *interp;
1361 PyThreadState *tstate;
1362 PyObject *filename_obj, *loader_type, *loader;
1363 int result = 0;
1364
1365 filename_obj = PyUnicode_DecodeFSDefault(filename);
1366 if (filename_obj == NULL)
1367 return -1;
1368 /* Get current thread state and interpreter pointer */
1369 tstate = PyThreadState_GET();
1370 interp = tstate->interp;
1371 loader_type = PyObject_GetAttrString(interp->importlib, loader_name);
1372 if (loader_type == NULL) {
1373 Py_DECREF(filename_obj);
1374 return -1;
1375 }
1376 loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
1377 Py_DECREF(loader_type);
1378 if (loader == NULL) {
1379 return -1;
1380 }
1381 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
1382 result = -1;
1383 }
1384 Py_DECREF(loader);
1385 return result;
1386 }
1387
1388 int
1244 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, 1389 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
1245 PyCompilerFlags *flags) 1390 PyCompilerFlags *flags)
1246 { 1391 {
1247 PyObject *m, *d, *v; 1392 PyObject *m, *d, *v;
1248 const char *ext; 1393 const char *ext;
1249 int set_file_name = 0, ret; 1394 int set_file_name = 0, ret = -1;
1250 size_t len; 1395 size_t len;
1251 1396
1252 m = PyImport_AddModule("__main__"); 1397 m = PyImport_AddModule("__main__");
1253 if (m == NULL) 1398 if (m == NULL)
1254 return -1; 1399 return -1;
1400 Py_INCREF(m);
1255 d = PyModule_GetDict(m); 1401 d = PyModule_GetDict(m);
1256 if (PyDict_GetItemString(d, "__file__") == NULL) { 1402 if (PyDict_GetItemString(d, "__file__") == NULL) {
1257 PyObject *f; 1403 PyObject *f;
1258 f = PyUnicode_DecodeFSDefault(filename); 1404 f = PyUnicode_DecodeFSDefault(filename);
1259 if (f == NULL) 1405 if (f == NULL)
1260 return -1; 1406 goto done;
1261 if (PyDict_SetItemString(d, "__file__", f) < 0) { 1407 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1262 Py_DECREF(f); 1408 Py_DECREF(f);
1263 return -1; 1409 goto done;
1264 } 1410 }
1265 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) { 1411 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1266 Py_DECREF(f); 1412 Py_DECREF(f);
1267 return -1; 1413 goto done;
1268 } 1414 }
1269 set_file_name = 1; 1415 set_file_name = 1;
1270 Py_DECREF(f); 1416 Py_DECREF(f);
1271 } 1417 }
1272 len = strlen(filename); 1418 len = strlen(filename);
1273 ext = filename + len - (len > 4 ? 4 : 0); 1419 ext = filename + len - (len > 4 ? 4 : 0);
1274 if (maybe_pyc_file(fp, filename, ext, closeit)) { 1420 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1421 FILE *pyc_fp;
1275 /* Try to run a pyc file. First, re-open in binary */ 1422 /* Try to run a pyc file. First, re-open in binary */
1276 if (closeit) 1423 if (closeit)
1277 fclose(fp); 1424 fclose(fp);
1278 if ((fp = fopen(filename, "rb")) == NULL) { 1425 if ((pyc_fp = fopen(filename, "rb")) == NULL) {
1279 fprintf(stderr, "python: Can't reopen .pyc file\n"); 1426 fprintf(stderr, "python: Can't reopen .pyc file\n");
1280 ret = -1;
1281 goto done; 1427 goto done;
1282 } 1428 }
1283 /* Turn on optimization if a .pyo file is given */ 1429 /* Turn on optimization if a .pyo file is given */
1284 if (strcmp(ext, ".pyo") == 0) 1430 if (strcmp(ext, ".pyo") == 0)
1285 Py_OptimizeFlag = 1; 1431 Py_OptimizeFlag = 1;
1286 v = run_pyc_file(fp, filename, d, d, flags); 1432
1433 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
1434 fprintf(stderr, "python: failed to set __main__.__loader__\n");
1435 ret = -1;
1436 fclose(pyc_fp);
1437 goto done;
1438 }
1439 v = run_pyc_file(pyc_fp, filename, d, d, flags);
1440 fclose(pyc_fp);
1287 } else { 1441 } else {
1442 /* When running from stdin, leave __main__.__loader__ alone */
1443 if (strcmp(filename, "<stdin>") != 0 &&
1444 set_main_loader(d, filename, "SourceFileLoader") < 0) {
1445 fprintf(stderr, "python: failed to set __main__.__loader__\n");
1446 ret = -1;
1447 goto done;
1448 }
1288 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, 1449 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1289 closeit, flags); 1450 closeit, flags);
1290 } 1451 }
1291 flush_io(); 1452 flush_io();
1292 if (v == NULL) { 1453 if (v == NULL) {
1293 PyErr_Print(); 1454 PyErr_Print();
1294 ret = -1;
1295 goto done; 1455 goto done;
1296 } 1456 }
1297 Py_DECREF(v); 1457 Py_DECREF(v);
1298 ret = 0; 1458 ret = 0;
1299 done: 1459 done:
1300 if (set_file_name && PyDict_DelItemString(d, "__file__")) 1460 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1301 PyErr_Clear(); 1461 PyErr_Clear();
1462 Py_DECREF(m);
1302 return ret; 1463 return ret;
1303 } 1464 }
1304 1465
1305 int 1466 int
1306 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) 1467 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
1307 { 1468 {
1308 PyObject *m, *d, *v; 1469 PyObject *m, *d, *v;
1309 m = PyImport_AddModule("__main__"); 1470 m = PyImport_AddModule("__main__");
1310 if (m == NULL) 1471 if (m == NULL)
1311 return -1; 1472 return -1;
(...skipping 12 matching lines...) Expand all
1324 int *lineno, int *offset, const char **text) 1485 int *lineno, int *offset, const char **text)
1325 { 1486 {
1326 long hold; 1487 long hold;
1327 PyObject *v; 1488 PyObject *v;
1328 _Py_IDENTIFIER(msg); 1489 _Py_IDENTIFIER(msg);
1329 _Py_IDENTIFIER(filename); 1490 _Py_IDENTIFIER(filename);
1330 _Py_IDENTIFIER(lineno); 1491 _Py_IDENTIFIER(lineno);
1331 _Py_IDENTIFIER(offset); 1492 _Py_IDENTIFIER(offset);
1332 _Py_IDENTIFIER(text); 1493 _Py_IDENTIFIER(text);
1333 1494
1334 /* old style errors */ 1495 *message = NULL;
1335 if (PyTuple_Check(err))
1336 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1337 lineno, offset, text);
1338 1496
1339 /* new style errors. `err' is an instance */ 1497 /* new style errors. `err' is an instance */
1340 1498 *message = _PyObject_GetAttrId(err, &PyId_msg);
1341 if (! (v = _PyObject_GetAttrId(err, &PyId_msg))) 1499 if (!*message)
1342 goto finally; 1500 goto finally;
1343 *message = v; 1501
1344 1502 v = _PyObject_GetAttrId(err, &PyId_filename);
1345 if (!(v = _PyObject_GetAttrId(err, &PyId_filename))) 1503 if (!v)
1346 goto finally; 1504 goto finally;
1347 if (v == Py_None) 1505 if (v == Py_None) {
1506 Py_DECREF(v);
1348 *filename = NULL; 1507 *filename = NULL;
1349 else if (! (*filename = _PyUnicode_AsString(v))) 1508 }
1350 goto finally; 1509 else {
1351 1510 *filename = _PyUnicode_AsString(v);
1352 Py_DECREF(v); 1511 Py_DECREF(v);
1353 if (!(v = _PyObject_GetAttrId(err, &PyId_lineno))) 1512 if (!*filename)
1513 goto finally;
1514 }
1515
1516 v = _PyObject_GetAttrId(err, &PyId_lineno);
1517 if (!v)
1354 goto finally; 1518 goto finally;
1355 hold = PyLong_AsLong(v); 1519 hold = PyLong_AsLong(v);
1356 Py_DECREF(v); 1520 Py_DECREF(v);
1357 v = NULL;
1358 if (hold < 0 && PyErr_Occurred()) 1521 if (hold < 0 && PyErr_Occurred())
1359 goto finally; 1522 goto finally;
1360 *lineno = (int)hold; 1523 *lineno = (int)hold;
1361 1524
1362 if (!(v = _PyObject_GetAttrId(err, &PyId_offset))) 1525 v = _PyObject_GetAttrId(err, &PyId_offset);
1526 if (!v)
1363 goto finally; 1527 goto finally;
1364 if (v == Py_None) { 1528 if (v == Py_None) {
1365 *offset = -1; 1529 *offset = -1;
1366 Py_DECREF(v); 1530 Py_DECREF(v);
1367 v = NULL;
1368 } else { 1531 } else {
1369 hold = PyLong_AsLong(v); 1532 hold = PyLong_AsLong(v);
1370 Py_DECREF(v); 1533 Py_DECREF(v);
1371 v = NULL;
1372 if (hold < 0 && PyErr_Occurred()) 1534 if (hold < 0 && PyErr_Occurred())
1373 goto finally; 1535 goto finally;
1374 *offset = (int)hold; 1536 *offset = (int)hold;
1375 } 1537 }
1376 1538
1377 if (!(v = _PyObject_GetAttrId(err, &PyId_text))) 1539 v = _PyObject_GetAttrId(err, &PyId_text);
1540 if (!v)
1378 goto finally; 1541 goto finally;
1379 if (v == Py_None) 1542 if (v == Py_None) {
1543 Py_DECREF(v);
1380 *text = NULL; 1544 *text = NULL;
1381 else if (!PyUnicode_Check(v) || 1545 }
1382 !(*text = _PyUnicode_AsString(v))) 1546 else {
1383 goto finally; 1547 *text = _PyUnicode_AsString(v);
1384 Py_DECREF(v); 1548 Py_DECREF(v);
1549 if (!*text)
1550 goto finally;
1551 }
1385 return 1; 1552 return 1;
1386 1553
1387 finally: 1554 finally:
1388 Py_XDECREF(v); 1555 Py_XDECREF(*message);
1389 return 0; 1556 return 0;
1390 } 1557 }
1391 1558
1392 void 1559 void
1393 PyErr_Print(void) 1560 PyErr_Print(void)
1394 { 1561 {
1395 PyErr_PrintEx(1); 1562 PyErr_PrintEx(1);
1396 } 1563 }
1397 1564
1398 static void 1565 static void
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
1634 } 1801 }
1635 } 1802 }
1636 if (err == 0 && (value != Py_None)) { 1803 if (err == 0 && (value != Py_None)) {
1637 PyObject *s = PyObject_Str(value); 1804 PyObject *s = PyObject_Str(value);
1638 /* only print colon if the str() of the 1805 /* only print colon if the str() of the
1639 object is not the empty string 1806 object is not the empty string
1640 */ 1807 */
1641 if (s == NULL) 1808 if (s == NULL)
1642 err = -1; 1809 err = -1;
1643 else if (!PyUnicode_Check(s) || 1810 else if (!PyUnicode_Check(s) ||
1644 PyUnicode_GetSize(s) != 0) 1811 PyUnicode_GetLength(s) != 0)
1645 err = PyFile_WriteString(": ", f); 1812 err = PyFile_WriteString(": ", f);
1646 if (err == 0) 1813 if (err == 0)
1647 err = PyFile_WriteObject(s, f, Py_PRINT_RAW); 1814 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1648 Py_XDECREF(s); 1815 Py_XDECREF(s);
1649 } 1816 }
1650 /* try to write a newline in any case */ 1817 /* try to write a newline in any case */
1651 err += PyFile_WriteString("\n", f); 1818 err += PyFile_WriteString("\n", f);
1652 Py_XDECREF(tb); 1819 Py_XDECREF(tb);
1653 Py_DECREF(value); 1820 Py_DECREF(value);
1654 /* If an error happened here, don't show it. 1821 /* If an error happened here, don't show it.
(...skipping 27 matching lines...) Expand all
1682 res = PySet_Contains(seen, cause); 1849 res = PySet_Contains(seen, cause);
1683 if (res == -1) 1850 if (res == -1)
1684 PyErr_Clear(); 1851 PyErr_Clear();
1685 if (res == 0) { 1852 if (res == 0) {
1686 print_exception_recursive( 1853 print_exception_recursive(
1687 f, cause, seen); 1854 f, cause, seen);
1688 err |= PyFile_WriteString( 1855 err |= PyFile_WriteString(
1689 cause_message, f); 1856 cause_message, f);
1690 } 1857 }
1691 } 1858 }
1692 else if (context) { 1859 else if (context &&
1860 !((PyBaseExceptionObject *)value)->suppress_context) {
1693 res = PySet_Contains(seen, context); 1861 res = PySet_Contains(seen, context);
1694 if (res == -1) 1862 if (res == -1)
1695 PyErr_Clear(); 1863 PyErr_Clear();
1696 if (res == 0) { 1864 if (res == 0) {
1697 print_exception_recursive( 1865 print_exception_recursive(
1698 f, context, seen); 1866 f, context, seen);
1699 err |= PyFile_WriteString( 1867 err |= PyFile_WriteString(
1700 context_message, f); 1868 context_message, f);
1701 } 1869 }
1702 } 1870 }
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 PyObject *v; 1993 PyObject *v;
1826 long magic; 1994 long magic;
1827 long PyImport_GetMagicNumber(void); 1995 long PyImport_GetMagicNumber(void);
1828 1996
1829 magic = PyMarshal_ReadLongFromFile(fp); 1997 magic = PyMarshal_ReadLongFromFile(fp);
1830 if (magic != PyImport_GetMagicNumber()) { 1998 if (magic != PyImport_GetMagicNumber()) {
1831 PyErr_SetString(PyExc_RuntimeError, 1999 PyErr_SetString(PyExc_RuntimeError,
1832 "Bad magic number in .pyc file"); 2000 "Bad magic number in .pyc file");
1833 return NULL; 2001 return NULL;
1834 } 2002 }
2003 /* Skip mtime and size */
2004 (void) PyMarshal_ReadLongFromFile(fp);
1835 (void) PyMarshal_ReadLongFromFile(fp); 2005 (void) PyMarshal_ReadLongFromFile(fp);
1836 v = PyMarshal_ReadLastObjectFromFile(fp); 2006 v = PyMarshal_ReadLastObjectFromFile(fp);
1837 fclose(fp);
1838 if (v == NULL || !PyCode_Check(v)) { 2007 if (v == NULL || !PyCode_Check(v)) {
1839 Py_XDECREF(v); 2008 Py_XDECREF(v);
1840 PyErr_SetString(PyExc_RuntimeError, 2009 PyErr_SetString(PyExc_RuntimeError,
1841 "Bad code object in .pyc file"); 2010 "Bad code object in .pyc file");
1842 return NULL; 2011 return NULL;
1843 } 2012 }
1844 co = (PyCodeObject *)v; 2013 co = (PyCodeObject *)v;
1845 v = PyEval_EvalCode((PyObject*)co, globals, locals); 2014 v = PyEval_EvalCode((PyObject*)co, globals, locals);
1846 if (v && flags) 2015 if (v && flags)
1847 flags->cf_flags |= (co->co_flags & PyCF_MASK); 2016 flags->cf_flags |= (co->co_flags & PyCF_MASK);
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
2107 Py_XDECREF(tb); 2276 Py_XDECREF(tb);
2108 break; 2277 break;
2109 } 2278 }
2110 case E_LINECONT: 2279 case E_LINECONT:
2111 msg = "unexpected character after line continuation character"; 2280 msg = "unexpected character after line continuation character";
2112 break; 2281 break;
2113 2282
2114 case E_IDENTIFIER: 2283 case E_IDENTIFIER:
2115 msg = "invalid character in identifier"; 2284 msg = "invalid character in identifier";
2116 break; 2285 break;
2286 case E_BADSINGLE:
2287 msg = "multiple statements found while compiling a single statement";
2288 break;
2117 default: 2289 default:
2118 fprintf(stderr, "error=%d\n", err->error); 2290 fprintf(stderr, "error=%d\n", err->error);
2119 msg = "unknown parsing error"; 2291 msg = "unknown parsing error";
2120 break; 2292 break;
2121 } 2293 }
2122 /* err->text may not be UTF-8 in case of decoding errors. 2294 /* err->text may not be UTF-8 in case of decoding errors.
2123 Explicitly convert to an object. */ 2295 Explicitly convert to an object. */
2124 if (!err->text) { 2296 if (!err->text) {
2125 errtext = Py_None; 2297 errtext = Py_None;
2126 Py_INCREF(Py_None); 2298 Py_INCREF(Py_None);
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
2549 #undef PyRun_InteractiveLoop 2721 #undef PyRun_InteractiveLoop
2550 PyAPI_FUNC(int) 2722 PyAPI_FUNC(int)
2551 PyRun_InteractiveLoop(FILE *f, const char *p) 2723 PyRun_InteractiveLoop(FILE *f, const char *p)
2552 { 2724 {
2553 return PyRun_InteractiveLoopFlags(f, p, NULL); 2725 return PyRun_InteractiveLoopFlags(f, p, NULL);
2554 } 2726 }
2555 2727
2556 #ifdef __cplusplus 2728 #ifdef __cplusplus
2557 } 2729 }
2558 #endif 2730 #endif
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+