From 0a87186030c07cb54f50488c01bd78c05662b498 Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Fri, 27 Dec 2019 10:53:08 -0300 Subject: [PATCH 1/2] Remove redundant stack unwind for findCaller() by directly getting the right frame on Lib/logging/__init__.py:currentframe() --- Lib/logging/__init__.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 62a87a71b1a3bd..ad873de08feacc 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -153,14 +153,14 @@ def addLevelName(level, levelName): _releaseLock() if hasattr(sys, '_getframe'): - currentframe = lambda: sys._getframe(3) + currentframe = lambda level: sys._getframe(level) else: #pragma: no cover - def currentframe(): + def currentframe(level): """Return the frame object for the caller's stack frame.""" try: raise Exception except Exception: - return sys.exc_info()[2].tb_frame.f_back + return sys.exc_info()[level-1].tb_frame.f_back # # _srcfile is used when walking the stack to check when we've got the first @@ -1506,17 +1506,11 @@ def findCaller(self, stack_info=False, stacklevel=1): Find the stack frame of the caller so that we can note the source file name, line number and function name. """ - f = currentframe() + f = currentframe(2 + stacklevel) #On some versions of IronPython, currentframe() returns None if #IronPython isn't run with -X:Frames. if f is not None: f = f.f_back - orig_f = f - while f and stacklevel > 1: - f = f.f_back - stacklevel -= 1 - if not f: - f = orig_f rv = "(unknown file)", 0, "(unknown function)", None while hasattr(f, "f_code"): co = f.f_code From 51ef33f2e91bc4e07bf4084bef3dcecc97621983 Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Fri, 27 Dec 2019 11:09:10 -0300 Subject: [PATCH 2/2] Add the logging.py findCaller() stacklevel as a debugger attribute because when extending the default Logger and implementing/specializing the `_log` function, all stacktraces need to be increased by 1. For example, the debug_tools pypi module inherits from Logger and defines its own _log() function: https://github.com/evandrocoan/debugtools/blob/d279bf3278f501294a72159f3aa189b7237528b2/all/debug_tools/logger.py#L166 https://github.com/evandrocoan/debugtools/blob/d279bf3278f501294a72159f3aa189b7237528b2/all/debug_tools/logger.py#L970 https://github.com/evandrocoan/debugtools/blob/d279bf3278f501294a72159f3aa189b7237528b2/all/debug_tools/logger.py#L1317 --- Lib/logging/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index ad873de08feacc..adcf6bf773374a 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1403,6 +1403,7 @@ def __init__(self, name, level=NOTSET): self.handlers = [] self.disabled = False self._cache = {} + self.default_stack_level = 2 def setLevel(self, level): """ @@ -1506,7 +1507,7 @@ def findCaller(self, stack_info=False, stacklevel=1): Find the stack frame of the caller so that we can note the source file name, line number and function name. """ - f = currentframe(2 + stacklevel) + f = currentframe(self.default_stack_level + stacklevel) #On some versions of IronPython, currentframe() returns None if #IronPython isn't run with -X:Frames. if f is not None: