@@ -387,36 +387,70 @@ pymain_run_file(PyConfig *config, PyCompilerFlags *cf)
387387static int
388388pymain_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