/* Top level execution of Python code (including in __main__) */ /* To help control the interfaces between the startup, execution and * shutdown code, the phases are split across separate modules (bootstrap, * pythonrun, shutdown) */ /* TODO: Cull includes following phase split */ #include "Python.h" #include "pycore_ast.h" // PyAST_mod2obj() #include "pycore_audit.h" // _PySys_Audit() #include "pycore_ceval.h" // _Py_EnterRecursiveCall() #include "pycore_compile.h" // _PyAST_Compile() #include "pycore_fileutils.h" // _PyFile_Flush #include "pycore_import.h" // _PyImport_GetImportlibExternalLoader() #include "pycore_interp.h" // PyInterpreterState.importlib #include "pycore_object.h" // _PyDebug_PrintTotalRefs() #include "pycore_parser.h" // _PyParser_ASTFromString() #include "pycore_pyerrors.h" // _PyErr_GetRaisedException() #include "pycore_pylifecycle.h" // _Py_FdIsInteractive() #include "pycore_pystate.h" // _PyInterpreterState_GET() #include "pycore_pythonrun.h" // export _PyRun_InteractiveLoopObject() #include "pycore_sysmodule.h" // _PySys_SetAttr() #include "pycore_traceback.h" // _PyTraceBack_Print() #include "pycore_unicodeobject.h" // _PyUnicode_Equal() #include "errcode.h" // E_EOF #include "marshal.h" // PyMarshal_ReadLongFromFile() #include #ifdef MS_WINDOWS # include "malloc.h" // alloca() #endif #ifdef MS_WINDOWS # undef BYTE # include "windows.h" #endif /* Forward */ static void flush_io(void); static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *, PyCompilerFlags *, PyArena *, PyObject*, int); static PyObject *run_pyc_file(FILE *, PyObject *, PyObject *, PyCompilerFlags *); static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *); static PyObject* pyrun_file(FILE *fp, PyObject *filename, int start, PyObject *globals, PyObject *locals, int closeit, PyCompilerFlags *flags); static PyObject * _PyRun_StringFlagsWithName(const char *str, PyObject* name, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags, int generate_new_source); int _PyRun_AnyFileObject(FILE *fp, PyObject *filename, int closeit, PyCompilerFlags *flags) { int decref_filename = 0; if (filename == NULL) { filename = PyUnicode_FromString("???"); if (filename == NULL) { PyErr_Print(); return -1; } decref_filename = 1; } int res; if (_Py_FdIsInteractive(fp, filename)) { res = _PyRun_InteractiveLoopObject(fp, filename, flags); if (closeit) { fclose(fp); } } else { res = _PyRun_SimpleFileObject(fp, filename, closeit, flags); } if (decref_filename) { Py_DECREF(filename); } return res; } int PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags) { PyObject *filename_obj = NULL; if (filename != NULL) { filename_obj = PyUnicode_DecodeFSDefault(filename); if (filename_obj == NULL) { PyErr_Print(); return -1; } } int res = _PyRun_AnyFileObject(fp, filename_obj, closeit, flags); Py_XDECREF(filename_obj); return res; } int _PyRun_InteractiveLoopObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags) { PyCompilerFlags local_flags = _PyCompilerFlags_INIT; if (flags == NULL) { flags = &local_flags; } PyObject *v; if (PySys_GetOptionalAttr(&_Py_ID(ps1), &v) < 0) { PyErr_Print(); return -1; } if (v == NULL) { v = PyUnicode_FromString(">>> "); if (v == NULL) { PyErr_Clear(); } if (_PySys_SetAttr(&_Py_ID(ps1), v) < 0) { PyErr_Clear(); } } Py_XDECREF(v); if (PySys_GetOptionalAttr(&_Py_ID(ps2), &v) < 0) { PyErr_Print(); return -1; } if (v == NULL) { v = PyUnicode_FromString("... "); if (v == NULL) { PyErr_Clear(); } if (_PySys_SetAttr(&_Py_ID(ps2), v) < 0) { PyErr_Clear(); } } Py_XDECREF(v); #ifdef Py_REF_DEBUG int show_ref_count = _Py_GetConfig()->show_ref_count; #endif int err = 0; int ret; int nomem_count = 0; do { ret = PyRun_InteractiveOneObjectEx(fp, filename, flags); if (ret == -1 && PyErr_Occurred()) { /* Prevent an endless loop after multiple consecutive MemoryErrors * while still allowing an interactive command to fail with a * MemoryError. */ if (PyErr_ExceptionMatches(PyExc_MemoryError)) { if (++nomem_count > 16) { PyErr_Clear(); err = -1; break; } } else { nomem_count = 0; } PyErr_Print(); flush_io(); } else { nomem_count = 0; } #ifdef Py_REF_DEBUG if (show_ref_count) { _PyDebug_PrintTotalRefs(); } #endif } while (ret != E_EOF); return err; } int PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) { PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename); if (filename_obj == NULL) { PyErr_Print(); return -1; } int err = _PyRun_InteractiveLoopObject(fp, filename_obj, flags); Py_DECREF(filename_obj); return err; } // Call _PyParser_ASTFromFile() with sys.stdin.encoding, sys.ps1 and sys.ps2 static int pyrun_one_parse_ast(FILE *fp, PyObject *filename, PyCompilerFlags *flags, PyArena *arena, mod_ty *pmod, PyObject** interactive_src) { // Get sys.stdin.encoding (as UTF-8) PyObject *attr; PyObject *encoding_obj = NULL; const char *encoding = NULL; if (fp == stdin) { if (PySys_GetOptionalAttr(&_Py_ID(stdin), &attr) < 0) { PyErr_Clear(); } else if (attr != NULL && attr != Py_None) { if (PyObject_GetOptionalAttr(attr, &_Py_ID(encoding), &encoding_obj) < 0) { PyErr_Clear(); } else if (encoding_obj && PyUnicode_Check(encoding_obj)) { encoding = PyUnicode_AsUTF8(encoding_obj); if (!encoding) { PyErr_Clear(); } } } Py_XDECREF(attr); } // Get sys.ps1 (as UTF-8) PyObject *ps1_obj = NULL; const char *ps1 = ""; if (PySys_GetOptionalAttr(&_Py_ID(ps1), &attr) < 0) { PyErr_Clear(); } else if (attr != NULL) { ps1_obj = PyObject_Str(attr); Py_DECREF(attr); if (ps1_obj == NULL) { PyErr_Clear(); } else if (PyUnicode_Check(ps1_obj)) { ps1 = PyUnicode_AsUTF8(ps1_obj); if (ps1 == NULL) { PyErr_Clear(); ps1 = ""; } } } // Get sys.ps2 (as UTF-8) PyObject *ps2_obj = NULL; const char *ps2 = ""; if (PySys_GetOptionalAttr(&_Py_ID(ps2), &attr) < 0) { PyErr_Clear(); } else if (attr != NULL) { ps2_obj = PyObject_Str(attr); Py_DECREF(attr); if (ps2_obj == NULL) { PyErr_Clear(); } else if (PyUnicode_Check(ps2_obj)) { ps2 = PyUnicode_AsUTF8(ps2_obj); if (ps2 == NULL) { PyErr_Clear(); ps2 = ""; } } } int errcode = 0; *pmod = _PyParser_InteractiveASTFromFile(fp, filename, encoding, Py_single_input, ps1, ps2, flags, &errcode, interactive_src, arena); Py_XDECREF(ps1_obj); Py_XDECREF(ps2_obj); Py_XDECREF(encoding_obj); if (*pmod == NULL) { if (errcode == E_EOF) { PyErr_Clear(); return E_EOF; } return -1; } return 0; } /* A PyRun_InteractiveOneObject() auxiliary function that does not print the * error on failure. */ static int PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, PyCompilerFlags *flags) { PyArena *arena = _PyArena_New(); if (arena == NULL) { return -1; } mod_ty mod; PyObject *interactive_src; int parse_res = pyrun_one_parse_ast(fp, filename, flags, arena, &mod, &interactive_src); if (parse_res != 0) { _PyArena_Free(arena); return parse_res; } PyObject *main_module = PyImport_AddModuleRef("__main__"); if (main_module == NULL) { _PyArena_Free(arena); return -1; } PyObject *main_dict = PyModule_GetDict(main_module); // borrowed ref PyObject *res = run_mod(mod, filename, main_dict, main_dict, flags, arena, interactive_src, 1); Py_INCREF(interactive_src); _PyArena_Free(arena); Py_DECREF(main_module); if (res == NULL) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *exc = _PyErr_GetRaisedException(tstate); if (PyType_IsSubtype(Py_TYPE(exc), (PyTypeObject *) PyExc_SyntaxError)) { /* fix "text" attribute */ assert(interactive_src != NULL); PyObject *xs = PyUnicode_Splitlines(interactive_src, 1); if (xs == NULL) { goto error; } PyObject *exc_lineno = PyObject_GetAttr(exc, &_Py_ID(lineno)); if (exc_lineno == NULL) { Py_DECREF(xs); goto error; } int n = PyLong_AsInt(exc_lineno); Py_DECREF(exc_lineno); if (n <= 0 || n > PyList_GET_SIZE(xs)) { Py_DECREF(xs); goto error;