]> granicus.if.org Git - python/commitdiff
inspect: Fix getfullargspec to support builtin module-level functions. Issue #20711
authorYury Selivanov <yselivanov@sprymix.com>
Fri, 21 Feb 2014 06:32:42 +0000 (01:32 -0500)
committerYury Selivanov <yselivanov@sprymix.com>
Fri, 21 Feb 2014 06:32:42 +0000 (01:32 -0500)
Lib/inspect.py
Lib/test/test_inspect.py

index 8b7840a1a0ed7c9f0f6f4753784cebee9a2f4750..b85fbcca6d86dcded3f546eca378bc4af2e936d1 100644 (file)
@@ -1827,9 +1827,16 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
         p(f.args.kwarg, empty)
 
     if self_parameter is not None:
+        # Possibly strip the bound argument:
+        #    - We *always* strip first bound argument if
+        #      it is a module.
+        #    - We don't strip first bound argument if
+        #      skip_bound_arg is False.
         assert parameters
-        if getattr(obj, '__self__', None) and skip_bound_arg:
-            # strip off self, it's already been bound
+        _self = getattr(obj, '__self__', None)
+        self_isbound = _self is not None
+        self_ismodule = ismodule(_self)
+        if self_isbound and (self_ismodule or skip_bound_arg):
             parameters.pop(0)
         else:
             # for builtins, self parameter is always positional-only!
index 711d2a3a6ba7172d13f72fa039ff7d49d73a63d6..95c1b324fb6bd58b5a52439c3936e8b053584c9e 100644 (file)
@@ -643,6 +643,13 @@ class TestClassesAndFunctions(unittest.TestCase):
         self.assertFullArgSpecEquals(_pickle.Pickler(io.BytesIO()).dump,
                                      args_e=['self', 'obj'], formatted='(self, obj)')
 
+        self.assertFullArgSpecEquals(
+             os.stat,
+             args_e=['path'],
+             kwonlyargs_e=['dir_fd', 'follow_symlinks'],
+             kwonlydefaults_e={'dir_fd': None, 'follow_symlinks': True},
+             formatted='(path, *, dir_fd=None, follow_symlinks=True)')
+
     @cpython_only
     @unittest.skipIf(MISSING_C_DOCSTRINGS,
                      "Signature information for builtins requires docstrings")