Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Check that the argument is a frame
  • Loading branch information
vstinner committed Sep 25, 2024
commit 2dd64c93b662e9d1fe5d81fa6e576c95a095e317
6 changes: 4 additions & 2 deletions Lib/test/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,11 @@ def make_frame():
proxy = FrameLocalsProxy(make_frame())
self.assertEqual(proxy, {'x': 1, 'y': 2})

# constructor expects 1 argument (frame)
# constructor expects 1 frame argument
with self.assertRaises(TypeError):
FrameLocalsProxy()
FrameLocalsProxy() # no arguments
with self.assertRaises(TypeError):
FrameLocalsProxy(123) # wrong type


class FrameLocalsProxyMappingTests(mapping_tests.TestHashMappingProtocol):
Expand Down
10 changes: 7 additions & 3 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,19 @@ framelocalsproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyTuple_GET_SIZE(args));
return NULL;
}
PyObject *item = PyTuple_GET_ITEM(args, 0);

if (!PyFrame_Check(item)) {
PyErr_Format(PyExc_TypeError, "expect frame, not %T", item);
return NULL;
}
PyFrameObject *frame = (PyFrameObject*)item;

Comment thread
vstinner marked this conversation as resolved.
PyFrameLocalsProxyObject *self = (PyFrameLocalsProxyObject *)type->tp_alloc(type, 0);
if (self == NULL) {
return NULL;
}

PyFrameObject *frame = (PyFrameObject*)PyTuple_GET_ITEM(args, 0);
assert(PyFrame_Check(frame));

((PyFrameLocalsProxyObject*)self)->frame = (PyFrameObject*)Py_NewRef(frame);

return (PyObject *)self;
Expand Down