1import pprint 2import test.test_support 3import unittest 4import test.test_set 5 6try: 7 uni = unicode 8except NameError: 9 def uni(x): 10 return x 11 12# list, tuple and dict subclasses that do or don't overwrite __repr__ 13class list2(list): 14 pass 15 16class list3(list): 17 def __repr__(self): 18 return list.__repr__(self) 19 20class tuple2(tuple): 21 pass 22 23class tuple3(tuple): 24 def __repr__(self): 25 return tuple.__repr__(self) 26 27class set2(set): 28 pass 29 30class set3(set): 31 def __repr__(self): 32 return set.__repr__(self) 33 34class frozenset2(frozenset): 35 pass 36 37class frozenset3(frozenset): 38 def __repr__(self): 39 return frozenset.__repr__(self) 40 41class dict2(dict): 42 pass 43 44class dict3(dict): 45 def __repr__(self): 46 return dict.__repr__(self) 47 48class QueryTestCase(unittest.TestCase): 49 50 def setUp(self): 51 self.a = range(100) 52 self.b = range(200) 53 self.a[-12] = self.b 54 55 def test_basic(self): 56 # Verify .isrecursive() and .isreadable() w/o recursion 57 pp = pprint.PrettyPrinter() 58 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"), 59 bytearray(b"ghi"), True, False, None, 60 self.a, self.b): 61 # module-level convenience functions 62 self.assertFalse(pprint.isrecursive(safe), 63 "expected not isrecursive for %r" % (safe,)) 64 self.assertTrue(pprint.isreadable(safe), 65 "expected isreadable for %r" % (safe,)) 66 # PrettyPrinter methods 67 self.assertFalse(pp.isrecursive(safe), 68 "expected not isrecursive for %r" % (safe,)) 69 self.assertTrue(pp.isreadable(safe), 70 "expected isreadable for %r" % (safe,)) 71 72 def test_knotted(self): 73 # Verify .isrecursive() and .isreadable() w/ recursion 74 # Tie a knot. 75 self.b[67] = self.a 76 # Messy dict. 77 self.d = {} 78 self.d[0] = self.d[1] = self.d[2] = self.d 79 80 pp = pprint.PrettyPrinter() 81 82 for icky in self.a, self.b, self.d, (self.d, self.d): 83 self.assertTrue(pprint.isrecursive(icky), "expected isrecursive") 84 self.assertFalse(pprint.isreadable(icky), "expected not isreadable") 85 self.assertTrue(pp.isrecursive(icky), "expected isrecursive") 86 self.assertFalse(pp.isreadable(icky), "expected not isreadable") 87 88 # Break the cycles. 89 self.d.clear() 90 del self.a[:] 91 del self.b[:] 92 93 for safe in self.a, self.b, self.d, (self.d, self.d): 94 # module-level convenience functions 95 self.assertFalse(pprint.isrecursive(safe), 96 "expected not isrecursive for %r" % (safe,)) 97 self.assertTrue(pprint.isreadable(safe), 98 "expected isreadable for %r" % (safe,)) 99 # PrettyPrinter methods 100 self.assertFalse(pp.isrecursive(safe), 101 "expected not isrecursive for %r" % (safe,)) 102 self.assertTrue(pp.isreadable(safe), 103 "expected isreadable for %r" % (safe,)) 104 105 def test_unreadable(self): 106 # Not recursive but not readable anyway 107 pp = pprint.PrettyPrinter() 108 for unreadable in type(3), pprint, pprint.isrecursive: 109 # module-level convenience functions 110 self.assertFalse(pprint.isrecursive(unreadable), 111 "expected not isrecursive for %r" % (unreadable,)) 112 self.assertFalse(pprint.isreadable(unreadable), 113 "expected not isreadable for %r" % (unreadable,)) 114 # PrettyPrinter methods 115 self.assertFalse(pp.isrecursive(unreadable), 116 "expected not isrecursive for %r" % (unreadable,)) 117 self.assertFalse(pp.isreadable(unreadable), 118 "expected not isreadable for %r" % (unreadable,)) 119 120 def test_same_as_repr(self): 121 # Simple objects, small containers and classes that overwrite __repr__ 122 # For those the result should be the same as repr(). 123 # Ahem. The docs don't say anything about that -- this appears to 124 # be testing an implementation quirk. Starting in Python 2.5, it's 125 # not true for dicts: pprint always sorts dicts by key now; before, 126 # it sorted a dict display if and only if the display required 127 # multiple lines. For that reason, dicts with more than one element 128 # aren't tested here. 129 for simple in (0, 0L, 0+0j, 0.0, "", uni(""), bytearray(), 130 (), tuple2(), tuple3(), 131 [], list2(), list3(), 132 set(), set2(), set3(), 133 frozenset(), frozenset2(), frozenset3(), 134 {}, dict2(), dict3(), 135 self.assertTrue, pprint, 136 -6, -6L, -6-6j, -1.5, "x", uni("x"), bytearray(b"x"), 137 (3,), [3], {3: 6}, 138 (1,2), [3,4], {5: 6}, 139 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), 140 [3,4], list2([3,4]), list3([3,4]), list3(range(100)), 141 set({7}), set2({7}), set3({7}), 142 frozenset({8}), frozenset2({8}), frozenset3({8}), 143 dict2({5: 6}), dict3({5: 6}), 144 range(10, -11, -1), 145 True, False, None, 146 ): 147 native = repr(simple) 148 self.assertEqual(pprint.pformat(simple), native) 149 self.assertEqual(pprint.pformat(simple, width=1, indent=0) 150 .replace('\n', ' '), native) 151 self.assertEqual(pprint.saferepr(simple), native) 152 153 def test_basic_line_wrap(self): 154 # verify basic line-wrapping operation 155 o = {'RPM_cal': 0, 156 'RPM_cal2': 48059, 157 'Speed_cal': 0, 158 'controldesk_runtime_us': 0, 159 'main_code_runtime_us': 0, 160 'read_io_runtime_us': 0, 161 'write_io_runtime_us': 43690} 162 exp = """\ 163{'RPM_cal': 0, 164 'RPM_cal2': 48059, 165 'Speed_cal': 0, 166 'controldesk_runtime_us': 0, 167 'main_code_runtime_us': 0, 168 'read_io_runtime_us': 0, 169 'write_io_runtime_us': 43690}""" 170 for type in [dict, dict2]: 171 self.assertEqual(pprint.pformat(type(o)), exp) 172 173 o = range(100) 174 exp = '[%s]' % ',\n '.join(map(str, o)) 175 for type in [list, list2]: 176 self.assertEqual(pprint.pformat(type(o)), exp) 177 178 o = tuple(range(100)) 179 exp = '(%s)' % ',\n '.join(map(str, o)) 180 for type in [tuple, tuple2]: 181 self.assertEqual(pprint.pformat(type(o)), exp) 182 183 # indent parameter 184 o = range(100) 185 exp = '[ %s]' % ',\n '.join(map(str, o)) 186 for type in [list, list2]: 187 self.assertEqual(pprint.pformat(type(o), indent=4), exp) 188 189 def test_nested_indentations(self): 190 o1 = list(range(10)) 191 o2 = dict(first=1, second=2, third=3) 192 o = [o1, o2] 193 expected = """\ 194[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 195 { 'first': 1, 196 'second': 2, 197 'third': 3}]""" 198 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected) 199 200 def test_sorted_dict(self): 201 # Starting in Python 2.5, pprint sorts dict displays by key regardless 202 # of how small the dictionary may be. 203 # Before the change, on 32-bit Windows pformat() gave order 204 # 'a', 'c', 'b' here, so this test failed. 205 d = {'a': 1, 'b': 1, 'c': 1} 206 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}") 207 self.assertEqual(pprint.pformat([d, d]), 208 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]") 209 210 # The next one is kind of goofy. The sorted order depends on the 211 # alphabetic order of type names: "int" < "str" < "tuple". Before 212 # Python 2.5, this was in the test_same_as_repr() test. It's worth 213 # keeping around for now because it's one of few tests of pprint 214 # against a crazy mix of types. 215 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}), 216 r"{5: [[]], 'xy\tab\n': (3,), (): {}}") 217 218 def test_subclassing(self): 219 o = {'names with spaces': 'should be presented using repr()', 220 'others.should.not.be': 'like.this'} 221 exp = """\ 222{'names with spaces': 'should be presented using repr()', 223 others.should.not.be: like.this}""" 224 self.assertEqual(DottedPrettyPrinter().pformat(o), exp) 225 226 def test_set_reprs(self): 227 self.assertEqual(pprint.pformat(set()), 'set([])') 228 self.assertEqual(pprint.pformat(set(range(3))), 'set([0, 1, 2])') 229 self.assertEqual(pprint.pformat(set(range(7)), width=20), '''\ 230set([0, 231 1, 232 2, 233 3, 234 4, 235 5, 236 6])''') 237 self.assertEqual(pprint.pformat(set2(range(7)), width=20), '''\ 238set2([0, 239 1, 240 2, 241 3, 242 4, 243 5, 244 6])''') 245 self.assertEqual(pprint.pformat(set3(range(7)), width=20), 246 'set3([0, 1, 2, 3, 4, 5, 6])') 247 248 self.assertEqual(pprint.pformat(frozenset()), 'frozenset([])') 249 self.assertEqual(pprint.pformat(frozenset(range(3))), 250 'frozenset([0, 1, 2])') 251 self.assertEqual(pprint.pformat(frozenset(range(7)), width=20), '''\ 252frozenset([0, 253 1, 254 2, 255 3, 256 4, 257 5, 258 6])''') 259 self.assertEqual(pprint.pformat(frozenset2(range(7)), width=20), '''\ 260frozenset2([0, 261 1, 262 2, 263 3, 264 4, 265 5, 266 6])''') 267 self.assertEqual(pprint.pformat(frozenset3(range(7)), width=20), 268 'frozenset3([0, 1, 2, 3, 4, 5, 6])') 269 270 def test_set_of_sets_reprs(self): 271 cube_repr_tgt = """\ 272{frozenset([]): frozenset([frozenset([2]), frozenset([0]), frozenset([1])]), 273 frozenset([0]): frozenset([frozenset([]), 274 frozenset([0, 2]), 275 frozenset([0, 1])]), 276 frozenset([1]): frozenset([frozenset([]), 277 frozenset([1, 2]), 278 frozenset([0, 1])]), 279 frozenset([2]): frozenset([frozenset([]), 280 frozenset([1, 2]), 281 frozenset([0, 2])]), 282 frozenset([1, 2]): frozenset([frozenset([2]), 283 frozenset([1]), 284 frozenset([0, 1, 2])]), 285 frozenset([0, 2]): frozenset([frozenset([2]), 286 frozenset([0]), 287 frozenset([0, 1, 2])]), 288 frozenset([0, 1]): frozenset([frozenset([0]), 289 frozenset([1]), 290 frozenset([0, 1, 2])]), 291 frozenset([0, 1, 2]): frozenset([frozenset([1, 2]), 292 frozenset([0, 2]), 293 frozenset([0, 1])])}""" 294 cube = test.test_set.cube(3) 295 self.assertEqual(pprint.pformat(cube), cube_repr_tgt) 296 cubo_repr_tgt = """\ 297{frozenset([frozenset([0, 2]), frozenset([0])]): frozenset([frozenset([frozenset([0, 298 2]), 299 frozenset([0, 300 1, 301 2])]), 302 frozenset([frozenset([0]), 303 frozenset([0, 304 1])]), 305 frozenset([frozenset([]), 306 frozenset([0])]), 307 frozenset([frozenset([2]), 308 frozenset([0, 309 2])])]), 310 frozenset([frozenset([0, 1]), frozenset([1])]): frozenset([frozenset([frozenset([0, 311 1]), 312 frozenset([0, 313 1, 314 2])]), 315 frozenset([frozenset([0]), 316 frozenset([0, 317 1])]), 318 frozenset([frozenset([1]), 319 frozenset([1, 320 2])]), 321 frozenset([frozenset([]), 322 frozenset([1])])]), 323 frozenset([frozenset([1, 2]), frozenset([1])]): frozenset([frozenset([frozenset([1, 324 2]), 325 frozenset([0, 326 1, 327 2])]), 328 frozenset([frozenset([2]), 329 frozenset([1, 330 2])]), 331 frozenset([frozenset([]), 332 frozenset([1])]), 333 frozenset([frozenset([1]), 334 frozenset([0, 335 1])])]), 336 frozenset([frozenset([1, 2]), frozenset([2])]): frozenset([frozenset([frozenset([1, 337 2]), 338 frozenset([0, 339 1, 340 2])]), 341 frozenset([frozenset([1]), 342 frozenset([1, 343 2])]), 344 frozenset([frozenset([2]), 345 frozenset([0, 346 2])]), 347 frozenset([frozenset([]), 348 frozenset([2])])]), 349 frozenset([frozenset([]), frozenset([0])]): frozenset([frozenset([frozenset([0]), 350 frozenset([0, 351 1])]), 352 frozenset([frozenset([0]), 353 frozenset([0, 354 2])]), 355 frozenset([frozenset([]), 356 frozenset([1])]), 357 frozenset([frozenset([]), 358 frozenset([2])])]), 359 frozenset([frozenset([]), frozenset([1])]): frozenset([frozenset([frozenset([]), 360 frozenset([0])]), 361 frozenset([frozenset([1]), 362 frozenset([1, 363 2])]), 364 frozenset([frozenset([]), 365 frozenset([2])]), 366 frozenset([frozenset([1]), 367 frozenset([0, 368 1])])]), 369 frozenset([frozenset([2]), frozenset([])]): frozenset([frozenset([frozenset([2]), 370 frozenset([1, 371 2])]), 372 frozenset([frozenset([]), 373 frozenset([0])]), 374 frozenset([frozenset([]), 375 frozenset([1])]), 376 frozenset([frozenset([2]), 377 frozenset([0, 378 2])])]), 379 frozenset([frozenset([0, 1, 2]), frozenset([0, 1])]): frozenset([frozenset([frozenset([1, 380 2]), 381 frozenset([0, 382 1, 383 2])]), 384 frozenset([frozenset([0, 385 2]), 386 frozenset([0, 387 1, 388 2])]), 389 frozenset([frozenset([0]), 390 frozenset([0, 391 1])]), 392 frozenset([frozenset([1]), 393 frozenset([0, 394 1])])]), 395 frozenset([frozenset([0]), frozenset([0, 1])]): frozenset([frozenset([frozenset([]), 396 frozenset([0])]), 397 frozenset([frozenset([0, 398 1]), 399 frozenset([0, 400 1, 401 2])]), 402 frozenset([frozenset([0]), 403 frozenset([0, 404 2])]), 405 frozenset([frozenset([1]), 406 frozenset([0, 407 1])])]), 408 frozenset([frozenset([2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([0, 409 2]), 410 frozenset([0, 411 1, 412 2])]), 413 frozenset([frozenset([2]), 414 frozenset([1, 415 2])]), 416 frozenset([frozenset([0]), 417 frozenset([0, 418 2])]), 419 frozenset([frozenset([]), 420 frozenset([2])])]), 421 frozenset([frozenset([0, 1, 2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([1, 422 2]), 423 frozenset([0, 424 1, 425 2])]), 426 frozenset([frozenset([0, 427 1]), 428 frozenset([0, 429 1, 430 2])]), 431 frozenset([frozenset([0]), 432 frozenset([0, 433 2])]), 434 frozenset([frozenset([2]), 435 frozenset([0, 436 2])])]), 437 frozenset([frozenset([1, 2]), frozenset([0, 1, 2])]): frozenset([frozenset([frozenset([0, 438 2]), 439 frozenset([0, 440 1, 441 2])]), 442 frozenset([frozenset([0, 443 1]), 444 frozenset([0, 445 1, 446 2])]), 447 frozenset([frozenset([2]), 448 frozenset([1, 449 2])]), 450 frozenset([frozenset([1]), 451 frozenset([1, 452 2])])])}""" 453 454 cubo = test.test_set.linegraph(cube) 455 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt) 456 457 def test_depth(self): 458 nested_tuple = (1, (2, (3, (4, (5, 6))))) 459 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}} 460 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]] 461 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple)) 462 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict)) 463 self.assertEqual(pprint.pformat(nested_list), repr(nested_list)) 464 465 lv1_tuple = '(1, (...))' 466 lv1_dict = '{1: {...}}' 467 lv1_list = '[1, [...]]' 468 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple) 469 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict) 470 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list) 471 472 473class DottedPrettyPrinter(pprint.PrettyPrinter): 474 475 def format(self, object, context, maxlevels, level): 476 if isinstance(object, str): 477 if ' ' in object: 478 return repr(object), 1, 0 479 else: 480 return object, 0, 0 481 else: 482 return pprint.PrettyPrinter.format( 483 self, object, context, maxlevels, level) 484 485 486def test_main(): 487 test.test_support.run_unittest(QueryTestCase) 488 489 490if __name__ == "__main__": 491 test_main() 492