From 9fc3c7a2deaf42472f8ac813a2d50beb59ab4ffd Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 10 Jul 2020 00:14:37 +0200 Subject: [PATCH] inspect: BlockFinder: handle nested parens with decorators Fixes: ``` import inspect def deco(*args, **kwargs): def wrapped(f): return f return wrapped @deco((lambda: True), (lambda: False)) def func(): pass print(inspect.getsource(func)) ``` Without this patch it would only print: > @deco((lambda: True), (lambda: False)) --- Lib/inspect.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 887a3424057b6e..ed4764948ac081 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -925,6 +925,7 @@ def __init__(self): self.passline = False self.indecorator = False self.decoratorhasargs = False + self.nestedparens = 0 self.last = 1 def tokeneater(self, type, token, srowcol, erowcol, line): @@ -941,10 +942,15 @@ def tokeneater(self, type, token, srowcol, erowcol, line): elif token == "(": if self.indecorator: self.decoratorhasargs = True + self.nestedparens += 1 elif token == ")": if self.indecorator: - self.indecorator = False - self.decoratorhasargs = False + self.nestedparens -= 1 + if self.nestedparens == 0: + self.indecorator = False + self.decoratorhasargs = False + else: + assert self.nestedparens > 0, self.nestedparens elif type == tokenize.NEWLINE: self.passline = False # stop skipping when a NEWLINE is seen self.last = srowcol[0]