]> granicus.if.org Git - python/commitdiff
Patch #449815: Set filesystemencoding based on CODESET.
authorMartin v. Löwis <martin@v.loewis.de>
Wed, 5 Sep 2001 17:09:48 +0000 (17:09 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Wed, 5 Sep 2001 17:09:48 +0000 (17:09 +0000)
Doc/api/api.tex
Lib/test/test_unicode_file.py
Modules/_localemodule.c

index a185509898bc357cc2180a642c3ab3a08300e8b2..b3a32d140d636fc30cd4a19dc2f6fc5830f46c66 100644 (file)
@@ -3020,7 +3020,11 @@ errors. These parameters encoding and errors have the same semantics
 as the ones of the builtin unicode() Unicode object constructor.
 
 Setting encoding to NULL causes the default encoding to be used which
-is UTF-8.
+is \ASCII{}. The file system calls should use
+\var{Py_FileSystemDefaultEncoding} as the encoding for file
+names. This variable should be treated as read-only: On some systems,
+it will be a pointer to a static string, on others, it will change at
+run-time, e.g. when the application invokes setlocale.
 
 Error handling is set by errors which may also be set to NULL meaning
 to use the default handling defined for the codec. Default error
index 707819727b736bfa91436d9ac12a850f23cb9bb2..8b5757cc692429141716d511b85d5206692d8bbe 100644 (file)
@@ -6,8 +6,20 @@ import os
 from test_support import verify, TestSkipped, TESTFN_UNICODE
 try:
     from test_support import TESTFN_ENCODING
+    oldlocale = None
 except ImportError:
-    raise TestSkipped("No Unicode filesystem semantics on this platform.")
+    import locale
+    # try to run the test in an UTF-8 locale. If this locale is not
+    # available, avoid running the test since the locale's encoding
+    # might not support TESTFN_UNICODE. Likewise, if the system does
+    # not support locale.CODESET, Unicode file semantics is not
+    # available, either.
+    oldlocale = locale.setlocale(locale.LC_CTYPE)
+    try:
+        locale.setlocale(locale.LC_CTYPE,"en_US.UTF-8")
+        TESTFN_ENCODING = locale.nl_langinfo(locale.CODESET)
+    except (locale.Error, AttributeError):
+        raise TestSkipped("No Unicode filesystem semantics on this platform.")
 
 TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
 
@@ -79,3 +91,5 @@ finally:
     os.chdir(cwd)
     os.rmdir(abs_encoded)
 print "All the Unicode tests appeared to work"
+if oldlocale:
+    locale.setlocale(locale.LC_CTYPE, oldlocale)
index 7f7bdd291c060d95c495885c1161df4ff11c14bf..a3a1d1297197a802caa4fdb3199058088e126bfc 100644 (file)
@@ -153,7 +153,10 @@ fixup_ulcase(void)
         PyDict_SetItemString(string, "letters", ulo);
     Py_DECREF(ulo);
 }
-  
+
+#if defined(HAVE_LANGINFO_H) && defined(CODESET)
+static int fileencoding_uses_locale = 0;
+#endif
 
 static PyObject*
 PyLocale_setlocale(PyObject* self, PyObject* args)
@@ -203,6 +206,22 @@ PyLocale_setlocale(PyObject* self, PyObject* args)
             fixup_ulcase();
         /* things that got wrong up to here are ignored */
         PyErr_Clear();
+#if defined(HAVE_LANGINFO_H) && defined(CODESET)
+       if (Py_FileSystemDefaultEncoding == NULL)
+           fileencoding_uses_locale = 1;
+       if (fileencoding_uses_locale) {
+           char *codeset = nl_langinfo(CODESET);
+           PyObject *enc = NULL;
+           if (*codeset && (enc = PyCodec_Encoder(codeset))) {
+               /* Release previous file encoding */
+               if (Py_FileSystemDefaultEncoding)
+                   free (Py_FileSystemDefaultEncoding);
+               Py_FileSystemDefaultEncoding = strdup(codeset);
+               Py_DECREF(enc);
+           } else
+               PyErr_Clear();
+       }
+#endif
     } else {
         /* get locale */
         /* restore LC_NUMERIC first, if appropriate */