8000 gh-110235: Raise `TypeError` for duplicate/unknown fields in PyStructSequence constructor by XuehaiPan · Pull Request #110258 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-110235: Raise TypeError for duplicate/unknown fields in PyStructSequence constructor #110258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Handle case for PyStructSequence with unnamed fields
  • Loading branch information
XuehaiPan committed Oct 3, 2023
commit 981e40371a20f153abe34137b02211e90414eb42
15 changes: 15 additions & 0 deletions Lib/test/test_structseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ def test_constructor_with_duplicate_fields(self):
with self.assertRaisesRegex(TypeError, "got multiple values for field 'tm_mon'"):
t("123456789", dict={"error": 0, "tm_zone": "some zone", "tm_mon": 1})

def test_constructor_with_duplicate_unnamed_fields(self):
assert os.stat_result.n_unnamed_fields > 0
n_visible_fields = os.stat_result.n_sequence_fields

r = os.stat_result(range(n_visible_fields), {'st_atime': -1.0})
self.assertEqual(r.st_atime, -1.0)
self.assertEqual(r, tuple(range(n_visible_fields)))

r = os.stat_result((*range(n_visible_fields), -1.0))
self.assertEqual(r.st_atime, -1.0)
self.assertEqual(r, tuple(range(n_visible_fields)))

with self.assertRaisesRegex(TypeError, "got multiple values for field 'st_atime'"):
os.stat_result((*range(n_visible_fields), -1.0), {'st_atime': -1.0})

def test_constructor_with_unknown_fields(self):
t = time.struct_time
with self.assertRaisesRegex(TypeError,
Expand Down
4 changes: 2 additions & 2 deletions Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
if (dict != NULL) {
for (i = 0; i < len; ++i) {
// unnamed fields can be present in both sequence and dict
if (i >= min_len - n_unnamed_fields && i < min_len + n_unnamed_fields) {
if (i >= min_len - n_unnamed_fields && i < min_len) {
continue;
}

Expand All @@ -243,7 +243,7 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
Py_DECREF(res);
return NULL;
}
for (i = len; i < max_len; ++i) {
for (i = min_len; i < max_len; ++i) {
PyObject *ob = NULL;
const char *name = type->tp_members[i - n_unnamed_fields].name;
PyObject *key = PyUnicode_FromString(name);
Expand Down
0