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