Skip to content

Commit 0c59c40

Browse files
vstinnerarun-mani-j
authored andcommitted
_PyPreConfig_Read() decodes argv at each iteration (pythonGH-20786)
_PyPreConfig_Read() now calls _PyPreCmdline_SetArgv() at each iteration, so bytes strings are decoded from the new encoding.
1 parent 5345673 commit 0c59c40

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Python/preconfig.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -829,13 +829,6 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
829829
int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
830830
#endif
831831

832-
if (args) {
833-
status = _PyPreCmdline_SetArgv(&cmdline, args);
834-
if (_PyStatus_EXCEPTION(status)) {
835-
goto done;
836-
}
837-
}
838-
839832
int locale_coerced = 0;
840833
int loops = 0;
841834

@@ -846,7 +839,7 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
846839
loops++;
847840
if (loops == 3) {
848841
status = _PyStatus_ERR("Encoding changed twice while "
849-
"reading the configuration");
842+
"reading the configuration");
850843
goto done;
851844
}
852845

@@ -857,6 +850,15 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
857850
Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
858851
#endif
859852

853+
if (args) {
854+
// Set command line arguments at each iteration. If they are bytes
855+
// strings, they are decoded from the new encoding.
856+
status = _PyPreCmdline_SetArgv(&cmdline, args);
857+
if (_PyStatus_EXCEPTION(status)) {
858+
goto done;
859+
}
860+
}
861+
860862
status = preconfig_read(config, &cmdline);
861863
if (_PyStatus_EXCEPTION(status)) {
862864
goto done;
@@ -896,7 +898,7 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
896898
}
897899

898900
/* Reset the configuration before reading again the configuration,
899-
just keep UTF-8 Mode value. */
901+
just keep UTF-8 Mode and coerce C locale value. */
900902
int new_utf8_mode = config->utf8_mode;
901903
int new_coerce_c_locale = config->coerce_c_locale;
902904
preconfig_copy(config, &save_config);

0 commit comments

Comments
 (0)