Make utils.python.collect_extra_debug_data support objects with __slots__.

This commit is contained in:
Valentin Lorentz 2019-02-07 21:00:56 +01:00
parent 0f82f89eec
commit 969b9ed341
2 changed files with 46 additions and 5 deletions

View File

@ -159,10 +159,12 @@ def collect_extra_debug_data():
frame_locals = frame.f_locals
for inspected in ('self', 'cls'):
if inspected in frame_locals:
if hasattr(frame_locals[inspected], '__dict__') and \
frame_locals[inspected].__dict__:
for (key, value) in frame_locals[inspected].__dict__.items():
frame_locals['%s.%s' % (inspected, key)] = value
for attr_name in dir(frame_locals[inspected]):
try:
v = getattr(frame_locals[inspected], attr_name)
except Exception:
v = '<ERROR WHILE GETTING VALUE>'
frame_locals['%s.%s' % (inspected, attr_name)] = v
for key, value in frame_locals.items():
if key == '__builtins__':
# This is flooding
@ -173,7 +175,7 @@ def collect_extra_debug_data():
#error we don't want.
try:
data += repr(value) + '\n'
except:
except Exception:
data += '<ERROR WHILE PRINTING VALUE>\n'
data += '\n'
data += '+-----------------------+\n'

View File

@ -1175,6 +1175,45 @@ class TestTruncatableSet(SupyTestCase):
s.truncate(3)
self.assertEqual(s, set(['foo', 'baz', 'qux']))
class UtilsPythonTest(SupyTestCase):
def test_dict(self):
class Foo:
def __hasattr__(self, n):
raise Exception()
def __getattr__(self, n):
raise Exception()
def f():
self = Foo()
self.bar = 'baz'
raise Exception('f')
try:
f()
except:
res = utils.python.collect_extra_debug_data()
self.assertTrue(re.search('self.bar.*=.*baz', res), res)
def test_slots(self):
class Foo:
__slots__ = ('bar',)
def __hasattr__(self, n):
raise Exception()
def __getattr__(self, n):
raise Exception(n)
def f():
self = Foo()
self.bar = 'baz'
raise Exception('f')
try:
f()
except:
res = utils.python.collect_extra_debug_data()
self.assertTrue(re.search('self.bar.*=.*baz', res), res)
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: