diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2019-06-01 17:08:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-01 17:08:04 (GMT) |
commit | cd74e66a8c420be675fd2fbf3fe708ac02ee9f21 (patch) | |
tree | 12f985512507967c339019c4c21b4a613cd6c61b /Lib | |
parent | 059b9ea5ac98f432e41b05d1fa5aab4ffa22df4d (diff) | |
download | cpython-cd74e66a8c420be675fd2fbf3fe708ac02ee9f21.zip cpython-cd74e66a8c420be675fd2fbf3fe708ac02ee9f21.tar.gz cpython-cd74e66a8c420be675fd2fbf3fe708ac02ee9f21.tar.bz2 |
bpo-37122: Make co->co_argcount represent the total number of positonal arguments in the code object (GH-13726)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/inspect.py | 28 | ||||
-rwxr-xr-x | Lib/pdb.py | 2 | ||||
-rw-r--r-- | Lib/test/test_code.py | 2 | ||||
-rw-r--r-- | Lib/test/test_dis.py | 2 | ||||
-rw-r--r-- | Lib/test/test_positional_only_arg.py | 4 |
5 files changed, 15 insertions, 23 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index ca81a24..91d209d 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1037,15 +1037,11 @@ def getargs(co): names = co.co_varnames nargs = co.co_argcount - nposonlyargs = co.co_posonlyargcount nkwargs = co.co_kwonlyargcount - nposargs = nargs + nposonlyargs - posonlyargs = list(names[:nposonlyargs]) - args = list(names[nposonlyargs:nposonlyargs+nargs]) - kwonlyargs = list(names[nposargs:nposargs+nkwargs]) + args = list(names[:nargs]) + kwonlyargs = list(names[nargs:nargs+nkwargs]) step = 0 - nargs += nposonlyargs nargs += nkwargs varargs = None if co.co_flags & CO_VARARGS: @@ -1054,7 +1050,7 @@ def getargs(co): varkw = None if co.co_flags & CO_VARKEYWORDS: varkw = co.co_varnames[nargs] - return Arguments(posonlyargs + args + kwonlyargs, varargs, varkw) + return Arguments(args + kwonlyargs, varargs, varkw) ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults') @@ -2136,11 +2132,9 @@ def _signature_from_function(cls, func, skip_bound_arg=True): pos_count = func_code.co_argcount arg_names = func_code.co_varnames posonly_count = func_code.co_posonlyargcount - positional_count = posonly_count + pos_count - positional_only = tuple(arg_names[:posonly_count]) - positional = tuple(arg_names[posonly_count:positional_count]) + positional = arg_names[:pos_count] keyword_only_count = func_code.co_kwonlyargcount - keyword_only = arg_names[positional_count:(positional_count + keyword_only_count)] + keyword_only = arg_names[pos_count:pos_count + keyword_only_count] annotations = func.__annotations__ defaults = func.__defaults__ kwdefaults = func.__kwdefaults__ @@ -2152,13 +2146,11 @@ def _signature_from_function(cls, func, skip_bound_arg=True): parameters = [] - non_default_count = positional_count - pos_default_count - all_positional = positional_only + positional - + non_default_count = pos_count - pos_default_count posonly_left = posonly_count # Non-keyword-only parameters w/o defaults. - for name in all_positional[:non_default_count]: + for name in positional[:non_default_count]: kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD annotation = annotations.get(name, _empty) parameters.append(Parameter(name, annotation=annotation, @@ -2167,7 +2159,7 @@ def _signature_from_function(cls, func, skip_bound_arg=True): posonly_left -= 1 # ... w/ defaults. - for offset, name in enumerate(all_positional[non_default_count:]): + for offset, name in enumerate(positional[non_default_count:]): kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD annotation = annotations.get(name, _empty) parameters.append(Parameter(name, annotation=annotation, @@ -2178,7 +2170,7 @@ def _signature_from_function(cls, func, skip_bound_arg=True): # *args if func_code.co_flags & CO_VARARGS: - name = arg_names[positional_count + keyword_only_count] + name = arg_names[pos_count + keyword_only_count] annotation = annotations.get(name, _empty) parameters.append(Parameter(name, annotation=annotation, kind=_VAR_POSITIONAL)) @@ -2195,7 +2187,7 @@ def _signature_from_function(cls, func, skip_bound_arg=True): default=default)) # **kwargs if func_code.co_flags & CO_VARKEYWORDS: - index = positional_count + keyword_only_count + index = pos_count + keyword_only_count if func_code.co_flags & CO_VARARGS: index += 1 @@ -1132,7 +1132,7 @@ class Pdb(bdb.Bdb, cmd.Cmd): """ co = self.curframe.f_code dict = self.curframe_locals - n = co.co_argcount + co.co_posonlyargcount + co.co_kwonlyargcount + n = co.co_argcount + co.co_kwonlyargcount if co.co_flags & inspect.CO_VARARGS: n = n+1 if co.co_flags & inspect.CO_VARKEYWORDS: n = n+1 for i in range(n): diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 91008c0..0d80af4 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -112,7 +112,7 @@ consts: ('None',) >>> dump(posonly_args.__code__) name: posonly_args -argcount: 1 +argcount: 3 posonlyargcount: 2 kwonlyargcount: 0 names: () diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 1561021..652af45 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -640,7 +640,7 @@ def tricky(a, b, /, x, y, z=True, *args, c, d, e=[], **kwds): code_info_tricky = """\ Name: tricky Filename: (.*) -Argument count: 3 +Argument count: 5 Positional-only arguments: 2 Kw-only arguments: 3 Number of locals: 10 diff --git a/Lib/test/test_positional_only_arg.py b/Lib/test/test_positional_only_arg.py index 0aaad84..59b0b8f 100644 --- a/Lib/test/test_positional_only_arg.py +++ b/Lib/test/test_positional_only_arg.py @@ -100,14 +100,14 @@ class PositionalOnlyTestCase(unittest.TestCase): def f(a, b, c, /, d, e=1, *, f, g=2): pass - self.assertEqual(2, f.__code__.co_argcount) # 2 "standard args" + self.assertEqual(5, f.__code__.co_argcount) # 3 posonly + 2 "standard args" self.assertEqual(3, f.__code__.co_posonlyargcount) self.assertEqual((1,), f.__defaults__) def f(a, b, c=1, /, d=2, e=3, *, f, g=4): pass - self.assertEqual(2, f.__code__.co_argcount) # 2 "standard args" + self.assertEqual(5, f.__code__.co_argcount) # 3 posonly + 2 "standard args" self.assertEqual(3, f.__code__.co_posonlyargcount) self.assertEqual((1, 2, 3), f.__defaults__) |