Skip to content

White space causes inconsistent frame traversal in With blocks #121865

Description

@dmadisetti

Bug report

Bug description:

White space causes inconsistent frame traversal in With blocks

Consider

withopen( # 0a# 1
) asb: # 2pass# 3

frame order goes:
2, 3, 0

versus

withopen(a) asb: # 0pass# 1

frame order goes 1, 0

This doesn't seem right, many formatters will make this adjustment as an expected NoOp.

Code to reproduce the behaviour:

importtracebackimportsysdeftrace(*args, **kwargs):
stack=traceback.extract_stack()
forframeinstack[::-1]:
_filename, lineno, function_name, _code=frameiffunction_namein ("bar", "foo"):
print("trace", frame.lineno)
classContext():
def__enter__(self):
frame=sys._getframe(1)
self.old=frame.f_tracesys.settrace(lambda*_args, **_keys: None)
frame.f_trace=tracedef__exit__(self, *kwargs):
sys.settrace(self.old)
deffoo():
withContext() asf:
stack=traceback.extract_stack()
print("---")
forframeinstack[::-1]:
_filename, lineno, function_name, _code=frameiffunction_name=="foo":
print(frame.lineno, lineno)
breakprint("Foo")
foo()
Foo
trace 3
trace 4
---
trace 5
trace 6
trace 7
trace 8
3 3
trace 9
trace 2
defbar():
with Context(
) as f: # NOTE: line break here !
stack = traceback.extract_stack()
print("---")
for frame in stack[::-1]:
_filename, lineno, function_name, _code = frame
if function_name =="bar":
print(frame.lineno, lineno)
breakprint("Bar")
bar()
Bar
trace 3
trace 4
trace 5
---
trace 6
trace 7
trace 8
trace 9
4 4
trace 10
trace 2

Expected behaviour

Foo
trace 3
trace 4
---
trace 5
trace 6
trace 7
trace 8
3 3
trace 9
trace 2
Bar
trace 4
trace 5
---
trace 6
trace 7
trace 8
trace 9
4 4
trace 10
trace 2 (or trace 3)

or maybe in the context of debuggers setting break points

Foo
trace 2
trace 3
trace 4
---
trace 5
trace 6
trace 7
trace 8
3 3
trace 9
trace 2
Bar
trace 3
trace 4
trace 5
---
trace 6
trace 7
trace 8
trace 9
4 4
trace 10
trace 2

Additional context

Please point me to where in the documentation if this is expected behavior,
The inconsistency seems like a bug to me.

Here's a wasm notebook, but I did test it locally as well: https://marimo.app/l/0eld5u

CPython versions tested on:

3.10, 3.11, 3.12

Operating systems tested on:

Linux, Other

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions