Skip to content

Commit e37ac5f

Browse files
authored
gh-96751: Remove dead code from CALL_FUNCTION_EX opcode (GH-96752)
1 parent 8e9a37d commit e37ac5f

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

Lib/test/test_extcall.py

+21
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,27 @@
382382
...
383383
TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'
384384
385+
Call with dict subtype:
386+
387+
>>> class MyDict(dict):
388+
... pass
389+
390+
>>> def s1(**kwargs):
391+
... return kwargs
392+
>>> def s2(*args, **kwargs):
393+
... return (args, kwargs)
394+
>>> def s3(*, n, **kwargs):
395+
... return (n, kwargs)
396+
397+
>>> md = MyDict({'a': 1, 'b': 2})
398+
>>> assert s1(**md) == {'a': 1, 'b': 2}
399+
>>> assert s2(*(1, 2), **md) == ((1, 2), {'a': 1, 'b': 2})
400+
>>> assert s3(**MyDict({'n': 1, 'b': 2})) == (1, {'b': 2})
401+
>>> s3(**md)
402+
Traceback (most recent call last):
403+
...
404+
TypeError: s3() missing 1 required keyword-only argument: 'n'
405+
385406
Another helper function
386407
387408
>>> def f2(*a, **b):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove dead code from ``CALL_FUNCTION_EX`` opcode.

Python/ceval.c

+2-13
Original file line numberDiff line numberDiff line change
@@ -4716,19 +4716,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
47164716
PyObject *func, *callargs, *kwargs = NULL, *result;
47174717
if (oparg & 0x01) {
47184718
kwargs = POP();
4719-
if (!PyDict_CheckExact(kwargs)) {
4720-
PyObject *d = PyDict_New();
4721-
if (d == NULL)
4722-
goto error;
4723-
if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
4724-
Py_DECREF(d);
4725-
format_kwargs_error(tstate, SECOND(), kwargs);
4726-
Py_DECREF(kwargs);
4727-
goto error;
4728-
}
4729-
Py_DECREF(kwargs);
4730-
kwargs = d;
4731-
}
4719+
// DICT_MERGE is called before this opcode if there are kwargs.
4720+
// It converts all dict subtypes in kwargs into regular dicts.
47324721
assert(PyDict_CheckExact(kwargs));
47334722
}
47344723
callargs = POP();

0 commit comments

Comments
 (0)