summaryrefslogtreecommitdiff
path: root/Python/pythonrun.c
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2003-03-05 15:13:47 +0000
committerMartin v. Löwis <martin@v.loewis.de>2003-03-05 15:13:47 +0000
commitb530947d6145f981f1566a719a4fcd73c73f4951 (patch)
tree848eec2e0b1d577a9fbbe1c0a60721718f444c3b /Python/pythonrun.c
parentac88c1b50f3b68bbb2d874f018ca721025559a2a (diff)
downloadcpython-b530947d6145f981f1566a719a4fcd73c73f4951.tar.gz
Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
and not as a side effect of setlocale. Expose it as sys.getfilesystemencoding. Adjust test case.
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r--Python/pythonrun.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 908bc42f6e..62dfd9306d 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -17,6 +17,11 @@
#include <signal.h>
#endif
+#ifdef HAVE_LANGINFO_H
+#include <locale.h>
+#include <langinfo.h>
+#endif
+
#ifdef MS_WINDOWS
#undef BYTE
#include "windows.h"
@@ -181,6 +186,29 @@ Py_Initialize(void)
initsite(); /* Module site */
PyModule_WarningsModule = PyImport_ImportModule("warnings");
+
+#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
+ /* On Unix, set the file system encoding according to the
+ user's preference, if the CODESET names a well-known
+ Python codec, and Py_FileSystemDefaultEncoding isn't
+ initialized by other means. */
+ if (!Py_FileSystemDefaultEncoding) {
+ char *saved_locale = setlocale(LC_CTYPE, NULL);
+ char *codeset;
+ setlocale(LC_CTYPE, "");
+ codeset = nl_langinfo(CODESET);
+ PyObject *enc = NULL;
+ if (*codeset) {
+ enc = PyCodec_Encoder(codeset);
+ if (enc) {
+ Py_FileSystemDefaultEncoding = strdup(codeset);
+ Py_DECREF(enc);
+ } else
+ PyErr_Clear();
+ }
+ setlocale(LC_CTYPE, saved_locale);
+ }
+#endif
}
#ifdef COUNT_ALLOCS