diff -r b65ae19bc42a Programs/python.c --- a/Programs/python.c Wed Dec 14 11:52:28 2016 +0100 +++ b/Programs/python.c Thu Dec 15 15:56:52 2016 +1000 @@ -22,7 +22,7 @@ /* We need a second copy, as Python might modify the first one. */ wchar_t **argv_copy2; int i, res; - char *oldloc; + char *oldloc, *ctype_loc; /* Force malloc() allocator to bootstrap Python */ (void)_PyMem_SetupAllocators("malloc"); @@ -49,7 +49,30 @@ return 1; } + /* Reconfigure the locale to the default for this process */ setlocale(LC_ALL, ""); + + /* When the LC_CTYPE category still claims to be using the C locale, + assume that's a configuration error and request C.UTF-8 instead. */ + ctype_loc = setlocale(LC_CTYPE, NULL); + if (ctype_loc != NULL && strcmp(ctype_loc, "C") == 0) { + const char *msg = + "Python detected LC_CTYPE=C. Setting LC_ALL & LANG to C.UTF-8.\n"; + fprintf(stderr, msg); + if (setenv("LC_ALL", "C.UTF-8", 1)) { + fprintf(stderr, "Failed to set LC_ALL\n"); + return 1; + } + if (setenv("LANG", "C.UTF-8", 1)) { + fprintf(stderr, "Failed to set LANG\n"); + return 1; + } + + /* Reconfigure with the overridden environment variables */ + setlocale(LC_ALL, ""); + } + + /* Convert from char to wchar_t based on the locale settings */ for (i = 0; i < argc; i++) { argv_copy[i] = Py_DecodeLocale(argv[i], NULL); if (!argv_copy[i]) {