diff options
author | Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com> | 2022-05-03 20:52:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-03 20:52:30 (GMT) |
commit | 65f88a6ef74c9b01017438e88e31570b02f1df9c (patch) | |
tree | 4ba6a919751b01f9b824ef836f491d899ac8362b /Lib/inspect.py | |
parent | 3a35b62ea003182c643516098cc781944d425800 (diff) | |
download | cpython-65f88a6ef74c9b01017438e88e31570b02f1df9c.zip cpython-65f88a6ef74c9b01017438e88e31570b02f1df9c.tar.gz cpython-65f88a6ef74c9b01017438e88e31570b02f1df9c.tar.bz2 |
gh-92062: `inspect.Parameter` checks whether `name` is a keyword (GH-92065)
Fixes #92062.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 5bc9c04..6e74471 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -149,6 +149,7 @@ import token import types import functools import builtins +from keyword import iskeyword from operator import attrgetter from collections import namedtuple, OrderedDict @@ -1645,7 +1646,7 @@ class Traceback(_Traceback): instance = super().__new__(cls, filename, lineno, function, code_context, index) instance.positions = positions return instance - + def __repr__(self): return ('Traceback(filename={!r}, lineno={!r}, function={!r}, ' 'code_context={!r}, index={!r}, positions={!r})'.format( @@ -1683,7 +1684,7 @@ def getframeinfo(frame, context=1): frame, *positions = (frame, lineno, *positions[1:]) else: frame, *positions = (frame, *positions) - + lineno = positions[0] if not isframe(frame): @@ -2707,7 +2708,10 @@ class Parameter: self._kind = _POSITIONAL_ONLY name = 'implicit{}'.format(name[1:]) - if not name.isidentifier(): + # It's possible for C functions to have a positional-only parameter + # where the name is a keyword, so for compatibility we'll allow it. + is_keyword = iskeyword(name) and self._kind is not _POSITIONAL_ONLY + if is_keyword or not name.isidentifier(): raise ValueError('{!r} is not a valid parameter name'.format(name)) self._name = name |