Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from test import mapping_tests


class FailingKeysDict(dict):
def keys(self):
raise RuntimeError("keys() failed")


class ClearTest(unittest.TestCase):
"""
Tests for frame.clear().
Expand Down Expand Up @@ -380,6 +385,9 @@ def test_as_dict(self):
self.assertEqual(d['x'], 3)
self.assertEqual(d['z'], 4)

with self.assertRaisesRegex(RuntimeError, r"keys\(\) failed"):
d.update(FailingKeysDict())

with self.assertRaises(TypeError):
d.update([1, 2])

Expand All @@ -399,6 +407,8 @@ def test_as_number(self):
self.assertEqual(d['z'], 3)
d |= {'y': 3}
self.assertEqual(d['y'], 3)
with self.assertRaisesRegex(RuntimeError, r"keys\(\) failed"):
d |= FailingKeysDict()
with self.assertRaises(TypeError):
d |= 3
with self.assertRaises(TypeError):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix ``frame.f_locals |= ...`` mishandling exceptions raised by the right-hand
mapping, and fix ``frame.f_locals.update()`` unconditionally replacing any
exception with :exc:`TypeError`.
8 changes: 6 additions & 2 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ framelocalsproxy_inplace_or(PyObject *self, PyObject *other)
}

if (framelocalsproxy_merge(self, other) < 0) {
Py_RETURN_NOTIMPLEMENTED;
return NULL;
}

return Py_NewRef(self);
Expand Down Expand Up @@ -720,11 +720,15 @@ static PyObject* framelocalsproxy___contains__(PyObject *self, PyObject *key)
static PyObject*
framelocalsproxy_update(PyObject *self, PyObject *other)
{
if (framelocalsproxy_merge(self, other) < 0) {
if (!PyDict_Check(other) && !PyFrameLocalsProxy_Check(other)) {
PyErr_SetString(PyExc_TypeError, "update() argument must be dict or another FrameLocalsProxy");
return NULL;
}

if (framelocalsproxy_merge(self, other) < 0) {
return NULL;
}

Py_RETURN_NONE;
}

Expand Down
Loading