Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3a7796

Browse files
bpo-27541: Reprs of subclasses of some classes now contain actual type name. (#3631)
Affected classes are bytearray, array, deque, defaultdict, count and repeat.
1 parent 9adda0c commit b3a7796

6 files changed

Lines changed: 40 additions & 20 deletions

File tree

Lib/test/test_defaultdict.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ def _factory(self):
157157
return []
158158
d = sub()
159159
self.assertRegex(repr(d),
160-
r"defaultdict\(<bound method .*sub\._factory "
161-
r"of defaultdict\(\.\.\., \{\}\)>, \{\}\)")
160+
r"sub\(<bound method .*sub\._factory "
161+
r"of sub\(\.\.\., \{\}\)>, \{\}\)")
162162

163163
# NOTE: printing a subclass of a builtin type does not call its
164164
# tp_print slot. So this part is essentially the same test as above.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Reprs of subclasses of some collection and iterator classes (`bytearray`,
2+
`array.array`, `collections.deque`, `collections.defaultdict`,
3+
`itertools.count`, `itertools.repeat`) now contain actual type name insteads
4+
of hardcoded name of the base class.

Modules/_collectionsmodule.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,10 +1384,12 @@ deque_repr(PyObject *deque)
13841384
return NULL;
13851385
}
13861386
if (((dequeobject *)deque)->maxlen >= 0)
1387-
result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)",
1388-
aslist, ((dequeobject *)deque)->maxlen);
1387+
result = PyUnicode_FromFormat("%s(%R, maxlen=%zd)",
1388+
_PyType_Name(Py_TYPE(deque)), aslist,
1389+
((dequeobject *)deque)->maxlen);
13891390
else
1390-
result = PyUnicode_FromFormat("deque(%R)", aslist);
1391+
result = PyUnicode_FromFormat("%s(%R)",
1392+
_PyType_Name(Py_TYPE(deque)), aslist);
13911393
Py_ReprLeave(deque);
13921394
Py_DECREF(aslist);
13931395
return result;
@@ -2127,7 +2129,8 @@ defdict_repr(defdictobject *dd)
21272129
Py_DECREF(baserepr);
21282130
return NULL;
21292131
}
2130-
result = PyUnicode_FromFormat("defaultdict(%U, %U)",
2132+
result = PyUnicode_FromFormat("%s(%U, %U)",
2133+
_PyType_Name(Py_TYPE(dd)),
21312134
defrepr, baserepr);
21322135
Py_DECREF(defrepr);
21332136
Py_DECREF(baserepr);

Modules/arraymodule.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,7 +2307,8 @@ array_repr(arrayobject *a)
23072307
len = Py_SIZE(a);
23082308
typecode = a->ob_descr->typecode;
23092309
if (len == 0) {
2310-
return PyUnicode_FromFormat("array('%c')", (int)typecode);
2310+
return PyUnicode_FromFormat("%s('%c')",
2311+
_PyType_Name(Py_TYPE(a)), (int)typecode);
23112312
}
23122313
if (typecode == 'u') {
23132314
v = array_array_tounicode_impl(a);
@@ -2317,7 +2318,8 @@ array_repr(arrayobject *a)
23172318
if (v == NULL)
23182319
return NULL;
23192320

2320-
s = PyUnicode_FromFormat("array('%c', %R)", (int)typecode, v);
2321+
s = PyUnicode_FromFormat("%s('%c', %R)",
2322+
_PyType_Name(Py_TYPE(a)), (int)typecode, v);
23212323
Py_DECREF(v);
23222324
return s;
23232325
}

Modules/itertoolsmodule.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4067,7 +4067,8 @@ static PyObject *
40674067
count_repr(countobject *lz)
40684068
{
40694069
if (lz->cnt != PY_SSIZE_T_MAX)
4070-
return PyUnicode_FromFormat("count(%zd)", lz->cnt);
4070+
return PyUnicode_FromFormat("%s(%zd)",
4071+
_PyType_Name(Py_TYPE(lz)), lz->cnt);
40714072

40724073
if (PyLong_Check(lz->long_step)) {
40734074
long step = PyLong_AsLong(lz->long_step);
@@ -4076,11 +4077,14 @@ count_repr(countobject *lz)
40764077
}
40774078
if (step == 1) {
40784079
/* Don't display step when it is an integer equal to 1 */
4079-
return PyUnicode_FromFormat("count(%R)", lz->long_cnt);
4080+
return PyUnicode_FromFormat("%s(%R)",
4081+
_PyType_Name(Py_TYPE(lz)),
4082+
lz->long_cnt);
40804083
}
40814084
}
4082-
return PyUnicode_FromFormat("count(%R, %R)",
4083-
lz->long_cnt, lz->long_step);
4085+
return PyUnicode_FromFormat("%s(%R, %R)",
4086+
_PyType_Name(Py_TYPE(lz)),
4087+
lz->long_cnt, lz->long_step);
40844088
}
40854089

40864090
static PyObject *
@@ -4220,9 +4224,12 @@ static PyObject *
42204224
repeat_repr(repeatobject *ro)
42214225
{
42224226
if (ro->cnt == -1)
4223-
return PyUnicode_FromFormat("repeat(%R)", ro->element);
4227+
return PyUnicode_FromFormat("%s(%R)",
4228+
_PyType_Name(Py_TYPE(ro)), ro->element);
42244229
else
4225-
return PyUnicode_FromFormat("repeat(%R, %zd)", ro->element, ro->cnt);
4230+
return PyUnicode_FromFormat("%s(%R, %zd)",
4231+
_PyType_Name(Py_TYPE(ro)), ro->element,
4232+
ro->cnt);
42264233
}
42274234

42284235
static PyObject *

Objects/bytearrayobject.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -891,11 +891,12 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
891891
static PyObject *
892892
bytearray_repr(PyByteArrayObject *self)
893893
{
894-
const char *quote_prefix = "bytearray(b";
894+
const char *className = _PyType_Name(Py_TYPE(self));
895+
const char *quote_prefix = "(b";
895896
const char *quote_postfix = ")";
896897
Py_ssize_t length = Py_SIZE(self);
897-
/* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
898-
size_t newsize;
898+
/* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
899+
Py_ssize_t newsize;
899900
PyObject *v;
900901
Py_ssize_t i;
901902
char *bytes;
@@ -905,13 +906,14 @@ bytearray_repr(PyByteArrayObject *self)
905906
char *test, *start;
906907
char *buffer;
907908

908-
if (length > (PY_SSIZE_T_MAX - 15) / 4) {
909+
newsize = strlen(className);
910+
if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
909911
PyErr_SetString(PyExc_OverflowError,
910912
"bytearray object is too large to make repr");
911913
return NULL;
912914
}
913915

914-
newsize = 15 + length * 4;
916+
newsize += 6 + length * 4;
915917
buffer = PyObject_Malloc(newsize);
916918
if (buffer == NULL) {
917919
PyErr_NoMemory();
@@ -931,6 +933,8 @@ bytearray_repr(PyByteArrayObject *self)
931933
}
932934

933935
p = buffer;
936+
while (*className)
937+
*p++ = *className++;
934938
while (*quote_prefix)
935939
*p++ = *quote_prefix++;
936940
*p++ = quote;
@@ -966,7 +970,7 @@ bytearray_repr(PyByteArrayObject *self)
966970
*p++ = *quote_postfix++;
967971
}
968972

969-
v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
973+
v = PyUnicode_FromStringAndSize(buffer, p - buffer);
970974
PyObject_Free(buffer);
971975
return v;
972976
}

0 commit comments

Comments
 (0)