Skip to content

gh-96751: Remove dead code from CALL_FUNCTION_EX opcode #96752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 15, 2022
Merged
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
21 changes: 21 additions & 0 deletions Lib/test/test_extcall.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,27 @@
...
TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'

Call with dict subtype:

>>> class MyDict(dict):
... pass

>>> def s1(**kwargs):
... return kwargs
>>> def s2(*args, **kwargs):
... return (args, kwargs)
>>> def s3(*, n, **kwargs):
... return (n, kwargs)

>>> md = MyDict({'a': 1, 'b': 2})
>>> assert s1(**md) == {'a': 1, 'b': 2}
>>> assert s2(*(1, 2), **md) == ((1, 2), {'a': 1, 'b': 2})
>>> assert s3(**MyDict({'n': 1, 'b': 2})) == (1, {'b': 2})
>>> s3(**md)
Traceback (most recent call last):
...
TypeError: s3() missing 1 required keyword-only argument: 'n'

Another helper function

>>> def f2(*a, **b):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove dead code from ``CALL_FUNCTION_EX`` opcode.
15 changes: 2 additions & 13 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -4719,19 +4719,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
PyObject *func, *callargs, *kwargs = NULL, *result;
if (oparg & 0x01) {
kwargs = POP();
if (!PyDict_CheckExact(kwargs)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add an assert that kwargs is a dict and a short comment explaining why it must be a dict.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyObject *d = PyDict_New();
if (d == NULL)
goto error;
if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Py_DECREF(d);
format_kwargs_error(tstate, SECOND(), kwargs);
Py_DECREF(kwargs);
goto error;
}
Py_DECREF(kwargs);
kwargs = d;
}
// DICT_MERGE is called before this opcode if there are kwargs.
// It converts all dict subtypes in kwargs into regular dicts.
assert(PyDict_CheckExact(kwargs));
}
callargs = POP();
Expand Down