gh-140287: Handle `PYTHONSTARTUP` script exceptions in the asyncio REPL
This is generally a work in progress; tests are needed. The asyncio patch is simple and ready.
- Issue: gh-140287
Please merge #140298 first.
This is likely it. I can't think of anything simpler but equally complete at the same time. This suite seems intentionally straightforward, as are the new tests proposed here. I'll give this a few more days in a daemon thread of my mind.
We can abstract as needed in the future. We could use some regexes. However, I don't think this is so much needed now, and the environments differ enough to justify some repetition.
It seems that the asyncio REPL on Windows leaks the event loop:
❯ PYTHONSTARTUP='' python -m asyncio
Running Debug|x64 interpreter...
asyncio REPL 3.15.0a1+ (heads/asyncio-repl-handle-python-startup-dirty:ce03ccef689, Nov 8 2025, 02:06:1) [MSC v.1944 64 bit (AMD64)] on win32
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> exit
exiting asyncio REPL...
C:\Users\zenon\Python\cpython\Lib\asyncio\base_events.py:758: ResourceWarning: unclosed event loop <ProactorEventLoop running=False closed=False debug=False>
Definitely a bug, we'll track it elsewhere soon.
EDIT: Tracking in #141262
This is ready, but Windows doesn't pass because of GH-141262.
It seems that these unclosed loop warnings regressed (stopped appearing) in the recent releases -- this is bad, but we can fix them separately and we can also fix the asyncio REPL in this regard by actually closing the loop (in a separate PR).
In the meantime, updating this branch should make the CI green.
Looks like I was confused about the regression -- that's good news!
This will have to wait until the loop is properly closed in the asyncio REPL.
Cherry-picked 92f629b (875fd2a) from GH-142785 to see if it fixes the problem.
to see if it fixes the problem.
It does! Cool!
I'll see if I can simplify this. I think the tests are slightly overcomplicated.
I've realized that this isn't testing what it is supposed to test. I'll come back to this later.