From 0f8a3f8db97ed2f606ac292eed2b83e8abbdb188 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Sun, 27 Feb 2022 22:41:23 +0200 Subject: [PATCH] Fix inspect.getsource - Count parentheses open/close in decorators --- Lib/inspect.py | 13 +++++++++---- Lib/test/inspect_fodder3.py | 9 +++++++++ Lib/test/test_inspect.py | 7 +++++++ 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 Lib/test/inspect_fodder3.py diff --git a/Lib/inspect.py b/Lib/inspect.py index 9c1283ab3734bf..36d1bcbfa9b5d5 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1174,9 +1174,13 @@ def __init__(self): self.started = False self.passline = False self.indecorator = False - self.decoratorhasargs = False self.last = 1 self.body_col0 = None + self._decoratorhasargs_count = 0 + + @property + def decoratorhasargs(self): + return bool(self._decoratorhasargs_count) def tokeneater(self, type, token, srowcol, erowcol, line): if not self.started and not self.indecorator: @@ -1191,11 +1195,12 @@ def tokeneater(self, type, token, srowcol, erowcol, line): self.passline = True # skip to the end of the line elif token == "(": if self.indecorator: - self.decoratorhasargs = True + self._decoratorhasargs_count += 1 elif token == ")": if self.indecorator: - self.indecorator = False - self.decoratorhasargs = False + self._decoratorhasargs_count -= 1 + if not self.decoratorhasargs: + self.indecorator = False elif type == tokenize.NEWLINE: self.passline = False # stop skipping when a NEWLINE is seen self.last = srowcol[0] diff --git a/Lib/test/inspect_fodder3.py b/Lib/test/inspect_fodder3.py new file mode 100644 index 00000000000000..20bb55c02414cd --- /dev/null +++ b/Lib/test/inspect_fodder3.py @@ -0,0 +1,9 @@ +# line 1 +def wrap_many(*foo): + def wrapper(func): + return func + return wrapper + +@wrap_many(lambda: bool(True), lambda: True) +def func8(): + return 9 diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 9e3c77056d70a0..6a7ba94ffa57cb 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -31,6 +31,7 @@ from test.support.script_helper import assert_python_ok, assert_python_failure from test import inspect_fodder as mod from test import inspect_fodder2 as mod2 +from test import inspect_fodder3 as mod3 from test import support from test import inspect_stock_annotations from test import inspect_stringized_annotations @@ -673,6 +674,12 @@ def test_getsource_unwrap(self): def test_decorator_with_lambda(self): self.assertSourceEqual(mod2.func114, 113, 115) +class TestDecoratorsMulti(GetSourceBase): + fodderModule = mod3 + + def test_decorator_with_multiple_lambdas_with_calls(self): + self.assertSourceEqual(mod3.func8, 7, 9) + class TestOneliners(GetSourceBase): fodderModule = mod2 def test_oneline_lambda(self): pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy