Skip to content

Commit 974e343

Browse files
committed
gh-125196: Use PyUnicodeWriter in HAMT
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 constant strings.
1 parent 5f4e5b5 commit 974e343

File tree

1 file changed

+30
-50
lines changed

1 file changed

+30
-50
lines changed

Python/hamt.c

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ hamt_node_find(PyHamtNode *node,
349349
#ifdef Py_DEBUG
350350
static int
351351
hamt_node_dump(PyHamtNode *node,
352-
_PyUnicodeWriter *writer, int level);
352+
PyUnicodeWriter *writer, int level);
353353
#endif
354354

355355
static PyHamtNode *
@@ -444,7 +444,7 @@ hamt_bitindex(uint32_t bitmap, uint32_t bit)
444444
#ifdef Py_DEBUG
445445

446446
static int
447-
_hamt_dump_ident(_PyUnicodeWriter *writer, int level)
447+
_hamt_dump_ident(PyUnicodeWriter *writer, int level)
448448
{
449449
/* Write `' ' * level` to the `writer` */
450450
PyObject *str = NULL;
@@ -467,7 +467,7 @@ _hamt_dump_ident(_PyUnicodeWriter *writer, int level)
467467
goto error;
468468
}
469469

470-
ret = _PyUnicodeWriter_WriteStr(writer, res);
470+
ret = PyUnicodeWriter_WriteStr(writer, res);
471471

472472
error:
473473
Py_XDECREF(res);
@@ -476,29 +476,6 @@ _hamt_dump_ident(_PyUnicodeWriter *writer, int level)
476476
return ret;
477477
}
478478

479-
static int
480-
_hamt_dump_format(_PyUnicodeWriter *writer, const char *format, ...)
481-
{
482-
/* A convenient helper combining _PyUnicodeWriter_WriteStr and
483-
PyUnicode_FromFormatV.
484-
*/
485-
PyObject* msg;
486-
int ret;
487-
488-
va_list vargs;
489-
va_start(vargs, format);
490-
msg = PyUnicode_FromFormatV(format, vargs);
491-
va_end(vargs);
492-
493-
if (msg == NULL) {
494-
return -1;
495-
}
496-
497-
ret = _PyUnicodeWriter_WriteStr(writer, msg);
498-
Py_DECREF(msg);
499-
return ret;
500-
}
501-
502479
#endif /* Py_DEBUG */
503480
/////////////////////////////////// Bitmap Node
504481

@@ -1154,7 +1131,7 @@ hamt_node_bitmap_dealloc(PyHamtNode_Bitmap *self)
11541131
#ifdef Py_DEBUG
11551132
static int
11561133
hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
1157-
_PyUnicodeWriter *writer, int level)
1134+
PyUnicodeWriter *writer, int level)
11581135
{
11591136
/* Debug build: __dump__() method implementation for Bitmap nodes. */
11601137

@@ -1166,8 +1143,8 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
11661143
goto error;
11671144
}
11681145

1169-
if (_hamt_dump_format(writer, "BitmapNode(size=%zd count=%zd ",
1170-
Py_SIZE(node), Py_SIZE(node) / 2))
1146+
if (PyUnicodeWriter_Format(writer, "BitmapNode(size=%zd count=%zd ",
1147+
Py_SIZE(node), Py_SIZE(node) / 2) < 0)
11711148
{
11721149
goto error;
11731150
}
@@ -1181,7 +1158,8 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
11811158
if (tmp2 == NULL) {
11821159
goto error;
11831160
}
1184-
if (_hamt_dump_format(writer, "bitmap=%S id=%p):\n", tmp2, node)) {
1161+
if (PyUnicodeWriter_Format(writer, "bitmap=%S id=%p):\n",
1162+
tmp2, node) < 0) {
11851163
Py_DECREF(tmp2);
11861164
goto error;
11871165
}
@@ -1196,7 +1174,7 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
11961174
}
11971175

11981176
if (key_or_null == NULL) {
1199-
if (_hamt_dump_format(writer, "NULL:\n")) {
1177+
if (PyUnicodeWriter_WriteUTF8(writer, "NULL:\n", -1) < 0) {
12001178
goto error;
12011179
}
12021180

@@ -1207,14 +1185,14 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
12071185
}
12081186
}
12091187
else {
1210-
if (_hamt_dump_format(writer, "%R: %R", key_or_null,
1211-
val_or_node))
1188+
if (PyUnicodeWriter_Format(writer, "%R: %R",
1189+
key_or_null, val_or_node) < 0)
12121190
{
12131191
goto error;
12141192
}
12151193
}
12161194

