[2.7] closes bpo-31608: Fix a crash in methods of a subclass of _collections.deque with a bad __new__(). (GH-9179)

This commit is contained in:
Benjamin Peterson 2018-09-11 13:41:57 -07:00 committed by GitHub
parent 7a501def4f
commit 253279c616
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 2 deletions

View File

@ -659,6 +659,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
@test_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):

View File

@ -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.

View File

@ -859,11 +859,20 @@ deque_traverse(dequeobject *deque, visitproc visit, void *arg)
static PyObject *
deque_copy(PyObject *deque)
{
PyObject *result;
if (((dequeobject *)deque)->maxlen == -1)
return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
result = PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
else
return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
result = PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
deque, ((dequeobject *)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.");