mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
PyString_Concat() and PyString_ConcatAndDel() (the name PyUnicode_Concat() was already taken). Change PyObject_Repr() to always return a unicode object. Update all repr implementations to return unicode objects. Add a function PyObject_ReprStr8() that calls PyObject_Repr() and converts the result to an 8bit string. Use PyObject_ReprStr8() where using PyObject_Repr() can't be done straightforward.
This commit is contained in:
parent
14176a56d3
commit
1ab8330827
49 changed files with 385 additions and 255 deletions
|
|
@ -941,11 +941,11 @@ dict_repr(dictobject *mp)
|
|||
|
||||
i = Py_ReprEnter((PyObject *)mp);
|
||||
if (i != 0) {
|
||||
return i > 0 ? PyString_FromString("{...}") : NULL;
|
||||
return i > 0 ? PyUnicode_FromString("{...}") : NULL;
|
||||
}
|
||||
|
||||
if (mp->ma_used == 0) {
|
||||
result = PyString_FromString("{}");
|
||||
result = PyUnicode_FromString("{}");
|
||||
goto Done;
|
||||
}
|
||||
|
||||
|
|
@ -953,7 +953,7 @@ dict_repr(dictobject *mp)
|
|||
if (pieces == NULL)
|
||||
goto Done;
|
||||
|
||||
colon = PyString_FromString(": ");
|
||||
colon = PyUnicode_FromString(": ");
|
||||
if (colon == NULL)
|
||||
goto Done;
|
||||
|
||||
|
|
@ -965,8 +965,8 @@ dict_repr(dictobject *mp)
|
|||
/* Prevent repr from deleting value during key format. */
|
||||
Py_INCREF(value);
|
||||
s = PyObject_Repr(key);
|
||||
PyString_Concat(&s, colon);
|
||||
PyString_ConcatAndDel(&s, PyObject_Repr(value));
|
||||
PyUnicode_Append(&s, colon);
|
||||
PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
|
||||
Py_DECREF(value);
|
||||
if (s == NULL)
|
||||
goto Done;
|
||||
|
|
@ -978,29 +978,29 @@ dict_repr(dictobject *mp)
|
|||
|
||||
/* Add "{}" decorations to the first and last items. */
|
||||
assert(PyList_GET_SIZE(pieces) > 0);
|
||||
s = PyString_FromString("{");
|
||||
s = PyUnicode_FromString("{");
|
||||
if (s == NULL)
|
||||
goto Done;
|
||||
temp = PyList_GET_ITEM(pieces, 0);
|
||||
PyString_ConcatAndDel(&s, temp);
|
||||
PyUnicode_AppendAndDel(&s, temp);
|
||||
PyList_SET_ITEM(pieces, 0, s);
|
||||
if (s == NULL)
|
||||
goto Done;
|
||||
|
||||
s = PyString_FromString("}");
|
||||
s = PyUnicode_FromString("}");
|
||||
if (s == NULL)
|
||||
goto Done;
|
||||
temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
|
||||
PyString_ConcatAndDel(&temp, s);
|
||||
PyUnicode_AppendAndDel(&temp, s);
|
||||
PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
|
||||
if (temp == NULL)
|
||||
goto Done;
|
||||
|
||||
/* Paste them all together with ", " between. */
|
||||
s = PyString_FromString(", ");
|
||||
s = PyUnicode_FromString(", ");
|
||||
if (s == NULL)
|
||||
goto Done;
|
||||
result = _PyString_Join(s, pieces);
|
||||
result = PyUnicode_Join(s, pieces);
|
||||
Py_DECREF(s);
|
||||
|
||||
Done:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue