[Python-checkins] bpo-37417: Fix error handling in bytearray.extend. (GH-14407)

Miss Islington (bot) webhook-mailer at python.org
Wed Jun 26 16:17:04 EDT 2019


https://github.com/python/cpython/commit/5c4ce3e2fa73125fb6f9c501e6c4c8512216b7e1
commit: 5c4ce3e2fa73125fb6f9c501e6c4c8512216b7e1
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-06-26T13:17:00-07:00
summary:

bpo-37417: Fix error handling in bytearray.extend. (GH-14407)

(cherry picked from commit 2a7d596f27b2342caf168a03c95ebf3b56e5dbbd)

Co-authored-by: Brandt Bucher <brandtbucher at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2019-06-26-18-41-00.bpo-37417.VsZeHL.rst
M Lib/test/test_builtin.py
M Objects/bytearrayobject.c

diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index b536cec06487..61155799c44a 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1592,6 +1592,11 @@ def test_bytearray_translate(self):
         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
index 000000000000..f004631e2361
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-06-26-18-41-00.bpo-37417.VsZeHL.rst	
@@ -0,0 +1,2 @@
+:meth:`bytearray.extend` now correctly handles errors that arise during iteration.
+Patch by Brandt Bucher.
\ No newline at end of file
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index c684db767364..1bb19a9271b6 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -1698,6 +1698,10 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
     }
     Py_DECREF(bytearray_obj);
 
+    if (PyErr_Occurred()) {
+        return NULL;
+    }
+
     Py_RETURN_NONE;
 }
 



More information about the Python-checkins mailing list