@@ -1462,10 +1462,11 @@ def visitModule(self, mod):
1462
1462
return PyObject_Repr(list);
1463
1463
}
1464
1464
1465
- _PyUnicodeWriter writer;
1466
- _PyUnicodeWriter_Init(&writer);
1467
- writer.overallocate = 1;
1468
1465
PyObject *items[2] = {NULL, NULL};
1466
+ PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
1467
+ if (writer == NULL) {
1468
+ goto error;
1469
+ }
1469
1470
1470
1471
items[0] = PySequence_GetItem(list, 0);
1471
1472
if (!items[0]) {
@@ -1479,52 +1480,54 @@ def visitModule(self, mod):
1479
1480
}
1480
1481
1481
1482
bool is_list = PyList_Check(list);
1482
- if (_PyUnicodeWriter_WriteChar(& writer, is_list ? '[' : '(') < 0) {
1483
+ if (PyUnicodeWriter_WriteChar( writer, is_list ? '[' : '(') < 0) {
1483
1484
goto error;
1484
1485
}
1485
1486
1486
1487
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
+ }
1489
1493
1494
+ PyObject *item = items[i];
1490
1495
if (PyType_IsSubtype(Py_TYPE(item), (PyTypeObject *)state->AST_type)) {
1496
+ PyObject *item_repr;
1491
1497
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);
1500
1503
goto error;
1501
1504
}
1502
- }
1503
- if (_PyUnicodeWriter_WriteStr(&writer, item_repr) < 0) {
1504
1505
Py_DECREF(item_repr);
1505
- goto error;
1506
+ } else {
1507
+ if (PyUnicodeWriter_WriteRepr(writer, item) < 0) {
1508
+ goto error;
1509
+ }
1506
1510
}
1511
+
1507
1512
if (i == 0 && length > 2) {
1508
- if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ...", 5) < 0) {
1509
- Py_DECREF(item_repr);
1513
+ if (PyUnicodeWriter_WriteUTF8(writer, ", ...", 5) < 0) {
1510
1514
goto error;
1511
1515
}
1512
1516
}
1513
- Py_DECREF(item_repr);
1514
1517
}
1515
1518
1516
- if (_PyUnicodeWriter_WriteChar(& writer, is_list ? ']' : ')') < 0) {
1519
+ if (PyUnicodeWriter_WriteChar( writer, is_list ? ']' : ')') < 0) {
1517
1520
goto error;
1518
1521
}
1519
1522
1520
1523
Py_XDECREF(items[0]);
1521
1524
Py_XDECREF(items[1]);
1522
- return _PyUnicodeWriter_Finish(& writer);
1525
+ return PyUnicodeWriter_Finish( writer);
1523
1526
1524
1527
error:
1525
1528
Py_XDECREF(items[0]);
1526
1529
Py_XDECREF(items[1]);
1527
- _PyUnicodeWriter_Dealloc(& writer);
1530
+ PyUnicodeWriter_Discard( writer);
1528
1531
return NULL;
1529
1532
}
1530
1533
@@ -1568,14 +1571,15 @@ def visitModule(self, mod):
1568
1571
}
1569
1572
1570
1573
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
+ }
1574
1578
1575
- if (_PyUnicodeWriter_WriteASCIIString(& writer, tp_name, strlen(tp_name) ) < 0) {
1579
+ if (PyUnicodeWriter_WriteUTF8( writer, tp_name, -1 ) < 0) {
1576
1580
goto error;
1577
1581
}
1578
- if (_PyUnicodeWriter_WriteChar(& writer, '(') < 0) {
1582
+ if (PyUnicodeWriter_WriteChar( writer, '(') < 0) {
1579
1583
goto error;
1580
1584
}
1581
1585
@@ -1610,43 +1614,43 @@ def visitModule(self, mod):
1610
1614
}
1611
1615
1612
1616
if (i > 0) {
1613
- if (_PyUnicodeWriter_WriteASCIIString(& writer, ", ", 2) < 0) {
1617
+ if (PyUnicodeWriter_WriteUTF8( writer, ", ", 2) < 0) {
1614
1618
Py_DECREF(name);
1615
1619
Py_DECREF(value_repr);
1616
1620
goto error;
1617
1621
}
1618
1622
}
1619
- if (_PyUnicodeWriter_WriteStr(& writer, name) < 0) {
1623
+ if (PyUnicodeWriter_WriteStr( writer, name) < 0) {
1620
1624
Py_DECREF(name);
1621
1625
Py_DECREF(value_repr);
1622
1626
goto error;
1623
1627
}
1624
1628
1625
1629
Py_DECREF(name);
1626
1630
1627
- if (_PyUnicodeWriter_WriteChar(& writer, '=') < 0) {
1631
+ if (PyUnicodeWriter_WriteChar( writer, '=') < 0) {
1628
1632
Py_DECREF(value_repr);
1629
1633
goto error;
1630
1634
}
1631
- if (_PyUnicodeWriter_WriteStr(& writer, value_repr) < 0) {
1635
+ if (PyUnicodeWriter_WriteStr( writer, value_repr) < 0) {
1632
1636
Py_DECREF(value_repr);
1633
1637
goto error;
1634
1638
}
1635
1639
1636
1640
Py_DECREF(value_repr);
1637
1641
}
1638
1642
1639
- if (_PyUnicodeWriter_WriteChar(& writer, ')') < 0) {
1643
+ if (PyUnicodeWriter_WriteChar( writer, ')') < 0) {
1640
1644
goto error;
1641
1645
}
1642
1646
Py_ReprLeave((PyObject *)self);
1643
1647
Py_DECREF(fields);
1644
- return _PyUnicodeWriter_Finish(& writer);
1648
+ return PyUnicodeWriter_Finish( writer);
1645
1649
1646
1650
error:
1647
1651
Py_ReprLeave((PyObject *)self);
1648
1652
Py_DECREF(fields);
1649
- _PyUnicodeWriter_Dealloc(& writer);
1653
+ PyUnicodeWriter_Discard( writer);
1650
1654
return NULL;
1651
1655
}
1652
1656
0 commit comments