diff --git a/Lib/inspect.py b/Lib/inspect.py --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1975,6 +1975,28 @@ parameters = [] empty = Parameter.empty + def parse_name(node): + id = node.id + ctx = node.ctx + if (not isinstance(ctx, ast.Load)) or (id not in sys.modules): + return None + return sys.modules[id] + + def parse_attribute(node): + if not isinstance(node.ctx, ast.Load): + return None + value = node.value + attr = node.attr + if isinstance(value, ast.Attribute): + o = parse_attribute(value) + elif isinstance(value, ast.Name): + o = parse_name(value) + else: + return None + if not (o and hasattr(o, attr)): + return None + return getattr(o, attr) + def p(name_node, default_node, default=empty): name = name_node.arg @@ -1982,6 +2004,8 @@ default = default.n elif isinstance(default_node, ast.NameConstant): default = default_node.value + elif isinstance(default_node, ast.Attribute): + default = parse_attribute(default_node) parameters.append(Parameter(name, kind, default=default, annotation=empty)) # non-keyword-only parameters diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -2430,7 +2430,7 @@ [clinic]*/ PyDoc_STRVAR(os_stat__doc__, -"stat(path, *, dir_fd=None, follow_symlinks=True)\n" +"stat(path, *, dir_fd=None, follow_symlinks=True, fake=sys.maxsize)\n" "Perform a stat system call on the given path.\n" "\n" " path\n" diff --git a/x.py b/x.py new file mode 100644 --- /dev/null +++ b/x.py @@ -0,0 +1,12 @@ +import ast +import os +import inspect + +fn = os.stat + +try: + print(inspect.signature(fn)) +except Exception as e: + print(e) + +# print(ast.dump(ast.parse("def foo" + fn.__text_signature__ + ": pass")))