-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
GH-94526: Force utf8 encoding in _bootstrap_python #96889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
35b374a
2a9efac
b45af78
b5dba1c
1622292
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,15 @@ main(int argc, char **argv) | |
PyStatus status; | ||
|
||
PyConfig config; | ||
PyPreConfig preconfig; | ||
PyPreConfig_InitIsolatedConfig(&preconfig); | ||
// Force utf8 encoding | ||
preconfig.utf8_mode = 1; | ||
status = Py_PreInitialize(&preconfig); | ||
if (PyStatus_Exception(status)) { | ||
Py_ExitStatusException(status); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tl;dr this PR initializes the preconfig differently, but that might be okay (or even what we actually want). FWIW, there is a subtle difference here. analysis:before:
after:
The key difference is how the preconfig is initialized:
(When the preconfig is initialized is also different, but that doesn't really affect the outcome.) Consequently, If we wanted the how to be strictly equivalent, we'd initialize the preconfig after the config: PyConfig config;
PyConfig_InitIsolatedConfig(&config);
// don't warn, pybuilddir.txt does not exist yet
config.pathconfig_warnings = 0;
// parse arguments
config.parse_argv = 1;
// add current script dir to sys.path
config.isolated = 0;
config.safe_path = 0;
#ifdef MS_WINDOWS
status = PyConfig_SetArgv(&config, argc, argv);
#else
status = PyConfig_SetBytesArgv(&config, argc, argv);
#endif
if (PyStatus_Exception(status)) {
goto error;
}
PyPreConfig preconfig;
_PyPreConfig_InitFromConfig(&preconfig, &config);
// Force utf8 encoding
preconfig.utf8_mode = 1;
_PyArgv config_args = {
.use_bytes_argv = 0,
.argc = config->argv.length,
.wchar_argv = config->argv.items};
status = _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
if (PyStatus_Exception(status)) {
Py_ExitStatusException(status);
}
status = PyConfig_Read(&config); All that said, it isn't clear that difference in runtime init is bad. In fact, it might be what we want for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW for what I did this PR mostly by reading the source code and in particular this example from the PEP. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You must not and cannot pre-initialize (PyPreConfig) Python once it's already initialized (PyConfig). PyConfig_Read() does not pre-initialize Python if it's already pre-initialized. (If it's the case, it's would a bug.) PyStatus
_Py_PreInitializeFromConfig(const PyConfig *config,
const _PyArgv *args)
{
...
if (runtime->preinitialized) {
/* Already initialized: do nothing */
return _PyStatus_OK();
}
...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn 7333 more. Calling PyConfig_SetArgv() first to then call _Py_PreInitializeFromPyArgv() doesn't work, since PyConfig_SetArgv() does pre-initialize Python :-) https://docs.python.org/dev/c-api/init_config.html#c.PyConfig.PyConfig_SetArgv |
||
|
||
kumaraditya303 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyConfig_InitIsolatedConfig(&config); | ||
// don't warn, pybuilddir.txt does not exist yet | ||
config.pathconfig_warnings = 0; | ||
|
Uh oh!
There was an error while loading. Please reload this page.