Index: Python/import.c =================================================================== --- Python/import.c (révision 80925) +++ Python/import.c (copie de travail) @@ -1633,8 +1633,7 @@ if (!v) return NULL; if (PyUnicode_Check(v)) { - v = PyUnicode_AsEncodedString(v, - Py_FileSystemDefaultEncoding, NULL); + v = PyUnicode_EncodeFSDefault(v); if (v == NULL) return NULL; } @@ -2752,14 +2751,7 @@ char *subname; PyObject *submod; char *p; - if (!Py_FileSystemDefaultEncoding) { - item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item), - PyUnicode_GetSize(item), - NULL); - } else { - item8 = PyUnicode_AsEncodedString(item, - Py_FileSystemDefaultEncoding, NULL); - } + item8 = PyUnicode_EncodeFSDefault(item); if (!item8) { PyErr_SetString(PyExc_ValueError, "Cannot encode path item"); return 0; Index: Include/unicodeobject.h =================================================================== --- Include/unicodeobject.h (révision 80925) +++ Include/unicodeobject.h (copie de travail) @@ -187,6 +187,7 @@ # define PyUnicode_EncodeUTF16 PyUnicodeUCS2_EncodeUTF16 # define PyUnicode_EncodeUTF8 PyUnicodeUCS2_EncodeUTF8 # define PyUnicode_EncodeUnicodeEscape PyUnicodeUCS2_EncodeUnicodeEscape +# define PyUnicode_EncodeFSDefault PyUnicodeUCS2_EncodeFSDefault # define PyUnicode_Find PyUnicodeUCS2_Find # define PyUnicode_Format PyUnicodeUCS2_Format # define PyUnicode_FromEncodedObject PyUnicodeUCS2_FromEncodedObject @@ -286,6 +287,7 @@ # define PyUnicode_EncodeUTF16 PyUnicodeUCS4_EncodeUTF16 # define PyUnicode_EncodeUTF8 PyUnicodeUCS4_EncodeUTF8 # define PyUnicode_EncodeUnicodeEscape PyUnicodeUCS4_EncodeUnicodeEscape +# define PyUnicode_EncodeFSDefault PyUnicodeUCS4_EncodeFSDefault # define PyUnicode_Find PyUnicodeUCS4_Find # define PyUnicode_Format PyUnicodeUCS4_Format # define PyUnicode_FromEncodedObject PyUnicodeUCS4_FromEncodedObject @@ -1262,6 +1264,10 @@ Py_ssize_t size /* size */ ); +PyAPI_FUNC(PyObject*) PyUnicode_EncodeFSDefault( + PyObject *unicode + ); + /* --- Methods & Slots ---------------------------------------------------- These are capable of handling Unicode objects and strings on input Index: Objects/unicodeobject.c =================================================================== --- Objects/unicodeobject.c (révision 80925) +++ Objects/unicodeobject.c (copie de travail) @@ -1461,6 +1461,18 @@ return NULL; } +PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode) +{ + if (Py_FileSystemDefaultEncoding) + return PyUnicode_AsEncodedString(unicode, + Py_FileSystemDefaultEncoding, + "surrogateescape"); + else + return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), + PyUnicode_GET_SIZE(unicode), + "surrogateescape"); +} + PyObject *PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors) @@ -1638,9 +1650,7 @@ arg = PyUnicode_FromObject(arg); if (!arg) return 0; - output = PyUnicode_AsEncodedObject(arg, - Py_FileSystemDefaultEncoding, - "surrogateescape"); + output = PyUnicode_EncodeFSDefault(arg); Py_DECREF(arg); if (!output) return 0; Index: Modules/grpmodule.c =================================================================== --- Modules/grpmodule.c (révision 80925) +++ Modules/grpmodule.c (copie de travail) @@ -111,8 +111,7 @@ if (!PyArg_ParseTuple(args, "U:getgrnam", &arg)) return NULL; - if ((bytes = PyUnicode_AsEncodedString(arg, Py_FileSystemDefaultEncoding, - "surrogateescape")) == NULL) + if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) return NULL; if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1) goto out; Index: Modules/_tkinter.c =================================================================== --- Modules/_tkinter.c (révision 80925) +++ Modules/_tkinter.c (copie de travail) @@ -3147,9 +3147,7 @@ it also helps Tcl find its encodings. */ uexe = PyUnicode_FromWideChar(Py_GetProgramName(), -1); if (uexe) { - cexe = PyUnicode_AsEncodedString(uexe, - Py_FileSystemDefaultEncoding, - NULL); + cexe = PyUnicode_EncodeFSDefault(uexe); if (cexe) Tcl_FindExecutable(PyBytes_AsString(cexe)); Py_XDECREF(cexe); Index: Modules/pwdmodule.c =================================================================== --- Modules/pwdmodule.c (révision 80925) +++ Modules/pwdmodule.c (copie de travail) @@ -132,9 +132,7 @@ if (!PyArg_ParseTuple(args, "U:getpwnam", &arg)) return NULL; - if ((bytes = PyUnicode_AsEncodedString(arg, - Py_FileSystemDefaultEncoding, - "surrogateescape")) == NULL) + if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) return NULL; if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1) goto out; Index: Modules/spwdmodule.c =================================================================== --- Modules/spwdmodule.c (révision 80925) +++ Modules/spwdmodule.c (copie de travail) @@ -118,9 +118,7 @@ if (!PyArg_ParseTuple(args, "U:getspnam", &arg)) return NULL; - if ((bytes = PyUnicode_AsEncodedString(arg, - Py_FileSystemDefaultEncoding, - "surrogateescape")) == NULL) + if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) return NULL; if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1) goto out;