Issue #19137: The pprint module now correctly formats instances of se… · python/cpython@5184438 (original) (raw)
`@@ -21,6 +21,20 @@ class tuple3(tuple):
`
21
21
`def repr(self):
`
22
22
`return tuple.repr(self)
`
23
23
``
``
24
`+
class set2(set):
`
``
25
`+
pass
`
``
26
+
``
27
`+
class set3(set):
`
``
28
`+
def repr(self):
`
``
29
`+
return set.repr(self)
`
``
30
+
``
31
`+
class frozenset2(frozenset):
`
``
32
`+
pass
`
``
33
+
``
34
`+
class frozenset3(frozenset):
`
``
35
`+
def repr(self):
`
``
36
`+
return frozenset.repr(self)
`
``
37
+
24
38
`class dict2(dict):
`
25
39
`pass
`
26
40
``
`@@ -115,22 +129,24 @@ def test_same_as_repr(self):
`
115
129
`for simple in (0, 0, 0+0j, 0.0, "", b"",
`
116
130
` (), tuple2(), tuple3(),
`
117
131
` [], list2(), list3(),
`
``
132
`+
set(), set2(), set3(),
`
``
133
`+
frozenset(), frozenset2(), frozenset3(),
`
118
134
` {}, dict2(), dict3(),
`
119
135
`self.assertTrue, pprint,
`
120
136
`-6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6},
`
121
137
` (1,2), [3,4], {5: 6},
`
122
138
`tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
`
123
139
` [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
`
``
140
`+
set({7}), set2({7}), set3({7}),
`
``
141
`+
frozenset({8}), frozenset2({8}), frozenset3({8}),
`
124
142
`dict2({5: 6}), dict3({5: 6}),
`
125
143
`range(10, -11, -1)
`
126
144
` ):
`
127
145
`native = repr(simple)
`
128
``
`-
for function in "pformat", "saferepr":
`
129
``
`-
f = getattr(pprint, function)
`
130
``
`-
got = f(simple)
`
131
``
`-
self.assertEqual(native, got,
`
132
``
`-
"expected %s got %s from pprint.%s" %
`
133
``
`-
(native, got, function))
`
``
146
`+
self.assertEqual(pprint.pformat(simple), native)
`
``
147
`+
self.assertEqual(pprint.pformat(simple, width=1, indent=0)
`
``
148
`+
.replace('\n', ' '), native)
`
``
149
`+
self.assertEqual(pprint.saferepr(simple), native)
`
134
150
``
135
151
`def test_basic_line_wrap(self):
`
136
152
`# verify basic line-wrapping operation
`
`@@ -219,10 +235,54 @@ def test_subclassing(self):
`
219
235
` others.should.not.be: like.this}"""
`
220
236
`self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
`
221
237
``
``
238
`+
def test_set_reprs(self):
`
``
239
`+
self.assertEqual(pprint.pformat(set()), 'set()')
`
``
240
`+
self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
`
``
241
`+
self.assertEqual(pprint.pformat(set(range(7)), width=20), '''\
`
``
242
`+
{0,
`
``
243
`+
1,
`
``
244
`+
2,
`
``
245
`+
3,
`
``
246
`+
4,
`
``
247
`+
5,
`
``
248
`+
6}''')
`
``
249
`+
self.assertEqual(pprint.pformat(set2(range(7)), width=20), '''\
`
``
250
`+
set2({0,
`
``
251
`+
1,
`
``
252
`+
2,
`
``
253
`+
3,
`
``
254
`+
4,
`
``
255
`+
5,
`
``
256
`+
6})''')
`
``
257
`+
self.assertEqual(pprint.pformat(set3(range(7)), width=20),
`
``
258
`+
'set3({0, 1, 2, 3, 4, 5, 6})')
`
``
259
+
``
260
`+
self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
`
``
261
`+
self.assertEqual(pprint.pformat(frozenset(range(3))),
`
``
262
`+
'frozenset({0, 1, 2})')
`
``
263
`+
self.assertEqual(pprint.pformat(frozenset(range(7)), width=20), '''\
`
``
264
`+
frozenset({0,
`
``
265
`+
1,
`
``
266
`+
2,
`
``
267
`+
3,
`
``
268
`+
4,
`
``
269
`+
5,
`
``
270
`+
6})''')
`
``
271
`+
self.assertEqual(pprint.pformat(frozenset2(range(7)), width=20), '''\
`
``
272
`+
frozenset2({0,
`
``
273
`+
1,
`
``
274
`+
2,
`
``
275
`+
3,
`
``
276
`+
4,
`
``
277
`+
5,
`
``
278
`+
6})''')
`
``
279
`+
self.assertEqual(pprint.pformat(frozenset3(range(7)), width=20),
`
``
280
`+
'frozenset3({0, 1, 2, 3, 4, 5, 6})')
`
``
281
+
222
282
`@unittest.expectedFailure
`
223
283
`#See http://bugs.python.org/issue13907
`
224
284
`@test.support.cpython_only
`
225
``
`-
def test_set_reprs(self):
`
``
285
`+
def test_set_of_sets_reprs(self):
`
226
286
`# This test creates a complex arrangement of frozensets and
`
227
287
`# compares the pretty-printed repr against a string hard-coded in
`
228
288
`# the test. The hard-coded repr depends on the sort order of
`
`@@ -245,11 +305,6 @@ def test_set_reprs(self):
`
245
305
`# algorithm cause the test to fail when it should pass.
`
246
306
`# XXX Or changes to the dictionary implmentation...
`
247
307
``
248
``
`-
self.assertEqual(pprint.pformat(set()), 'set()')
`
249
``
`-
self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
`
250
``
`-
self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
`
251
``
-
252
``
`-
self.assertEqual(pprint.pformat(frozenset(range(3))), 'frozenset({0, 1, 2})')
`
253
308
`cube_repr_tgt = """\
`
254
309
`{frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
`
255
310
` frozenset({0}): frozenset({frozenset(),
`