Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch some pathological cases inside oinspect #3484

Merged
merged 1 commit into from
Jul 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions IPython/core/oinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ def call_tip(oinfo, format_call=True):

return call_line, doc

def safe_hasattr(obj, attr):
"""In recent versions of Python, hasattr() only catches AttributeError.
This catches all errors.
"""
try:
getattr(obj, attr)
return True
except:
return False


def find_file(obj):
"""Find the absolute path to the file where an object was defined.
Expand All @@ -279,7 +289,7 @@ def find_file(obj):
The absolute path to the file where the object was defined.
"""
# get source if obj was decorated with @decorator
if hasattr(obj, '__wrapped__'):
if safe_hasattr(obj, '__wrapped__'):
obj = obj.__wrapped__

fname = None
Expand Down Expand Up @@ -316,7 +326,7 @@ def find_source_lines(obj):
The line number where the object definition starts.
"""
# get source if obj was decorated with @decorator
if hasattr(obj, '__wrapped__'):
if safe_hasattr(obj, '__wrapped__'):
obj = obj.__wrapped__

try:
Expand Down Expand Up @@ -779,7 +789,7 @@ def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
out['init_docstring'] = init_ds

# Call form docstring for callable instances
if hasattr(obj, '__call__'):
if safe_hasattr(obj, '__call__'):
call_def = self._getdef(obj.__call__, oname)
if call_def is not None:
out['call_def'] = self.format(call_def)
Expand Down
9 changes: 9 additions & 0 deletions IPython/core/tests/test_oinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ def Clcmagic(self, cline, ccell=None):
"A class-based line/cell magic"


class Awkward(object):
def __getattr__(self, name):
raise Exception(name)


def check_calltip(obj, name, call, docstring):
"""Generic check pattern all calltip tests will use"""
info = inspector.info(obj, name)
Expand Down Expand Up @@ -261,6 +266,10 @@ def test_info():
nt.assert_equal(i['type_name'], 'instance')
nt.assert_equal(i['docstring'], OldStyle.__doc__)

def test_info_awkward():
# Just test that this doesn't throw an error.
i = inspector.info(Awkward())

def test_getdoc():
class A(object):
"""standard docstring"""
Expand Down