]> granicus.if.org Git - python/commitdiff
bpo-34974: Do not replace unexpected errors in bytes() and bytearray(). (GH-9852)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 14 Oct 2018 21:02:57 +0000 (00:02 +0300)
committerGitHub <noreply@github.com>
Sun, 14 Oct 2018 21:02:57 +0000 (00:02 +0300)
bytes and bytearray constructors converted unexpected exceptions
(e.g. MemoryError and KeyboardInterrupt) to TypeError.

Lib/test/test_bytes.py
Misc/NEWS.d/next/Core and Builtins/2018-10-13-22-24-19.bpo-34974.7LgTc2.rst [new file with mode: 0644]
Objects/bytearrayobject.c
Objects/bytesobject.c

index b7b48bfe17f6eed411a532656bfc566df490d86b..b9c5b628c4eb8ed2baa6e9f56a60bdf2ce7de10a 100644 (file)
@@ -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])
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-10-13-22-24-19.bpo-34974.7LgTc2.rst b/Misc/NEWS.d/next/Core and Builtins/2018-10-13-22-24-19.bpo-34974.7LgTc2.rst
new file mode 100644 (file)
index 0000000..2a7e773
--- /dev/null
@@ -0,0 +1,3 @@
+:class:`bytes` and :class:`bytearray` constructors no longer convert
+unexpected exceptions (e.g. :exc:`MemoryError` and :exc:`KeyboardInterrupt`)
+to :exc:`TypeError`.
index 4e2bb603151b92f2715ef148414a5763994be802..58fe53bcca4734dfda5d9f76479ba0ced60ffb43 100644 (file)
@@ -41,7 +41,6 @@ _getbytevalue(PyObject* arg, int *value)
     } else {
         PyObject *index = PyNumber_Index(arg);
         if (index == NULL) {
-            PyErr_Format(PyExc_TypeError, "an integer is required");
             *value = -1;
             return 0;
         }
@@ -821,7 +820,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
     if (PyIndex_Check(arg)) {
         count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
         if (count == -1 && PyErr_Occurred()) {
-            if (PyErr_ExceptionMatches(PyExc_OverflowError))
+            if (!PyErr_ExceptionMatches(PyExc_TypeError))
                 return -1;
             PyErr_Clear();  /* fall through */
         }
index d51d1ba023c3853ba1c67baf16b40f8fa42a2203..22d46878a38e162e4ec50bf08a232f890f4b44f7 100644 (file)
@@ -2596,7 +2596,7 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     if (PyIndex_Check(x)) {
         size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
         if (size == -1 && PyErr_Occurred()) {
-            if (PyErr_ExceptionMatches(PyExc_OverflowError))
+            if (!PyErr_ExceptionMatches(PyExc_TypeError))
                 return NULL;
             PyErr_Clear();  /* fall through */
         }
@@ -2778,6 +2778,9 @@ PyBytes_FromObject(PyObject *x)
             Py_DECREF(it);
             return result;
         }
+        if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
+            return NULL;
+        }
     }
 
     PyErr_Format(PyExc_TypeError,