diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-10-14 21:32:03 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-14 21:32:03 (GMT) |
commit | 08ba7eb89d5353af31ef9e66a5337abea1b676ef (patch) | |
tree | 5e9ec56963b8e0fc4ebe7f1b08bdf6e9aa991775 /Lib/test/test_bytes.py | |
parent | 7c1c42b3209f1d2546daab6cd77f953eb255df6c (diff) | |
download | cpython-08ba7eb89d5353af31ef9e66a5337abea1b676ef.zip cpython-08ba7eb89d5353af31ef9e66a5337abea1b676ef.tar.gz cpython-08ba7eb89d5353af31ef9e66a5337abea1b676ef.tar.bz2 |
bpo-34974: Do not replace unexpected errors in bytes() and bytearray(). (GH-9852)
bytes and bytearray constructors converted unexpected exceptions
(e.g. MemoryError and KeyboardInterrupt) to TypeError.
(cherry picked from commit e890421e334ccf0c000c6b29c4a521d86cd12f47)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/test/test_bytes.py')
-rw-r--r-- | Lib/test/test_bytes.py | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 8c9d17f..8b19732 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -126,8 +126,8 @@ class BaseBytesTest: a = self.type2test(b"\x01\x02\x03") self.assertEqual(a, b"\x01\x02\x03") - # http://bugs.python.org/issue29159 - # Fallback when __index__ raises exception other than OverflowError + # Issues #29159 and #34974. + # Fallback when __index__ raises a TypeError class B(bytes): def __index__(self): raise TypeError @@ -184,6 +184,20 @@ class BaseBytesTest: except (OverflowError, MemoryError): pass + def test_constructor_exceptions(self): + # Issue #34974: bytes and bytearray constructors replace unexpected + # exceptions. + class BadInt: + def __index__(self): + 1/0 + self.assertRaises(ZeroDivisionError, self.type2test, BadInt()) + self.assertRaises(ZeroDivisionError, self.type2test, [BadInt()]) + + class BadIterable: + def __iter__(self): + 1/0 + self.assertRaises(ZeroDivisionError, self.type2test, BadIterable()) + def test_compare(self): b1 = self.type2test([1, 2, 3]) b2 = self.type2test([1, 2, 3]) |