Issue #8715: Create PyUnicode_EncodeFSDefault() function: Encode a Unicode

object to Py_FileSystemDefaultEncoding with the "surrogateescape" error
handler, return a bytes object. If Py_FileSystemDefaultEncoding is not set,
fall back to UTF-8.
This commit is contained in:
Victor Stinner 2010-05-15 16:27:27 +00:00
parent 59e62db0a3
commit ae6265f8d0
10 changed files with 46 additions and 26 deletions

View file

@ -1461,6 +1461,18 @@ PyObject *PyUnicode_AsEncodedObject(PyObject *unicode,
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)
@ -1646,9 +1658,7 @@ PyUnicode_FSConverter(PyObject* arg, void* addr)
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;