Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differing behavior for sys.setrace between 3.12 and 3.13 #113728

Closed
markshannon opened this issue Jan 5, 2024 · 9 comments
Closed

Differing behavior for sys.setrace between 3.12 and 3.13 #113728

markshannon opened this issue Jan 5, 2024 · 9 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@markshannon
Copy link
Member

markshannon commented Jan 5, 2024

Bug report

Bug description:

def gen():
    print(2)
    yield 3
    print(4)

print(next(gen()))

3.12 events

call 14: def gen():
line 15:     print(2)
line 16:     yield 3
retu 16:     yield 3
call 16:     yield 3
exce 16:     yield 3
retu 16:     yield 3

main:

call 14: def gen():
line 15:     print(2)
line 16:     yield 3
retu 16:     yield 3

Note the extra except and return events for 3.12.

Originally discovered by @nedbat #113680 (comment)

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

No response

@markshannon
Copy link
Member Author

This is a sys.monitoring change. sys.settrace merely reflects that.
For

events = []
with record_events(events):
    next(gen())
print(events)

3.12:

['call', 'start', 'call', 'yield', 'raise', 'exception_handled', 'unwind']

3.13:

['call', 'start', 'call', 'yield']

The events for 3.13 look to be correct. The additional 'raise', 'exception_handled', 'unwind' events for 3.12 seem spurious.
Even if 3.13 is now correct we should at the very least document the change.

@markshannon
Copy link
Member Author

markshannon commented Jan 5, 2024

This has nothing to do with monitoring or tracing. It is about the lifetime of generators.
If we change

with record_events(events):
    next(gen())

to

g = gen()
with record_events(events):
    next(g)

The events recorded are the same for 3.12 and 3.13. The additional events in 3.12 are from the generator being closed.

@markshannon
Copy link
Member Author

Bisected to #111069

@markshannon
Copy link
Member Author

So what's happening is this:

In 3.12 the generator is closed by calling throw() which causes the 'raise', 'exception_handled', 'unwind' sequence of events.
However, that isn't necessary for a simple generator, so we simply close it in 3.13.

@nedbat is this a problem for coverage?

@nedbat
Copy link
Member

nedbat commented Jan 5, 2024

@markshannon I think this and other sys.monitoring changes are going to require me to do a bit of a re-think. There are tests in the coverage.py test suite that check data based on the raw events. The final user-visible coverage data might come out the same, but the tests are failing now because of changes like this. I'll be working on this.

@markshannon
Copy link
Member Author

This isn't a sys.monitoring change.

The change is that we no longer throw into generators to close them.
The events that you were seeing in 3.12 happened in the __del__() method of the generator, which is no longer called in 3.13.

@nedbat
Copy link
Member

nedbat commented Jan 5, 2024

This isn't a sys.monitoring change.

I understand. I meant to say, this and also sys.monitoring changes are changing the way I collect data, and therefore the way I test.

@gvanrossum
Copy link
Member

So... There's nothing to see here, move along, and close the issue?

@nedbat
Copy link
Member

nedbat commented Jan 6, 2024

I will re-open if needed.

@nedbat nedbat closed this as completed Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants