From 5326df5892a3fadcad2f01fca54413f058914efd 2011-09-07 11:18:48 From: Thomas Kluyver Date: 2011-09-07 11:18:48 Subject: [PATCH] Python 3 compatibility in IPython.lib.pretty --- diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index 2c1a556..4361f21 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -454,8 +454,10 @@ class GroupQueue(object): except ValueError: pass - -_baseclass_reprs = (object.__repr__, types.InstanceType.__repr__) +try: + _baseclass_reprs = (object.__repr__, types.InstanceType.__repr__) +except AttributeError: # Python 3 + _baseclass_reprs = (object.__repr__,) def _default_pprint(obj, p, cycle): @@ -648,23 +650,33 @@ _type_pprinters = { tuple: _seq_pprinter_factory('(', ')', tuple), list: _seq_pprinter_factory('[', ']', list), dict: _dict_pprinter_factory('{', '}', dict), - types.DictProxyType: _dict_pprinter_factory(''), + set: _seq_pprinter_factory('set([', '])', set), frozenset: _seq_pprinter_factory('frozenset([', '])', frozenset), super: _super_pprint, _re_pattern_type: _re_pattern_pprint, type: _type_pprint, - types.ClassType: _type_pprint, types.FunctionType: _function_pprint, types.BuiltinFunctionType: _function_pprint, types.SliceType: _repr_pprint, types.MethodType: _repr_pprint, - xrange: _repr_pprint, + datetime.datetime: _repr_pprint, datetime.timedelta: _repr_pprint, _exception_base: _exception_pprint } +try: + _type_pprinters[types.DictProxyType] = _dict_pprinter_factory('') + _type_pprinters[types.ClassType] = _type_pprint, +except AttributeError: # Python 3 + pass + +try: + _type_pprinters[xrange] = _repr_pprint +except NameError: + _type_pprinters[range] = _repr_pprint + #: printers for types specified by name _deferred_type_pprinters = { }