]> granicus.if.org Git - python/commitdiff
bpo-37417: Fix error handling in bytearray.extend. (GH-14407)
authorBrandt Bucher <brandtbucher@gmail.com>
Wed, 26 Jun 2019 19:06:18 +0000 (12:06 -0700)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 26 Jun 2019 19:06:18 +0000 (22:06 +0300)
Lib/test/test_builtin.py
Misc/NEWS.d/next/Core and Builtins/2019-06-26-18-41-00.bpo-37417.VsZeHL.rst [new file with mode: 0644]
Objects/bytearrayobject.c

index b536cec064878ff1a694a4ca16e0f5f2ef3328ed..61155799c44a92ac6360403c45aa3e9b538d85f5 100644 (file)
@@ -1592,6 +1592,11 @@ class BuiltinTest(unittest.TestCase):
         self.assertRaises(ValueError, x.translate, b"1", 1)
         self.assertRaises(TypeError, x.translate, b"1"*256, 1)
 
+    def test_bytearray_extend_error(self):
+        array = bytearray()
+        bad_iter = map(int, "X")
+        self.assertRaises(ValueError, array.extend, bad_iter)
+
     def test_construct_singletons(self):
         for const in None, Ellipsis, NotImplemented:
             tp = type(const)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-06-26-18-41-00.bpo-37417.VsZeHL.rst b/Misc/NEWS.d/next/Core and Builtins/2019-06-26-18-41-00.bpo-37417.VsZeHL.rst
new file mode 100644 (file)
index 0000000..f004631
--- /dev/null
@@ -0,0 +1,2 @@
+:meth:`bytearray.extend` now correctly handles errors that arise during iteration.\r
+Patch by Brandt Bucher.
\ No newline at end of file
index c684db76736462b69e768fe5804398ce284e3019..1bb19a9271b6c31a5331fb68fdf4a1c90acbee83 100644 (file)
@@ -1698,6 +1698,10 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
     }
     Py_DECREF(bytearray_obj);
 
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
+
     Py_RETURN_NONE;
 }