@@ -375,36 +375,70 @@ pymain_run_file(const PyConfig *config, PyCompilerFlags *cf)
375
375
static int
376
376
pymain_run_startup (PyConfig * config , PyCompilerFlags * cf , int * exitcode )
377
377
{
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
378
399
const char * startup = _Py_GetEnv (config -> use_environment , "PYTHONSTARTUP" );
379
400
if (startup == NULL ) {
380
401
return 0 ;
381
402
}
382
- PyObject * startup_obj = PyUnicode_DecodeFSDefault (startup );
403
+ startup_obj = PyUnicode_DecodeFSDefault (startup );
383
404
if (startup_obj == NULL ) {
384
- return pymain_err_print ( exitcode ) ;
405
+ goto error ;
385
406
}
407
+ #endif
386
408
if (PySys_Audit ("cpython.run_startup" , "O" , startup_obj ) < 0 ) {
387
- Py_DECREF (startup_obj );
388
- return pymain_err_print (exitcode );
409
+ goto error ;
389
410
}
390
- Py_DECREF (startup_obj );
391
411
412
+ #ifdef MS_WINDOWS
413
+ FILE * fp = _Py_wfopen (wstartup , L"r" );
414
+ #else
392
415
FILE * fp = _Py_fopen (startup , "r" );
416
+ #endif
393
417
if (fp == NULL ) {
394
418
int save_errno = errno ;
395
419
PyErr_Clear ();
396
420
PySys_WriteStderr ("Could not open PYTHONSTARTUP\n" );
397
421
398
422
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 ;
402
425
}
403
426
404
427
(void ) PyRun_SimpleFileExFlags (fp , startup , 0 , cf );
405
428
PyErr_Clear ();
406
429
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 ;
408
442
}
409
443
410
444
0 commit comments