diff --git a/Lib/inspect.py b/Lib/inspect.py index 8bb3a37573..3b3a2f5bd4 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2213,6 +2213,8 @@ def parse_name(node): return node.arg def wrap_value(s): + if isinstance(s, (int, float, bytes, bool, type(None))): + return ast.Constant(s) try: value = eval(s, module_dict) except NameError: @@ -2221,8 +2223,8 @@ def wrap_value(s): except NameError: raise ValueError - if isinstance(value, (str, int, float, bytes, bool, type(None))): - return ast.Constant(value) + if isinstance(s, str): + return ast.Name(s) raise ValueError class RewriteSymbolics(ast.NodeTransformer): @@ -2263,7 +2265,13 @@ def p(name_node, default_node, default=empty): if default_node and default_node is not _empty: try: default_node = RewriteSymbolics().visit(default_node) - default = ast.literal_eval(default_node) + try: + default = ast.literal_eval(default_node) + except ValueError: + if isinstance(default_node, ast.Name): + default = default_node + else: + raise except ValueError: raise ValueError("{!r} builtin has invalid signature".format(obj)) from None parameters.append(Parameter(name, kind, default=default, annotation=empty)) @@ -2788,11 +2796,16 @@ def __str__(self): formatted = '{}: {}'.format(formatted, formatannotation(self._annotation)) + if isinstance(self._default, ast.Name): + default = self._default.id + else: + default = repr(self._default) + if self._default is not _empty: if self._annotation is not _empty: - formatted = '{} = {}'.format(formatted, repr(self._default)) + formatted = '{} = {}'.format(formatted, default) else: - formatted = '{}={}'.format(formatted, repr(self._default)) + formatted = '{}={}'.format(formatted, default) if kind == _VAR_POSITIONAL: formatted = '*' + formatted diff --git a/Modules/clinic/cmathmodule.c.h b/Modules/clinic/cmathmodule.c.h index b1da9452c6..dc0bae51a5 100644 --- a/Modules/clinic/cmathmodule.c.h +++ b/Modules/clinic/cmathmodule.c.h @@ -639,7 +639,7 @@ exit: } PyDoc_STRVAR(cmath_log__doc__, -"log($module, z, base=, /)\n" +"log($module, z, base=e, /)\n" "--\n" "\n" "log(z[, base]) -> the logarithm of z to the given base.\n" @@ -982,4 +982,4 @@ skip_optional_kwonly: exit: return return_value; } -/*[clinic end generated code: output=0146c656e67f5d5f input=a9049054013a1b77]*/ +/*[clinic end generated code: output=4bef798226126327 input=a9049054013a1b77]*/ diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c index 2038ac26e6..861a824d77 100644 --- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -952,7 +952,7 @@ cmath_tanh_impl(PyObject *module, Py_complex z) cmath.log z as x: Py_complex - base as y_obj: object = NULL + base as y_obj: object(c_default="NULL") = e / log(z[, base]) -> the logarithm of z to the given base. @@ -962,7 +962,7 @@ If the base not specified, returns the natural logarithm (base e) of z. static PyObject * cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj) -/*[clinic end generated code: output=4effdb7d258e0d94 input=230ed3a71ecd000a]*/ +/*[clinic end generated code: output=4effdb7d258e0d94 input=beb2861c8206a73c]*/ { Py_complex y;