Mercurial > cpython-lru_cache-weakref
diff Python/codecs.c @ 47496:2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Use faster PyUnicode_FromEncodedObject() for bytes/bytearray.decode().
Add new PyCodec_KnownEncoding() API.
Add new PyUnicode_AsDecodedUnicode() and PyUnicode_AsEncodedUnicode() APIs.
Add missing PyUnicode_AsDecodedObject() to unicodeobject.h
Fix punicode codec to also work on memoryviews.
| author | Marc-André Lemburg <mal@egenix.com> |
|---|---|
| date | Fri, 06 Jun 2008 12:18:17 +0000 |
| parents | 23ab586c427a |
| children | efef5fc45a9a |
line wrap: on
line diff
--- a/Python/codecs.c Fri Jun 06 11:49:51 2008 +0000 +++ b/Python/codecs.c Fri Jun 06 12:18:17 2008 +0000 @@ -183,6 +183,23 @@ return NULL; } +/* Codec registry encoding check API. */ + +int PyCodec_KnownEncoding(const char *encoding) +{ + PyObject *codecs; + + codecs = _PyCodec_Lookup(encoding); + if (!codecs) { + PyErr_Clear(); + return 0; + } + else { + Py_DECREF(codecs); + return 1; + } +} + static PyObject *args_tuple(PyObject *object, const char *errors) @@ -344,32 +361,20 @@ "encoder must return a tuple (object, integer)"); goto onError; } - v = PyTuple_GET_ITEM(result, 0); - if (PyByteArray_Check(v)) { - char msg[100]; - PyOS_snprintf(msg, sizeof(msg), - "encoder %s returned buffer instead of bytes", - encoding); - if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { - v = NULL; - goto onError; - } - v = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); - } - else if (PyBytes_Check(v)) - Py_INCREF(v); - else { - PyErr_SetString(PyExc_TypeError, - "encoding must return a tuple(bytes, integer)"); - v = NULL; - } + v = PyTuple_GET_ITEM(result,0); + Py_INCREF(v); /* We don't check or use the second (integer) entry. */ + Py_DECREF(args); + Py_DECREF(encoder); + Py_DECREF(result); + return v; + onError: Py_XDECREF(result); Py_XDECREF(args); Py_XDECREF(encoder); - return v; + return NULL; } /* Decode an object (usually a Python string) using the given encoding
