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
14 changes: 13 additions & 1 deletion 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 FailingKeys(dict):
def keys(self):
raise RuntimeError("keys failed")


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

with self.assertRaises(TypeError):
with self.assertRaisesRegex(RuntimeError, "keys failed"):
d.update(FailingKeys())

with self.assertRaisesRegex(
TypeError, r"update\(\) argument must be dict or another FrameLocalsProxy"
):
d.update([1, 2])

self.assertEqual(d.setdefault('x', 5), 3)
Expand All @@ -399,6 +409,8 @@ def test_as_number(self):
self.assertEqual(d['z'], 3)
d |= {'y': 3}
self.assertEqual(d['y'], 3)
with self.assertRaisesRegex(RuntimeError, "keys failed"):
d |= FailingKeys()
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,4 @@
Fix error handling in the ``FrameLocalsProxy`` returned by
:attr:`frame.f_locals`. Exceptions raised by the argument of ``update()``
or ``|=`` are now propagated instead of being replaced with
:exc:`TypeError` or :exc:`SystemError`, or aborting debug builds.
5 changes: 3 additions & 2 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ static int
framelocalsproxy_merge(PyObject* self, PyObject* other)
{
if (!PyDict_Check(other) && !PyFrameLocalsProxy_Check(other)) {
PyErr_SetString(PyExc_TypeError,
"update() argument must be dict or another FrameLocalsProxy");
return -1;
}

Expand Down Expand Up @@ -573,7 +575,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 @@ -721,7 +723,6 @@ static PyObject*
framelocalsproxy_update(PyObject *self, PyObject *other)
{
if (framelocalsproxy_merge(self, other) < 0) {
PyErr_SetString(PyExc_TypeError, "update() argument must be dict or another FrameLocalsProxy");
return NULL;
}

Expand Down
Loading