summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Doc/c-api/unicode.rst4
-rw-r--r--Modules/_testcapi/unicode.c2
-rw-r--r--Objects/unicodeobject.c9
3 files changed, 11 insertions, 4 deletions
diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst
index d17e63d..5fa3796 100644
--- a/Doc/c-api/unicode.rst
+++ b/Doc/c-api/unicode.rst
@@ -971,8 +971,8 @@ These are the UTF-8 codec APIs:
returned buffer always has an extra null byte appended (not included in
*size*), regardless of whether there are any other null code points.
- In the case of an error, ``NULL`` is returned with an exception set and no
- *size* is stored.
+ On error, set an exception, set *size* to ``-1`` (if it's not NULL) and
+ return ``NULL``.
This caches the UTF-8 representation of the string in the Unicode object, and
subsequent calls will return a pointer to the same buffer. The caller is not
diff --git a/Modules/_testcapi/unicode.c b/Modules/_testcapi/unicode.c
index d52d88a..a10183d 100644
--- a/Modules/_testcapi/unicode.c
+++ b/Modules/_testcapi/unicode.c
@@ -634,7 +634,7 @@ unicode_asutf8andsize(PyObject *self, PyObject *args)
NULLABLE(unicode);
s = PyUnicode_AsUTF8AndSize(unicode, &size);
if (s == NULL) {
- assert(size == UNINITIALIZED_SIZE);
+ assert(size == -1);
return NULL;
}
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 07d1b6e..80b1956 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -3820,17 +3820,24 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
{
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
+ if (psize) {
+ *psize = -1;
+ }
return NULL;
}
if (PyUnicode_UTF8(unicode) == NULL) {
if (unicode_fill_utf8(unicode) == -1) {
+ if (psize) {
+ *psize = -1;
+ }
return NULL;
}
}
- if (psize)
+ if (psize) {
*psize = PyUnicode_UTF8_LENGTH(unicode);
+ }
return PyUnicode_UTF8(unicode);
}