diff Python/pythonrun.c @ 28174:3f90d21debe9 legacy-trunk

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.
author Martin v. Löwis <martin@v.loewis.de>
date Wed, 05 Mar 2003 15:13:47 +0000
parents c61ef183ce2f
children 04df965d675b
line wrap: on
line diff
--- a/Python/pythonrun.c	Wed Mar 05 14:44:54 2003 +0000
+++ b/Python/pythonrun.c	Wed Mar 05 15:13:47 2003 +0000
@@ -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 @@
 		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