From: Martin v. Löwis Date: Wed, 5 Sep 2001 17:09:48 +0000 (+0000) Subject: Patch #449815: Set filesystemencoding based on CODESET. X-Git-Tag: v2.2a3~77 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7c82a3e0fcde5b0d58bdfbb0aed6c0a245ade4bf;p=python Patch #449815: Set filesystemencoding based on CODESET. --- diff --git a/Doc/api/api.tex b/Doc/api/api.tex index a185509898..b3a32d140d 100644 --- a/Doc/api/api.tex +++ b/Doc/api/api.tex @@ -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 diff --git a/Lib/test/test_unicode_file.py b/Lib/test/test_unicode_file.py index 707819727b..8b5757cc69 100644 --- a/Lib/test/test_unicode_file.py +++ b/Lib/test/test_unicode_file.py @@ -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) diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c index 7f7bdd291c..a3a1d12971 100644 --- a/Modules/_localemodule.c +++ b/Modules/_localemodule.c @@ -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 */