mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
Anti-registration of various ABC methods.
- Issue #25958: Support "anti-registration" of special methods from various ABCs, like __hash__, __iter__ or __len__. All these (and several more) can be set to None in an implementation class and the behavior will be as if the method is not defined at all. (Previously, this mechanism existed only for __hash__, to make mutable classes unhashable.) Code contributed by Andrew Barnert and Ivan Levkivskyi.
This commit is contained in:
parent
0a6996d87d
commit
97c1adf393
15 changed files with 300 additions and 62 deletions
|
@ -223,7 +223,7 @@ class TestReversed(unittest.TestCase, PickleTest):
|
|||
def test_objmethods(self):
|
||||
# Objects must have __len__() and __getitem__() implemented.
|
||||
class NoLen(object):
|
||||
def __getitem__(self): return 1
|
||||
def __getitem__(self, i): return 1
|
||||
nl = NoLen()
|
||||
self.assertRaises(TypeError, reversed, nl)
|
||||
|
||||
|
@ -232,6 +232,13 @@ class TestReversed(unittest.TestCase, PickleTest):
|
|||
ngi = NoGetItem()
|
||||
self.assertRaises(TypeError, reversed, ngi)
|
||||
|
||||
class Blocked(object):
|
||||
def __getitem__(self, i): return 1
|
||||
def __len__(self): return 2
|
||||
__reversed__ = None
|
||||
b = Blocked()
|
||||
self.assertRaises(TypeError, reversed, b)
|
||||
|
||||
def test_pickle(self):
|
||||
for data in 'abc', range(5), tuple(enumerate('abc')), range(1,17,5):
|
||||
self.check_pickle(reversed(data), list(data)[::-1])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue