Skip to content

Commit a5982a7

Browse files
refactor identifying new-style classes
1 parent 98e7214 commit a5982a7

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

bpython/inspection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __enter__(self):
6969
# original methods. :-(
7070
# The upshot being that introspecting on an object to display its
7171
# attributes will avoid unwanted side-effects.
72-
if py3 or type_ != types.InstanceType:
72+
if is_new_style(self.obj):
7373
__getattr__ = getattr(type_, '__getattr__', None)
7474
if __getattr__ is not None:
7575
try:
@@ -98,6 +98,14 @@ def __exit__(self, exc_type, exc_val, exc_tb):
9898
# /Dark magic
9999

100100

101+
if py3:
102+
is_new_style = lambda obj: True
103+
else:
104+
def is_new_style(obj):
105+
"""Returns True if obj is a new-style class or object"""
106+
return type(obj) != types.InstanceType and hasattr(obj, '__class__')
107+
108+
101109
class _Repr(object):
102110
"""
103111
Helper for `fixlongargs()`: Returns the given value in `__repr__()`.

bpython/test/test_autocomplete.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ def test_catches_syntax_error(self):
3232
with self.assertRaises(autocomplete.EvaluationError):
3333
autocomplete.safe_eval('1re', {})
3434

35-
def test_doesnt_eval_properties(self):
36-
self.assertRaises(autocomplete.EvaluationError,
37-
autocomplete.safe_eval, '1re', {})
38-
p = Properties()
39-
autocomplete.safe_eval('p.asserts_when_called', {'p': p})
40-
4135

4236
class TestFormatters(unittest.TestCase):
4337

bpython/test/test_inspection.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,35 @@
2020
'''
2121

2222

23-
class TestInspection(unittest.TestCase):
24-
def test_is_callable(self):
25-
class OldCallable:
26-
def __call__(self):
27-
pass
23+
class OldCallable:
24+
def __call__(self):
25+
pass
2826

29-
class Callable(object):
30-
def __call__(self):
31-
pass
3227

33-
class OldNoncallable:
34-
pass
28+
class Callable(object):
29+
def __call__(self):
30+
pass
3531

36-
class Noncallable(object):
37-
pass
3832

39-
def spam():
40-
pass
33+
class OldNoncallable:
34+
pass
35+
36+
37+
class Noncallable(object):
38+
pass
4139

42-
class CallableMethod(object):
43-
def method(self):
44-
pass
4540

41+
def spam():
42+
pass
43+
44+
45+
class CallableMethod(object):
46+
def method(self):
47+
pass
48+
49+
50+
class TestInspection(unittest.TestCase):
51+
def test_is_callable(self):
4652
self.assertTrue(inspection.is_callable(spam))
4753
self.assertTrue(inspection.is_callable(Callable))
4854
self.assertTrue(inspection.is_callable(Callable()))
@@ -53,6 +59,14 @@ def method(self):
5359
self.assertFalse(inspection.is_callable(None))
5460
self.assertTrue(inspection.is_callable(CallableMethod().method))
5561

62+
def test_is_new_style(self):
63+
self.assertTrue(inspection.is_new_style(spam))
64+
self.assertTrue(inspection.is_new_style(Noncallable))
65+
self.assertFalse(inspection.is_new_style(OldNoncallable))
66+
self.assertTrue(inspection.is_new_style(Noncallable()))
67+
self.assertFalse(inspection.is_new_style(OldNoncallable()))
68+
self.assertTrue(inspection.is_new_style(None))
69+
5670
def test_parsekeywordpairs(self):
5771
# See issue #109
5872
def fails(spam=['-a', '-b']):

0 commit comments

Comments
 (0)