gh-85267: Improvements to inspect.signature text_signature handli… · python/cpython@79311cb (original) (raw)

`@@ -2122,7 +2122,7 @@ def _signature_strip_non_python_syntax(signature):

`

2122

2122

`self_parameter = None

`

2123

2123

`last_positional_only = None

`

2124

2124

``

2125

``

`-

lines = [l.encode('ascii') for l in signature.split('\n')]

`

``

2125

`+

lines = [l.encode('ascii') for l in signature.split('\n') if l]

`

2126

2126

`generator = iter(lines).next

`

2127

2127

`token_stream = tokenize.tokenize(generator)

`

2128

2128

``

`@@ -2221,11 +2221,11 @@ def wrap_value(s):

`

2221

2221

`try:

`

2222

2222

`value = eval(s, sys_module_dict)

`

2223

2223

`except NameError:

`

2224

``

`-

raise RuntimeError()

`

``

2224

`+

raise ValueError

`

2225

2225

``

2226

2226

`if isinstance(value, (str, int, float, bytes, bool, type(None))):

`

2227

2227

`return ast.Constant(value)

`

2228

``

`-

raise RuntimeError()

`

``

2228

`+

raise ValueError

`

2229

2229

``

2230

2230

`class RewriteSymbolics(ast.NodeTransformer):

`

2231

2231

`def visit_Attribute(self, node):

`

`@@ -2235,7 +2235,7 @@ def visit_Attribute(self, node):

`

2235

2235

`a.append(n.attr)

`

2236

2236

`n = n.value

`

2237

2237

`if not isinstance(n, ast.Name):

`

2238

``

`-

raise RuntimeError()

`

``

2238

`+

raise ValueError

`

2239

2239

`a.append(n.id)

`

2240

2240

`value = ".".join(reversed(a))

`

2241

2241

`return wrap_value(value)

`

`@@ -2245,14 +2245,29 @@ def visit_Name(self, node):

`

2245

2245

`raise ValueError()

`

2246

2246

`return wrap_value(node.id)

`

2247

2247

``

``

2248

`+

def visit_BinOp(self, node):

`

``

2249

`+

Support constant folding of a couple simple binary operations

`

``

2250

`+

commonly used to define default values in text signatures

`

``

2251

`+

left = self.visit(node.left)

`

``

2252

`+

right = self.visit(node.right)

`

``

2253

`+

if not isinstance(left, ast.Constant) or not isinstance(right, ast.Constant):

`

``

2254

`+

raise ValueError

`

``

2255

`+

if isinstance(node.op, ast.Add):

`

``

2256

`+

return ast.Constant(left.value + right.value)

`

``

2257

`+

elif isinstance(node.op, ast.Sub):

`

``

2258

`+

return ast.Constant(left.value - right.value)

`

``

2259

`+

elif isinstance(node.op, ast.BitOr):

`

``

2260

`+

return ast.Constant(left.value | right.value)

`

``

2261

`+

raise ValueError

`

``

2262

+

2248

2263

`def p(name_node, default_node, default=empty):

`

2249

2264

`name = parse_name(name_node)

`

2250

2265

`if default_node and default_node is not _empty:

`

2251

2266

`try:

`

2252

2267

`default_node = RewriteSymbolics().visit(default_node)

`

2253

2268

`default = ast.literal_eval(default_node)

`

2254

2269

`except ValueError:

`

2255

``

`-

return None

`

``

2270

`+

raise ValueError("{!r} builtin has invalid signature".format(obj)) from None

`

2256

2271

`parameters.append(Parameter(name, kind, default=default, annotation=empty))

`

2257

2272

``

2258

2273

`# non-keyword-only parameters

`