Skip to content

Commit ce41287

Browse files
Issue python#18531: Single var-keyword argument of dict subtype was passed
unscathed to the C-defined function. Now it is converted to exact dict.
1 parent a26e4b9 commit ce41287

4 files changed

Lines changed: 82 additions & 1 deletion

File tree

Lib/test/test_getargs2.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ class BadInt3(int):
7070
def __int__(self):
7171
return True
7272

73+
class TupleSubclass(tuple):
74+
pass
75+
76+
class DictSubclass(dict):
77+
pass
78+
7379

7480
class Unsigned_TestCase(unittest.TestCase):
7581
def test_b(self):
@@ -321,6 +327,33 @@ def test_p(self):
321327

322328

323329
class Tuple_TestCase(unittest.TestCase):
330+
def test_args(self):
331+
from _testcapi import get_args
332+
333+
ret = get_args(1, 2)
334+
self.assertEqual(ret, (1, 2))
335+
self.assertIs(type(ret), tuple)
336+
337+
ret = get_args(1, *(2, 3))
338+
self.assertEqual(ret, (1, 2, 3))
339+
self.assertIs(type(ret), tuple)
340+
341+
ret = get_args(*[1, 2])
342+
self.assertEqual(ret, (1, 2))
343+
self.assertIs(type(ret), tuple)
344+
345+
ret = get_args(*TupleSubclass([1, 2]))
346+
self.assertEqual(ret, (1, 2))
347+
self.assertIs(type(ret), tuple)
348+
349+
ret = get_args()
350+
self.assertIn(ret, ((), None))
351+
self.assertIn(type(ret), (tuple, type(None)))
352+
353+
ret = get_args(*())
354+
self.assertIn(ret, ((), None))
355+
self.assertIn(type(ret), (tuple, type(None)))
356+
324357
def test_tuple(self):
325358
from _testcapi import getargs_tuple
326359

@@ -336,6 +369,29 @@ def __getitem__(self, n):
336369
self.assertRaises(TypeError, getargs_tuple, 1, seq())
337370

338371
class Keywords_TestCase(unittest.TestCase):
372+
def test_kwargs(self):
373+
from _testcapi import get_kwargs
374+
375+
ret = get_kwargs(a=1, b=2)
376+
self.assertEqual(ret, {'a': 1, 'b': 2})
377+
self.assertIs(type(ret), dict)
378+
379+
ret = get_kwargs(a=1, **{'b': 2, 'c': 3})
380+
self.assertEqual(ret, {'a': 1, 'b': 2, 'c': 3})
381+
self.assertIs(type(ret), dict)
382+
383+
ret = get_kwargs(**DictSubclass({'a': 1, 'b': 2}))
384+
self.assertEqual(ret, {'a': 1, 'b': 2})
385+
self.assertIs(type(ret), dict)
386+
387+
ret = get_kwargs()
388+
self.assertIn(ret, ({}, None))
389+
self.assertIn(type(ret), (dict, type(None)))
390+
391+
ret = get_kwargs(**{})
392+
self.assertIn(ret, ({}, None))
393+
self.assertIn(type(ret), (dict, type(None)))
394+
339395
def test_positional_args(self):
340396
# using all positional args
341397
self.assertEqual(

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #18531: Single var-keyword argument of dict subtype was passed
14+
unscathed to the C-defined function. Now it is converted to exact dict.
15+
1316
- Issue #26811: gc.get_objects() no longer contains a broken tuple with NULL
1417
pointer.
1518

Modules/_testcapimodule.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,26 @@ test_L_code(PyObject *self)
872872

873873
#endif /* ifdef HAVE_LONG_LONG */
874874

875+
static PyObject *
876+
get_args(PyObject *self, PyObject *args)
877+
{
878+
if (args == NULL) {
879+
args = Py_None;
880+
}
881+
Py_INCREF(args);
882+
return args;
883+
}
884+
885+
static PyObject *
886+
get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs)
887+
{
888+
if (kwargs == NULL) {
889+
kwargs = Py_None;
890+
}
891+
Py_INCREF(kwargs);
892+
return kwargs;
893+
}
894+
875895
/* Test tuple argument processing */
876896
static PyObject *
877897
getargs_tuple(PyObject *self, PyObject *args)
@@ -3784,6 +3804,8 @@ static PyMethodDef TestMethods[] = {
37843804
{"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
37853805
#endif
37863806
{"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
3807+
{"get_args", get_args, METH_VARARGS},
3808+
{"get_kwargs", (PyCFunction)get_kwargs, METH_VARARGS|METH_KEYWORDS},
37873809
{"getargs_tuple", getargs_tuple, METH_VARARGS},
37883810
{"getargs_keywords", (PyCFunction)getargs_keywords,
37893811
METH_VARARGS|METH_KEYWORDS},

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4993,7 +4993,7 @@ ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
49934993

49944994
if (flags & CALL_FLAG_KW) {
49954995
kwdict = EXT_POP(*pp_stack);
4996-
if (!PyDict_Check(kwdict)) {
4996+
if (!PyDict_CheckExact(kwdict)) {
49974997
PyObject *d;
49984998
d = PyDict_New();
49994999
if (d == NULL)

0 commit comments

Comments
 (0)