Skip to content

Commit 2dd64c9

Browse files
committed
Check that the argument is a frame
1 parent cdae0a4 commit 2dd64c9

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Lib/test/test_frame.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,11 @@ def make_frame():
507507
proxy = FrameLocalsProxy(make_frame())
508508
self.assertEqual(proxy, {'x': 1, 'y': 2})
509509

510-
# constructor expects 1 argument (frame)
510+
# constructor expects 1 frame argument
511511
with self.assertRaises(TypeError):
512-
FrameLocalsProxy()
512+
FrameLocalsProxy() # no arguments
513+
with self.assertRaises(TypeError):
514+
FrameLocalsProxy(123) # wrong type
513515

514516

515517
class FrameLocalsProxyMappingTests(mapping_tests.TestHashMappingProtocol):

Objects/frameobject.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,19 @@ framelocalsproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
316316
PyTuple_GET_SIZE(args));
317317
return NULL;
318318
}
319+
PyObject *item = PyTuple_GET_ITEM(args, 0);
320+
321+
if (!PyFrame_Check(item)) {
322+
PyErr_Format(PyExc_TypeError, "expect frame, not %T", item);
323+
return NULL;
324+
}
325+
PyFrameObject *frame = (PyFrameObject*)item;
319326

320327
PyFrameLocalsProxyObject *self = (PyFrameLocalsProxyObject *)type->tp_alloc(type, 0);
321328
if (self == NULL) {
322329
return NULL;
323330
}
324331

325-
PyFrameObject *frame = (PyFrameObject*)PyTuple_GET_ITEM(args, 0);
326-
assert(PyFrame_Check(frame));
327-
328332
((PyFrameLocalsProxyObject*)self)->frame = (PyFrameObject*)Py_NewRef(frame);
329333

330334
return (PyObject *)self;

0 commit comments

Comments
 (0)