Skip to content

Commit 427dd1f

Browse files
make old-style classes work
1 parent 39b22ca commit 427dd1f

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

bpython/inspection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ def safe_get_attribute(obj, attr):
292292
Returns AttributeIsEmptySlot if requested attribute does not have a value,
293293
but is a slot entry.
294294
"""
295+
if is_new_style(obj):
296+
return safe_get_attribute_new_style(obj, attr)
297+
return getattr(obj, attr)
298+
299+
300+
def safe_get_attribute_new_style(obj, attr):
295301
if not is_new_style(obj):
296302
raise ValueError("%r is not a new-style class or object" % obj)
297303
to_look_through = (obj.mro()

bpython/test/test_inspection.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,37 +146,39 @@ class Property(object):
146146
def prop(self):
147147
raise AssertionError('Property __get__ executed')
148148

149+
149150
class Slots(object):
150151
__slots__ = ['s1', 's2']
151152

153+
152154
class TestSafeGetAttribute(unittest.TestCase):
153155

154156
def test_lookup_on_object(self):
155157
a = A()
156158
a.x = 1
157-
self.assertEquals(inspection.safe_get_attribute(a, 'x'), 1)
158-
self.assertEquals(inspection.safe_get_attribute(a, 'a'), 'a')
159+
self.assertEquals(inspection.safe_get_attribute_new_style(a, 'x'), 1)
160+
self.assertEquals(inspection.safe_get_attribute_new_style(a, 'a'), 'a')
159161
b = B()
160162
b.y = 2
161-
self.assertEquals(inspection.safe_get_attribute(b, 'y'), 2)
162-
self.assertEquals(inspection.safe_get_attribute(b, 'a'), 'a')
163-
self.assertEquals(inspection.safe_get_attribute(b, 'b'), 'b')
163+
self.assertEquals(inspection.safe_get_attribute_new_style(b, 'y'), 2)
164+
self.assertEquals(inspection.safe_get_attribute_new_style(b, 'a'), 'a')
165+
self.assertEquals(inspection.safe_get_attribute_new_style(b, 'b'), 'b')
164166

165167
def test_avoid_running_properties(self):
166168
p = Property()
167-
self.assertEquals(inspection.safe_get_attribute(p, 'prop'),
169+
self.assertEquals(inspection.safe_get_attribute_new_style(p, 'prop'),
168170
Property.prop)
169171

170172
def test_raises_on_old_style_class(self):
171173
class Old: pass
172174
with self.assertRaises(ValueError):
173-
inspection.safe_get_attribute(Old, 'asdf')
175+
inspection.safe_get_attribute_new_style(Old, 'asdf')
174176

175177
def test_lookup_with_slots(self):
176178
s = Slots()
177179
s.s1 = 's1'
178-
self.assertEquals(inspection.safe_get_attribute(s, 's1'), 's1')
179-
self.assertEquals(inspection.safe_get_attribute(s, 's2'),
180+
self.assertEquals(inspection.safe_get_attribute_new_style(s, 's1'), 's1')
181+
self.assertEquals(inspection.safe_get_attribute_new_style(s, 's2'),
180182
inspection.AttributeIsEmptySlot)
181183

182184

0 commit comments

Comments
 (0)