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

Skip to content

Commit fce2225

Browse files
committed
Pretty print dict_proxy as prefixed in CPython 2.7
- Add tests for dict proxies (instances of types.DictProxyType) - Uses the "dict_proxy({...})" pattern for representation, as happens in CPython 2.7 with types.DictProxyType.__repr__ - Used ctypes.pythonapi.PyDictProxy_New to instantiate dict proxies inside the tests - Only for CPython 2.7: in PyPy the types.DictProxyType is dict, and in CPython 3 there's no such type, just the new types.MappingProxyType in CPython 3.3+
1 parent e9d4437 commit fce2225

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

IPython/lib/pretty.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ def _exception_pprint(obj, p, cycle):
757757
# In PyPy, types.DictProxyType is dict, setting the dictproxy printer
758758
# using dict.setdefault avoids overwritting the dict printer
759759
_type_pprinters.setdefault(types.DictProxyType,
760-
_dict_pprinter_factory('<dictproxy {', '}>'))
760+
_dict_pprinter_factory('dict_proxy({', '})'))
761761
_type_pprinters[types.ClassType] = _type_pprint
762762
_type_pprinters[types.SliceType] = _repr_pprint
763763
except AttributeError: # Python 3

IPython/lib/tests/test_pretty.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from __future__ import print_function
88

99
from collections import Counter, defaultdict, deque, OrderedDict
10-
import types, string
10+
import types, string, ctypes
1111

1212
import nose.tools as nt
1313

@@ -482,3 +482,52 @@ def test_mappingproxy():
482482
]
483483
for obj, expected in cases:
484484
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: {...}})}")

0 commit comments

Comments
 (0)