Skip to content
This repository
Browse code

Merge pull request #1072 from takluyver/i89

If object has a getdoc() method, use its return value (as long as it's a string) as the object's docstring.

Closes gh-89 (duplicate docstrings when objects have getdoc).
  • Loading branch information...
commit 8a4e8d9b8d79fea96b6f7c2ed6805a2bf333be34 2 parents 487b6b9 + b20fb8e
Fernando Perez fperez authored

Showing 2 changed files with 34 additions and 15 deletions. Show diff stats Hide diff stats

  1. +11 15 IPython/core/oinspect.py
  2. +23 0 IPython/core/tests/test_oinspect.py
26 IPython/core/oinspect.py
@@ -97,26 +97,22 @@ def getdoc(obj):
97 97 It also attempts to call a getdoc() method on the given object. This
98 98 allows objects which provide their docstrings via non-standard mechanisms
99 99 (like Pyro proxies) to still be inspected by ipython's ? system."""
100   -
101   - ds = None # default return value
102   - try:
103   - ds = inspect.getdoc(obj)
104   - except:
105   - # Harden against an inspect failure, which can occur with
106   - # SWIG-wrapped extensions.
107   - pass
108 100 # Allow objects to offer customized documentation via a getdoc method:
109 101 try:
110   - ds2 = obj.getdoc()
111   - except:
  102 + ds = obj.getdoc()
  103 + except Exception:
112 104 pass
113 105 else:
114 106 # if we get extra info, we add it to the normal docstring.
115   - if ds is None:
116   - ds = ds2
117   - else:
118   - ds = '%s\n%s' % (ds,ds2)
119   - return ds
  107 + if isinstance(ds, basestring):
  108 + return inspect.cleandoc(ds)
  109 +
  110 + try:
  111 + return inspect.getdoc(obj)
  112 + except Exception:
  113 + # Harden against an inspect failure, which can occur with
  114 + # SWIG-wrapped extensions.
  115 + return None
120 116
121 117
122 118 def getsource(obj,is_binary=False):
23 IPython/core/tests/test_oinspect.py
@@ -134,3 +134,26 @@ def test_info():
134 134 i = inspector.info(OldStyle())
135 135 nt.assert_equal(i['type_name'], 'instance')
136 136 nt.assert_equal(i['docstring'], OldStyle.__doc__)
  137 +
  138 +def test_getdoc():
  139 + class A(object):
  140 + """standard docstring"""
  141 + pass
  142 +
  143 + class B(object):
  144 + """standard docstring"""
  145 + def getdoc(self):
  146 + return "custom docstring"
  147 +
  148 + class C(object):
  149 + """standard docstring"""
  150 + def getdoc(self):
  151 + return None
  152 +
  153 + a = A()
  154 + b = B()
  155 + c = C()
  156 +
  157 + nt.assert_equal(oinspect.getdoc(a), "standard docstring")
  158 + nt.assert_equal(oinspect.getdoc(b), "custom docstring")
  159 + nt.assert_equal(oinspect.getdoc(c), "standard docstring")

0 comments on commit 8a4e8d9

Please sign in to comment.
Something went wrong with that request. Please try again.