PyAPI: use PYTHONUTF8/Py_UTF8Mode on all platforms

System encoding issues have been a paint-point for us with Python 3,
since Blender always uses UTF-8 which might not be the case for the OS.

While the Py_SetStandardStreamEncoding was already set to utf-8,
the file-system could still have an incompatible encoding.

See PEP-540 for details.
This commit is contained in:
Campbell Barton 2021-01-28 09:27:08 +11:00
parent e285765a6b
commit df135b74fc
1 changed files with 14 additions and 6 deletions

View File

@ -341,12 +341,20 @@ void BPY_python_start(bContext *C, int argc, const char **argv)
}
}
/* Without this the `sys.stdout` may be set to 'ascii'
* (it is on my system at least), where printing unicode values will raise
* an error, this is highly annoying, another stumbling block for developers,
* so use a more relaxed error handler and enforce utf-8 since the rest of
* Blender is utf-8 too - campbell */
Py_SetStandardStreamEncoding("utf-8", "surrogateescape");
/* Force `utf-8` on all platforms, since this is what's used for Blender's internal strings,
* providing consistent encoding behavior across all Blender installations.
*
* This also uses the `surrogateescape` error handler ensures any unexpected bytes are escaped
* instead of raising an error.
*
* Without this `sys.getfilesystemencoding()` and `sys.stdout` for example may be set to ASCII
* or some other encoding - where printing some `utf-8` values will raise an error.
*
* This can cause scripts to fail entirely on some systems.
*
* This assignment is the equivalent of enabling the `PYTHONUTF8` environment variable.
* See `PEP-540` for details on exactly what this changes. */
Py_UTF8Mode = 1;
/* Suppress error messages when calculating the module search path.
* While harmless, it's noisy. */