diff --git a/docs/api.rst b/docs/api.rst index a8c4c6f..2681a4d 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -262,6 +262,7 @@ Not supported: * ``PyInitConfig_SetStrList()`` * ``PyType_GetBaseByToken()`` * ``PyUnicodeWriter_DecodeUTF8Stateful()`` +* ``PyUnicodeWriter_WriteUCS4()`` * ``Py_InitializeFromInitConfig()`` diff --git a/pythoncapi_compat.h b/pythoncapi_compat.h index 99970f6..08b6915 100644 --- a/pythoncapi_compat.h +++ b/pythoncapi_compat.h @@ -1425,6 +1425,11 @@ PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj) static inline int PyUnicodeWriter_WriteRepr(PyUnicodeWriter *writer, PyObject *obj) { + if (obj == NULL) { + return _PyUnicodeWriter_WriteASCIIString((_PyUnicodeWriter*)writer, + "", 6); + } + PyObject *str = PyObject_Repr(obj); if (str == NULL) { return -1; diff --git a/tests/test_pythoncapi_compat_cext.c b/tests/test_pythoncapi_compat_cext.c index 19db02e..1074266 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -1908,13 +1908,17 @@ test_unicodewriter(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) goto error; } Py_CLEAR(str); + if (PyUnicodeWriter_WriteRepr(writer, NULL) < 0) { + goto error; + } { PyObject *result = PyUnicodeWriter_Finish(writer); if (result == NULL) { return NULL; } - assert(PyUnicode_EqualToUTF8(result, "var=long non-ASCII valu\xC3\xA9 'repr'")); + const char *expected = "var=long non-ASCII valu\xC3\xA9 'repr'"; + assert(PyUnicode_EqualToUTF8(result, expected)); Py_DECREF(result); }