Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 550e467

Browse files
authored
bpo-32381: Add _PyRun_SimpleFileObject() (GH-23709)
pymain_run_startup() now pass the filename as a Python object to _PyRun_SimpleFileObject().
1 parent 98a5417 commit 550e467

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

Include/cpython/pythonrun.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#endif
44

55
PyAPI_FUNC(int) PyRun_SimpleStringFlags(const char *, PyCompilerFlags *);
6+
PyAPI_FUNC(int) _PyRun_SimpleFileObject(
7+
FILE *fp,
8+
PyObject *filename,
9+
int closeit,
10+
PyCompilerFlags *flags);
611
PyAPI_FUNC(int) PyRun_AnyFileExFlags(
712
FILE *fp,
813
const char *filename, /* decoded from the filesystem encoding */

Modules/main.c

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -380,64 +380,51 @@ static int
380380
pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
381381
{
382382
int ret;
383-
PyObject *startup_obj = NULL;
384383
if (!config->use_environment) {
385384
return 0;
386385
}
386+
PyObject *startup = NULL;
387387
#ifdef MS_WINDOWS
388-
const wchar_t *wstartup = _wgetenv(L"PYTHONSTARTUP");
389-
if (wstartup == NULL || wstartup[0] == L'\0') {
388+
const wchar_t *env = _wgetenv(L"PYTHONSTARTUP");
389+
if (env == NULL || env[0] == L'\0') {
390390
return 0;
391391
}
392-
PyObject *startup_bytes = NULL;
393-
startup_obj = PyUnicode_FromWideChar(wstartup, wcslen(wstartup));
394-
if (startup_obj == NULL) {
395-
goto error;
396-
}
397-
startup_bytes = PyUnicode_EncodeFSDefault(startup_obj);
398-
if (startup_bytes == NULL) {
392+
startup = PyUnicode_FromWideChar(env, wcslen(env));
393+
if (startup == NULL) {
399394
goto error;
400395
}
401-
const char *startup = PyBytes_AS_STRING(startup_bytes);
402396
#else
403-
const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
404-
if (startup == NULL) {
397+
const char *env = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
398+
if (env == NULL) {
405399
return 0;
406400
}
407-
startup_obj = PyUnicode_DecodeFSDefault(startup);
408-
if (startup_obj == NULL) {
401+
startup = PyUnicode_DecodeFSDefault(env);
402+
if (startup == NULL) {
409403
goto error;
410404
}
411405
#endif
412-
if (PySys_Audit("cpython.run_startup", "O", startup_obj) < 0) {
406+
if (PySys_Audit("cpython.run_startup", "O", startup) < 0) {
413407
goto error;
414408
}
415409

416-
#ifdef MS_WINDOWS
417-
FILE *fp = _Py_wfopen(wstartup, L"r");
418-
#else
419-
FILE *fp = _Py_fopen(startup, "r");
420-
#endif
410+
FILE *fp = _Py_fopen_obj(startup, "r");
421411
if (fp == NULL) {
422412
int save_errno = errno;
423413
PyErr_Clear();
424414
PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
425415

426416
errno = save_errno;
427-
PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup_obj, NULL);
417+
PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup, NULL);
428418
goto error;
429419
}
430420

431-
(void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
421+
(void) _PyRun_SimpleFileObject(fp, startup, 0, cf);
432422
PyErr_Clear();
433423
fclose(fp);
434424
ret = 0;
435425

436426
done:
437-
#ifdef MS_WINDOWS
438-
Py_XDECREF(startup_bytes);
439-
#endif
440-
Py_XDECREF(startup_obj);
427+
Py_XDECREF(startup);
441428
return ret;
442429

443430
error:

Python/pythonrun.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ set_main_loader(PyObject *d, PyObject *filename, const char *loader_name)
350350
}
351351

352352

353-
static int
354-
pyrun_simple_file(FILE *fp, PyObject *filename, int closeit,
355-
PyCompilerFlags *flags)
353+
int
354+
_PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
355+
PyCompilerFlags *flags)
356356
{
357357
PyObject *m, *d, *v;
358358
int set_file_name = 0, ret = -1;
@@ -441,7 +441,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
441441
if (filename_obj == NULL) {
442442
return -1;
443443
}
444-
int res = pyrun_simple_file(fp, filename_obj, closeit, flags);
444+
int res = _PyRun_SimpleFileObject(fp, filename_obj, closeit, flags);
445445
Py_DECREF(filename_obj);
446446
return res;
447447
}

0 commit comments

Comments
 (0)