FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159813: Add a fast path for None in PyUnicodeWriter_WriteRepr by corona10 · Pull Request #159814 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
15 changes: 15 additions & 0 deletions Lib/test/test_capi/test_unicode.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add a fast path for :const:`None` in :c:func:`PyUnicodeWriter_WriteRepr`.
Patch by Donghee Na.
18 changes: 18 additions & 0 deletions Modules/_testcapi/unicode.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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},
Expand Down
4 changes: 4 additions & 0 deletions Objects/unicode_writer.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ PyUnicodeWriter_WriteRepr(PyUnicodeWriter *pub_writer, PyObject *obj)
if (Py_TYPE(obj) == &PyLong_Type) {
return _PyLong_FormatWriter(writer, obj, 10, 0);
}
// Fast path for None; see gh-159813 for the rationale.
if (obj == Py_None) {
return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
}

PyObject *repr = PyObject_Repr(obj);
if (repr == NULL) {
Expand Down
Loading

Back | FazBrowse Home | New Git URL