Skip to content

Commit fcf7a59

Browse files
committed
pythongh-124513: Check args in framelocalsproxy_new()
1 parent 8447c93 commit fcf7a59

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Lib/test/test_frame.py

+17
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,23 @@ class ObjectSubclass:
494494
with self.assertRaises(TypeError):
495495
proxy[obj] = 0
496496

497+
def test_constructor(self):
498+
FrameLocalsProxy = type([sys._getframe().f_locals
499+
for x in range(1)][0])
500+
self.assertEqual(FrameLocalsProxy.__name__, 'FrameLocalsProxy')
501+
502+
def make_frame():
503+
x = 1
504+
y = 2
505+
return sys._getframe()
506+
507+
proxy = FrameLocalsProxy(make_frame())
508+
self.assertEqual(proxy, {'x': 1, 'y': 2})
509+
510+
# constructor expects 1 argument (frame)
511+
with self.assertRaises(TypeError):
512+
FrameLocalsProxy()
513+
497514

498515
class FrameLocalsProxyMappingTests(mapping_tests.TestHashMappingProtocol):
499516
"""Test that FrameLocalsProxy behaves like a Mapping (with exceptions)"""

Objects/frameobject.c

+7
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ framelocalsproxy_dealloc(PyObject *self)
310310
static PyObject *
311311
framelocalsproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
312312
{
313+
if (PyTuple_GET_SIZE(args) != 1) {
314+
PyErr_Format(PyExc_TypeError,
315+
"FrameLocalsProxy expected 1 argument, got %zd",
316+
PyTuple_GET_SIZE(args));
317+
return NULL;
318+
}
319+
313320
PyFrameLocalsProxyObject *self = (PyFrameLocalsProxyObject *)type->tp_alloc(type, 0);
314321
if (self == NULL) {
315322
return NULL;

0 commit comments

Comments
 (0)