Skip to content

Commit fc46e56

Browse files
committed
don't misuse ast.Constant() in the inspect module
1 parent 53e9620 commit fc46e56

2 files changed

Lines changed: 13 additions & 23 deletions

File tree

Lib/inspect.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,7 +2209,7 @@ def wrap_value(s):
22092209

22102210
if isinstance(value, (str, int, float, bytes, bool, type(None),
22112211
sentinel)):
2212-
return ast.Constant(value)
2212+
return ast.parse(s)
22132213
raise ValueError
22142214

22152215
class RewriteSymbolics(ast.NodeTransformer):
@@ -2230,28 +2230,20 @@ def visit_Name(self, node):
22302230
raise ValueError()
22312231
return wrap_value(node.id)
22322232

2233-
def visit_BinOp(self, node):
2234-
# Support constant folding of a couple simple binary operations
2235-
# commonly used to define default values in text signatures
2236-
left = self.visit(node.left)
2237-
right = self.visit(node.right)
2238-
if not isinstance(left, ast.Constant) or not isinstance(right, ast.Constant):
2239-
raise ValueError
2240-
if isinstance(node.op, ast.Add):
2241-
return ast.Constant(left.value + right.value)
2242-
elif isinstance(node.op, ast.Sub):
2243-
return ast.Constant(left.value - right.value)
2244-
elif isinstance(node.op, ast.BitOr):
2245-
return ast.Constant(left.value | right.value)
2246-
raise ValueError
2247-
22482233
def p(name_node, default_node, default=empty):
22492234
name = parse_name(name_node)
22502235
if default_node and default_node is not _empty:
22512236
try:
22522237
default_node = RewriteSymbolics().visit(default_node)
2253-
default = ast.literal_eval(default_node)
2254-
except ValueError:
2238+
default_source = ast.unparse(default_node)
2239+
try:
2240+
default = eval(default_source, module_dict)
2241+
except NameError:
2242+
try:
2243+
default = eval(default_source, sys_module_dict)
2244+
except NameError:
2245+
raise ValueError
2246+
except ValueError as exc:
22552247
raise ValueError("{!r} builtin has invalid signature".format(obj)) from None
22562248
parameters.append(Parameter(name, kind, default=default, annotation=empty))
22572249

Lib/test/test_inspect/test_inspect.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6287,9 +6287,9 @@ def test_operator_module_has_signatures(self):
62876287
self._test_module_has_signatures(operator)
62886288

62896289
def test_os_module_has_signatures(self):
6290-
unsupported_signature = {'chmod', 'utime'}
6290+
unsupported_signature = {'utime'}
62916291
unsupported_signature |= {name for name in
6292-
['get_terminal_size', 'link', 'register_at_fork', 'startfile']
6292+
['get_terminal_size', 'register_at_fork', 'startfile']
62936293
if hasattr(os, name)}
62946294
self._test_module_has_signatures(os, unsupported_signature=unsupported_signature)
62956295

@@ -6339,9 +6339,7 @@ def test_threading_module_has_signatures(self):
63396339
def test_thread_module_has_signatures(self):
63406340
import _thread
63416341
no_signature = {'RLock'}
6342-
unsupported_signature = {'interrupt_main'}
6343-
self._test_module_has_signatures(_thread, no_signature,
6344-
unsupported_signature)
6342+
self._test_module_has_signatures(_thread, no_signature)
63456343

63466344
def test_time_module_has_signatures(self):
63476345
no_signature = {

0 commit comments

Comments
 (0)