Skip to content

Commit

Permalink
Backport the tests only.
Browse files Browse the repository at this point in the history
Merged revisions 78800 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r78800 | florent.xicluna | 2010-03-08 16:20:28 +0100 (lun, 08 mar 2010) | 2 lines

  python#7624: Fix isinstance(foo(), collections.Callable) for old-style classes.
........
  • Loading branch information
florentx committed Mar 8, 2010
1 parent ad8c5ca commit ce153f6
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Lib/test/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,17 @@ def validate_abstract_methods(self, abc, *names):
C = type('C', (abc,), stubs)
self.assertRaises(TypeError, C, name)

def validate_isinstance(self, abc, name):
stub = lambda s, *args: 0

C = type('C', (object,), {'__hash__': None})
setattr(C, name, stub)
self.assertIsInstance(C(), abc)
self.assertTrue(issubclass(C, abc))

C = type('C', (object,), {'__hash__': None})
self.assertNotIsInstance(C(), abc)
self.assertFalse(issubclass(C, abc))

class TestOneTrickPonyABCs(ABCTestCase):

Expand Down Expand Up @@ -262,6 +273,7 @@ def __hash__(self):
self.assertEqual(hash(H()), 0)
self.assertFalse(issubclass(int, H))
self.validate_abstract_methods(Hashable, '__hash__')
self.validate_isinstance(Hashable, '__hash__')

def test_Iterable(self):
# Check some non-iterables
Expand All @@ -286,6 +298,7 @@ def __iter__(self):
self.assertEqual(list(I()), [])
self.assertFalse(issubclass(str, I))
self.validate_abstract_methods(Iterable, '__iter__')
self.validate_isinstance(Iterable, '__iter__')

def test_Iterator(self):
non_samples = [None, 42, 3.14, 1j, b"", "", (), [], {}, set()]
Expand All @@ -304,6 +317,7 @@ def test_Iterator(self):
self.assertIsInstance(x, Iterator)
self.assertTrue(issubclass(type(x), Iterator), repr(type(x)))
self.validate_abstract_methods(Iterator, '__next__')
self.validate_isinstance(Iterator, '__next__')

def test_Sized(self):
non_samples = [None, 42, 3.14, 1j,
Expand All @@ -321,6 +335,7 @@ def test_Sized(self):
self.assertIsInstance(x, Sized)
self.assertTrue(issubclass(type(x), Sized), repr(type(x)))
self.validate_abstract_methods(Sized, '__len__')
self.validate_isinstance(Sized, '__len__')

def test_Container(self):
non_samples = [None, 42, 3.14, 1j,
Expand All @@ -338,6 +353,7 @@ def test_Container(self):
self.assertIsInstance(x, Container)
self.assertTrue(issubclass(type(x), Container), repr(type(x)))
self.validate_abstract_methods(Container, '__contains__')
self.validate_isinstance(Container, '__contains__')

def test_Callable(self):
non_samples = [None, 42, 3.14, 1j,
Expand All @@ -357,6 +373,7 @@ def test_Callable(self):
self.assertIsInstance(x, Callable)
self.assertTrue(issubclass(type(x), Callable), repr(type(x)))
self.validate_abstract_methods(Callable, '__call__')
self.validate_isinstance(Callable, '__call__')

def test_direct_subclassing(self):
for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
Expand Down

0 comments on commit ce153f6

Please sign in to comment.