Skip to content

Commit 1dda40c

Browse files
bpo-41094: Additional fix for PYTHONSTARTUP. (GH-21119)
(cherry picked from commit a7dc714) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 1813d31 commit 1dda40c

2 files changed

Lines changed: 45 additions & 11 deletions

File tree

Lib/test/test_embed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ def test_audit_run_file(self):
13091309
self.run_embedded_interpreter("test_audit_run_file", timeout=3, returncode=1)
13101310

13111311
def test_audit_run_interactivehook(self):
1312-
startup = os.path.join(self.oldcwd, support.TESTFN) + (support.TESTFN or '') + ".py"
1312+
startup = os.path.join(self.oldcwd, support.TESTFN) + (support.FS_NONASCII or '') + ".py"
13131313
with open(startup, "w", encoding="utf-8") as f:
13141314
print("import sys", file=f)
13151315
print("sys.__interactivehook__ = lambda: None", file=f)
@@ -1321,7 +1321,7 @@ def test_audit_run_interactivehook(self):
13211321
os.unlink(startup)
13221322

13231323
def test_audit_run_startup(self):
1324-
startup = os.path.join(self.oldcwd, support.TESTFN) + (support.TESTFN or '') + ".py"
1324+
startup = os.path.join(self.oldcwd, support.TESTFN) + (support.FS_NONASCII or '') + ".py"
13251325
with open(startup, "w", encoding="utf-8") as f:
13261326
print("pass", file=f)
13271327
try:

Modules/main.c

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -387,36 +387,70 @@ pymain_run_file(PyConfig *config, PyCompilerFlags *cf)
387387
static int
388388
pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
389389
{
390+
int ret;
391+
PyObject *startup_obj = NULL;
392+
if (!config->use_environment) {
393+
return 0;
394+
}
395+
#ifdef MS_WINDOWS
396+
const wchar_t *wstartup = _wgetenv(L"PYTHONSTARTUP");
397+
if (wstartup == NULL || wstartup[0] == L'\0') {
398+
return 0;
399+
}
400+
PyObject *startup_bytes = NULL;
401+
startup_obj = PyUnicode_FromWideChar(wstartup, wcslen(wstartup));
402+
if (startup_obj == NULL) {
403+
goto error;
404+
}
405+
startup_bytes = PyUnicode_EncodeFSDefault(startup_obj);
406+
if (startup_bytes == NULL) {
407+
goto error;
408+
}
409+
const char *startup = PyBytes_AS_STRING(startup_bytes);
410+
#else
390411
const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
391412
if (startup == NULL) {
392413
return 0;
393414
}
394-
PyObject *startup_obj = PyUnicode_DecodeFSDefault(startup);
415+
startup_obj = PyUnicode_DecodeFSDefault(startup);
395416
if (startup_obj == NULL) {
396-
return pymain_err_print(exitcode);
417+
goto error;
397418
}
419+
#endif
398420
if (PySys_Audit("cpython.run_startup", "O", startup_obj) < 0) {
399-
Py_DECREF(startup_obj);
400-
return pymain_err_print(exitcode);
421+
goto error;
401422
}
402-
Py_DECREF(startup_obj);
403423

424+
#ifdef MS_WINDOWS
425+
FILE *fp = _Py_wfopen(wstartup, L"r");
426+
#else
404427
FILE *fp = _Py_fopen(startup, "r");
428+
#endif
405429
if (fp == NULL) {
406430
int save_errno = errno;
407431
PyErr_Clear();
408432
PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
409433

410434
errno = save_errno;
411-
PyErr_SetFromErrnoWithFilename(PyExc_OSError, startup);
412-
413-
return pymain_err_print(exitcode);
435+
PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup_obj, NULL);
436+
goto error;
414437
}
415438

416439
(void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
417440
PyErr_Clear();
418441
fclose(fp);
419-
return 0;
442+
ret = 0;
443+
444+
done:
445+
#ifdef MS_WINDOWS
446+
Py_XDECREF(startup_bytes);
447+
#endif
448+
Py_XDECREF(startup_obj);
449+
return ret;
450+
451+
error:
452+
ret = pymain_err_print(exitcode);
453+
goto done;
420454
}
421455

422456

0 commit comments

Comments
 (0)