Python-2.7.3/Python/pythonrun.c

Location Tool Test ID Function Issue
/builddir/build/BUILD/Python-2.7.3/Python/pythonrun.c:332:9 clang-analyzer Potential leak of memory pointed to by 'loc_codeset'
/builddir/build/BUILD/Python-2.7.3/Python/pythonrun.c:332:9 clang-analyzer Potential leak of memory pointed to by 'loc_codeset'
   1 /* Python interpreter top-level routines, including init/exit */
   2 
   3 #include "Python.h"
   4 
   5 #include "Python-ast.h"
   6 #undef Yield /* undefine macro conflicting with winbase.h */
   7 #include "grammar.h"
   8 #include "node.h"
   9 #include "token.h"
  10 #include "parsetok.h"
  11 #include "errcode.h"
  12 #include "code.h"
  13 #include "compile.h"
  14 #include "symtable.h"
  15 #include "pyarena.h"
  16 #include "ast.h"
  17 #include "eval.h"
  18 #include "marshal.h"
  19 #include "abstract.h"
  20 
  21 #ifdef HAVE_SIGNAL_H
  22 #include <signal.h>
  23 #endif
  24 
  25 #ifdef MS_WINDOWS
  26 #include "malloc.h" /* for alloca */
  27 #endif
  28 
  29 #ifdef HAVE_LANGINFO_H
  30 #include <locale.h>
  31 #include <langinfo.h>
  32 #endif
  33 
  34 #ifdef MS_WINDOWS
  35 #undef BYTE
  36 #include "windows.h"
  37 #endif
  38 
  39 #ifndef Py_REF_DEBUG
  40 #define PRINT_TOTAL_REFS()
  41 #else /* Py_REF_DEBUG */
  42 #define PRINT_TOTAL_REFS() fprintf(stderr,                              \
  43                    "[%" PY_FORMAT_SIZE_T "d refs]\n",                   \
  44                    _Py_GetRefTotal())
  45 #endif
  46 
  47 #ifdef __cplusplus
  48 extern "C" {
  49 #endif
  50 
  51 extern char *Py_GetPath(void);
  52 
  53 extern grammar _PyParser_Grammar; /* From graminit.c */
  54 
  55 /* Forward */
  56 static void initmain(void);
  57 static void initsite(void);
  58 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
  59                           PyCompilerFlags *, PyArena *);
  60 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
  61                               PyCompilerFlags *);
  62 static void err_input(perrdetail *);
  63 static void initsigs(void);
  64 static void wait_for_thread_shutdown(void);
  65 static void call_sys_exitfunc(void);
  66 static void call_ll_exitfuncs(void);
  67 extern void _PyUnicode_Init(void);
  68 extern void _PyUnicode_Fini(void);
  69 
  70 #ifdef WITH_THREAD
  71 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
  72 extern void _PyGILState_Fini(void);
  73 #endif /* WITH_THREAD */
  74 
  75 int Py_DebugFlag; /* Needed by parser.c */
  76 int Py_VerboseFlag; /* Needed by import.c */
  77 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
  78 int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
  79 int Py_NoSiteFlag; /* Suppress 'import site' */
  80 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
  81 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
  82 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
  83 int Py_FrozenFlag; /* Needed by getpath.c */
  84 int Py_UnicodeFlag = 0; /* Needed by compile.c */
  85 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
  86 /* _XXX Py_QnewFlag should go away in 2.3.  It's true iff -Qnew is passed,
  87   on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
  88   true divisions (which they will be in 2.3). */
  89 int _Py_QnewFlag = 0;
  90 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
  91 int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
  92 
  93 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
  94 since _warnings is builtin.  This API should not be used. */
  95 PyObject *
  96 PyModule_GetWarningsModule(void)
  97 {
  98     return PyImport_ImportModule("warnings");
  99 }
 100 
 101 static int initialized = 0;
 102 
 103 /* API to access the initialized flag -- useful for esoteric use */
 104 
 105 int
 106 Py_IsInitialized(void)
 107 {
 108     return initialized;
 109 }
 110 
 111 /* Global initializations.  Can be undone by Py_Finalize().  Don't
 112    call this twice without an intervening Py_Finalize() call.  When
 113    initializations fail, a fatal error is issued and the function does
 114    not return.  On return, the first thread and interpreter state have
 115    been created.
 116 
 117    Locking: you must hold the interpreter lock while calling this.
 118    (If the lock has not yet been initialized, that's equivalent to
 119    having the lock, but you cannot use multiple threads.)
 120 
 121 */
 122 
 123 static int
 124 add_flag(int flag, const char *envs)
 125 {
 126     int env = atoi(envs);
 127     if (flag < env)
 128         flag = env;
 129     if (flag < 1)
 130         flag = 1;
 131     return flag;
 132 }
 133 
 134 void
 135 Py_InitializeEx(int install_sigs)
 136 {
 137     PyInterpreterState *interp;
 138     PyThreadState *tstate;
 139     PyObject *bimod, *sysmod;
 140     char *p;
 141     char *icodeset = NULL; /* On Windows, input codeset may theoretically
 142                               differ from output codeset. */
 143     char *codeset = NULL;
 144     char *errors = NULL;
 145     int free_codeset = 0;
 146     int overridden = 0;
 147     PyObject *sys_stream, *sys_isatty;
 148 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
 149     char *saved_locale, *loc_codeset;
 150 #endif
 151 #ifdef MS_WINDOWS
 152     char ibuf[128];
 153     char buf[128];
 154 #endif
 155     extern void _Py_ReadyTypes(void);
 156 
 157     if (initialized)
 158         return;
 159     initialized = 1;
 160 
 161     if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
 162         Py_DebugFlag = add_flag(Py_DebugFlag, p);
 163     if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
 164         Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
 165     if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
 166         Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
 167     if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
 168         Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
 169     /* The variable is only tested for existence here; _PyRandom_Init will
 170        check its value further. */
 171     if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
 172         Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
 173 
 174     _PyRandom_Init();
 175 
 176     interp = PyInterpreterState_New();
 177     if (interp == NULL)
 178         Py_FatalError("Py_Initialize: can't make first interpreter");
 179 
 180     tstate = PyThreadState_New(interp);
 181     if (tstate == NULL)
 182         Py_FatalError("Py_Initialize: can't make first thread");
 183     (void) PyThreadState_Swap(tstate);
 184 
 185     _Py_ReadyTypes();
 186 
 187     if (!_PyFrame_Init())
 188         Py_FatalError("Py_Initialize: can't init frames");
 189 
 190     if (!_PyInt_Init())
 191         Py_FatalError("Py_Initialize: can't init ints");
 192 
 193     if (!_PyLong_Init())
 194         Py_FatalError("Py_Initialize: can't init longs");
 195 
 196     if (!PyByteArray_Init())
 197         Py_FatalError("Py_Initialize: can't init bytearray");
 198 
 199     _PyFloat_Init();
 200 
 201     interp->modules = PyDict_New();
 202     if (interp->modules == NULL)
 203         Py_FatalError("Py_Initialize: can't make modules dictionary");
 204     interp->modules_reloading = PyDict_New();
 205     if (interp->modules_reloading == NULL)
 206         Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
 207 
 208 #ifdef Py_USING_UNICODE
 209     /* Init Unicode implementation; relies on the codec registry */
 210     _PyUnicode_Init();
 211 #endif
 212 
 213     bimod = _PyBuiltin_Init();
 214     if (bimod == NULL)
 215         Py_FatalError("Py_Initialize: can't initialize __builtin__");
 216     interp->builtins = PyModule_GetDict(bimod);
 217     if (interp->builtins == NULL)
 218         Py_FatalError("Py_Initialize: can't initialize builtins dict");
 219     Py_INCREF(interp->builtins);
 220 
 221     sysmod = _PySys_Init();
 222     if (sysmod == NULL)
 223         Py_FatalError("Py_Initialize: can't initialize sys");
 224     interp->sysdict = PyModule_GetDict(sysmod);
 225     if (interp->sysdict == NULL)
 226         Py_FatalError("Py_Initialize: can't initialize sys dict");
 227     Py_INCREF(interp->sysdict);
 228     _PyImport_FixupExtension("sys", "sys");
 229     PySys_SetPath(Py_GetPath());
 230     PyDict_SetItemString(interp->sysdict, "modules",
 231                          interp->modules);
 232 
 233     _PyImport_Init();
 234 
 235     /* initialize builtin exceptions */
 236     _PyExc_Init();
 237     _PyImport_FixupExtension("exceptions", "exceptions");
 238 
 239     /* phase 2 of builtins */
 240     _PyImport_FixupExtension("__builtin__", "__builtin__");
 241 
 242     _PyImportHooks_Init();
 243 
 244     if (install_sigs)
 245         initsigs(); /* Signal handling stuff, including initintr() */
 246 
 247     /* Initialize warnings. */
 248     _PyWarnings_Init();
 249     if (PySys_HasWarnOptions()) {
 250         PyObject *warnings_module = PyImport_ImportModule("warnings");
 251         if (!warnings_module)
 252             PyErr_Clear();
 253         Py_XDECREF(warnings_module);
 254     }
 255 
 256     initmain(); /* Module __main__ */
 257 
 258     /* auto-thread-state API, if available */
 259 #ifdef WITH_THREAD
 260     _PyGILState_Init(interp, tstate);
 261 #endif /* WITH_THREAD */
 262 
 263     if (!Py_NoSiteFlag)
 264         initsite(); /* Module site */
 265 
 266     if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
 267         p = icodeset = codeset = strdup(p);
 268         free_codeset = 1;
 269         errors = strchr(p, ':');
 270         if (errors) {
 271             *errors = '\0';
 272             errors++;
 273         }
 274         overridden = 1;
 275     }
 276 
 277 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
 278     /* On Unix, set the file system encoding according to the
 279        user's preference, if the CODESET names a well-known
 280        Python codec, and Py_FileSystemDefaultEncoding isn't
 281        initialized by other means. Also set the encoding of
 282        stdin and stdout if these are terminals, unless overridden.  */
 283 
 284     if (!overridden || !Py_FileSystemDefaultEncoding) {
 285         saved_locale = strdup(setlocale(LC_CTYPE, NULL));
 286         setlocale(LC_CTYPE, "");
 287         loc_codeset = nl_langinfo(CODESET);
 288         if (loc_codeset && *loc_codeset) {
 289             PyObject *enc = PyCodec_Encoder(loc_codeset);
 290             if (enc) {
 291                 loc_codeset = strdup(loc_codeset);
 292                 Py_DECREF(enc);
 293             } else {
 294                 if (PyErr_ExceptionMatches(PyExc_LookupError)) {
 295                     PyErr_Clear();
 296                     loc_codeset = NULL;
 297                 } else {
 298                     PyErr_Print();
 299                     exit(1);
 300                 }
 301             }
 302         } else
 303             loc_codeset = NULL;
 304         setlocale(LC_CTYPE, saved_locale);
 305         free(saved_locale);
 306 
 307         if (!overridden) {
 308             codeset = icodeset = loc_codeset;
 309             free_codeset = 1;
 310         }
 311 
 312         /* Initialize Py_FileSystemDefaultEncoding from
 313            locale even if PYTHONIOENCODING is set. */
 314         if (!Py_FileSystemDefaultEncoding) {
 315             Py_FileSystemDefaultEncoding = loc_codeset;
 316             if (!overridden)
 317                 free_codeset = 0;
 318         }
 319     }
 320 #endif
 321 
 322 #ifdef MS_WINDOWS
 323     if (!overridden) {
 324         icodeset = ibuf;
 325         codeset = buf;
 326         sprintf(ibuf, "cp%d", GetConsoleCP());
 327         sprintf(buf, "cp%d", GetConsoleOutputCP());
 328     }
 329 #endif
 330 
 331     if (codeset) {
 332         sys_stream = PySys_GetObject("stdin");
Potential leak of memory pointed to by 'loc_codeset'
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

Potential leak of memory pointed to by 'loc_codeset'
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

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