Merge pull request #2778 from LeoHuckvale/fix-abstractproperty-part2

fix inspect: return defargs[0] if obj.__dict__ is a property and raises exception
This commit is contained in:
Takeshi KOMIYA 2016-07-21 11:12:34 +09:00 committed by GitHub
commit ed70c71126
2 changed files with 77 additions and 1 deletions

View File

@ -110,12 +110,18 @@ def safe_getattr(obj, name, *defargs):
except Exception:
# sometimes accessing a property raises an exception (e.g.
# NotImplementedError), so let's try to read the attribute directly
if name in obj.__dict__:
try:
# In case the object does weird things with attribute access
# such that accessing `obj.__dict__` may raise an exception
return obj.__dict__[name]
except Exception:
pass
# this is a catch-all for all the weird things that some modules do
# with attribute access
if defargs:
return defargs[0]
raise AttributeError(name)

View File

@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
"""
test_util_inspect
~~~~~~~~~~~~~~~
Tests util.inspect functions.
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from unittest import TestCase
from sphinx.util import inspect
class TestSafeGetAttr(TestCase):
def test_safe_getattr_with_default(self):
class Foo(object):
def __getattr__(self, item):
raise Exception
obj = Foo()
result = inspect.safe_getattr(obj, 'bar', 'baz')
assert result == 'baz'
def test_safe_getattr_with_exception(self):
class Foo(object):
def __getattr__(self, item):
raise Exception
obj = Foo()
try:
inspect.safe_getattr(obj, 'bar')
except AttributeError as exc:
self.assertEqual(exc.args[0], 'bar')
else:
self.fail('AttributeError not raised')
def test_safe_getattr_with_property_exception(self):
class Foo(object):
@property
def bar(self):
raise Exception
obj = Foo()
try:
inspect.safe_getattr(obj, 'bar')
except AttributeError as exc:
self.assertEqual(exc.args[0], 'bar')
else:
self.fail('AttributeError not raised')
def test_safe_getattr_with___dict___override(self):
class Foo(object):
@property
def __dict__(self):
raise Exception
obj = Foo()
try:
inspect.safe_getattr(obj, 'bar')
except AttributeError as exc:
self.assertEqual(exc.args[0], 'bar')
else:
self.fail('AttributeError not raised')