Skip to content

Commit 919045c

Browse files
authored
[3.10] gh-85267: Improvements to inspect.signature __text_signature__ handling (GH-98796) (#100393)
This makes a couple related changes to inspect.signature's behaviour when parsing a signature from `__text_signature__`. First, `inspect.signature` is documented as only raising ValueError or TypeError. However, in some cases, we could raise RuntimeError. This PR changes that, thereby fixing GH-83685. (Note that the new ValueErrors in RewriteSymbolics are caught and then reraised with a message) Second, `inspect.signature` could randomly drop parameters that it didn't understand (corresponding to `return None` in the `p` function). This is the core issue in GH-85267. I think this is very surprising behaviour and it seems better to fail outright. Third, adding this new failure broke a couple tests. To fix them (and to e.g. allow `inspect.signature(select.epoll.register)` as in GH-85267), I add constant folding of a couple binary operations to RewriteSymbolics. (There's some discussion of making signature expression evaluation arbitrary powerful in GH-68155. I think that's out of scope. The additional constant folding here is pretty straightforward, useful, and not much of a slippery slope) Fourth, while GH-85267 is incorrect about the cause of the issue, it turns out if you had consecutive newlines in __text_signature__, you'd get `tokenize.TokenError`. Finally, the `if name is invalid:` code path was dead, since `parse_name` never returned `invalid`.. (cherry picked from commit 79311cb) Co-authored-by: Shantanu <[email protected]>
1 parent 561e15b commit 919045c

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

Lib/inspect.py

+21-12
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,7 @@ def _signature_strip_non_python_syntax(signature):
20612061
self_parameter = None
20622062
last_positional_only = None
20632063

2064-
lines = [l.encode('ascii') for l in signature.split('\n')]
2064+
lines = [l.encode('ascii') for l in signature.split('\n') if l]
20652065
generator = iter(lines).__next__
20662066
token_stream = tokenize.tokenize(generator)
20672067

@@ -2141,7 +2141,6 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
21412141

21422142
parameters = []
21432143
empty = Parameter.empty
2144-
invalid = object()
21452144

21462145
module = None
21472146
module_dict = {}
@@ -2165,11 +2164,11 @@ def wrap_value(s):
21652164
try:
21662165
value = eval(s, sys_module_dict)
21672166
except NameError:
2168-
raise RuntimeError()
2167+
raise ValueError
21692168

21702169
if isinstance(value, (str, int, float, bytes, bool, type(None))):
21712170
return ast.Constant(value)
2172-
raise RuntimeError()
2171+
raise ValueError
21732172

21742173
class RewriteSymbolics(ast.NodeTransformer):
21752174
def visit_Attribute(self, node):
@@ -2179,7 +2178,7 @@ def visit_Attribute(self, node):
21792178
a.append(n.attr)
21802179
n = n.value
21812180
if not isinstance(n, ast.Name):
2182-
raise RuntimeError()
2181+
raise ValueError
21832182
a.append(n.id)
21842183
value = ".".join(reversed(a))
21852184
return wrap_value(value)
@@ -2189,19 +2188,29 @@ def visit_Name(self, node):
21892188
raise ValueError()
21902189
return wrap_value(node.id)
21912190

2191+
def visit_BinOp(self, node):
2192+
# Support constant folding of a couple simple binary operations
2193+
# commonly used to define default values in text signatures
2194+
left = self.visit(node.left)
2195+
right = self.visit(node.right)
2196+
if not isinstance(left, ast.Constant) or not isinstance(right, ast.Constant):
2197+
raise ValueError
2198+
if isinstance(node.op, ast.Add):
2199+
return ast.Constant(left.value + right.value)
2200+
elif isinstance(node.op, ast.Sub):
2201+
return ast.Constant(left.value - right.value)
2202+
elif isinstance(node.op, ast.BitOr):
2203+
return ast.Constant(left.value | right.value)
2204+
raise ValueError
2205+
21922206
def p(name_node, default_node, default=empty):
21932207
name = parse_name(name_node)
2194-
if name is invalid:
2195-
return None
21962208
if default_node and default_node is not _empty:
21972209
try:
21982210
default_node = RewriteSymbolics().visit(default_node)
2199-
o = ast.literal_eval(default_node)
2211+
default = ast.literal_eval(default_node)
22002212
except ValueError:
2201-
o = invalid
2202-
if o is invalid:
2203-
return None
2204-
default = o if o is not invalid else default
2213+
raise ValueError("{!r} builtin has invalid signature".format(obj)) from None
22052214
parameters.append(Parameter(name, kind, default=default, annotation=empty))
22062215

22072216
# non-keyword-only parameters

Lib/test/test_inspect.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -2477,7 +2477,7 @@ def p(name): return signature.parameters[name].default
24772477
self.assertEqual(p('f'), False)
24782478
self.assertEqual(p('local'), 3)
24792479
self.assertEqual(p('sys'), sys.maxsize)
2480-
self.assertNotIn('exp', signature.parameters)
2480+
self.assertEqual(p('exp'), sys.maxsize - 1)
24812481

24822482
test_callable(object)
24832483

@@ -4246,10 +4246,29 @@ def func(*args, **kwargs):
42464246
sig = inspect.signature(func)
42474247
self.assertIsNotNone(sig)
42484248
self.assertEqual(str(sig), '(self, /, a, b=1, *args, c, d=2, **kwargs)')
4249+
42494250
func.__text_signature__ = '($self, a, b=1, /, *args, c, d=2, **kwargs)'
42504251
sig = inspect.signature(func)
42514252
self.assertEqual(str(sig), '(self, a, b=1, /, *args, c, d=2, **kwargs)')
42524253

4254+
func.__text_signature__ = '(self, a=1+2, b=4-3, c=1 | 3 | 16)'
4255+
sig = inspect.signature(func)
4256+
self.assertEqual(str(sig), '(self, a=3, b=1, c=19)')
4257+
4258+
func.__text_signature__ = '(self, a=1,\nb=2,\n\n\n c=3)'
4259+
sig = inspect.signature(func)
4260+
self.assertEqual(str(sig), '(self, a=1, b=2, c=3)')
4261+
4262+
func.__text_signature__ = '(self, x=does_not_exist)'
4263+
with self.assertRaises(ValueError):
4264+
inspect.signature(func)
4265+
func.__text_signature__ = '(self, x=sys, y=inspect)'
4266+
with self.assertRaises(ValueError):
4267+
inspect.signature(func)
4268+
func.__text_signature__ = '(self, 123)'
4269+
with self.assertRaises(ValueError):
4270+
inspect.signature(func)
4271+
42534272
def test_base_class_have_text_signature(self):
42544273
# see issue 43118
42554274
from test.ann_module7 import BufferedReader
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Several improvements to :func:`inspect.signature`'s handling of ``__text_signature``.
2+
- Fixes a case where :func:`inspect.signature` dropped parameters
3+
- Fixes a case where :func:`inspect.signature` raised :exc:`tokenize.TokenError`
4+
- Allows :func:`inspect.signature` to understand defaults involving binary operations of constants
5+
- :func:`inspect.signature` is documented as only raising :exc:`TypeError` or :exc:`ValueError`, but sometimes raised :exc:`RuntimeError`. These cases now raise :exc:`ValueError`
6+
- Removed a dead code path

0 commit comments

Comments
 (0)