Patch #449815: Set filesystemencoding based on CODESET.

This commit is contained in:
Martin v. Löwis 2001-09-05 17:09:48 +00:00
parent 044d95e9f7
commit 7c82a3e0fc
3 changed files with 40 additions and 3 deletions

View 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

View 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)

View 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 */