diff options
author | Xuehai Pan <XuehaiPan@pku.edu.cn> | 2023-10-04 16:44:17 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-04 16:44:17 (GMT) |
commit | 9561648f4a5d8486b67ee4bbe24a239b2a93212c (patch) | |
tree | b884d30b0b02bf869fe9de9fc5d887e2298f0dd2 /Objects | |
parent | bf4bc36069ef1ed4be4be2ae70404f78bff056d9 (diff) | |
download | cpython-9561648f4a5d8486b67ee4bbe24a239b2a93212c.zip cpython-9561648f4a5d8486b67ee4bbe24a239b2a93212c.tar.gz cpython-9561648f4a5d8486b67ee4bbe24a239b2a93212c.tar.bz2 |
gh-110235: Raise TypeError for duplicate/unknown fields in PyStructSequence constructor (GH-110258)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/structseq.c | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/Objects/structseq.c b/Objects/structseq.c index 0ca622e..2c98288 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -216,19 +216,34 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict) res->ob_item[i] = Py_NewRef(v); } Py_DECREF(arg); - for (; i < max_len; ++i) { - PyObject *ob = NULL; - if (dict != NULL) { - const char *name = type->tp_members[i-n_unnamed_fields].name; + if (dict != NULL && PyDict_GET_SIZE(dict) > 0) { + Py_ssize_t n_found_keys = 0; + for (i = len; i < max_len; ++i) { + PyObject *ob = NULL; + const char *name = type->tp_members[i - n_unnamed_fields].name; if (PyDict_GetItemStringRef(dict, name, &ob) < 0) { Py_DECREF(res); return NULL; } + if (ob == NULL) { + ob = Py_NewRef(Py_None); + } + else { + ++n_found_keys; + } + res->ob_item[i] = ob; + } + if (PyDict_GET_SIZE(dict) > n_found_keys) { + PyErr_Format(PyExc_TypeError, + "%.500s() got duplicate or unexpected field name(s)", + type->tp_name); + Py_DECREF(res); + return NULL; } - if (ob == NULL) { - ob = Py_NewRef(Py_None); + } else { + for (i = len; i < max_len; ++i) { + res->ob_item[i] = Py_NewRef(Py_None); } - res->ob_item[i] = ob; } _PyObject_GC_TRACK(res); |