Skip to content

Add asyncio.windows_utils.Popen #7396

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

Merged
merged 9 commits into from
Mar 2, 2022
Merged

Add asyncio.windows_utils.Popen #7396

merged 9 commits into from
Mar 2, 2022

Conversation

JelleZijlstra
Copy link
Member

As requested in #7356, cc @AlexWaygood.

@AlexWaygood
Copy link
Member

Thank you!!

@github-actions

This comment has been minimized.

Comment on lines 44 to 46
stdin: PipeHandle | IO[AnyStr] | None
stdout: PipeHandle | IO[AnyStr] | None
stderr: PipeHandle | IO[AnyStr] | None
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
stdin: PipeHandle | IO[AnyStr] | None
stdout: PipeHandle | IO[AnyStr] | None
stderr: PipeHandle | IO[AnyStr] | None
stdin: PipeHandle | IO[AnyStr] | None # type: ignore[override]
stdout: PipeHandle | IO[AnyStr] | None # type: ignore[override]
stderr: PipeHandle | IO[AnyStr] | None # type: ignore[override]

Copy link
Member

Choose a reason for hiding this comment

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

Needs to be [assignment] rather than [override]

Copy link
Member Author

Choose a reason for hiding this comment

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

Huh, thanks. That's a weird code to use there; I'm not even assigning anything.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

class Popen(subprocess.Popen[AnyStr]):
# The docstring says "The stdin, stdout, stderr are None or instances of PipeHandle."
# but that is incorrect: if you pass in a file object, that will still be
# put in the stdin/stdout/stderr attributes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you sure this is correct? The attributes are set to file objects in super().__init__(), but later replaced in the else: below.

Copy link
Member Author

@JelleZijlstra JelleZijlstra Feb 28, 2022

Choose a reason for hiding this comment

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

I might be misreading the code; I don't have access to Windows myself. But I think it sets these attrs to a PipeHandle only if stdin == PIPE, so if you pass stdin=<some file object>, that file object ends up in self.stdin.

Copy link
Member

@AlexWaygood AlexWaygood Feb 28, 2022

Choose a reason for hiding this comment

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

Looks like @Akuli is correct. If I apply this patch(!) to asyncio/windows_utils.py in my local clone of CPython, on my Windows machine:

diff --git a/Lib/asyncio/windows_utils.py b/Lib/asyncio/windows_utils.py
index ef277fac3e..24dda3263d 100644
--- a/Lib/asyncio/windows_utils.py
+++ b/Lib/asyncio/windows_utils.py
@@ -171,3 +171,4 @@ def __init__(self, args, stdin=None, stdout=None, stderr=None, **kwds):
                 os.close(stdout_wfd)
             if stderr == PIPE:
                 os.close(stderr_wfd)
+        print(f'{type(self.stdin)=}')

... then this is what I get in an interactive shell:

Python 3.11.0a5+ (main, Feb 11 2022, 10:54:07) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> from asyncio.windows_utils import Popen
>>> with open('foo.txt', 'w') as f:
...     p = Popen(sys.executable, stdin=f)
...
type(self.stdin)=<class 'NoneType'>

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm starting to think that maybe you aren't supposed to pass in file objects at all, only PipeHandles or None or PIPE.

For testing this on windows, maybe print each of self.stdin, self.stdout and self.stderr, and then run a subprocess with some high level function? As the file name suggests, this class seems to be mostly a platform-specific lower level implementation detail.

Copy link
Member Author

Choose a reason for hiding this comment

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

@AlexWaygood Interesting, I'll have to look at the code to figure out how that's possible.

@Akuli Interestingly, Popen is not actually used or documented anywhere else in asyncio. It looks more like they kept it in by accident.

Copy link
Member

Choose a reason for hiding this comment

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

We could always just cop out and use Any...

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is used, at least in Python 3.9 where windows_events.py has this code:

class _WindowsSubprocessTransport(base_subprocess.BaseSubprocessTransport):

    def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
        self._proc = windows_utils.Popen(
            args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr,
            bufsize=bufsize, **kwargs)

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, sorry for missing that usage. I looked in subprocess.py and it seems like it only sets the stdin attribute if it's a PIPE, so the comment in asyncio was right after all.

@JelleZijlstra JelleZijlstra requested a review from Akuli March 2, 2022 04:59
@github-actions

This comment has been minimized.

1 similar comment
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

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

This matches the docstring, which is the best we can make things in asyncio as the source code is complicated.

@Akuli Akuli merged commit 0a6b6b0 into python:master Mar 2, 2022
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.

3 participants