@@ -375,36 +375,70 @@ pymain_run_file(const PyConfig *config, PyCompilerFlags *cf)
375375static int
376376pymain_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