Skip to content

Commit 3b23004

Browse files
Issue python#29354: Fixed inspect.getargs() for parameters which are cell
variables.
1 parent 8e21cc3 commit 3b23004

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

Lib/inspect.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,11 @@ def getargs(co):
769769
if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
770770
remain.append(value)
771771
count.append(value)
772-
elif opname == 'STORE_FAST':
773-
stack.append(names[value])
772+
elif opname in ('STORE_FAST', 'STORE_DEREF'):
773+
if opname == 'STORE_FAST':
774+
stack.append(names[value])
775+
else:
776+
stack.append(co.co_cellvars[value])
774777

775778
# Special case for sublists of length 1: def foo((bar))
776779
# doesn't generate the UNPACK_TUPLE bytecode, so if

Lib/test/test_inspect.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,15 @@ def test_getargspec(self):
502502
'g', 'h', (3, (4, (5,))),
503503
'(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)')
504504

505+
def spam_deref(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h):
506+
def eggs():
507+
return a + b + c + d + e + f + g + h
508+
return eggs
509+
self.assertArgSpecEquals(spam_deref,
510+
['a', 'b', 'c', 'd', ['e', ['f']]],
511+
'g', 'h', (3, (4, (5,))),
512+
'(a, b, c, d=3, (e, (f,))=(4, (5,)), *g, **h)')
513+
505514
def test_getargspec_method(self):
506515
class A(object):
507516
def m(self):
@@ -515,9 +524,15 @@ def test_getargspec_sublistofone(self):
515524
exec 'def sublistOfOne((foo,)): return 1'
516525
self.assertArgSpecEquals(sublistOfOne, [['foo']])
517526

527+
exec 'def sublistOfOne((foo,)): return (lambda: foo)'
528+
self.assertArgSpecEquals(sublistOfOne, [['foo']])
529+
518530
exec 'def fakeSublistOfOne((foo)): return 1'
519531
self.assertArgSpecEquals(fakeSublistOfOne, ['foo'])
520532

533+
exec 'def sublistOfOne((foo)): return (lambda: foo)'
534+
self.assertArgSpecEquals(sublistOfOne, ['foo'])
535+
521536

522537
def _classify_test(self, newstyle):
523538
"""Helper for testing that classify_class_attrs finds a bunch of
@@ -820,6 +835,23 @@ def test_errors(self):
820835
self.assertEqualException(f3, '1, 2')
821836
self.assertEqualException(f3, '1, 2, a=1, b=2')
822837

838+
839+
class TestGetcallargsFunctionsCellVars(TestGetcallargsFunctions):
840+
841+
def makeCallable(self, signature):
842+
"""Create a function that returns its locals(), excluding the
843+
autogenerated '.1', '.2', etc. tuple param names (if any)."""
844+
with check_py3k_warnings(
845+
("tuple parameter unpacking has been removed", SyntaxWarning),
846+
quiet=True):
847+
code = """lambda %s: (
848+
(lambda: a+b+c+d+d+e+f+g+h), # make parameters cell vars
849+
dict(i for i in locals().items()
850+
if not is_tuplename(i[0]))
851+
)[1]"""
852+
return eval(code % signature, {'is_tuplename' : self.is_tuplename})
853+
854+
823855
class TestGetcallargsMethods(TestGetcallargsFunctions):
824856

825857
def setUp(self):
@@ -857,8 +889,8 @@ def test_main():
857889
run_unittest(
858890
TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
859891
TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
860-
TestGetcallargsFunctions, TestGetcallargsMethods,
861-
TestGetcallargsUnboundMethods)
892+
TestGetcallargsFunctions, TestGetcallargsFunctionsCellVars,
893+
TestGetcallargsMethods, TestGetcallargsUnboundMethods)
862894

863895
if __name__ == "__main__":
864896
test_main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Extension Modules
2626
Library
2727
-------
2828

29+
- Issue #29354: Fixed inspect.getargs() for parameters which are cell
30+
variables.
31+
2932
- Issue #29335: Fix subprocess.Popen.wait() when the child process has
3033
exited to a stopped instead of terminated state (ex: when under ptrace).
3134

0 commit comments

Comments
 (0)