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