test_pprint.py 26 KB

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