Skip to content

Commit ccc23d4

Browse files
committed
Don't catch all ValueError
1 parent 55526ee commit ccc23d4

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

Lib/ast.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,12 @@ def __instancecheck__(cls, inst):
520520

521521
def _new(cls, *args, **kwargs):
522522
for key in kwargs:
523-
try:
524-
pos = cls._fields.index(key)
525-
if pos < len(args):
526-
raise TypeError(f"{cls.__name__} got multiple values for argument {key!r}")
527-
except ValueError:
523+
if key not in cls._fields:
528524
# arbitrary keyword arguments are accepted
529-
pass
525+
continue
526+
pos = cls._fields.index(key)
527+
if pos < len(args):
528+
raise TypeError(f"{cls.__name__} got multiple values for argument {key!r}")
530529
if cls in _const_types:
531530
return Constant(*args, **kwargs)
532531
return Constant.__new__(cls, *args, **kwargs)

Parser/asdl_c.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -720,17 +720,16 @@ def visitModule(self, mod):
720720
if (kw) {
721721
i = 0; /* needed by PyDict_Next */
722722
while (PyDict_Next(kw, &i, &key, &value)) {
723-
Py_ssize_t p = PySequence_Index(fields, key);
724-
PyObject* err = PyErr_Occurred();
725-
if (p == -1) {
726-
// arbitrary keyword arguments are accepted
727-
if (!PyErr_GivenExceptionMatches(err, PyExc_ValueError)) {
723+
int contains = PySequence_Contains(fields, key);
724+
if (contains == -1) {
725+
res = -1;
726+
goto cleanup;
727+
} else if (contains == 1) {
728+
Py_ssize_t p = PySequence_Index(fields, key);
729+
if (p == -1) {
728730
res = -1;
729731
goto cleanup;
730732
}
731-
PyErr_Clear();
732-
}
733-
else {
734733
if (p < PyTuple_GET_SIZE(args)) {
735734
PyErr_Format(PyExc_TypeError,
736735
"%.400s got multiple values for argument '%U'",

Python/Python-ast.c

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)