diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2003-03-05 15:13:47 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2003-03-05 15:13:47 (GMT) |
commit | 73d538b9c6d2f262a7d615248c84e509ebd64731 (patch) | |
tree | 777d023ce5bb75fa1831d4815533fd085a62b0b7 /Python | |
parent | 620c0837bd4bcc7253cf402c553a50f64e5b9619 (diff) | |
download | cpython-73d538b9c6d2f262a7d615248c84e509ebd64731.zip cpython-73d538b9c6d2f262a7d615248c84e509ebd64731.tar.gz cpython-73d538b9c6d2f262a7d615248c84e509ebd64731.tar.bz2 |
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')
-rw-r--r-- | Python/pythonrun.c | 28 | ||||
-rw-r--r-- | Python/sysmodule.c | 20 |
2 files changed, 48 insertions, 0 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 908bc42..62dfd93 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 diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 13b86f2..fa7f3c4 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -236,6 +236,22 @@ PyDoc_STRVAR(setdefaultencoding_doc, Set the current default string encoding used by the Unicode implementation." ); +static PyObject * +sys_getfilesystemencoding(PyObject *self) +{ + if (Py_FileSystemDefaultEncoding) + return PyString_FromString(Py_FileSystemDefaultEncoding); + Py_INCREF(Py_None); + return Py_None; +} + +PyDoc_STRVAR(getfilesystemencoding_doc, +"getfilesystemencoding() -> string\n\ +\n\ +Return the encoding used to convert Unicode filenames in\n\ +operating system filenames." +); + #endif /* @@ -649,6 +665,10 @@ static PyMethodDef sys_methods[] = { #ifdef DYNAMIC_EXECUTION_PROFILE {"getdxp", _Py_GetDXProfile, METH_VARARGS}, #endif +#ifdef Py_USING_UNICODE + {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding, + METH_NOARGS, getfilesystemencoding_doc}, +#endif #ifdef Py_TRACE_REFS {"getobjects", _Py_GetObjects, METH_VARARGS}, #endif |