diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-06-24 16:46:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-24 16:46:30 (GMT) |
commit | a7dc71470156680f1fd5243290c6d377824b7ef4 (patch) | |
tree | bf3e39efe4cb3c94a9a83c4d6d470bd762452c77 /Modules | |
parent | 33b79b11b891adea5a916df8e3779505b37aabe7 (diff) | |
download | cpython-a7dc71470156680f1fd5243290c6d377824b7ef4.zip cpython-a7dc71470156680f1fd5243290c6d377824b7ef4.tar.gz cpython-a7dc71470156680f1fd5243290c6d377824b7ef4.tar.bz2 |
bpo-41094: Additional fix for PYTHONSTARTUP. (GH-21119)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/main.c | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/Modules/main.c b/Modules/main.c index 8e3b35c..4a76f44 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -375,36 +375,70 @@ pymain_run_file(const PyConfig *config, PyCompilerFlags *cf) static int pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode) { + int ret; + PyObject *startup_obj = NULL; + if (!config->use_environment) { + return 0; + } +#ifdef MS_WINDOWS + const wchar_t *wstartup = _wgetenv(L"PYTHONSTARTUP"); + if (wstartup == NULL || wstartup[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) { + goto error; + } + const char *startup = PyBytes_AS_STRING(startup_bytes); +#else const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP"); if (startup == NULL) { return 0; } - PyObject *startup_obj = PyUnicode_DecodeFSDefault(startup); + startup_obj = PyUnicode_DecodeFSDefault(startup); if (startup_obj == NULL) { - return pymain_err_print(exitcode); + goto error; } +#endif if (PySys_Audit("cpython.run_startup", "O", startup_obj) < 0) { - Py_DECREF(startup_obj); - return pymain_err_print(exitcode); + goto error; } - Py_DECREF(startup_obj); +#ifdef MS_WINDOWS + FILE *fp = _Py_wfopen(wstartup, L"r"); +#else FILE *fp = _Py_fopen(startup, "r"); +#endif if (fp == NULL) { int save_errno = errno; PyErr_Clear(); PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); errno = save_errno; - PyErr_SetFromErrnoWithFilename(PyExc_OSError, startup); - - return pymain_err_print(exitcode); + PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup_obj, NULL); + goto error; } (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); PyErr_Clear(); fclose(fp); - return 0; + ret = 0; + +done: +#ifdef MS_WINDOWS + Py_XDECREF(startup_bytes); +#endif + Py_XDECREF(startup_obj); + return ret; + +error: + ret = pymain_err_print(exitcode); + goto done; } |