diff options
author | Guido van Rossum <guido@dropbox.com> | 2016-08-18 16:22:23 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@dropbox.com> | 2016-08-18 16:22:23 (GMT) |
commit | 97c1adf3935234da716d3289b85f72dcd67e90c2 (patch) | |
tree | 0af6f9f258cf26ee9e59db463cc89d04c45bc0b8 /Lib/test/test_bytes.py | |
parent | 0a6996d87d19a524c2a11dd315d96d12083c47d4 (diff) | |
download | cpython-97c1adf3935234da716d3289b85f72dcd67e90c2.zip cpython-97c1adf3935234da716d3289b85f72dcd67e90c2.tar.gz cpython-97c1adf3935234da716d3289b85f72dcd67e90c2.tar.bz2 |
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.
Diffstat (limited to 'Lib/test/test_bytes.py')
-rw-r--r-- | Lib/test/test_bytes.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 129b4ab..64644e7 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -843,6 +843,36 @@ class BytesTest(BaseBytesTest, unittest.TestCase): self.assertRaises(OverflowError, PyBytes_FromFormat, b'%c', c_int(256)) + def test_bytes_blocking(self): + class IterationBlocked(list): + __bytes__ = None + i = [0, 1, 2, 3] + self.assertEqual(bytes(i), b'\x00\x01\x02\x03') + self.assertRaises(TypeError, bytes, IterationBlocked(i)) + + # At least in CPython, because bytes.__new__ and the C API + # PyBytes_FromObject have different fallback rules, integer + # fallback is handled specially, so test separately. + class IntBlocked(int): + __bytes__ = None + self.assertEqual(bytes(3), b'\0\0\0') + self.assertRaises(TypeError, bytes, IntBlocked(3)) + + # While there is no separately-defined rule for handling bytes + # subclasses differently from other buffer-interface classes, + # an implementation may well special-case them (as CPython 2.x + # str did), so test them separately. + class BytesSubclassBlocked(bytes): + __bytes__ = None + self.assertEqual(bytes(b'ab'), b'ab') + self.assertRaises(TypeError, bytes, BytesSubclassBlocked(b'ab')) + + class BufferBlocked(bytearray): + __bytes__ = None + ba, bb = bytearray(b'ab'), BufferBlocked(b'ab') + self.assertEqual(bytes(ba), b'ab') + self.assertRaises(TypeError, bytes, bb) + class ByteArrayTest(BaseBytesTest, unittest.TestCase): type2test = bytearray |