Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aa18fd5

Browse filesBrowse files
authored
gh-125196: Use PyUnicodeWriter in HAMT (#125458)
1 parent 546dddc commit aa18fd5
Copy full SHA for aa18fd5

File tree

1 file changed

+31
-50
lines changed
Filter options

1 file changed

+31
-50
lines changed

‎Python/hamt.c

Copy file name to clipboardExpand all lines: Python/hamt.c
+31-50Lines changed: 31 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,9 @@ 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)
1163+
{
11851164
Py_DECREF(tmp2);
11861165
goto error;
11871166
}
@@ -1196,7 +1175,7 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
11961175
}
11971176

11981177
if (key_or_null == NULL) {
1199-
if (_hamt_dump_format(writer, "NULL:\n")) {
1178+
if (PyUnicodeWriter_WriteUTF8(writer, "NULL:\n", -1) < 0) {
12001179
goto error;
12011180
}
12021181

@@ -1207,14 +1186,14 @@ hamt_node_bitmap_dump(PyHamtNode_Bitmap *node,
12071186
}
12081187
}
12091188
else {
1210-
if (_hamt_dump_format(writer, "%R: %R", key_or_null,
1211-
val_or_node))
1189+
if (PyUnicodeWriter_Format(writer, "%R: %R",
1190+
key_or_null, val_or_node) < 0)
12121191
{
12131192
goto error;
12141193
}
12151194
}
12161195

1217-
if (_hamt_dump_format(writer, "\n")) {
1196+
if (PyUnicodeWriter_WriteUTF8(writer, "\n", 1) < 0) {
12181197
goto error;
12191198
}
12201199
}
@@ -1548,7 +1527,7 @@ hamt_node_collision_dealloc(PyHamtNode_Collision *self)
15481527
#ifdef Py_DEBUG
15491528
static int
15501529
hamt_node_collision_dump(PyHamtNode_Collision *node,
1551-
_PyUnicodeWriter *writer, int level)
1530+
PyUnicodeWriter *writer, int level)
15521531
{
15531532
/* Debug build: __dump__() method implementation for Collision nodes. */
15541533

@@ -1558,8 +1537,8 @@ hamt_node_collision_dump(PyHamtNode_Collision *node,
15581537
goto error;
15591538
}
15601539

1561-
if (_hamt_dump_format(writer, "CollisionNode(size=%zd id=%p):\n",
1562-
Py_SIZE(node), node))
1540+
if (PyUnicodeWriter_Format(writer, "CollisionNode(size=%zd id=%p):\n",
1541+
Py_SIZE(node), node) < 0)
15631542
{
15641543
goto error;
15651544
}
@@ -1572,7 +1551,7 @@ hamt_node_collision_dump(PyHamtNode_Collision *node,
15721551
goto error;
15731552
}
15741553

1575-
if (_hamt_dump_format(writer, "%R: %R\n", key, val)) {
1554+
if (PyUnicodeWriter_Format(writer, "%R: %R\n", key, val) < 0) {
15761555
goto error;
15771556
}
15781557
}
@@ -1924,7 +1903,7 @@ hamt_node_array_dealloc(PyHamtNode_Array *self)
19241903
#ifdef Py_DEBUG
19251904
static int
19261905
hamt_node_array_dump(PyHamtNode_Array *node,
1927-
_PyUnicodeWriter *writer, int level)
1906+
PyUnicodeWriter *writer, int level)
19281907
{
19291908
/* Debug build: __dump__() method implementation for Array nodes. */
19301909

@@ -1934,7 +1913,7 @@ hamt_node_array_dump(PyHamtNode_Array *node,
19341913
goto error;
19351914
}
19361915

1937-
if (_hamt_dump_format(writer, "ArrayNode(id=%p):\n", node)) {
1916+
if (PyUnicodeWriter_Format(writer, "ArrayNode(id=%p):\n", node) < 0) {
19381917
goto error;
19391918
}
19401919

@@ -1947,15 +1926,15 @@ hamt_node_array_dump(PyHamtNode_Array *node,
19471926
goto error;
19481927
}
19491928

1950-
if (_hamt_dump_format(writer, "%zd::\n", i)) {
1929+
if (PyUnicodeWriter_Format(writer, "%zd::\n", i) < 0) {
19511930
goto error;
19521931
}
19531932

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

1958-
if (_hamt_dump_format(writer, "\n")) {
1937+
if (PyUnicodeWriter_WriteUTF8(writer, "\n", 1) < 0) {
19591938
goto error;
19601939
}
19611940
}
@@ -2071,7 +2050,7 @@ hamt_node_find(PyHamtNode *node,
20712050
#ifdef Py_DEBUG
20722051
static int
20732052
hamt_node_dump(PyHamtNode *node,
2074-
_PyUnicodeWriter *writer, int level)
2053+
PyUnicodeWriter *writer, int level)
20752054
{
20762055
/* Debug build: __dump__() method implementation for a node.
20772056
@@ -2440,22 +2419,24 @@ _PyHamt_New(void)
24402419
static PyObject *
24412420
hamt_dump(PyHamtObject *self)
24422421
{
2443-
_PyUnicodeWriter writer;
2444-
2445-
_PyUnicodeWriter_Init(&writer);
2422+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
2423+
if (writer == NULL) {
2424+
return NULL;
2425+
}
24462426

2447-
if (_hamt_dump_format(&writer, "HAMT(len=%zd):\n", self->h_count)) {
2427+
if (PyUnicodeWriter_Format(writer, "HAMT(len=%zd):\n",
2428+
self->h_count) < 0) {
24482429
goto error;
24492430
}
24502431

2451-
if (hamt_node_dump(self->h_root, &writer, 0)) {
2432+
if (hamt_node_dump(self->h_root, writer, 0)) {
24522433
goto error;
24532434
}
24542435

2455-
return _PyUnicodeWriter_Finish(&writer);
2436+
return PyUnicodeWriter_Finish(writer);
24562437

24572438
error:
2458-
_PyUnicodeWriter_Dealloc(&writer);
2439+
PyUnicodeWriter_Discard(writer);
24592440
return NULL;
24602441
}
24612442
#endif /* Py_DEBUG */

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.