diff options
author | Benjamin Peterson <benjamin@python.org> | 2010-04-24 18:21:17 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2010-04-24 18:21:17 (GMT) |
commit | fb88636199c12f63d6c8c89f311cdafc91f30d2f (patch) | |
tree | 24b2758c45ac543fe9970f861ca091852a8f4a94 /Objects/dictobject.c | |
parent | b962171414aa2cfe3dbfbd4294819a4153a7bd6c (diff) | |
download | cpython-fb88636199c12f63d6c8c89f311cdafc91f30d2f.zip cpython-fb88636199c12f63d6c8c89f311cdafc91f30d2f.tar.gz cpython-fb88636199c12f63d6c8c89f311cdafc91f30d2f.tar.bz2 |
prevent the dict constructor from accepting non-string keyword args #8419
This adds PyArg_ValidateKeywordArguments, which checks that keyword arguments
are all strings, using an optimized method if possible.
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r-- | Objects/dictobject.c | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 1d36e1d..5433ff7 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -458,6 +458,21 @@ lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash) return 0; } +int +_PyDict_HasOnlyStringKeys(PyObject *dict) +{ + Py_ssize_t pos = 0; + PyObject *key, *value; + assert(PyDict_CheckExact(dict)); + /* Shortcut */ + if (((PyDictObject *)dict)->ma_lookup == lookdict_unicode) + return 1; + while (PyDict_Next(dict, &pos, &key, &value)) + if (!PyUnicode_Check(key)) + return 0; + return 1; +} + #ifdef SHOW_TRACK_COUNT #define INCREASE_TRACK_COUNT \ (count_tracked++, count_untracked--); @@ -1386,8 +1401,12 @@ dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methnam else result = PyDict_MergeFromSeq2(self, arg, 1); } - if (result == 0 && kwds != NULL) - result = PyDict_Merge(self, kwds, 1); + if (result == 0 && kwds != NULL) { + if (PyArg_ValidateKeywordArguments(kwds)) + result = PyDict_Merge(self, kwds, 1); + else + result = -1; + } return result; } |