| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -218,11 +218,15 @@ typedef struct { | |||
| 218 | 218 | always exists and is never empty. */ | |
| 219 | 219 | _PyWstrList argv; | |
| 220 | 220 | ||
| 221 | - /* Program: argv[0] or "". | ||
| 222 | - Used to display Python usage if parsing command line arguments fails. | ||
| 223 | - Used to initialize the default value of program_name */ | ||
| 224 | - wchar_t *program; | ||
| 225 | - wchar_t *program_name; /* Program name, see also Py_GetProgramName() */ | ||
| 221 | + /* Program name: | ||
| 222 | + | ||
| 223 | + - If Py_SetProgramName() was called, use its value. | ||
| 224 | + - On macOS, use PYTHONEXECUTABLE environment variable if set. | ||
| 225 | + - If WITH_NEXT_FRAMEWORK macro is defined, use __PYVENV_LAUNCHER__ | ||
| 226 | + environment variable is set. | ||
| 227 | + - Use argv[0] if available and non-empty. | ||
| 228 | + - Use "python" on Windows, or "python3 on other platforms. */ | ||
| 229 | + wchar_t *program_name; | ||
| 226 | 230 | ||
| 227 | 231 | _PyWstrList xoptions; /* Command line -X options */ | |
| 228 | 232 | _PyWstrList warnoptions; /* Warnings options */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -306,7 +306,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): | |||
| 306 | 306 | 'program_name': GET_DEFAULT_CONFIG, | |
| 307 | 307 | 'parse_argv': 1, | |
| 308 | 308 | 'argv': [""], | |
| 309 | - 'program': '', | ||
| 310 | 309 | ||
| 311 | 310 | 'xoptions': [], | |
| 312 | 311 | 'warnoptions': [], | |
@@ -586,7 +585,6 @@ def test_init_from_config(self): | |||
| 586 | 585 | 'pycache_prefix': 'conf_pycache_prefix', | |
| 587 | 586 | 'program_name': './conf_program_name', | |
| 588 | 587 | 'argv': ['-c', 'arg2'], | |
| 589 | - 'program': 'conf_program', | ||
| 590 | 588 | 'xoptions': ['core_xoption1=3', 'core_xoption2=', 'core_xoption3'], | |
| 591 | 589 | 'warnoptions': ['error::ResourceWarning', 'default::BytesWarning'], | |
| 592 | 590 | 'run_command': 'pass\n', | |
@@ -704,7 +702,6 @@ def test_init_run_main(self): | |||
| 704 | 702 | 'print(json.dumps(_testinternalcapi.get_configs()))') | |
| 705 | 703 | core_config = { | |
| 706 | 704 | 'argv': ['-c', 'arg2'], | |
| 707 | - 'program': 'python3', | ||
| 708 | 705 | 'program_name': './python3', | |
| 709 | 706 | 'run_command': code + '\n', | |
| 710 | 707 | } | |
@@ -716,7 +713,6 @@ def test_init_main(self): | |||
| 716 | 713 | 'print(json.dumps(_testinternalcapi.get_configs()))') | |
| 717 | 714 | core_config = { | |
| 718 | 715 | 'argv': ['-c', 'arg2'], | |
| 719 | - 'program': 'python3', | ||
| 720 | 716 | 'program_name': './python3', | |
| 721 | 717 | 'run_command': code + '\n', | |
| 722 | 718 | '_init_main': 0, | |
@@ -728,7 +724,6 @@ def test_init_dont_parse_argv(self): | |||
| 728 | 724 | core_config = { | |
| 729 | 725 | 'argv': ['-v', '-c', 'arg1', '-W', 'arg2'], | |
| 730 | 726 | 'parse_argv': 0, | |
| 731 | - 'program': 'program', | ||
| 732 | 727 | } | |
| 733 | 728 | self.check_config("init_dont_parse_argv", core_config, {}) | |
| 734 | 729 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -283,7 +283,7 @@ pymain_run_file(_PyCoreConfig *config, PyCompilerFlags *cf) | |||
| 283 | 283 | else | |
| 284 | 284 | cfilename = "<unprintable file name>"; | |
| 285 | 285 | fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n", | |
| 286 | - config->program, cfilename, err, strerror(err)); | ||
| 286 | + config->program_name, cfilename, err, strerror(err)); | ||
| 287 | 287 | PyMem_RawFree(cfilename_buffer); | |
| 288 | 288 | return 2; | |
| 289 | 289 | } | |
@@ -303,7 +303,7 @@ pymain_run_file(_PyCoreConfig *config, PyCompilerFlags *cf) | |||
| 303 | 303 | if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) { | |
| 304 | 304 | fprintf(stderr, | |
| 305 | 305 | "%ls: '%ls' is a directory, cannot continue\n", | |
| 306 | - config->program, filename); | ||
| 306 | + config->program_name, filename); | ||
| 307 | 307 | fclose(fp); | |
| 308 | 308 | return 1; | |
| 309 | 309 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -433,8 +433,6 @@ static int test_init_from_config(void) | |||
| 433 | 433 | config.argv.length = Py_ARRAY_LENGTH(argv); | |
| 434 | 434 | config.argv.items = argv; | |
| 435 | 435 | ||
| 436 | - config.program = L"conf_program"; | ||
| 437 | - | ||
| 438 | 436 | static wchar_t* xoptions[3] = { | |
| 439 | 437 | L"core_xoption1=3", | |
| 440 | 438 | L"core_xoption2=", | |
@@ -532,7 +530,6 @@ static int test_init_dont_parse_argv(void) | |||
| 532 | 530 | L"arg2", | |
| 533 | 531 | }; | |
| 534 | 532 | ||
| 535 | - config.program = L"program"; | ||
| 536 | 533 | config.program_name = L"./_testembed"; | |
| 537 | 534 | ||
| 538 | 535 | config.argv.length = Py_ARRAY_LENGTH(argv); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -492,7 +492,6 @@ _PyCoreConfig_Clear(_PyCoreConfig *config) | |||
| 492 | 492 | CLEAR(config->module_search_path_env); | |
| 493 | 493 | CLEAR(config->home); | |
| 494 | 494 | CLEAR(config->program_name); | |
| 495 | - CLEAR(config->program); | ||
| 496 | 495 | ||
| 497 | 496 | _PyWstrList_Clear(&config->argv); | |
| 498 | 497 | _PyWstrList_Clear(&config->warnoptions); | |
@@ -626,7 +625,6 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2) | |||
| 626 | 625 | COPY_WSTR_ATTR(module_search_path_env); | |
| 627 | 626 | COPY_WSTR_ATTR(home); | |
| 628 | 627 | COPY_WSTR_ATTR(program_name); | |
| 629 | - COPY_WSTR_ATTR(program); | ||
| 630 | 628 | ||
| 631 | 629 | COPY_ATTR(parse_argv); | |
| 632 | 630 | COPY_WSTRLIST(argv); | |
@@ -732,7 +730,6 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config) | |||
| 732 | 730 | SET_ITEM_WSTR(program_name); | |
| 733 | 731 | SET_ITEM_INT(parse_argv); | |
| 734 | 732 | SET_ITEM_WSTRLIST(argv); | |
| 735 | - SET_ITEM_WSTR(program); | ||
| 736 | 733 | SET_ITEM_WSTRLIST(xoptions); | |
| 737 | 734 | SET_ITEM_WSTRLIST(warnoptions); | |
| 738 | 735 | SET_ITEM_WSTR(module_search_path_env); | |
@@ -918,7 +915,6 @@ static _PyInitError | |||
| 918 | 915 | config_init_program_name(_PyCoreConfig *config) | |
| 919 | 916 | { | |
| 920 | 917 | _PyInitError err; | |
| 921 | - assert(config->program_name == NULL); | ||
| 922 | 918 | ||
| 923 | 919 | /* If Py_SetProgramName() was called, use its value */ | |
| 924 | 920 | const wchar_t *program_name = _Py_path_config.program_name; | |
@@ -967,16 +963,17 @@ config_init_program_name(_PyCoreConfig *config) | |||
| 967 | 963 | #endif /* WITH_NEXT_FRAMEWORK */ | |
| 968 | 964 | #endif /* __APPLE__ */ | |
| 969 | 965 | ||
| 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(); | ||
| 975 | 972 | } | |
| 976 | 973 | return _Py_INIT_OK(); | |
| 977 | 974 | } | |
| 978 | 975 | ||
| 979 | - /* Last fall back: hardcoded string */ | ||
| 976 | + /* Last fall back: hardcoded name */ | ||
| 980 | 977 | #ifdef MS_WINDOWS | |
| 981 | 978 | const wchar_t *default_program_name = L"python"; | |
| 982 | 979 | #else | |
@@ -1518,13 +1515,6 @@ config_read(_PyCoreConfig *config) | |||
| 1518 | 1515 | } | |
| 1519 | 1516 | } | |
| 1520 | 1517 | ||
| 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 | - | ||
| 1528 | 1518 | if (config->executable == NULL) { | |
| 1529 | 1519 | err = config_init_executable(config); | |
| 1530 | 1520 | if (_Py_INIT_FAILED(err)) { | |
@@ -1678,6 +1668,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyWstrList *warnoptions, | |||
| 1678 | 1668 | _PyInitError err; | |
| 1679 | 1669 | const _PyWstrList *argv = &config->argv; | |
| 1680 | 1670 | int print_version = 0; | |
| 1671 | + const wchar_t* program = config->program_name; | ||
| 1681 | 1672 | ||
| 1682 | 1673 | _PyOS_ResetGetOpt(); | |
| 1683 | 1674 | do { | |
@@ -1734,7 +1725,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyWstrList *warnoptions, | |||
| 1734 | 1725 | } else { | |
| 1735 | 1726 | fprintf(stderr, "--check-hash-based-pycs must be one of " | |
| 1736 | 1727 | "'default', 'always', or 'never'\n"); | |
| 1737 | - config_usage(1, config->program); | ||
| 1728 | + config_usage(1, program); | ||
| 1738 | 1729 | return _Py_INIT_EXIT(2); | |
| 1739 | 1730 | } | |
| 1740 | 1731 | break; | |
@@ -1794,7 +1785,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyWstrList *warnoptions, | |||
| 1794 | 1785 | ||
| 1795 | 1786 | case 'h': | |
| 1796 | 1787 | case '?': | |
| 1797 | - config_usage(0, config->program); | ||
| 1788 | + config_usage(0, program); | ||
| 1798 | 1789 | return _Py_INIT_EXIT(0); | |
| 1799 | 1790 | ||
| 1800 | 1791 | case 'V': | |
@@ -1819,7 +1810,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyWstrList *warnoptions, | |||
| 1819 | 1810 | ||
| 1820 | 1811 | default: | |
| 1821 | 1812 | /* unknown argument: parsing failed */ | |
| 1822 | - config_usage(1, config->program); | ||
| 1813 | + config_usage(1, program); | ||
| 1823 | 1814 | return _Py_INIT_EXIT(2); | |
| 1824 | 1815 | } | |
| 1825 | 1816 | } while (1); | |
@@ -1892,26 +1883,6 @@ config_init_env_warnoptions(const _PyCoreConfig *config, _PyWstrList *warnoption | |||
| 1892 | 1883 | } | |
| 1893 | 1884 | ||
| 1894 | 1885 | ||
| 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 | - | ||
| 1915 | 1886 | static int | |
| 1916 | 1887 | config_add_warnoption(_PyCoreConfig *config, const wchar_t *option) | |
| 1917 | 1888 | { | |
@@ -2084,10 +2055,10 @@ config_read_cmdline(_PyCoreConfig *config) | |||
| 2084 | 2055 | config->parse_argv = 1; | |
| 2085 | 2056 | } | |
| 2086 | 2057 | ||
| 2087 | - if (config->program == NULL) { | ||
| 2088 | - err = config_init_program(config); | ||
| 2058 | + if (config->program_name == NULL) { | ||
| 2059 | + err = config_init_program_name(config); | ||
| 2089 | 2060 | if (_Py_INIT_FAILED(err)) { | |
| 2090 | - goto done; | ||
| 2061 | + return err; | ||
| 2091 | 2062 | } | |
| 2092 | 2063 | } | |
| 2093 | 2064 | ||
@@ -2218,6 +2189,8 @@ _PyCoreConfig_Read(_PyCoreConfig *config) | |||
| 2218 | 2189 | goto done; | |
| 2219 | 2190 | } | |
| 2220 | 2191 | ||
| 2192 | + /* precmdline.argv is a copy of config.argv which is modified | ||
| 2193 | + by config_read_cmdline() */ | ||
| 2221 | 2194 | const _PyWstrList *argv = &precmdline.argv; | |
| 2222 | 2195 | if (_Py_SetArgcArgv(argv->length, argv->items) < 0) { | |
| 2223 | 2196 | err = _Py_INIT_NO_MEMORY(); | |
@@ -2246,7 +2219,6 @@ _PyCoreConfig_Read(_PyCoreConfig *config) | |||
| 2246 | 2219 | assert(config->configure_c_stdio >= 0); | |
| 2247 | 2220 | assert(config->buffered_stdio >= 0); | |
| 2248 | 2221 | assert(config->program_name != NULL); | |
| 2249 | - assert(config->program != NULL); | ||
| 2250 | 2222 | assert(_PyWstrList_CheckConsistency(&config->argv)); | |
| 2251 | 2223 | /* sys.argv must be non-empty: empty argv is replaced with [''] */ | |
| 2252 | 2224 | assert(config->argv.length >= 1); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments