Commit d64e8a75 authored by Victor Stinner's avatar Victor Stinner

Issue #9642: Fix filesystem encoding initialization: use the ANSI code page on

Windows if the mbcs codec is not available, and fail with a fatal error if we
cannot get the locale encoding (if nl_langinfo(CODESET) is not available)
instead of using UTF-8.
parent c5ee7f21
...@@ -10,6 +10,11 @@ What's New in Python 3.3 Alpha 1? ...@@ -10,6 +10,11 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #9642: Fix filesystem encoding initialization: use the ANSI code page
on Windows if the mbcs codec is not available, and fail with a fatal error if
we cannot get the locale encoding (if nl_langinfo(CODESET) is not available)
instead of using UTF-8.
- When a generator yields, do not retain the caller's exception state on the - When a generator yields, do not retain the caller's exception state on the
generator. generator.
......
...@@ -24,12 +24,9 @@ int Py_HasFileSystemDefaultEncoding = 1; ...@@ -24,12 +24,9 @@ int Py_HasFileSystemDefaultEncoding = 1;
#elif defined(__APPLE__) #elif defined(__APPLE__)
const char *Py_FileSystemDefaultEncoding = "utf-8"; const char *Py_FileSystemDefaultEncoding = "utf-8";
int Py_HasFileSystemDefaultEncoding = 1; int Py_HasFileSystemDefaultEncoding = 1;
#elif defined(HAVE_LANGINFO_H) && defined(CODESET) #else
const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */ const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */
int Py_HasFileSystemDefaultEncoding = 0; int Py_HasFileSystemDefaultEncoding = 0;
#else
const char *Py_FileSystemDefaultEncoding = "utf-8";
int Py_HasFileSystemDefaultEncoding = 1;
#endif #endif
static PyObject * static PyObject *
......
...@@ -168,18 +168,25 @@ error: ...@@ -168,18 +168,25 @@ error:
return NULL; return NULL;
} }
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
static char* static char*
get_codeset(void) get_locale_encoding(void)
{ {
#ifdef MS_WINDOWS
char codepage[100];
PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
return get_codec_name(codepage);
#elif defined(HAVE_LANGINFO_H) && defined(CODESET)
char* codeset = nl_langinfo(CODESET); char* codeset = nl_langinfo(CODESET);
if (!codeset || codeset[0] == '\0') { if (!codeset || codeset[0] == '\0') {
PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty"); PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
return NULL; return NULL;
} }
return get_codec_name(codeset); return get_codec_name(codeset);
} #else
PyErr_SetNone(PyExc_NotImplementedError);
return NULL;
#endif #endif
}
void void
Py_InitializeEx(int install_sigs) Py_InitializeEx(int install_sigs)
...@@ -746,24 +753,17 @@ static int ...@@ -746,24 +753,17 @@ static int
initfsencoding(PyInterpreterState *interp) initfsencoding(PyInterpreterState *interp)
{ {
PyObject *codec; PyObject *codec;
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
char *codeset = NULL; if (Py_FileSystemDefaultEncoding == NULL)
{
if (Py_FileSystemDefaultEncoding == NULL) { Py_FileSystemDefaultEncoding = get_locale_encoding();
/* On Unix, set the file system encoding according to the if (Py_FileSystemDefaultEncoding == NULL)
user's preference, if the CODESET names a well-known
Python codec, and Py_FileSystemDefaultEncoding isn't
initialized by other means. */
codeset = get_codeset();
if (codeset == NULL)
Py_FatalError("Py_Initialize: Unable to get the locale encoding"); Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Py_FileSystemDefaultEncoding = codeset;
Py_HasFileSystemDefaultEncoding = 0; Py_HasFileSystemDefaultEncoding = 0;
interp->fscodec_initialized = 1; interp->fscodec_initialized = 1;
return 0; return 0;
} }
#endif
/* the encoding is mbcs, utf-8 or ascii */ /* the encoding is mbcs, utf-8 or ascii */
codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment