From 1fc84b3205ec9a6168ac01b8b47b06c027daf622 2013-05-29 22:55:09 From: Min RK Date: 2013-05-29 22:55:09 Subject: [PATCH] Merge pull request #3377 from minrk/nonemod allow class.__module__ to be None --- diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index 9a60f43..36d7824 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -638,10 +638,10 @@ def _re_pattern_pprint(obj, p, cycle): def _type_pprint(obj, p, cycle): """The pprint for classes and types.""" - try: - mod = obj.__module__ - except AttributeError: - # Heap allocated types might not have the module attribute. + mod = getattr(obj, '__module__', None) + if mod is None: + # Heap allocated types might not have the module attribute, + # and others may set it to None. return p.text(obj.__name__) if mod in ('__builtin__', 'exceptions'): diff --git a/IPython/lib/tests/test_pretty.py b/IPython/lib/tests/test_pretty.py index eff710f..689ae48 100644 --- a/IPython/lib/tests/test_pretty.py +++ b/IPython/lib/tests/test_pretty.py @@ -53,6 +53,11 @@ class Dummy1(object): class Dummy2(Dummy1): _repr_pretty_ = None +class NoModule(object): + pass + +NoModule.__module__ = None + def test_indentation(): """Test correct indentation in groups""" @@ -106,3 +111,10 @@ def test_pprint_heap_allocated_type(): import xxlimited output = pretty.pretty(xxlimited.Null) nt.assert_equal(output, 'xxlimited.Null') + +def test_pprint_nomod(): + """ + Test that pprint works for classes with no __module__. + """ + output = pretty.pretty(NoModule) + nt.assert_equal(output, 'NoModule')