Index: Doc/library/sys.rst =================================================================== --- Doc/library/sys.rst (revision 66695) +++ Doc/library/sys.rst (working copy) @@ -578,7 +578,15 @@ :file:`/usr/include/dlfcn.h` using the :program:`h2py` script. Availability: Unix. +.. function:: setfilesystemencoding(enc) + Set the encoding used when converting Python strings to file names to *enc*. + By default, Python tries to determine the encoding it should use automatically + on Unix; on Windows, it avoids such conversion completely. This function can + be used when Python's determination of the encoding needs to be overwritten, + e.g. when not all file names on disk can be decoded using the encoding that + Python had chosen. + .. function:: setprofile(profilefunc) .. index:: Index: Python/bltinmodule.c =================================================================== --- Python/bltinmodule.c (revision 66695) +++ Python/bltinmodule.c (working copy) @@ -15,6 +15,7 @@ Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the values for Py_FileSystemDefaultEncoding! */ +static PyObject *file_system_encoding = NULL; #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) const char *Py_FileSystemDefaultEncoding = "mbcs"; const int Py_HasFileSystemDefaultEncoding = 1; @@ -26,6 +27,24 @@ const int Py_HasFileSystemDefaultEncoding = 0; #endif +int +_Py_SetFileSystemEncoding(PyObject *s) +{ + PyObject *defenc; + if (!PyUnicode_Check(s)) { + PyErr_BadInternalCall(); + return -1; + } + defenc = _PyUnicode_AsDefaultEncodedString(s, NULL); + if (!defenc) + return -1; + Py_INCREF(s); + Py_FileSystemDefaultEncoding = PyBytes_AsString(defenc); + Py_XDECREF(file_system_encoding); + file_system_encoding = s; + return 0; +} + static PyObject * builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) { Index: Python/sysmodule.c =================================================================== --- Python/sysmodule.c (revision 66695) +++ Python/sysmodule.c (working copy) @@ -216,7 +216,24 @@ operating system filenames." ); +static PyObject * +sys_setfilesystemencoding(PyObject *self, PyObject *args) +{ + PyObject *new_encoding; + if (!PyArg_ParseTuple(args, "U:setfilesystemencoding", &new_encoding)) + return NULL; + if (_Py_SetFileSystemEncoding(new_encoding)) + return NULL; + Py_INCREF(Py_None); + return Py_None; +} +PyDoc_STRVAR(setfilesystemencoding_doc, +"setfilesystemencoding(string) -> None\n\ +\n\ +Set the encoding used to convert Unicode filenames in\n\ +operating system filenames." +); static PyObject * sys_intern(PyObject *self, PyObject *args) @@ -872,6 +889,8 @@ #endif {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS, setdefaultencoding_doc}, + {"setfilesystemencoding", sys_setfilesystemencoding, METH_VARARGS, + setfilesystemencoding_doc}, {"setcheckinterval", sys_setcheckinterval, METH_VARARGS, setcheckinterval_doc}, {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,