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

Appearance settings

White space causes inconsistent frame traversal in With blocks #121865

Copy link
Copy link
@dmadisetti

Description

@dmadisetti
Issue body actions

Bug report

Bug description:

White space causes inconsistent frame traversal in With blocks

Consider

with open( # 0
   a       # 1
) as b:    # 2
    pass   # 3

frame order goes:
2, 3, 0

versus

with open(a) as b: # 0
    pass           # 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:

import traceback
import sys

def trace(*args, **kwargs):
        stack = traceback.extract_stack()
        for frame in stack[::-1]:
            _filename, lineno, function_name, _code = frame
            if function_name in ("bar", "foo"):
                print("trace", frame.lineno)


class Context():
    def __enter__(self):
        frame = sys._getframe(1)
        self.old = frame.f_trace
        sys.settrace(lambda *_args, **_keys: None)
        frame.f_trace = trace

    def __exit__(self, *kwargs):
        sys.settrace(self.old)
def foo():
    with Context() as f:
        stack = traceback.extract_stack()
        print("---")
        for frame in stack[::-1]:
            _filename, lineno, function_name, _code = frame
            if function_name == "foo":
                print(frame.lineno, lineno)
                break
print("Foo")
foo()
Foo
trace 3
trace 4
---
trace 5
trace 6
trace 7
trace 8
3 3
trace 9
trace 2
def bar():
    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)
                break

print("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

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.