bpo-32381: Add _PyRun_SimpleFileObject() (GH-23709)

pymain_run_startup() now pass the filename as a Python object to
_PyRun_SimpleFileObject().
This commit is contained in:
Victor Stinner 2020-12-09 00:32:54 +01:00 committed by GitHub
parent 98a5417193
commit 550e4673be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 33 deletions

View File

@ -3,6 +3,11 @@
#endif #endif
PyAPI_FUNC(int) PyRun_SimpleStringFlags(const char *, PyCompilerFlags *); PyAPI_FUNC(int) PyRun_SimpleStringFlags(const char *, PyCompilerFlags *);
PyAPI_FUNC(int) _PyRun_SimpleFileObject(
FILE *fp,
PyObject *filename,
int closeit,
PyCompilerFlags *flags);
PyAPI_FUNC(int) PyRun_AnyFileExFlags( PyAPI_FUNC(int) PyRun_AnyFileExFlags(
FILE *fp, FILE *fp,
const char *filename, /* decoded from the filesystem encoding */ const char *filename, /* decoded from the filesystem encoding */

View File

@ -380,64 +380,51 @@ static int
pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode) pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
{ {
int ret; int ret;
PyObject *startup_obj = NULL;
if (!config->use_environment) { if (!config->use_environment) {
return 0; return 0;
} }
PyObject *startup = NULL;
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
const wchar_t *wstartup = _wgetenv(L"PYTHONSTARTUP"); const wchar_t *env = _wgetenv(L"PYTHONSTARTUP");
if (wstartup == NULL || wstartup[0] == L'\0') { if (env == NULL || env[0] == L'\0') {
return 0; return 0;
} }
PyObject *startup_bytes = NULL; startup = PyUnicode_FromWideChar(env, wcslen(env));
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) { if (startup == NULL) {
goto error;
}
#else
const char *env = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
if (env == NULL) {
return 0; return 0;
} }
startup_obj = PyUnicode_DecodeFSDefault(startup); startup = PyUnicode_DecodeFSDefault(env);
if (startup_obj == NULL) { if (startup == NULL) {
goto error; goto error;
} }
#endif #endif
if (PySys_Audit("cpython.run_startup", "O", startup_obj) < 0) { if (PySys_Audit("cpython.run_startup", "O", startup) < 0) {
goto error; goto error;
} }
#ifdef MS_WINDOWS FILE *fp = _Py_fopen_obj(startup, "r");
FILE *fp = _Py_wfopen(wstartup, L"r");
#else
FILE *fp = _Py_fopen(startup, "r");
#endif
if (fp == NULL) { if (fp == NULL) {
int save_errno = errno; int save_errno = errno;
PyErr_Clear(); PyErr_Clear();
PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
errno = save_errno; errno = save_errno;
PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup_obj, NULL); PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup, NULL);
goto error; goto error;
} }
(void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); (void) _PyRun_SimpleFileObject(fp, startup, 0, cf);
PyErr_Clear(); PyErr_Clear();
fclose(fp); fclose(fp);
ret = 0; ret = 0;
done: done:
#ifdef MS_WINDOWS Py_XDECREF(startup);
Py_XDECREF(startup_bytes);
#endif
Py_XDECREF(startup_obj);
return ret; return ret;
error: error:

View File

@ -350,8 +350,8 @@ set_main_loader(PyObject *d, PyObject *filename, const char *loader_name)
} }
static int int
pyrun_simple_file(FILE *fp, PyObject *filename, int closeit, _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
PyCompilerFlags *flags) PyCompilerFlags *flags)
{ {
PyObject *m, *d, *v; PyObject *m, *d, *v;
@ -441,7 +441,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
if (filename_obj == NULL) { if (filename_obj == NULL) {
return -1; return -1;
} }
int res = pyrun_simple_file(fp, filename_obj, closeit, flags); int res = _PyRun_SimpleFileObject(fp, filename_obj, closeit, flags);
Py_DECREF(filename_obj); Py_DECREF(filename_obj);
return res; return res;
} }