Skip to content

Commit a7dc714

Browse files
bpo-41094: Additional fix for PYTHONSTARTUP. (GH-21119)
1 parent 33b79b1 commit a7dc714

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

Lib/test/test_embed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ def test_audit_run_file(self):
13491349
returncode=1)
13501350

13511351
def test_audit_run_interactivehook(self):
1352-
startup = os.path.join(self.oldcwd, support.TESTFN) + (support.TESTFN or '') + ".py"
1352+
startup = os.path.join(self.oldcwd, support.TESTFN) + (support.FS_NONASCII or '') + ".py"
13531353
with open(startup, "w", encoding="utf-8") as f:
13541354
print("import sys", file=f)
13551355
print("sys.__interactivehook__ = lambda: None", file=f)
@@ -1362,7 +1362,7 @@ def test_audit_run_interactivehook(self):
13621362
os.unlink(startup)
13631363

13641364
def test_audit_run_startup(self):
1365-
startup = os.path.join(self.oldcwd, support.TESTFN) + (support.TESTFN or '') + ".py"
1365+
startup = os.path.join(self.oldcwd, support.TESTFN) + (support.FS_NONASCII or '') + ".py"
13661366
with open(startup, "w", encoding="utf-8") as f:
13671367
print("pass", file=f)
13681368
try:

Modules/main.c

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,36 +375,70 @@ pymain_run_file(const PyConfig *config, PyCompilerFlags *cf)
375375
static int
376376
pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
377377
{
378+
int ret;
379+
PyObject *startup_obj = NULL;
380+
if (!config->use_environment) {
381+
return 0;
382+
}
383+
#ifdef MS_WINDOWS
384+
const wchar_t *wstartup = _wgetenv(L"PYTHONSTARTUP");
385+
if (wstartup == NULL || wstartup[0] == L'\0') {
386+
return 0;
387+
}
388+
PyObject *startup_bytes = NULL;
389+
startup_obj = PyUnicode_FromWideChar(wstartup, wcslen(wstartup));
390+
if (startup_obj == NULL) {
391+
goto error;
392+
}
393+
startup_bytes = PyUnicode_EncodeFSDefault(startup_obj);
394+
if (startup_bytes == NULL) {
395+
goto error;
396+
}
397+
const char *startup = PyBytes_AS_STRING(startup_bytes);
398+
#else
378399
const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
379400
if (startup == NULL) {
380401
return 0;
381402
}
382-
PyObject *startup_obj = PyUnicode_DecodeFSDefault(startup);
403+
startup_obj = PyUnicode_DecodeFSDefault(startup);
383404
if (startup_obj == NULL) {
384-
return pymain_err_print(exitcode);
405+
goto error;
385406
}
407+
#endif
386408
if (PySys_Audit("cpython.run_startup", "O", startup_obj) < 0) {
387-
Py_DECREF(startup_obj);
388-
return pymain_err_print(exitcode);
409+
goto error;
389410
}
390-
Py_DECREF(startup_obj);
391411

412+
#ifdef MS_WINDOWS
413+
FILE *fp = _Py_wfopen(wstartup, L"r");
414+
#else
392415
FILE *fp = _Py_fopen(startup, "r");
416+
#endif
393417
if (fp == NULL) {
394418
int save_errno = errno;
395419
PyErr_Clear();
396420
PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
397421

398422
errno = save_errno;
399-
PyErr_SetFromErrnoWithFilename(PyExc_OSError, startup);
400-
401-
return pymain_err_print(exitcode);
423+
PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup_obj, NULL);
424+
goto error;
402425
}
403426

404427
(void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
405428
PyErr_Clear();
406429
fclose(fp);
407-
return 0;
430+
ret = 0;
431+
432+
done:
433+
#ifdef MS_WINDOWS
434+
Py_XDECREF(startup_bytes);
435+
#endif
436+
Py_XDECREF(startup_obj);
437+
return ret;
438+
439+
error:
440+
ret = pymain_err_print(exitcode);
441+
goto done;
408442
}
409443

410444

0 commit comments

Comments
 (0)