GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-96092: restore frame height of traceback.walk_stack to that of pyt… by graingert · Pull Request #99015 · python/cpython

Conversation

@graingert

@graingert graingert commented Nov 2, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

…hon 3.10

  • Issue: traceback.walk_stack(None) does not behave the same as traceback.walk_stack(inspect.currentframe()) #96092

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot mentioned this pull request Nov 2, 2022
traceback.walk_stack(None) does not behave the same as traceback.walk_stack(inspect.currentframe()) #96092
Closed
@bedevere-bot bedevere-bot added the awaiting review label Nov 2, 2022
graingert and others added 2 commits November 2, 2022 16:49
@graingert
pythonGH-96092: restore frame height of traceback.walk_stack to that …
2376fe7
…of python 3.10
@blurb-it @graingert
📜🤖 Added by blurb_it.
b5eb38b
@graingert
graingert force-pushed the fix-walk-stack branch from 7ed1575 to b5eb38b Compare November 2, 2022 16:49
@graingert
graingert marked this pull request as ready for review November 2, 2022 16:56
@graingert
graingert requested a review from iritkatriel as a code owner November 2, 2022 16:56
@iritkatriel
iritkatriel requested a review from isidentical November 2, 2022 17:15
@graingert
Merge branch 'main' into fix-walk-stack
6d28c32
@ammaraskar

ammaraskar commented Nov 2, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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

In StackSummary._extract_from_extended_frame_gen we iterate over the frame generator here:

cpython/Lib/traceback.py

Line 417 in 908e81f

for f, (lineno, end_lineno, colno, end_colno) in frame_gen:

Since walk_stack is a generator, the initial call to it in say

cpython/Lib/test/test_traceback.py

Line 2324 in c053284

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

simply instantiates it. Only in _extract_from_extended_frame_gen does the code

if f is None:
    f = sys._getframe().f_back.f_back

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

Since this code path was originally StackSummary.extract -> walk_stack, that would explain why it had two f_backs instead of just one, unlike other traceback methods like format_stack and extract_stack.

walk_stack was likely never meant to be used without a corresponding StackSummary.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 make walk_stack return a generator that returns the frames from when it was called, not when the generator is iterated over.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

graingert
graingert commented Nov 3, 2022
View reviewed changes
Comment thread Lib/test/test_traceback.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@graingert
Update Lib/test/test_traceback.py
bdc5814
graingert added a commit to graingert/distributed that referenced this pull request Nov 3, 2022
@graingert
pass the desired frame to traceback.walk_stack for 3.11
35da6dd
see python/cpython#96092
and it's not really intended for use outside of StackSummary.extract,
see python/cpython#99015 (comment)
@ammaraskar

ammaraskar commented Nov 4, 2022

Copy link
Copy Markdown
Member

Here's my recommended fix for this:

diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 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.py
index 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
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

graingert added a commit to graingert/distributed that referenced this pull request Nov 9, 2022
@graingert
pass the desired frame to traceback.walk_stack for 3.11
38cf55a
see python/cpython#96092
and it's not really intended for use outside of StackSummary.extract,
see python/cpython#99015 (comment)
graingert added a commit to graingert/distributed that referenced this pull request Nov 17, 2022
@graingert
pass the desired frame to traceback.walk_stack for 3.11
3029329
see python/cpython#96092
and it's not really intended for use outside of StackSummary.extract,
see python/cpython#99015 (comment)
graingert added a commit to graingert/distributed that referenced this pull request Nov 23, 2022
@graingert
pass the desired frame to traceback.walk_stack for 3.11
3af29e6
see python/cpython#96092
and it's not really intended for use outside of StackSummary.extract,
see python/cpython#99015 (comment)
graingert added a commit to graingert/distributed that referenced this pull request Nov 24, 2022
@graingert
pass the desired frame to traceback.walk_stack for 3.11
41cc625
see python/cpython#96092
and it's not really intended for use outside of StackSummary.extract,
see python/cpython#99015 (comment)
graingert added a commit to graingert/distributed that referenced this pull request Nov 30, 2022
@graingert
pass the desired frame to traceback.walk_stack for 3.11
af34a7b
see python/cpython#96092
and it's not really intended for use outside of StackSummary.extract,
see python/cpython#99015 (comment)
graingert added a commit to dask/distributed that referenced this pull request Dec 14, 2022
@graingert
Add support for Python 3.11 (#7249)
64a7cae
* use sys.executable for python binary

* pass the desired frame to traceback.walk_stack for 3.11

see python/cpython#96092
and it's not really intended for use outside of StackSummary.extract,
see python/cpython#99015 (comment)

* the GC overhead now depends on _PyType_PreHeaderSize

faster-cpython/ideas#125
python/cpython@8319114#diff-a3a5c73931235f7f344c072dc755d6508e13923db3f5d581c5e88652075871cbR1684

* GH-6785: asyncio.wait no longer calls ensure_future

python/cpython#95601

* test on python 3.11

* exclude macos py 3.10
@ammaraskar ammaraskar mentioned this pull request Jan 27, 2025
gh-96092: Fix traceback.walk_stack(None) skipping too many frames #129330
Merged
@github-actions

github-actions Bot commented Apr 11, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 11, 2026
@serhiy-storchaka serhiy-storchaka closed this Aug 13, 2026
@serhiy-storchaka

serhiy-storchaka commented Aug 13, 2026

Copy link
Copy Markdown
Member

Superseded by the merged #129330.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iritkatriel iritkatriel Awaiting requested review from iritkatriel
@isidentical isidentical Awaiting requested review from isidentical

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@graingert @ammaraskar @serhiy-storchaka @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.