Skip to content

Commit d64e8a7

Browse files
author
Victor Stinner
committed
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.
1 parent c5ee7f2 commit d64e8a7

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #9642: Fix filesystem encoding initialization: use the ANSI code page
14+
on Windows if the mbcs codec is not available, and fail with a fatal error if
15+
we cannot get the locale encoding (if nl_langinfo(CODESET) is not available)
16+
instead of using UTF-8.
17+
1318
- When a generator yields, do not retain the caller's exception state on the
1419
generator.
1520

Python/bltinmodule.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ int Py_HasFileSystemDefaultEncoding = 1;
2424
#elif defined(__APPLE__)
2525
const char *Py_FileSystemDefaultEncoding = "utf-8";
2626
int Py_HasFileSystemDefaultEncoding = 1;
27-
#elif defined(HAVE_LANGINFO_H) && defined(CODESET)
27+
#else
2828
const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */
2929
int Py_HasFileSystemDefaultEncoding = 0;
30-
#else
31-
const char *Py_FileSystemDefaultEncoding = "utf-8";
32-
int Py_HasFileSystemDefaultEncoding = 1;
3330
#endif
3431

3532
static PyObject *

Python/pythonrun.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,25 @@ get_codec_name(const char *encoding)
168168
return NULL;
169169
}
170170

171-
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
172171
static char*
173-
get_codeset(void)
172+
get_locale_encoding(void)
174173
{
174+
#ifdef MS_WINDOWS
175+
char codepage[100];
176+
PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
177+
return get_codec_name(codepage);
178+
#elif defined(HAVE_LANGINFO_H) && defined(CODESET)
175179
char* codeset = nl_langinfo(CODESET);
176180
if (!codeset || codeset[0] == '\0') {
177181
PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
178182
return NULL;
179183
}
180184
return get_codec_name(codeset);
181-
}
185+
#else
186+
PyErr_SetNone(PyExc_NotImplementedError);
187+
return NULL;
182188
#endif
189+
}
183190

184191
void
185192
Py_InitializeEx(int install_sigs)
@@ -746,24 +753,17 @@ static int
746753
initfsencoding(PyInterpreterState *interp)
747754
{
748755
PyObject *codec;
749-
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
750-
char *codeset = NULL;
751-
752-
if (Py_FileSystemDefaultEncoding == NULL) {
753-
/* On Unix, set the file system encoding according to the
754-
user's preference, if the CODESET names a well-known
755-
Python codec, and Py_FileSystemDefaultEncoding isn't
756-
initialized by other means. */
757-
codeset = get_codeset();
758-
if (codeset == NULL)
756+
757+
if (Py_FileSystemDefaultEncoding == NULL)
758+
{
759+
Py_FileSystemDefaultEncoding = get_locale_encoding();
760+
if (Py_FileSystemDefaultEncoding == NULL)
759761
Py_FatalError("Py_Initialize: Unable to get the locale encoding");
760762

761-
Py_FileSystemDefaultEncoding = codeset;
762763
Py_HasFileSystemDefaultEncoding = 0;
763764
interp->fscodec_initialized = 1;
764765
return 0;
765766
}
766-
#endif
767767

768768
/* the encoding is mbcs, utf-8 or ascii */
769769
codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy