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

Skip to content

Commit bdba5cf

Browse files
committed
Change the repr() of frozenset instances (and set subclasses)
from name([e1, e2, ...]) to name({e1, e2, ...}). This makes more sense now we have the set notation.
1 parent 0cb85a9 commit bdba5cf

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

Lib/test/test_set.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def test_cyclical_repr(self):
265265
self.assertEqual(repr(s), '{set(...)}')
266266
else:
267267
name = repr(s).partition('(')[0] # strip class name
268-
self.assertEqual(repr(s), '%s([%s(...)])' % (name, name))
268+
self.assertEqual(repr(s), '%s({%s(...)})' % (name, name))
269269

270270
def test_cyclical_print(self):
271271
w = ReprWrapper()

Objects/setobject.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ set_repr(PySetObject *so)
571571
PyObject *keys, *result=NULL;
572572
Py_UNICODE *u;
573573
int status = Py_ReprEnter((PyObject*)so);
574+
PyObject *listrepr;
575+
Py_ssize_t newsize;
574576

575577
if (status != 0) {
576578
if (status < 0)
@@ -588,30 +590,30 @@ set_repr(PySetObject *so)
588590
if (keys == NULL)
589591
goto done;
590592

591-
if (Py_Type(so) != &PySet_Type) {
592-
result = PyUnicode_FromFormat("%s(%R)", Py_Type(so)->tp_name, keys);
593+
listrepr = PyObject_Repr(keys);
594+
Py_DECREF(keys);
595+
if (listrepr == NULL) {
593596
Py_DECREF(keys);
597+
goto done;
594598
}
595-
else {
596-
PyObject *listrepr = PyObject_Repr(keys);
597-
Py_ssize_t newsize;
598-
Py_DECREF(keys);
599-
if (listrepr == NULL) {
600-
Py_DECREF(keys);
601-
goto done;
602-
}
603-
newsize = PyUnicode_GET_SIZE(listrepr);
604-
result = PyUnicode_FromUnicode(NULL, newsize);
605-
if (result) {
606-
u = PyUnicode_AS_UNICODE(result);
607-
*u++ = '{';
608-
/* Omit the brackets from the listrepr */
609-
Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
610-
PyUnicode_GET_SIZE(listrepr)-2);
611-
u += newsize-2;
612-
*u++ = '}';
613-
}
614-
Py_DECREF(listrepr);
599+
newsize = PyUnicode_GET_SIZE(listrepr);
600+
result = PyUnicode_FromUnicode(NULL, newsize);
601+
if (result) {
602+
u = PyUnicode_AS_UNICODE(result);
603+
*u++ = '{';
604+
/* Omit the brackets from the listrepr */
605+
Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
606+
PyUnicode_GET_SIZE(listrepr)-2);
607+
u += newsize-2;
608+
*u++ = '}';
609+
}
610+
Py_DECREF(listrepr);
611+
if (Py_Type(so) != &PySet_Type) {
612+
PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
613+
Py_Type(so)->tp_name,
614+
result);
615+
Py_DECREF(result);
616+
result = tmp;
615617
}
616618
done:
617619
Py_ReprLeave((PyObject*)so);

0 commit comments

Comments
 (0)