@@ -492,7 +492,6 @@ _PyCoreConfig_Clear(_PyCoreConfig *config)
492492 CLEAR (config -> module_search_path_env );
493493 CLEAR (config -> home );
494494 CLEAR (config -> program_name );
495- CLEAR (config -> program );
496495
497496 _PyWstrList_Clear (& config -> argv );
498497 _PyWstrList_Clear (& config -> warnoptions );
@@ -626,7 +625,6 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
626625 COPY_WSTR_ATTR (module_search_path_env );
627626 COPY_WSTR_ATTR (home );
628627 COPY_WSTR_ATTR (program_name );
629- COPY_WSTR_ATTR (program );
630628
631629 COPY_ATTR (parse_argv );
632630 COPY_WSTRLIST (argv );
@@ -732,7 +730,6 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config)
732730 SET_ITEM_WSTR (program_name );
733731 SET_ITEM_INT (parse_argv );
734732 SET_ITEM_WSTRLIST (argv );
735- SET_ITEM_WSTR (program );
736733 SET_ITEM_WSTRLIST (xoptions );
737734 SET_ITEM_WSTRLIST (warnoptions );
738735 SET_ITEM_WSTR (module_search_path_env );
@@ -918,7 +915,6 @@ static _PyInitError
918915config_init_program_name (_PyCoreConfig * config )
919916{
920917 _PyInitError err ;
921- assert (config -> program_name == NULL );
922918
923919 /* If Py_SetProgramName() was called, use its value */
924920 const wchar_t * program_name = _Py_path_config .program_name ;
@@ -967,16 +963,17 @@ config_init_program_name(_PyCoreConfig *config)
967963#endif /* WITH_NEXT_FRAMEWORK */
968964#endif /* __APPLE__ */
969965
970- /* Use argv[0] by default, if available */
971- if (config -> program != NULL ) {
972- err = _PyCoreConfig_SetString (& config -> program_name , config -> program );
973- if (_Py_INIT_FAILED (err )) {
974- return err ;
966+ /* Use argv[0] if available and non-empty */
967+ const _PyWstrList * argv = & config -> argv ;
968+ if (argv -> length >= 1 && argv -> items [0 ][0 ] != L'\0' ) {
969+ config -> program_name = _PyMem_RawWcsdup (argv -> items [0 ]);
970+ if (config -> program_name == NULL ) {
971+ return _Py_INIT_NO_MEMORY ();
975972 }
976973 return _Py_INIT_OK ();
977974 }
978975
979- /* Last fall back: hardcoded string */
976+ /* Last fall back: hardcoded name */
980977#ifdef MS_WINDOWS
981978 const wchar_t * default_program_name = L"python" ;
982979#else
@@ -1518,13 +1515,6 @@ config_read(_PyCoreConfig *config)
15181515 }
15191516 }
15201517
1521- if (config -> program_name == NULL ) {
1522- err = config_init_program_name (config );
1523- if (_Py_INIT_FAILED (err )) {
1524- return err ;
1525- }
1526- }
1527-
15281518 if (config -> executable == NULL ) {
15291519 err = config_init_executable (config );
15301520 if (_Py_INIT_FAILED (err )) {
@@ -1678,6 +1668,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyWstrList *warnoptions,
16781668 _PyInitError err ;
16791669 const _PyWstrList * argv = & config -> argv ;
16801670 int print_version = 0 ;
1671+ const wchar_t * program = config -> program_name ;
16811672
16821673 _PyOS_ResetGetOpt ();
16831674 do {
@@ -1734,7 +1725,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyWstrList *warnoptions,
17341725 } else {
17351726 fprintf (stderr , "--check-hash-based-pycs must be one of "
17361727 "'default', 'always', or 'never'\n" );
1737- config_usage (1 , config -> program );
1728+ config_usage (1 , program );
17381729 return _Py_INIT_EXIT (2 );
17391730 }
17401731 break ;
@@ -1794,7 +1785,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyWstrList *warnoptions,
17941785
17951786 case 'h' :
17961787 case '?' :
1797- config_usage (0 , config -> program );
1788+ config_usage (0 , program );
17981789 return _Py_INIT_EXIT (0 );
17991790
18001791 case 'V' :
@@ -1819,7 +1810,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyWstrList *warnoptions,
18191810
18201811 default :
18211812 /* unknown argument: parsing failed */
1822- config_usage (1 , config -> program );
1813+ config_usage (1 , program );
18231814 return _Py_INIT_EXIT (2 );
18241815 }
18251816 } while (1 );
@@ -1892,26 +1883,6 @@ config_init_env_warnoptions(const _PyCoreConfig *config, _PyWstrList *warnoption
18921883}
18931884
18941885
1895- static _PyInitError
1896- config_init_program (_PyCoreConfig * config )
1897- {
1898- const _PyWstrList * argv = & config -> argv ;
1899- wchar_t * program ;
1900- if (argv -> length >= 1 ) {
1901- program = argv -> items [0 ];
1902- }
1903- else {
1904- program = L"" ;
1905- }
1906- config -> program = _PyMem_RawWcsdup (program );
1907- if (config -> program == NULL ) {
1908- return _Py_INIT_NO_MEMORY ();
1909- }
1910-
1911- return _Py_INIT_OK ();
1912- }
1913-
1914-
19151886static int
19161887config_add_warnoption (_PyCoreConfig * config , const wchar_t * option )
19171888{
@@ -2084,10 +2055,10 @@ config_read_cmdline(_PyCoreConfig *config)
20842055 config -> parse_argv = 1 ;
20852056 }
20862057
2087- if (config -> program == NULL ) {
2088- err = config_init_program (config );
2058+ if (config -> program_name == NULL ) {
2059+ err = config_init_program_name (config );
20892060 if (_Py_INIT_FAILED (err )) {
2090- goto done ;
2061+ return err ;
20912062 }
20922063 }
20932064
@@ -2218,6 +2189,8 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
22182189 goto done ;
22192190 }
22202191
2192+ /* precmdline.argv is a copy of config.argv which is modified
2193+ by config_read_cmdline() */
22212194 const _PyWstrList * argv = & precmdline .argv ;
22222195 if (_Py_SetArgcArgv (argv -> length , argv -> items ) < 0 ) {
22232196 err = _Py_INIT_NO_MEMORY ();
@@ -2246,7 +2219,6 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
22462219 assert (config -> configure_c_stdio >= 0 );
22472220 assert (config -> buffered_stdio >= 0 );
22482221 assert (config -> program_name != NULL );
2249- assert (config -> program != NULL );
22502222 assert (_PyWstrList_CheckConsistency (& config -> argv ));
22512223 /* sys.argv must be non-empty: empty argv is replaced with [''] */
22522224 assert (config -> argv .length >= 1 );
0 commit comments