Skip to content

Commit 6b7f681

Browse files
factor out and test is_new_style
1 parent 835847d commit 6b7f681

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

bpython/inspection.py

Lines changed: 10 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,15 @@ def __exit__(self, exc_type, exc_val, exc_tb):
9898
# /Dark magic
9999

100100

101+
if py3:
102+
def is_new_style(obj):
103+
return True
104+
else:
105+
def is_new_style(obj):
106+
"""Returns True if obj is a new-style class or object"""
107+
return type(obj) != types.InstanceType
108+
109+
101110
class _Repr(object):
102111
"""
103112
Helper for `fixlongargs()`: Returns the given value in `__repr__()`.

bpython/test/test_inspection.py

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44

5+
from bpython._py3compat import py3
56
from bpython import inspection
67
from bpython.test import unittest
78
from bpython.test.fodder import encoding_ascii
@@ -20,29 +21,35 @@
2021
'''
2122

2223

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

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

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

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

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

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

42+
def spam():
43+
pass
44+
45+
46+
class CallableMethod(object):
47+
def method(self):
48+
pass
49+
50+
51+
class TestInspection(unittest.TestCase):
52+
def test_is_callable(self):
4653
self.assertTrue(inspection.is_callable(spam))
4754
self.assertTrue(inspection.is_callable(Callable))
4855
self.assertTrue(inspection.is_callable(Callable()))
@@ -53,6 +60,25 @@ def method(self):
5360
self.assertFalse(inspection.is_callable(None))
5461
self.assertTrue(inspection.is_callable(CallableMethod().method))
5562

63+
@unittest.skipIf(py3, 'old-style classes only exist in Python 2')
64+
def test_is_new_style_py2(self):
65+
self.assertTrue(inspection.is_new_style(spam))
66+
self.assertTrue(inspection.is_new_style(Noncallable))
67+
self.assertFalse(inspection.is_new_style(OldNoncallable))
68+
self.assertTrue(inspection.is_new_style(Noncallable()))
69+
self.assertFalse(inspection.is_new_style(OldNoncallable()))
70+
self.assertTrue(inspection.is_new_style(None))
71+
72+
@unittest.skipUnless(py3, 'only in Python 3 are all classes new-style')
73+
def test_is_new_style_py3(self):
74+
self.assertTrue(inspection.is_new_style(spam))
75+
self.assertTrue(inspection.is_new_style(Noncallable))
76+
self.assertTrue(inspection.is_new_style(OldNoncallable))
77+
self.assertTrue(inspection.is_new_style(Noncallable()))
78+
self.assertTrue(inspection.is_new_style(OldNoncallable()))
79+
self.assertTrue(inspection.is_new_style(None))
80+
81+
5682
def test_parsekeywordpairs(self):
5783
# See issue #109
5884
def fails(spam=['-a', '-b']):

0 commit comments

Comments
 (0)