##// END OF EJS Templates
pretty-rendering for os.environ by default...
Min RK -
Show More
@@ -77,11 +77,13 b' Inheritance diagram:'
77 Portions (c) 2009 by Robert Kern.
77 Portions (c) 2009 by Robert Kern.
78 :license: BSD License.
78 :license: BSD License.
79 """
79 """
80
80 from contextlib import contextmanager
81 from contextlib import contextmanager
82 import datetime
83 import os
84 import re
81 import sys
85 import sys
82 import types
86 import types
83 import re
84 import datetime
85 from collections import deque
87 from collections import deque
86 from io import StringIO
88 from io import StringIO
87 from warnings import warn
89 from warnings import warn
@@ -742,7 +744,6 b' _type_pprinters = {'
742 tuple: _seq_pprinter_factory('(', ')'),
744 tuple: _seq_pprinter_factory('(', ')'),
743 list: _seq_pprinter_factory('[', ']'),
745 list: _seq_pprinter_factory('[', ']'),
744 dict: _dict_pprinter_factory('{', '}'),
746 dict: _dict_pprinter_factory('{', '}'),
745
746 set: _set_pprinter_factory('{', '}'),
747 set: _set_pprinter_factory('{', '}'),
747 frozenset: _set_pprinter_factory('frozenset({', '})'),
748 frozenset: _set_pprinter_factory('frozenset({', '})'),
748 super: _super_pprint,
749 super: _super_pprint,
@@ -751,12 +752,17 b' _type_pprinters = {'
751 types.FunctionType: _function_pprint,
752 types.FunctionType: _function_pprint,
752 types.BuiltinFunctionType: _function_pprint,
753 types.BuiltinFunctionType: _function_pprint,
753 types.MethodType: _repr_pprint,
754 types.MethodType: _repr_pprint,
754
755 datetime.datetime: _repr_pprint,
755 datetime.datetime: _repr_pprint,
756 datetime.timedelta: _repr_pprint,
756 datetime.timedelta: _repr_pprint,
757 _exception_base: _exception_pprint
757 _exception_base: _exception_pprint
758 }
758 }
759
759
760 # render os.environ like a dict
761 _env_type = type(os.environ)
762 # future-proof in case os.environ becomes a plain dict?
763 if _env_type is not dict:
764 _type_pprinters[_env_type] = _dict_pprinter_factory('environ{', '}')
765
760 try:
766 try:
761 # In PyPy, types.DictProxyType is dict, setting the dictproxy printer
767 # In PyPy, types.DictProxyType is dict, setting the dictproxy printer
762 # using dict.setdefault avoids overwritting the dict printer
768 # using dict.setdefault avoids overwritting the dict printer
@@ -768,7 +774,7 b' except AttributeError: # Python 3'
768 _type_pprinters[types.MappingProxyType] = \
774 _type_pprinters[types.MappingProxyType] = \
769 _dict_pprinter_factory('mappingproxy({', '})')
775 _dict_pprinter_factory('mappingproxy({', '})')
770 _type_pprinters[slice] = _repr_pprint
776 _type_pprinters[slice] = _repr_pprint
771
777
772 try:
778 try:
773 _type_pprinters[long] = _repr_pprint
779 _type_pprinters[long] = _repr_pprint
774 _type_pprinters[unicode] = _repr_pprint
780 _type_pprinters[unicode] = _repr_pprint
@@ -6,6 +6,7 b''
6
6
7
7
8 from collections import Counter, defaultdict, deque, OrderedDict
8 from collections import Counter, defaultdict, deque, OrderedDict
9 import os
9 import types
10 import types
10 import string
11 import string
11 import unittest
12 import unittest
@@ -406,6 +407,15 b' def test_mappingproxy():'
406 for obj, expected in cases:
407 for obj, expected in cases:
407 nt.assert_equal(pretty.pretty(obj), expected)
408 nt.assert_equal(pretty.pretty(obj), expected)
408
409
410
411 def test_pretty_environ():
412 dict_repr = pretty.pretty(dict(os.environ))
413 # reindent to align with 'environ' prefix
414 dict_indented = dict_repr.replace('\n', '\n' + (' ' * len('environ')))
415 env_repr = pretty.pretty(os.environ)
416 nt.assert_equals(env_repr, 'environ' + dict_indented)
417
418
409 def test_function_pretty():
419 def test_function_pretty():
410 "Test pretty print of function"
420 "Test pretty print of function"
411 # posixpath is a pure python module, its interface is consistent
421 # posixpath is a pure python module, its interface is consistent
General Comments 0
You need to be logged in to leave comments. Login now