mirror of
https://github.com/python/cpython.git
synced 2025-10-17 04:08:28 +00:00
Issue #9542: Create PyUnicode_FSDecoder() function
It's a ParseTuple converter: decode bytes objects to unicode using PyUnicode_DecodeFSDefaultAndSize(); str objects are output as-is. * Don't specify surrogateescape error handler in the comments nor the documentation, but PyUnicode_DecodeFSDefaultAndSize() and PyUnicode_EncodeFSDefault() because these functions use strict error handler for the mbcs encoding (on Windows). * Remove PyUnicode_FSConverter() comment in unicodeobject.c to avoid inconsistency with unicodeobject.h.
This commit is contained in:
parent
f2e08b34f1
commit
47fcb5b4c3
4 changed files with 70 additions and 10 deletions
|
@ -1652,9 +1652,6 @@ PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
|
|||
}
|
||||
}
|
||||
|
||||
/* Convert the argument to a bytes object, according to the file
|
||||
system encoding. The addr param must be a PyObject**.
|
||||
This is designed to be used with "O&" in PyArg_Parse APIs. */
|
||||
|
||||
int
|
||||
PyUnicode_FSConverter(PyObject* arg, void* addr)
|
||||
|
@ -1696,6 +1693,47 @@ PyUnicode_FSConverter(PyObject* arg, void* addr)
|
|||
}
|
||||
|
||||
|
||||
int
|
||||
PyUnicode_FSDecoder(PyObject* arg, void* addr)
|
||||
{
|
||||
PyObject *output = NULL;
|
||||
Py_ssize_t size;
|
||||
void *data;
|
||||
if (arg == NULL) {
|
||||
Py_DECREF(*(PyObject**)addr);
|
||||
return 1;
|
||||
}
|
||||
if (PyUnicode_Check(arg)) {
|
||||
output = arg;
|
||||
Py_INCREF(output);
|
||||
}
|
||||
else {
|
||||
arg = PyBytes_FromObject(arg);
|
||||
if (!arg)
|
||||
return 0;
|
||||
output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg),
|
||||
PyBytes_GET_SIZE(arg));
|
||||
Py_DECREF(arg);
|
||||
if (!output)
|
||||
return 0;
|
||||
if (!PyUnicode_Check(output)) {
|
||||
Py_DECREF(output);
|
||||
PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
size = PyUnicode_GET_SIZE(output);
|
||||
data = PyUnicode_AS_UNICODE(output);
|
||||
if (size != Py_UNICODE_strlen(data)) {
|
||||
PyErr_SetString(PyExc_TypeError, "embedded NUL character");
|
||||
Py_DECREF(output);
|
||||
return 0;
|
||||
}
|
||||
*(PyObject**)addr = output;
|
||||
return Py_CLEANUP_SUPPORTED;
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
_PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue