Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Include/cpython/pythonrun.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
#endif

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(
FILE *fp,
const char *filename, /* decoded from the filesystem encoding */
Expand Down
41 changes: 14 additions & 27 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ set_main_loader(PyObject *d, PyObject *filename, const char *loader_name)
}


static int
pyrun_simple_file(FILE *fp, PyObject *filename, int closeit,
PyCompilerFlags *flags)
int
_PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
PyCompilerFlags *flags)
{
PyObject *m, *d, *v;
int set_file_name = 0, ret = -1;
Expand Down Expand Up @@ -441,7 +441,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
if (filename_obj == NULL) {
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);
return res;
}
Expand Down