Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f609654

Browse files
committed
handle dict subclasses gracefully in PyArg_ValidateKeywordArguments
1 parent 12ae290 commit f609654

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

Lib/test/test_dict.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
class DictTest(unittest.TestCase):
99

1010
def test_invalid_keyword_arguments(self):
11-
with self.assertRaises(TypeError):
12-
dict(**{1 : 2})
13-
with self.assertRaises(TypeError):
14-
{}.update(**{1 : 2})
11+
class Custom(dict):
12+
pass
13+
for invalid in {1 : 2}, Custom({1 : 2}):
14+
with self.assertRaises(TypeError):
15+
dict(**invalid)
16+
with self.assertRaises(TypeError):
17+
{}.update(**invalid)
1518

1619
def test_constructor(self):
1720
# calling built-in types without argument must return empty

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Library
2626
- Issue #10429: IMAP.starttls() stored the capabilities as bytes objects,
2727
rather than strings.
2828

29+
C-API
30+
-----
31+
32+
- Loosen PyArg_ValidateKeywordArguments to allow dict subclasses.
33+
2934

3035
What's New in Python 3.2 Alpha 4?
3136
=================================

Objects/dictobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ _PyDict_HasOnlyStringKeys(PyObject *dict)
454454
{
455455
Py_ssize_t pos = 0;
456456
PyObject *key, *value;
457-
assert(PyDict_CheckExact(dict));
457+
assert(PyDict_Check(dict));
458458
/* Shortcut */
459459
if (((PyDictObject *)dict)->ma_lookup == lookdict_unicode)
460460
return 1;

Python/getargs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
13941394
int
13951395
PyArg_ValidateKeywordArguments(PyObject *kwargs)
13961396
{
1397-
if (!PyDict_CheckExact(kwargs)) {
1397+
if (!PyDict_Check(kwargs)) {
13981398
PyErr_BadInternalCall();
13991399
return 0;
14001400
}

0 commit comments

Comments
 (0)