diff options
author | Victor Stinner <vstinner@python.org> | 2020-12-08 23:32:54 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-08 23:32:54 (GMT) |
commit | 550e4673be538d98b6ddf5550b3922539cf5c4b2 (patch) | |
tree | db27471f32ea5a682cb6f73004552d7dd64a23d0 /Modules/main.c | |
parent | 98a54171932584883cb3973f78dd30f92d7a3a78 (diff) | |
download | cpython-550e4673be538d98b6ddf5550b3922539cf5c4b2.zip cpython-550e4673be538d98b6ddf5550b3922539cf5c4b2.tar.gz cpython-550e4673be538d98b6ddf5550b3922539cf5c4b2.tar.bz2 |
bpo-32381: Add _PyRun_SimpleFileObject() (GH-23709)
pymain_run_startup() now pass the filename as a Python object to
_PyRun_SimpleFileObject().
Diffstat (limited to 'Modules/main.c')
-rw-r--r-- | Modules/main.c | 41 |
1 files changed, 14 insertions, 27 deletions
diff --git a/Modules/main.c b/Modules/main.c index 2cc891f..3aa4d91 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -380,64 +380,51 @@ static int pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode) { int ret; - PyObject *startup_obj = NULL; if (!config->use_environment) { return 0; } + PyObject *startup = NULL; #ifdef MS_WINDOWS - const wchar_t *wstartup = _wgetenv(L"PYTHONSTARTUP"); - if (wstartup == NULL || wstartup[0] == L'\0') { + const wchar_t *env = _wgetenv(L"PYTHONSTARTUP"); + if (env == NULL || env[0] == L'\0') { return 0; } - PyObject *startup_bytes = NULL; - startup_obj = PyUnicode_FromWideChar(wstartup, wcslen(wstartup)); - if (startup_obj == NULL) { - goto error; - } - startup_bytes = PyUnicode_EncodeFSDefault(startup_obj); - if (startup_bytes == NULL) { + startup = PyUnicode_FromWideChar(env, wcslen(env)); + if (startup == NULL) { goto error; } - const char *startup = PyBytes_AS_STRING(startup_bytes); #else - const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP"); - if (startup == NULL) { + const char *env = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP"); + if (env == NULL) { return 0; } - startup_obj = PyUnicode_DecodeFSDefault(startup); - if (startup_obj == NULL) { + startup = PyUnicode_DecodeFSDefault(env); + if (startup == NULL) { goto error; } #endif - if (PySys_Audit("cpython.run_startup", "O", startup_obj) < 0) { + if (PySys_Audit("cpython.run_startup", "O", startup) < 0) { goto error; } -#ifdef MS_WINDOWS - FILE *fp = _Py_wfopen(wstartup, L"r"); -#else - FILE *fp = _Py_fopen(startup, "r"); -#endif + FILE *fp = _Py_fopen_obj(startup, "r"); if (fp == NULL) { int save_errno = errno; PyErr_Clear(); PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); errno = save_errno; - PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup_obj, NULL); + PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup, NULL); goto error; } - (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); + (void) _PyRun_SimpleFileObject(fp, startup, 0, cf); PyErr_Clear(); fclose(fp); ret = 0; done: -#ifdef MS_WINDOWS - Py_XDECREF(startup_bytes); -#endif - Py_XDECREF(startup_obj); + Py_XDECREF(startup); return ret; error: |