##// END OF EJS Templates
Pretty print dict_proxy as prefixed in CPython 2.7...
Danilo J. S. Bellini -
Show More
@@ -757,7 +757,7 b' try:'
757 # In PyPy, types.DictProxyType is dict, setting the dictproxy printer
757 # In PyPy, types.DictProxyType is dict, setting the dictproxy printer
758 # using dict.setdefault avoids overwritting the dict printer
758 # using dict.setdefault avoids overwritting the dict printer
759 _type_pprinters.setdefault(types.DictProxyType,
759 _type_pprinters.setdefault(types.DictProxyType,
760 _dict_pprinter_factory('<dictproxy {', '}>'))
760 _dict_pprinter_factory('dict_proxy({', '})'))
761 _type_pprinters[types.ClassType] = _type_pprint
761 _type_pprinters[types.ClassType] = _type_pprint
762 _type_pprinters[types.SliceType] = _repr_pprint
762 _type_pprinters[types.SliceType] = _repr_pprint
763 except AttributeError: # Python 3
763 except AttributeError: # Python 3
@@ -7,7 +7,7 b''
7 from __future__ import print_function
7 from __future__ import print_function
8
8
9 from collections import Counter, defaultdict, deque, OrderedDict
9 from collections import Counter, defaultdict, deque, OrderedDict
10 import types, string
10 import types, string, ctypes
11
11
12 import nose.tools as nt
12 import nose.tools as nt
13
13
@@ -482,3 +482,52 b' def test_mappingproxy():'
482 ]
482 ]
483 for obj, expected in cases:
483 for obj, expected in cases:
484 nt.assert_equal(pretty.pretty(obj), expected)
484 nt.assert_equal(pretty.pretty(obj), expected)
485
486 @py2_only
487 def test_dictproxy():
488 # This is the dictproxy constructor itself from the Python API,
489 DP = ctypes.pythonapi.PyDictProxy_New
490 DP.argtypes, DP.restype = (ctypes.py_object,), ctypes.py_object
491
492 underlying_dict = {}
493 mp_recursive = DP(underlying_dict)
494 underlying_dict[0] = mp_recursive
495 underlying_dict[-3] = underlying_dict
496
497 cases = [
498 (DP({}), "dict_proxy({})"),
499 (DP({None: DP({})}), "dict_proxy({None: dict_proxy({})})"),
500 (DP({k: k.lower() for k in string.ascii_uppercase}),
501 "dict_proxy({'A': 'a',\n"
502 " 'B': 'b',\n"
503 " 'C': 'c',\n"
504 " 'D': 'd',\n"
505 " 'E': 'e',\n"
506 " 'F': 'f',\n"
507 " 'G': 'g',\n"
508 " 'H': 'h',\n"
509 " 'I': 'i',\n"
510 " 'J': 'j',\n"
511 " 'K': 'k',\n"
512 " 'L': 'l',\n"
513 " 'M': 'm',\n"
514 " 'N': 'n',\n"
515 " 'O': 'o',\n"
516 " 'P': 'p',\n"
517 " 'Q': 'q',\n"
518 " 'R': 'r',\n"
519 " 'S': 's',\n"
520 " 'T': 't',\n"
521 " 'U': 'u',\n"
522 " 'V': 'v',\n"
523 " 'W': 'w',\n"
524 " 'X': 'x',\n"
525 " 'Y': 'y',\n"
526 " 'Z': 'z'})"),
527 (mp_recursive, "dict_proxy({-3: {-3: {...}, 0: {...}}, 0: {...}})"),
528 ]
529 for obj, expected in cases:
530 nt.assert_is_instance(obj, types.DictProxyType) # Meta-test
531 nt.assert_equal(pretty.pretty(obj), expected)
532 nt.assert_equal(pretty.pretty(underlying_dict),
533 "{-3: {...}, 0: dict_proxy({-3: {...}, 0: {...}})}")
General Comments 0
You need to be logged in to leave comments. Login now