Skip to content

Commit 0093a31

Browse files
authored
gh-119182: Use public PyUnicodeWriter in Python-ast.c (#129209)
Replace the private _PyUnicodeWriter API with the public PyUnicodeWriter API. Use PyUnicodeWriter_WriteRepr() in ast_repr_list().
1 parent 8eb9e76 commit 0093a31

File tree

2 files changed

+78
-70
lines changed

2 files changed

+78
-70
lines changed

Parser/asdl_c.py

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,10 +1462,11 @@ def visitModule(self, mod):
14621462
return PyObject_Repr(list);
14631463
}
14641464
1465-
_PyUnicodeWriter writer;
1466-
_PyUnicodeWriter_Init(&writer);
1467-
writer.overallocate = 1;
14681465
PyObject *items[2] = {NULL, NULL};
1466+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
1467+
if (writer == NULL) {
1468+
goto error;
1469+
}
14691470
14701471
items[0] = PySequence_GetItem(list, 0);
14711472
if (!items[0]) {
@@ -1479,52 +1480,54 @@ def visitModule(self, mod):
14791480
}
14801481
14811482
bool is_list = PyList_Check(list);
1482-
if (_PyUnicodeWriter_WriteChar(&writer, is_list ? '[' : '(') < 0) {
1483+
if (PyUnicodeWriter_WriteChar(writer, is_list ? '[' : '(') < 0) {
14831484
goto error;
14841485
}
14851486
14861487
for (Py_ssize_t i = 0; i < Py_MIN(length, 2); i++) {
1487-
PyObject *item = items[i];
1488-
PyObject *item_repr;
1488+
if (i > 0) {
1489+
if (PyUnicodeWriter_WriteUTF8(writer, ", ", 2) < 0) {
1490+
goto error;
1491+
}
1492+
}
14891493
1494+
PyObject *item = items[i];
14901495
if (PyType_IsSubtype(Py_TYPE(item), (PyTypeObject *)state->AST_type)) {
1496+
PyObject *item_repr;
14911497
item_repr = ast_repr_max_depth((AST_object*)item, depth - 1);
1492-
} else {
1493-
item_repr = PyObject_Repr(item);
1494-
}
1495-
if (!item_repr) {
1496-
goto error;
1497-
}
1498-
if (i > 0) {
1499-
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
1498+
if (!item_repr) {
1499+
goto error;
1500+
}
1501+
if (PyUnicodeWriter_WriteStr(writer, item_repr) < 0) {
1502+
Py_DECREF(item_repr);
15001503
goto error;
15011504
}
1502-
}
1503-
if (_PyUnicodeWriter_WriteStr(&writer, item_repr) < 0) {
15041505
Py_DECREF(item_repr);
1505-
goto error;
1506+
} else {
1507+
if (PyUnicodeWriter_WriteRepr(writer, item) < 0) {
1508+
goto error;
1509+
}
15061510
}
1511+
15071512
if (i == 0 && length > 2) {
1508-
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ...", 5) < 0) {
1509-
Py_DECREF(item_repr);
1513+
if (PyUnicodeWriter_WriteUTF8(writer, ", ...", 5) < 0) {
15101514
goto error;
15111515
}
15121516
}
1513-
Py_DECREF(item_repr);
15141517
}
15151518
1516-
if (_PyUnicodeWriter_WriteChar(&writer, is_list ? ']' : ')') < 0) {
1519+
if (PyUnicodeWriter_WriteChar(writer, is_list ? ']' : ')') < 0) {
15171520
goto error;
15181521
}
15191522
15201523
Py_XDECREF(items[0]);
15211524
Py_XDECREF(items[1]);
1522-
return _PyUnicodeWriter_Finish(&writer);
1525+
return PyUnicodeWriter_Finish(writer);
15231526
15241527
error:
15251528
Py_XDECREF(items[0]);
15261529
Py_XDECREF(items[1]);
1527-
_PyUnicodeWriter_Dealloc(&writer);
1530+
PyUnicodeWriter_Discard(writer);
15281531
return NULL;
15291532
}
15301533
@@ -1568,14 +1571,15 @@ def visitModule(self, mod):
15681571
}
15691572
15701573
const char* tp_name = Py_TYPE(self)->tp_name;
1571-
_PyUnicodeWriter writer;
1572-
_PyUnicodeWriter_Init(&writer);
1573-
writer.overallocate = 1;
1574+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
1575+
if (writer == NULL) {
1576+
goto error;
1577+
}
15741578
1575-
if (_PyUnicodeWriter_WriteASCIIString(&writer, tp_name, strlen(tp_name)) < 0) {
1579+
if (PyUnicodeWriter_WriteUTF8(writer, tp_name, -1) < 0) {
15761580
goto error;
15771581
}
1578-
if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0) {
1582+
if (PyUnicodeWriter_WriteChar(writer, '(') < 0) {
15791583
goto error;
15801584
}
15811585
@@ -1610,43 +1614,43 @@ def visitModule(self, mod):
16101614
}
16111615
16121616
if (i > 0) {
1613-
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
1617+
if (PyUnicodeWriter_WriteUTF8(writer, ", ", 2) < 0) {
16141618
Py_DECREF(name);
16151619
Py_DECREF(value_repr);
16161620
goto error;
16171621
}
16181622
}
1619-
if (_PyUnicodeWriter_WriteStr(&writer, name) < 0) {
1623+
if (PyUnicodeWriter_WriteStr(writer, name) < 0) {
16201624
Py_DECREF(name);
16211625
Py_DECREF(value_repr);
16221626
goto error;
16231627
}
16241628
16251629
Py_DECREF(name);
16261630
1627-
if (_PyUnicodeWriter_WriteChar(&writer, '=') < 0) {
1631+
if (PyUnicodeWriter_WriteChar(writer, '=') < 0) {
16281632
Py_DECREF(value_repr);
16291633
goto error;
16301634
}
1631-
if (_PyUnicodeWriter_WriteStr(&writer, value_repr) < 0) {
1635+
if (PyUnicodeWriter_WriteStr(writer, value_repr) < 0) {
16321636
Py_DECREF(value_repr);
16331637
goto error;
16341638
}
16351639
16361640
Py_DECREF(value_repr);
16371641
}
16381642
1639-
if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0) {
1643+
if (PyUnicodeWriter_WriteChar(writer, ')') < 0) {
16401644
goto error;
16411645
}
16421646
Py_ReprLeave((PyObject *)self);
16431647
Py_DECREF(fields);
1644-
return _PyUnicodeWriter_Finish(&writer);
1648+
return PyUnicodeWriter_Finish(writer);
16451649
16461650
error:
16471651
Py_ReprLeave((PyObject *)self);
16481652
Py_DECREF(fields);
1649-
_PyUnicodeWriter_Dealloc(&writer);
1653+
PyUnicodeWriter_Discard(writer);
16501654
return NULL;
16511655
}
16521656

Python/Python-ast.c

Lines changed: 39 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)