diff options
author | Inada Naoki <songofacandy@gmail.com> | 2020-03-14 03:43:18 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-14 03:43:18 (GMT) |
commit | c7ad974d341d3edb6b9d2a2dcae4d3d4794ada6b (patch) | |
tree | 2026fd46b762fb2deaf9091e4d7e09dc198bc2d3 /Objects | |
parent | 8fb02b6e1942811c8d81041e7df3f5f1f4b1d410 (diff) | |
download | cpython-c7ad974d341d3edb6b9d2a2dcae4d3d4794ada6b.zip cpython-c7ad974d341d3edb6b9d2a2dcae4d3d4794ada6b.tar.gz cpython-c7ad974d341d3edb6b9d2a2dcae4d3d4794ada6b.tar.bz2 |
bpo-39087: Add _PyUnicode_GetUTF8Buffer() (GH-17659)
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 3d99f11..0fea435 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -3991,6 +3991,41 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr) } +int +_PyUnicode_GetUTF8Buffer(PyObject *unicode, const char *errors, + Py_buffer *view) +{ + if (!PyUnicode_Check(unicode)) { + PyErr_BadArgument(); + return -1; + } + if (PyUnicode_READY(unicode) == -1) { + return -1; + } + + if (PyUnicode_UTF8(unicode) != NULL + && Py_TYPE(unicode)->tp_as_buffer == NULL) { + return PyBuffer_FillInfo(view, unicode, + PyUnicode_UTF8(unicode), + PyUnicode_UTF8_LENGTH(unicode), + /* readonly */ 1, PyBUF_SIMPLE); + } + + // Unlike PyUnicode_AsUTF8AndSize(), this function doesn't + // create a UTF-8 cache for speed and efficiency. + PyObject *bytes = _PyUnicode_AsUTF8String(unicode, errors); + if (bytes == NULL) { + return -1; + } + assert(PyBytes_CheckExact(bytes)); + if (PyObject_GetBuffer(bytes, view, PyBUF_SIMPLE) < 0) { + Py_DECREF(bytes); + return -1; + } + return 0; +} + + static int unicode_fill_utf8(PyObject *unicode); const char * |