Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-96092: restore frame height of traceback.walk_stack to that of pyt…#99015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
graingert wants to merge4 commits intopython:main
base:main
Choose a base branch
Loading
fromgraingert:fix-walk-stack

Conversation

@graingert
Copy link
Contributor

@graingertgraingert commentedNov 2, 2022
edited by bedevere-bot
Loading

@graingertgraingert marked this pull request as ready for reviewNovember 2, 2022 16:56
@ammaraskar
Copy link
Member

ammaraskar commentedNov 2, 2022
edited
Loading

Aah I think I see how these incorrectf_backs ended up in here (and maybe why it really should have been onef_back instead of two originally).

InStackSummary._extract_from_extended_frame_gen we iterate over the frame generator here:

forf, (lineno,end_lineno,colno,end_colno)inframe_gen:

Sincewalk_stack is a generator, the initial call to it in say

traceback.walk_stack(None),capture_locals=True,limit=1)

simply instantiates it. Only in_extract_from_extended_frame_gen does the code

iffisNone:f=sys._getframe().f_back.f_back

get invoked and by that point we are already inside of theStackSummary.extract ->StackSummary._extract_from_extended_frame_gen ->extended_frame_gen ->walk_stack (generator) (hence the fourf_backs.)

Since this code path was originallyStackSummary.extract ->walk_stack, that would explain why it had twof_backs instead of just one, unlike other traceback methods likeformat_stack andextract_stack.

walk_stack was likely never meant to be used without a correspondingStackSummary.extract, the docs saying

Usually used with StackSummary.extract

and the lack of direct tests for it in the commit adding it seems to suggest so:6bc2c1e#diff-8a52124ffceebd98f91bf96caa0f341ae109f79e190cd5acc1c56c8968d75252R514


One possible fix is to makewalk_stack return a generator that returns the frames from when it was called, not when the generator is iterated over.

graingert added a commit to graingert/distributed that referenced this pull requestNov 3, 2022
seepython/cpython#96092and it's not really intended for use outside of StackSummary.extract,seepython/cpython#99015 (comment)
@ammaraskar
Copy link
Member

Here's my recommended fix for this:

diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.pyindex 149d0234fe..48d5c1a809 100644--- a/Lib/test/test_traceback.py+++ b/Lib/test/test_traceback.py@@ -2241,8 +2241,7 @@ class TestStack(unittest.TestCase):     def test_walk_stack(self):         def deeper():             return list(traceback.walk_stack(None))-        s1 = list(traceback.walk_stack(None))-        s2 = deeper()+        s1, s2 = list(traceback.walk_stack(None)), deeper()         self.assertEqual(len(s2) - len(s1), 1)         self.assertEqual(s2[1:], s1)diff --git a/Lib/traceback.py b/Lib/traceback.pyindex cf5f355ff0..701ce9e4bb 100644--- a/Lib/traceback.py+++ b/Lib/traceback.py@@ -323,17 +323,21 @@ def line(self):         return self._line.strip()-def walk_stack(f):+def walk_stack(frame):     """Walk a stack yielding the frame and line number for each frame.     This will follow f.f_back from the given frame. If no frame is given, the     current stack is used. Usually used with StackSummary.extract.     """-    if f is None:-        f = sys._getframe().f_back.f_back.f_back.f_back-    while f is not None:-        yield f, f.f_lineno-        f = f.f_back+    if frame is None:+        frame = sys._getframe().f_back++    def walk_stack_generator(f):+        while f is not None:+            yield f, f.f_lineno+            f = f.f_back++    return walk_stack_generator(frame)

As mentioned in the comment on the issue, this is a change in behavior we should definitely have a NEWS entry and documentation change. Code in practice is unlikely to break due to it.@graingert, would you like to take this on?

isidentical and methane reacted with thumbs up emoji

graingert added a commit to graingert/distributed that referenced this pull requestNov 9, 2022
seepython/cpython#96092and it's not really intended for use outside of StackSummary.extract,seepython/cpython#99015 (comment)
graingert added a commit to graingert/distributed that referenced this pull requestNov 17, 2022
seepython/cpython#96092and it's not really intended for use outside of StackSummary.extract,seepython/cpython#99015 (comment)
graingert added a commit to graingert/distributed that referenced this pull requestNov 23, 2022
seepython/cpython#96092and it's not really intended for use outside of StackSummary.extract,seepython/cpython#99015 (comment)
graingert added a commit to graingert/distributed that referenced this pull requestNov 24, 2022
seepython/cpython#96092and it's not really intended for use outside of StackSummary.extract,seepython/cpython#99015 (comment)
graingert added a commit to graingert/distributed that referenced this pull requestNov 30, 2022
seepython/cpython#96092and it's not really intended for use outside of StackSummary.extract,seepython/cpython#99015 (comment)
graingert added a commit to dask/distributed that referenced this pull requestDec 14, 2022
* use sys.executable for python binary* pass the desired frame to traceback.walk_stack for 3.11seepython/cpython#96092and it's not really intended for use outside of StackSummary.extract,seepython/cpython#99015 (comment)* the GC overhead now depends on _PyType_PreHeaderSizefaster-cpython/ideas#125python/cpython@8319114#diff-a3a5c73931235f7f344c072dc755d6508e13923db3f5d581c5e88652075871cbR1684*GH-6785: asyncio.wait no longer calls ensure_futurepython/cpython#95601* test on python 3.11* exclude macos py 3.10
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@iritkatrieliritkatrielAwaiting requested review from iritkatriel

@isidenticalisidenticalAwaiting requested review from isidentical

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@graingert@ammaraskar@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp