summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOren Milman <orenmn@gmail.com>2018-09-11 18:46:55 (GMT)
committerBenjamin Peterson <benjamin@python.org>2018-09-11 18:46:55 (GMT)
commit24bd50bdcc97d65130c07d6cd26085fd06c3e972 (patch)
tree910e8fbb4e6df9a32a19caaef1338c78a1f37d7a
parentb4ec36200a959da70eba94c19826446a8efdffdd (diff)
downloadcpython-24bd50bdcc97d65130c07d6cd26085fd06c3e972.zip
cpython-24bd50bdcc97d65130c07d6cd26085fd06c3e972.tar.gz
cpython-24bd50bdcc97d65130c07d6cd26085fd06c3e972.tar.bz2
closes bpo-31608: Fix a crash in methods of a subclass of _collections.deque with a bad __new__(). (GH-3788)
-rw-r--r--Lib/test/test_deque.py15
-rw-r--r--Misc/NEWS.d/next/Library/2017-10-29-10-37-55.bpo-31608.wkp8Nw.rst2
-rw-r--r--Modules/_collectionsmodule.c17
3 files changed, 30 insertions, 4 deletions
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
index e895c3c..9211360 100644
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -892,6 +892,21 @@ class TestSubclass(unittest.TestCase):
d1 == d2 # not clear if this is supposed to be True or False,
# but it used to give a SystemError
+ @support.cpython_only
+ def test_bug_31608(self):
+ # The interpreter used to crash in specific cases where a deque
+ # subclass returned a non-deque.
+ class X(deque):
+ pass
+ d = X()
+ def bad___new__(cls, *args, **kwargs):
+ return [42]
+ X.__new__ = bad___new__
+ with self.assertRaises(TypeError):
+ d * 42 # shouldn't crash
+ with self.assertRaises(TypeError):
+ d + deque([1, 2, 3]) # shouldn't crash
+
class SubclassWithKwargs(deque):
def __init__(self, newarg=1):
diff --git a/Misc/NEWS.d/next/Library/2017-10-29-10-37-55.bpo-31608.wkp8Nw.rst b/Misc/NEWS.d/next/Library/2017-10-29-10-37-55.bpo-31608.wkp8Nw.rst
new file mode 100644
index 0000000..d657a86
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-10-29-10-37-55.bpo-31608.wkp8Nw.rst
@@ -0,0 +1,2 @@
+Raise a ``TypeError`` instead of crashing if a ``collections.deque`` subclass
+returns a non-deque from ``__new__``. Patch by Oren Milman.
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index c044857..935b434 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -478,6 +478,7 @@ deque_inplace_concat(dequeobject *deque, PyObject *other)
static PyObject *
deque_copy(PyObject *deque, PyObject *Py_UNUSED(ignored))
{
+ PyObject *result;
dequeobject *old_deque = (dequeobject *)deque;
if (Py_TYPE(deque) == &deque_type) {
dequeobject *new_deque;
@@ -502,11 +503,19 @@ deque_copy(PyObject *deque, PyObject *Py_UNUSED(ignored))
return NULL;
}
if (old_deque->maxlen < 0)
- return PyObject_CallFunctionObjArgs((PyObject *)(Py_TYPE(deque)),
- deque, NULL);
+ result = PyObject_CallFunctionObjArgs((PyObject *)(Py_TYPE(deque)),
+ deque, NULL);
else
- return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
- deque, old_deque->maxlen, NULL);
+ result = PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
+ deque, old_deque->maxlen, NULL);
+ if (result != NULL && !PyObject_TypeCheck(result, &deque_type)) {
+ PyErr_Format(PyExc_TypeError,
+ "%.200s() must return a deque, not %.200s",
+ Py_TYPE(deque)->tp_name, Py_TYPE(result)->tp_name);
+ Py_DECREF(result);
+ return NULL;
+ }
+ return result;
}
PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");