-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Conversation
Thank you!! |
This comment has been minimized.
This comment has been minimized.
stdlib/asyncio/windows_utils.pyi
Outdated
stdin: PipeHandle | IO[AnyStr] | None | ||
stdout: PipeHandle | IO[AnyStr] | None | ||
stderr: PipeHandle | IO[AnyStr] | None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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] |
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
stdlib/asyncio/windows_utils.pyi
Outdated
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'>
There was a problem hiding this comment.
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 PipeHandle
s 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
...
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the use is still there in 3.11 https://github.com/python/cpython/blob/df9f7597559b6256924fcd3a1c3dc24cd5c5edaf/Lib/asyncio/windows_events.py#L890
There was a problem hiding this comment.
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.
This comment has been minimized.
This comment has been minimized.
1 similar comment
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
There was a problem hiding this 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.
As requested in #7356, cc @AlexWaygood.