1217-
if (_hamt_dump_format(writer, "\n")) {
1195+
if (PyUnicodeWriter_WriteUTF8(writer, "\n", 1) < 0) {
12181196
goto error;
12191197
}
12201198
}
@@ -1548,7 +1526,7 @@ hamt_node_collision_dealloc(PyHamtNode_Collision *self)
15481526
#ifdef Py_DEBUG
15491527
static int
15501528
hamt_node_collision_dump(PyHamtNode_Collision *node,
1551-
_PyUnicodeWriter *writer, int level)
1529+
PyUnicodeWriter *writer, int level)
15521530
{
15531531
/* Debug build: __dump__() method implementation for Collision nodes. */
15541532

@@ -1558,8 +1536,8 @@ hamt_node_collision_dump(PyHamtNode_Collision *node,
15581536
goto error;
15591537
}
15601538

1561-
if (_hamt_dump_format(writer, "CollisionNode(size=%zd id=%p):\n",
1562-
Py_SIZE(node), node))
1539+
if (PyUnicodeWriter_Format(writer, "CollisionNode(size=%zd id=%p):\n",
1540+
Py_SIZE(node), node) < 0)
15631541
{
15641542
goto error;
15651543
}
@@ -1572,7 +1550,7 @@ hamt_node_collision_dump(PyHamtNode_Collision *node,
15721550
goto error;
15731551
}
15741552

1575-
if (_hamt_dump_format(writer, "%R: %R\n", key, val)) {
1553+
if (PyUnicodeWriter_Format(writer, "%R: %R\n", key, val) < 0) {
15761554
goto error;
15771555
}
15781556
}
@@ -1924,7 +1902,7 @@ hamt_node_array_dealloc(PyHamtNode_Array *self)
19241902
#ifdef Py_DEBUG
19251903
static int
19261904
hamt_node_array_dump(PyHamtNode_Array *node,
1927-
_PyUnicodeWriter *writer, int level)
1905+
PyUnicodeWriter *writer, int level)
19281906
{
19291907
/* Debug build: __dump__() method implementation for Array nodes. */
19301908

@@ -1934,7 +1912,7 @@ hamt_node_array_dump(PyHamtNode_Array *node,
19341912
goto error;
19351913
}
19361914

1937-
if (_hamt_dump_format(writer, "ArrayNode(id=%p):\n", node)) {
1915+
if (PyUnicodeWriter_Format(writer, "ArrayNode(id=%p):\n", node) < 0) {
19381916
goto error;
19391917
}
19401918

@@ -1947,15 +1925,15 @@ hamt_node_array_dump(PyHamtNode_Array *node,
19471925
goto error;
19481926
}
19491927

1950-
if (_hamt_dump_format(writer, "%zd::\n", i)) {
1928+
if (PyUnicodeWriter_Format(writer, "%zd::\n", i) < 0) {
19511929
goto error;
19521930
}
19531931

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

1958-
if (_hamt_dump_format(writer, "\n")) {
1936+
if (PyUnicodeWriter_WriteUTF8(writer, "\n", 1) < 0) {
19591937
goto error;
19601938
}
19611939
}
@@ -2071,7 +2049,7 @@ hamt_node_find(PyHamtNode *node,
20712049
#ifdef Py_DEBUG
20722050
static int
20732051
hamt_node_dump(PyHamtNode *node,
2074-
_PyUnicodeWriter *writer, int level)
2052+
PyUnicodeWriter *writer, int level)
20752053
{
20762054
/* Debug build: __dump__() method implementation for a node.
20772055

@@ -2440,22 +2418,24 @@ _PyHamt_New(void)
24402418
static PyObject *
24412419
hamt_dump(PyHamtObject *self)
24422420
{
2443-
_PyUnicodeWriter writer;
2444-
2445-
_PyUnicodeWriter_Init(&writer);
2421+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
2422+
if (writer == NULL) {
2423+
return NULL;
2424+
}
24462425

2447-
if (_hamt_dump_format(&writer, "HAMT(len=%zd):\n", self->h_count)) {
2426+
if (PyUnicodeWriter_Format(writer, "HAMT(len=%zd):\n",
2427+
self->h_count) < 0) {
24482428
goto error;
24492429
}
24502430

2451-
if (hamt_node_dump(self->h_root, &writer, 0)) {
2431+
if (hamt_node_dump(self->h_root, writer, 0)) {
24522432
goto error;
24532433
}
24542434

2455-
return _PyUnicodeWriter_Finish(&writer);
2435+
return PyUnicodeWriter_Finish(writer);
24562436

24572437
error:
2458-
_PyUnicodeWriter_Dealloc(&writer);
2438+
PyUnicodeWriter_Discard(writer);
24592439
return NULL;
24602440
}
24612441
#endif /* Py_DEBUG */

0 commit comments

Comments
 (0)