summaryrefslogtreecommitdiff
path: root/Python/pythonrun.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r--Python/pythonrun.c384
1 files changed, 271 insertions, 113 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index c2ca563d1a..ccf82af36b 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -35,18 +35,30 @@
#define PATH_MAX MAXPATHLEN
#endif
+_Py_IDENTIFIER(builtins);
+_Py_IDENTIFIER(excepthook);
+_Py_IDENTIFIER(flush);
+_Py_IDENTIFIER(last_traceback);
+_Py_IDENTIFIER(last_type);
+_Py_IDENTIFIER(last_value);
+_Py_IDENTIFIER(name);
+_Py_IDENTIFIER(ps1);
+_Py_IDENTIFIER(ps2);
+_Py_IDENTIFIER(stdin);
+_Py_IDENTIFIER(stdout);
+_Py_IDENTIFIER(stderr);
+_Py_static_string(PyId_string, "<string>");
+
#ifdef Py_REF_DEBUG
static
void _print_total_refs(void) {
- PyObject *xoptions, *key, *value;
+ PyObject *xoptions, *value;
+ _Py_IDENTIFIER(showrefcount);
+
xoptions = PySys_GetXOptions();
if (xoptions == NULL)
return;
- key = PyUnicode_FromString("showrefcount");
- if (key == NULL)
- return;
- value = PyDict_GetItem(xoptions, key);
- Py_DECREF(key);
+ value = _PyDict_GetItemId(xoptions, &PyId_showrefcount);
if (value == Py_True)
fprintf(stderr,
"[%" PY_FORMAT_SIZE_T "d refs, "
@@ -75,7 +87,7 @@ static int initfsencoding(PyInterpreterState *interp);
static void initsite(void);
static int initstdio(void);
static void flush_io(void);
-static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
+static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
PyCompilerFlags *, PyArena *);
static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
PyCompilerFlags *);
@@ -92,6 +104,9 @@ extern int _PyLong_Init(void);
extern void PyLong_Fini(void);
extern int _PyFaulthandler_Init(void);
extern void _PyFaulthandler_Fini(void);
+extern void _PyHash_Fini(void);
+extern int _PyTraceMalloc_Init(void);
+extern int _PyTraceMalloc_Fini(void);
#ifdef WITH_THREAD
extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
@@ -116,6 +131,10 @@ int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */
PyThreadState *_Py_Finalizing = NULL;
+/* Hack to force loading of object files */
+int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
+ PyOS_mystrnicmp; /* Python/pystrcmp.o */
+
/* PyModule_GetWarningsModule is no longer necessary as of 2.6
since _warnings is builtin. This API should not be used. */
PyObject *
@@ -134,6 +153,45 @@ Py_IsInitialized(void)
return initialized;
}
+/* Helper to allow an embedding application to override the normal
+ * mechanism that attempts to figure out an appropriate IO encoding
+ */
+
+static char *_Py_StandardStreamEncoding = NULL;
+static char *_Py_StandardStreamErrors = NULL;
+
+int
+Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
+{
+ if (Py_IsInitialized()) {
+ /* This is too late to have any effect */
+ return -1;
+ }
+ /* Can't call PyErr_NoMemory() on errors, as Python hasn't been
+ * initialised yet.
+ *
+ * However, the raw memory allocators are initialised appropriately
+ * as C static variables, so _PyMem_RawStrdup is OK even though
+ * Py_Initialize hasn't been called yet.
+ */
+ if (encoding) {
+ _Py_StandardStreamEncoding = _PyMem_RawStrdup(encoding);
+ if (!_Py_StandardStreamEncoding) {
+ return -2;
+ }
+ }
+ if (errors) {
+ _Py_StandardStreamErrors = _PyMem_RawStrdup(errors);
+ if (!_Py_StandardStreamErrors) {
+ if (_Py_StandardStreamEncoding) {
+ PyMem_RawFree(_Py_StandardStreamEncoding);
+ }
+ return -3;
+ }
+ }
+ return 0;
+}
+
/* Global initializations. Can be undone by Py_Finalize(). Don't
call this twice without an intervening Py_Finalize() call. When
initializations fail, a fatal error is issued and the function does
@@ -162,7 +220,6 @@ get_codec_name(const char *encoding)
{
char *name_utf8, *name_str;
PyObject *codec, *name = NULL;
- _Py_IDENTIFIER(name);
codec = _PyCodec_Lookup(encoding);
if (!codec)
@@ -371,7 +428,7 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
pstderr = PyFile_NewStdPrinter(fileno(stderr));
if (pstderr == NULL)
Py_FatalError("Py_Initialize: can't set preliminary stderr");
- PySys_SetObject("stderr", pstderr);
+ _PySys_SetObjectId(&PyId_stderr, pstderr);
PySys_SetObject("__stderr__", pstderr);
Py_DECREF(pstderr);
@@ -399,6 +456,9 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
if (install_sigs)
initsigs(); /* Signal handling stuff, including initintr() */
+ if (_PyTraceMalloc_Init() < 0)
+ Py_FatalError("Py_Initialize: can't initialize tracemalloc");
+
initmain(interp); /* Module __main__ */
if (initstdio() < 0)
Py_FatalError(
@@ -456,10 +516,9 @@ file_is_closed(PyObject *fobj)
static void
flush_std_files(void)
{
- PyObject *fout = PySys_GetObject("stdout");
- PyObject *ferr = PySys_GetObject("stderr");
+ PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
+ PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
PyObject *tmp;
- _Py_IDENTIFIER(flush);
if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
@@ -523,11 +582,13 @@ Py_Finalize(void)
_Py_Finalizing = tstate;
initialized = 0;
- /* Flush stdout+stderr */
- flush_std_files();
-
- /* Disable signal handling */
- PyOS_FiniInterrupts();
+ /* Destroy the state of all threads except of the current thread: in
+ practice, only daemon threads should still be alive. Clear frames of
+ other threads to call objects destructor. Destructors will be called in
+ the current Python thread. Since _Py_Finalizing has been set, no other
+ Python threads can lock the GIL at this point (if they try, they will
+ exit immediately). */
+ _PyThreadState_DeleteExcept(tstate);
/* Collect garbage. This may call finalizers; it's nice to call these
* before all modules are destroyed.
@@ -542,6 +603,7 @@ Py_Finalize(void)
* XXX I haven't seen a real-life report of either of these.
*/
PyGC_Collect();
+
#ifdef COUNT_ALLOCS
/* With COUNT_ALLOCS, it helps to run GC multiple times:
each collection might release some types from the type
@@ -549,6 +611,13 @@ Py_Finalize(void)
while (PyGC_Collect() > 0)
/* nothing */;
#endif
+
+ /* Flush stdout+stderr */
+ flush_std_files();
+
+ /* Disable signal handling */
+ PyOS_FiniInterrupts();
+
/* Destroy all modules */
PyImport_Cleanup();
@@ -574,6 +643,10 @@ Py_Finalize(void)
PyGC_Collect();
#endif
+ /* Disable tracemalloc after all Python objects have been destroyed,
+ so it is possible to use tracemalloc in objects destructor. */
+ _PyTraceMalloc_Fini();
+
/* Destroy the database used by _PyImport_{Fixup,Find}Extension */
_PyImport_Fini();
@@ -587,6 +660,8 @@ Py_Finalize(void)
#ifdef COUNT_ALLOCS
dump_counts(stdout);
#endif
+ /* dump hash stats */
+ _PyHash_Fini();
PRINT_TOTAL_REFS();
@@ -735,7 +810,7 @@ Py_NewInterpreter(void)
pstderr = PyFile_NewStdPrinter(fileno(stderr));
if (pstderr == NULL)
Py_FatalError("Py_Initialize: can't set preliminary stderr");
- PySys_SetObject("stderr", pstderr);
+ _PySys_SetObjectId(&PyId_stderr, pstderr);
PySys_SetObject("__stderr__", pstderr);
Py_DECREF(pstderr);
@@ -837,8 +912,9 @@ Py_GetPythonHome(void)
if (home == NULL && !Py_IgnoreEnvironmentFlag) {
char* chome = Py_GETENV("PYTHONHOME");
if (chome) {
- size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
- if (r != (size_t)-1 && r <= PATH_MAX)
+ size_t size = Py_ARRAY_LENGTH(env_home);
+ size_t r = mbstowcs(env_home, chome, size);
+ if (r != (size_t)-1 && r < size)
home = env_home;
}
@@ -946,7 +1022,6 @@ create_stdio(PyObject* io,
_Py_IDENTIFIER(open);
_Py_IDENTIFIER(isatty);
_Py_IDENTIFIER(TextIOWrapper);
- _Py_IDENTIFIER(name);
_Py_IDENTIFIER(mode);
/* stdin is always opened in buffered mode, first because it shouldn't
@@ -1088,23 +1163,29 @@ initstdio(void)
}
Py_DECREF(wrapper);
- pythonioencoding = Py_GETENV("PYTHONIOENCODING");
- encoding = errors = NULL;
- if (pythonioencoding) {
- pythonioencoding = _PyMem_Strdup(pythonioencoding);
- if (pythonioencoding == NULL) {
- PyErr_NoMemory();
- goto error;
- }
- errors = strchr(pythonioencoding, ':');
- if (errors) {
- *errors = '\0';
- errors++;
- if (!*errors)
- errors = NULL;
+ encoding = _Py_StandardStreamEncoding;
+ errors = _Py_StandardStreamErrors;
+ if (!encoding || !errors) {
+ pythonioencoding = Py_GETENV("PYTHONIOENCODING");
+ if (pythonioencoding) {
+ char *err;
+ pythonioencoding = _PyMem_Strdup(pythonioencoding);
+ if (pythonioencoding == NULL) {
+ PyErr_NoMemory();
+ goto error;
+ }
+ err = strchr(pythonioencoding, ':');
+ if (err) {
+ *err = '\0';
+ err++;
+ if (*err && !errors) {
+ errors = err;
+ }
+ }
+ if (*pythonioencoding && !encoding) {
+ encoding = pythonioencoding;
+ }
}
- if (*pythonioencoding)
- encoding = pythonioencoding;
}
/* Set sys.stdin */
@@ -1123,7 +1204,7 @@ initstdio(void)
goto error;
} /* if (fd < 0) */
PySys_SetObject("__stdin__", std);
- PySys_SetObject("stdin", std);
+ _PySys_SetObjectId(&PyId_stdin, std);
Py_DECREF(std);
/* Set sys.stdout */
@@ -1138,7 +1219,7 @@ initstdio(void)
goto error;
} /* if (fd < 0) */
PySys_SetObject("__stdout__", std);
- PySys_SetObject("stdout", std);
+ _PySys_SetObjectId(&PyId_stdout, std);
Py_DECREF(std);
#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
@@ -1172,7 +1253,7 @@ initstdio(void)
Py_DECREF(std);
goto error;
}
- if (PySys_SetObject("stderr", std) < 0) {
+ if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
Py_DECREF(std);
goto error;
}
@@ -1184,6 +1265,15 @@ initstdio(void)
status = -1;
}
+ /* We won't need them anymore. */
+ if (_Py_StandardStreamEncoding) {
+ PyMem_RawFree(_Py_StandardStreamEncoding);
+ _Py_StandardStreamEncoding = NULL;
+ }
+ if (_Py_StandardStreamErrors) {
+ PyMem_RawFree(_Py_StandardStreamErrors);
+ _Py_StandardStreamErrors = NULL;
+ }
PyMem_Free(pythonioencoding);
Py_XDECREF(bimod);
Py_XDECREF(iomod);
@@ -1209,36 +1299,47 @@ PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
}
int
-PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
+PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
{
- PyObject *v;
- int ret;
+ PyObject *filename, *v;
+ int ret, err;
PyCompilerFlags local_flags;
+ filename = PyUnicode_DecodeFSDefault(filename_str);
+ if (filename == NULL) {
+ PyErr_Print();
+ return -1;
+ }
+
if (flags == NULL) {
flags = &local_flags;
local_flags.cf_flags = 0;
}
- v = PySys_GetObject("ps1");
+ v = _PySys_GetObjectId(&PyId_ps1);
if (v == NULL) {
- PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
+ _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
Py_XDECREF(v);
}
- v = PySys_GetObject("ps2");
+ v = _PySys_GetObjectId(&PyId_ps2);
if (v == NULL) {
- PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
+ _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
Py_XDECREF(v);
}
+ err = -1;
for (;;) {
- ret = PyRun_InteractiveOneFlags(fp, filename, flags);
+ ret = PyRun_InteractiveOneObject(fp, filename, flags);
PRINT_TOTAL_REFS();
- if (ret == E_EOF)
- return 0;
+ if (ret == E_EOF) {
+ err = 0;
+ break;
+ }
/*
if (ret == E_NOMEM)
- return -1;
+ break;
*/
}
+ Py_DECREF(filename);
+ return err;
}
/* compute parser flags based on compiler flags */
@@ -1266,18 +1367,25 @@ static int PARSER_FLAGS(PyCompilerFlags *flags)
#endif
int
-PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
+PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
{
- PyObject *m, *d, *v, *w, *oenc = NULL;
+ PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
mod_ty mod;
PyArena *arena;
char *ps1 = "", *ps2 = "", *enc = NULL;
int errcode = 0;
_Py_IDENTIFIER(encoding);
+ _Py_IDENTIFIER(__main__);
+
+ mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
+ if (mod_name == NULL) {
+ PyErr_Print();
+ return -1;
+ }
if (fp == stdin) {
/* Fetch encoding from sys.stdin if possible. */
- v = PySys_GetObject("stdin");
+ v = _PySys_GetObjectId(&PyId_stdin);
if (v && v != Py_None) {
oenc = _PyObject_GetAttrId(v, &PyId_encoding);
if (oenc)
@@ -1286,7 +1394,7 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags
PyErr_Clear();
}
}
- v = PySys_GetObject("ps1");
+ v = _PySys_GetObjectId(&PyId_ps1);
if (v != NULL) {
v = PyObject_Str(v);
if (v == NULL)
@@ -1299,7 +1407,7 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags
}
}
}
- w = PySys_GetObject("ps2");
+ w = _PySys_GetObjectId(&PyId_ps2);
if (w != NULL) {
w = PyObject_Str(w);
if (w == NULL)
@@ -1319,9 +1427,9 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags
Py_XDECREF(oenc);
return -1;
}
- mod = PyParser_ASTFromFile(fp, filename, enc,
- Py_single_input, ps1, ps2,
- flags, &errcode, arena);
+ mod = PyParser_ASTFromFileObject(fp, filename, enc,
+ Py_single_input, ps1, ps2,
+ flags, &errcode, arena);
Py_XDECREF(v);
Py_XDECREF(w);
Py_XDECREF(oenc);
@@ -1334,7 +1442,7 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags
PyErr_Print();
return -1;
}
- m = PyImport_AddModule("__main__");
+ m = PyImport_AddModuleObject(mod_name);
if (m == NULL) {
PyArena_Free(arena);
return -1;
@@ -1351,6 +1459,23 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags
return 0;
}
+int
+PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
+{
+ PyObject *filename;
+ int res;
+
+ filename = PyUnicode_DecodeFSDefault(filename_str);
+ if (filename == NULL) {
+ PyErr_Print();
+ return -1;
+ }
+ res = PyRun_InteractiveOneObject(fp, filename, flags);
+ Py_DECREF(filename);
+ return res;
+}
+
+
/* Check whether a file maybe a pyc file: Look at the extension,
the file type, and, if we may close it, at the first few bytes. */
@@ -1518,8 +1643,8 @@ PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
}
static int
-parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
- int *lineno, int *offset, const char **text)
+parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
+ int *lineno, int *offset, PyObject **text)
{
long hold;
PyObject *v;
@@ -1530,6 +1655,7 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
_Py_IDENTIFIER(text);
*message = NULL;
+ *filename = NULL;
/* new style errors. `err' is an instance */
*message = _PyObject_GetAttrId(err, &PyId_msg);
@@ -1541,13 +1667,13 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
goto finally;
if (v == Py_None) {
Py_DECREF(v);
- *filename = NULL;
+ *filename = _PyUnicode_FromId(&PyId_string);
+ if (*filename == NULL)
+ goto finally;
+ Py_INCREF(*filename);
}
else {
- *filename = _PyUnicode_AsString(v);
- Py_DECREF(v);
- if (!*filename)
- goto finally;
+ *filename = v;
}
v = _PyObject_GetAttrId(err, &PyId_lineno);
@@ -1581,15 +1707,13 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
*text = NULL;
}
else {
- *text = _PyUnicode_AsString(v);
- Py_DECREF(v);
- if (!*text)
- goto finally;
+ *text = v;
}
return 1;
finally:
Py_XDECREF(*message);
+ Py_XDECREF(*filename);
return 0;
}
@@ -1600,9 +1724,15 @@ PyErr_Print(void)
}
static void
-print_error_text(PyObject *f, int offset, const char *text)
+print_error_text(PyObject *f, int offset, PyObject *text_obj)
{
+ char *text;
char *nl;
+
+ text = _PyUnicode_AsString(text_obj);
+ if (text == NULL)
+ return;
+
if (offset >= 0) {
if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
offset--;
@@ -1661,7 +1791,7 @@ handle_system_exit(void)
if (PyLong_Check(value))
exitcode = (int)PyLong_AsLong(value);
else {
- PyObject *sys_stderr = PySys_GetObject("stderr");
+ PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr);
if (sys_stderr != NULL && sys_stderr != Py_None) {
PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
} else {
@@ -1704,11 +1834,11 @@ PyErr_PrintEx(int set_sys_last_vars)
return;
/* Now we know v != NULL too */
if (set_sys_last_vars) {
- PySys_SetObject("last_type", exception);
- PySys_SetObject("last_value", v);
- PySys_SetObject("last_traceback", tb);
+ _PySys_SetObjectId(&PyId_last_type, exception);
+ _PySys_SetObjectId(&PyId_last_value, v);
+ _PySys_SetObjectId(&PyId_last_traceback, tb);
}
- hook = PySys_GetObject("excepthook");
+ hook = _PySys_GetObjectId(&PyId_excepthook);
if (hook) {
PyObject *args = PyTuple_Pack(3, exception, v, tb);
PyObject *result = PyEval_CallObject(hook, args);
@@ -1773,27 +1903,30 @@ print_exception(PyObject *f, PyObject *value)
if (err == 0 &&
_PyObject_HasAttrId(value, &PyId_print_file_and_line))
{
- PyObject *message;
- const char *filename, *text;
+ PyObject *message, *filename, *text;
int lineno, offset;
if (!parse_syntax_error(value, &message, &filename,
&lineno, &offset, &text))
PyErr_Clear();
else {
- char buf[10];
- PyFile_WriteString(" File \"", f);
- if (filename == NULL)
- PyFile_WriteString("<string>", f);
- else
- PyFile_WriteString(filename, f);
- PyFile_WriteString("\", line ", f);
- PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
- PyFile_WriteString(buf, f);
- PyFile_WriteString("\n", f);
- if (text != NULL)
- print_error_text(f, offset, text);
+ PyObject *line;
+
Py_DECREF(value);
value = message;
+
+ line = PyUnicode_FromFormat(" File \"%U\", line %d\n",
+ filename, lineno);
+ Py_DECREF(filename);
+ if (line != NULL) {
+ PyFile_WriteObject(line, f, Py_PRINT_RAW);
+ Py_DECREF(line);
+ }
+
+ if (text != NULL) {
+ print_error_text(f, offset, text);
+ Py_DECREF(text);
+ }
+
/* Can't be bothered to check all those
PyFile_WriteString() calls */
if (PyErr_Occurred())
@@ -1822,10 +1955,9 @@ print_exception(PyObject *f, PyObject *value)
err = PyFile_WriteString("<unknown>", f);
}
else {
- char* modstr = _PyUnicode_AsString(moduleName);
- if (modstr && strcmp(modstr, "builtins"))
+ if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0)
{
- err = PyFile_WriteString(modstr, f);
+ err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
err += PyFile_WriteString(".", f);
}
Py_DECREF(moduleName);
@@ -1918,7 +2050,17 @@ void
PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
{
PyObject *seen;
- PyObject *f = PySys_GetObject("stderr");
+ PyObject *f = _PySys_GetObjectId(&PyId_stderr);
+ if (PyExceptionInstance_Check(value)
+ && tb != NULL && PyTraceBack_Check(tb)) {
+ /* Put the traceback on the exception, otherwise it won't get
+ displayed. See issue #18776. */
+ PyObject *cur_tb = PyException_GetTraceback(value);
+ if (cur_tb == NULL)
+ PyException_SetTraceback(value, tb);
+ else
+ Py_DECREF(cur_tb);
+ }
if (f == Py_None) {
/* pass */
}
@@ -1944,37 +2086,54 @@ PyRun_StringFlags(const char *str, int start, PyObject *globals,
{
PyObject *ret = NULL;
mod_ty mod;
- PyArena *arena = PyArena_New();
+ PyArena *arena;
+ PyObject *filename;
+
+ filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
+ if (filename == NULL)
+ return NULL;
+
+ arena = PyArena_New();
if (arena == NULL)
return NULL;
- mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
+ mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
if (mod != NULL)
- ret = run_mod(mod, "<string>", globals, locals, flags, arena);
+ ret = run_mod(mod, filename, globals, locals, flags, arena);
PyArena_Free(arena);
return ret;
}
PyObject *
-PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
+PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
PyObject *locals, int closeit, PyCompilerFlags *flags)
{
- PyObject *ret;
+ PyObject *ret = NULL;
mod_ty mod;
- PyArena *arena = PyArena_New();
+ PyArena *arena = NULL;
+ PyObject *filename;
+
+ filename = PyUnicode_DecodeFSDefault(filename_str);
+ if (filename == NULL)
+ goto exit;
+
+ arena = PyArena_New();
if (arena == NULL)
- return NULL;
+ goto exit;
- mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
- flags, NULL, arena);
+ mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
+ flags, NULL, arena);
if (closeit)
fclose(fp);
if (mod == NULL) {
- PyArena_Free(arena);
- return NULL;
+ goto exit;
}
ret = run_mod(mod, filename, globals, locals, flags, arena);
- PyArena_Free(arena);
+
+exit:
+ Py_XDECREF(filename);
+ if (arena != NULL)
+ PyArena_Free(arena);
return ret;
}
@@ -1983,12 +2142,11 @@ flush_io(void)
{
PyObject *f, *r;
PyObject *type, *value, *traceback;
- _Py_IDENTIFIER(flush);
/* Save the current exception */
PyErr_Fetch(&type, &value, &traceback);
- f = PySys_GetObject("stderr");
+ f = _PySys_GetObjectId(&PyId_stderr);
if (f != NULL) {
r = _PyObject_CallMethodId(f, &PyId_flush, "");
if (r)
@@ -1996,7 +2154,7 @@ flush_io(void)
else
PyErr_Clear();
}
- f = PySys_GetObject("stdout");
+ f = _PySys_GetObjectId(&PyId_stdout);
if (f != NULL) {
r = _PyObject_CallMethodId(f, &PyId_flush, "");
if (r)
@@ -2009,12 +2167,12 @@ flush_io(void)
}
static PyObject *
-run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
- PyCompilerFlags *flags, PyArena *arena)
+run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
+ PyCompilerFlags *flags, PyArena *arena)
{
PyCodeObject *co;
PyObject *v;
- co = PyAST_Compile(mod, filename, flags, arena);
+ co = PyAST_CompileObject(mod, filename, flags, -1, arena);
if (co == NULL)
return NULL;
v = PyEval_EvalCode((PyObject*)co, globals, locals);