Skip to content

Conversation

@johnslavik
Copy link
Contributor

@johnslavik johnslavik commented Oct 18, 2025

This is generally a work in progress; tests are needed.
The asyncio patch is simple and ready.

@johnslavik
Copy link
Contributor Author

Please merge #140298 first.

@johnslavik johnslavik marked this pull request as ready for review October 23, 2025 14:57
@johnslavik johnslavik marked this pull request as draft October 23, 2025 15:00
# Isolated mode implies -E, -P and -s, purifies sys.path and ignores PYTHON*
# variables.
if isolated:
cmd_line.append('-I')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reuse this routine and spawn_asyncio_repl below, we need to have a knob to not pass -I since that ignores PYTHONSTARTUP completely.

The asyncio REPL currently doesn't comply to that, which is a separate issue I'll report having learnt this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracking the -I edge case in #140648.

Comment on lines -295 to -311
def test_asyncio_repl_reaches_python_startup_script(self):
with os_helper.temp_dir() as tmpdir:
script = os.path.join(tmpdir, "pythonstartup.py")
with open(script, "w") as f:
f.write("print('pythonstartup done!')" + os.linesep)
f.write("exit(0)" + os.linesep)

env = os.environ.copy()
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".asyncio_history")
env["PYTHONSTARTUP"] = script
subprocess.check_call(
[sys.executable, "-m", "asyncio"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
timeout=SHORT_TIMEOUT,
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covered by TestPythonStartup now, so not needed here.

@johnslavik
Copy link
Contributor Author

johnslavik commented Nov 8, 2025

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.

self.assertEqual(traceback_lines, expected_lines)


@support.force_not_colorized_test_class
Copy link
Contributor Author

@johnslavik johnslavik Nov 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is needed, but I think it's OK to keep it as is.

@johnslavik
Copy link
Contributor Author

johnslavik commented Nov 8, 2025

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

@johnslavik johnslavik force-pushed the asyncio-repl-handle-python-startup branch from b85a31b to 0a50a50 Compare November 8, 2025 12:35
@johnslavik johnslavik closed this Dec 2, 2025
@johnslavik johnslavik deleted the asyncio-repl-handle-python-startup branch December 2, 2025 00:44
@johnslavik johnslavik restored the asyncio-repl-handle-python-startup branch December 5, 2025 12:28
@johnslavik johnslavik reopened this Dec 5, 2025
@johnslavik
Copy link
Contributor Author

johnslavik commented Dec 5, 2025

This is ready, but Windows doesn't pass because of GH-141262.

@johnslavik johnslavik marked this pull request as ready for review December 5, 2025 12:31
@johnslavik
Copy link
Contributor Author

johnslavik commented Dec 16, 2025

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.

@johnslavik
Copy link
Contributor Author

johnslavik commented Dec 16, 2025

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.

@johnslavik
Copy link
Contributor Author

Cherry-picked 92f629b (875fd2a) from GH-142785 to see if it fixes the problem.

@johnslavik
Copy link
Contributor Author

to see if it fixes the problem.

It does! Cool!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant