| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
Can you please apply this patch to add tests on PyUnicodeWriter_WriteRepr()? diff --git a/Lib/test/test_capi/test_unicode.py b/Lib/test/test_capi/test_unicode.py
index 9237809e7f0..0358bc38dfd 100644
--- a/Lib/test/test_capi/test_unicode.py
+++ b/Lib/test/test_capi/test_unicode.py
@@ -2038,6 +2038,21 @@ def __str__(self):
self.assertEqual(result, 'abc')
self.assertIs(type(result), str)
+ def test_repr(self):
+ # Test PyUnicodeWriter_WriteRepr()
+ writer = self.create_writer(0)
+ writer.write_str('str=')
+ writer.write_repr('abc')
+ writer.write_str(' int=')
+ writer.write_repr(123)
+ writer.write_str(' none=')
+ writer.write_repr_none()
+ obj = object()
+ writer.write_str(' obj=')
+ writer.write_repr(obj)
+ self.assertEqual(writer.finish(),
+ f"str='abc' int=123 none=None obj={obj!r}")
+
def test_repr_null(self):
writer = self.create_writer(0)
writer.write_utf8(b'var=', -1)
diff --git a/Modules/_testcapi/unicode.c b/Modules/_testcapi/unicode.c
index ec3e9a62351..19178b2977e 100644
--- a/Modules/_testcapi/unicode.c
+++ b/Modules/_testcapi/unicode.c
@@ -689,6 +689,23 @@ writer_write_repr(PyObject *self_raw, PyObject *obj)
}
+// write_repr(None) calls PyUnicodeWriter_WriteRepr(writer, NULL), so add
+// a specialized function calling PyUnicodeWriter_WriteRepr(writer, Py_None).
+static PyObject*
+writer_write_repr_none(PyObject *self_raw, PyObject *Py_UNUSED(arg))
+{
+ WriterObject *self = (WriterObject *)self_raw;
+ if (writer_check(self) < 0) {
+ return NULL;
+ }
+
+ if (PyUnicodeWriter_WriteRepr(self->writer, Py_None) < 0) {
+ return NULL;
+ }
+ Py_RETURN_NONE;
+}
+
+
static PyObject*
writer_write_substring(PyObject *self_raw, PyObject *args)
{
@@ -854,6 +871,7 @@ static PyMethodDef writer_methods[] = {
{"write_ucs4", _PyCFunction_CAST(writer_write_ucs4), METH_VARARGS},
{"write_str", _PyCFunction_CAST(writer_write_str), METH_O},
{"write_repr", _PyCFunction_CAST(writer_write_repr), METH_O},
+ {"write_repr_none", _PyCFunction_CAST(writer_write_repr_none), METH_NOARGS},
{"write_substring", _PyCFunction_CAST(writer_write_substring), METH_VARARGS},
{"write_latin1", _PyCFunction_CAST(writer_write_latin1), METH_VARARGS},
{"decodeutf8stateful", _PyCFunction_CAST(writer_decodeutf8stateful), METH_VARARGS}, |
Sorry, something went wrong.
Co-authored-by: Victor Stinner <vstinner@python.org>
There was a problem hiding this comment.
LGTM.
The implementation looks correct to me, and I think that it's worth it to optimize this use case according to statistics that you collected.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.