FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-33165: Remove redundant stack unwind for findCaller() by evandrocoan · Pull Request #17714 · python/cpython · GitHub

Repository navigation

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
15 changes: 5 additions & 10 deletions Lib/logging/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I believe this is incorrect. exc_info returns three items and probably all we can do is

Suggested change
return sys.exc_info()[level-1].tb_frame.f_back
frame = sys.exc_info()[2].tb_frame.f_back
while frame and stacklevel > 0:
frame = frame.f_back
stacklevel -= 1
if not frame:
raise ValueError("call stack is not deep enough")
return frame


#
# _srcfile is used when walking the stack to check when we've got the first
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -1506,17 +1507,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(self.default_stack_level + stacklevel)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Shouldn't the unwinding really be done later? A few lines down, we read:

        while hasattr(f, "f_code"):
            co = f.f_code
            filename = os.path.normcase(co.co_filename)
            if filename == _srcfile:
                f = f.f_back
                continue

This 'escapes' the stack frames generated inside the current file. I would expect any additional stacklevel treatment to happen afterwards.

As a result of the current implementation (with or without the proposed changes in the current PR), the stacklevel parameter to .warn must be one higher than the parameter to .warning in order to get to the same frame.

#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
Expand Down

Back | FazBrowse Home | New Git URL