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

bpo-33165: Remove redundant stack unwind for findCaller() - #17714

Closed
evandrocoan wants to merge 2 commits into
python:mainfrom
evandroforks:f_currentframe
Closed

evandrocoan wants to merge 2 commits into
python:mainfrom
evandroforks:f_currentframe

Conversation

evandrocoan commented Dec 27, 2019 •
edited by bedevere-bot
Loading

Copy link
Copy Markdown

by directly getting the right frame on Lib/logging/init.py:currentframe()

This is a slight improvement for #7424 (bpo-33165: Added stacklevel parameter to logging APIs)

Instead of getting the fullstack trace and only then unwind the desired frames, just pass the required frame index and get it directly, i.e., without "unstacking" n frames.

I also added the frame level as a Logger 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

https://bugs.python.org/issue33165

evandrocoan requested a review from vsajip as a code owner December 27, 2019 14:15
evandrocoan added a commit to evandrocoan/debugtools that referenced this pull request Dec 27, 2019
evandrocoan added a commit to evandrocoan/debugtools that referenced this pull request Dec 27, 2019
Comment thread Lib/logging/__init__.py
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.

Comment thread Lib/logging/__init__.py
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

Copy link
Copy Markdown
Contributor

This PR conflicts with #28287. I believe it is best to close/reject the current PR in favor of #28287.

Copy link
Copy Markdown
Author

This was already fixed/implemented by dde9fdb

evandrocoan deleted the f_currentframe branch September 11, 2021 14:56
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL