Skip to content

Commit

Permalink
pythongh-125196: Use PyUnicodeWriter in HAMT
Browse files Browse the repository at this point in the history
Replace the private _PyUnicodeWriter API with the public
PyUnicodeWriter API in hamt.c.

Remove _hamt_dump_format(): replace it with PyUnicodeWriter_Format().

Use PyUnicodeWriter_WriteUTF8() to write just a newline character.
  • Loading branch information
vstinner committed Oct 14, 2024
1 parent 5f4e5b5 commit df1de4e
Showing 1 changed file with 27 additions and 49 deletions.
76 changes: 27 additions & 49 deletions Python/hamt.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ hamt_node_find(PyHamtNode *node,
#ifdef Py_DEBUG
static int
hamt_node_dump(PyHamtNode *node,
_PyUnicodeWriter *writer, int level);
PyUnicodeWriter *writer, int level);
#endif

static PyHamtNode *
Expand Down Expand Up @@ -444,7 +444,7 @@ hamt_bitindex(uint32_t bitmap, uint32_t bit)
#ifdef Py_DEBUG

static int
_hamt_dump_ident(_PyUnicodeWriter *writer, int level)
_hamt_dump_ident(PyUnicodeWriter *writer, int level)
{
/* Write `' ' * level` to the `writer` */
PyObject *str = NULL;
Expand All @@ -467,7 +467,7 @@ _hamt_dump_ident(_PyUnicodeWriter *writer, int level)
goto error;
}

ret = _PyUnicodeWriter_WriteStr(writer, res);
ret = PyUnicodeWriter_WriteStr(writer, res);

error:
Py_XDECREF(res);
Expand All @@ -476,29 +476,6 @@ _hamt_dump_ident(_PyUnicodeWriter *writer, int level)
return ret;
}

static int
_hamt_dump_format(_PyUnicodeWriter *writer, const char *format, ...)
{
/* A convenient helper combining _PyUnicodeWriter_WriteStr and
PyUnicode_FromFormatV.
*/
PyObject* msg;
int ret;

va_list vargs;
va_start(vargs, format);
msg = PyUnicode_FromFormatV(format, vargs);
va_end(vargs);

if (msg == NULL) {
return -1;
}

ret = _PyUnicodeWriter_WriteStr(writer, msg);
Py_DECREF(msg);
return ret;
}

#endif /* Py_DEBUG */
/////////////////////////////////// Bitmap Node

Expand Down Expand Up @@ -1154,7 +1131,7 @@ hamt_node_bitmap_dealloc(PyHamtNode_Bitmap *self)
#ifdef Py_DEBUG
static int
hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
_PyUnicodeWriter *writer, int level)
PyUnicodeWriter *writer, int level)
{
/* Debug build: __dump__() method implementation for Bitmap nodes. */

Expand All @@ -1166,8 +1143,8 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
goto error;
}

if (_hamt_dump_format(writer, "BitmapNode(size=%zd count=%zd ",
Py_SIZE(node), Py_SIZE(node) / 2))
if (PyUnicodeWriter_Format(writer, "BitmapNode(size=%zd count=%zd ",
Py_SIZE(node), Py_SIZE(node) / 2))
{
goto error;
}
Expand All @@ -1181,7 +1158,7 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
if (tmp2 == NULL) {
goto error;
}
if (_hamt_dump_format(writer, "bitmap=%S id=%p):\n", tmp2, node)) {
if (PyUnicodeWriter_Format(writer, "bitmap=%S id=%p):\n", tmp2, node)) {
Py_DECREF(tmp2);
goto error;
}
Expand All @@ -1196,7 +1173,7 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
}

if (key_or_null == NULL) {
if (_hamt_dump_format(writer, "NULL:\n")) {
if (PyUnicodeWriter_Format(writer, "NULL:\n")) {
goto error;
}

Expand All @@ -1207,14 +1184,14 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
}
}
else {
if (_hamt_dump_format(writer, "%R: %R", key_or_null,
val_or_node))
if (PyUnicodeWriter_Format(writer, "%R: %R",
key_or_null, val_or_node))
{
goto error;
}
}

