From e37ac5fbb6de593521cf218aa05bc58a45c5a7c9 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Thu, 15 Sep 2022 12:33:13 +0300 Subject: [PATCH] gh-96751: Remove dead code from `CALL_FUNCTION_EX` opcode (GH-96752) --- Lib/test/test_extcall.py | 21 +++++++++++++++++++ ...2-09-11-12-43-43.gh-issue-96751.anRT6a.rst | 1 + Python/ceval.c | 15 ++----------- 3 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-09-11-12-43-43.gh-issue-96751.anRT6a.rst diff --git a/Lib/test/test_extcall.py b/Lib/test/test_extcall.py index 11d39ec63a4..d9d85fe79af 100644 --- a/Lib/test/test_extcall.py +++ b/Lib/test/test_extcall.py @@ -382,6 +382,27 @@ Test a kwargs mapping with duplicated keys. ... 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): diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-09-11-12-43-43.gh-issue-96751.anRT6a.rst b/Misc/NEWS.d/next/Core and Builtins/2022-09-11-12-43-43.gh-issue-96751.anRT6a.rst new file mode 100644 index 00000000000..fb5b73e1ac7 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-09-11-12-43-43.gh-issue-96751.anRT6a.rst @@ -0,0 +1 @@ +Remove dead code from ``CALL_FUNCTION_EX`` opcode. diff --git a/Python/ceval.c b/Python/ceval.c index 1cf72461f4c..b61cc0852ed 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -4716,19 +4716,8 @@ handle_eval_breaker: PyObject *func, *callargs, *kwargs = NULL, *result; if (oparg & 0x01) { kwargs = POP(); - if (!PyDict_CheckExact(kwargs)) { - 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();