if (_hamt_dump_format(writer, "\n")) {
if (PyUnicodeWriter_WriteUTF8(writer, "\n", 1)) {
goto error;
}
}
Expand Down Expand Up @@ -1548,7 +1525,7 @@ hamt_node_collision_dealloc(PyHamtNode_Collision *self)
#ifdef Py_DEBUG
static int
hamt_node_collision_dump(PyHamtNode_Collision *node,
_PyUnicodeWriter *writer, int level)
PyUnicodeWriter *writer, int level)
{
/* Debug build: __dump__() method implementation for Collision nodes. */

Expand All @@ -1558,7 +1535,7 @@ hamt_node_collision_dump(PyHamtNode_Collision *node,
goto error;
}

if (_hamt_dump_format(writer, "CollisionNode(size=%zd id=%p):\n",
if (PyUnicodeWriter_Format(writer, "CollisionNode(size=%zd id=%p):\n",
Py_SIZE(node), node))
{
goto error;
Expand All @@ -1572,7 +1549,7 @@ hamt_node_collision_dump(PyHamtNode_Collision *node,
goto error;
}

if (_hamt_dump_format(writer, "%R: %R\n", key, val)) {
if (PyUnicodeWriter_Format(writer, "%R: %R\n", key, val)) {
goto error;
}
}
Expand Down Expand Up @@ -1924,7 +1901,7 @@ hamt_node_array_dealloc(PyHamtNode_Array *self)
#ifdef Py_DEBUG
static int
hamt_node_array_dump(PyHamtNode_Array *node,
_PyUnicodeWriter *writer, int level)
PyUnicodeWriter *writer, int level)
{
/* Debug build: __dump__() method implementation for Array nodes. */

Expand All @@ -1934,7 +1911,7 @@ hamt_node_array_dump(PyHamtNode_Array *node,
goto error;
}

if (_hamt_dump_format(writer, "ArrayNode(id=%p):\n", node)) {
if (PyUnicodeWriter_Format(writer, "ArrayNode(id=%p):\n", node)) {
goto error;
}

Expand All @@ -1947,15 +1924,15 @@ hamt_node_array_dump(PyHamtNode_Array *node,
goto error;
}

if (_hamt_dump_format(writer, "%zd::\n", i)) {
if (PyUnicodeWriter_Format(writer, "%zd::\n", i)) {
goto error;
}

if (hamt_node_dump(node->a_array[i], writer, level + 1)) {
goto error;
}

if (_hamt_dump_format(writer, "\n")) {
if (PyUnicodeWriter_WriteUTF8(writer, "\n", 1)) {
goto error;
}
}
Expand Down Expand Up @@ -2071,7 +2048,7 @@ hamt_node_find(PyHamtNode *node,
#ifdef Py_DEBUG
static int
hamt_node_dump(PyHamtNode *node,
_PyUnicodeWriter *writer, int level)
PyUnicodeWriter *writer, int level)
{
/* Debug build: __dump__() method implementation for a node.
Expand Down Expand Up @@ -2440,22 +2417,23 @@ _PyHamt_New(void)
static PyObject *
hamt_dump(PyHamtObject *self)
{
_PyUnicodeWriter writer;

_PyUnicodeWriter_Init(&writer);
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
if (writer == NULL) {
return NULL;
}

if (_hamt_dump_format(&writer, "HAMT(len=%zd):\n", self->h_count)) {
if (PyUnicodeWriter_Format(writer, "HAMT(len=%zd):\n", self->h_count)) {
goto error;
}

if (hamt_node_dump(self->h_root, &writer, 0)) {
if (hamt_node_dump(self->h_root, writer, 0)) {
goto error;
}

return _PyUnicodeWriter_Finish(&writer);
return PyUnicodeWriter_Finish(writer);

error:
_PyUnicodeWriter_Dealloc(&writer);
PyUnicodeWriter_Discard(writer);
return NULL;
}
#endif /* Py_DEBUG */
Expand Down

0 comments on commit df1de4e

Please sign in to comment.