Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpython/Lib/asyncio/windows_utils.py at main · python/cpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
python
/
cpython
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
35.4k
Star
77.2k
Code
Issues
5k+
Pull requests
2.6k
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
cpython
/
Lib
/
asyncio
/
windows_utils.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
181 lines (145 loc) · 5.34 KB
main
Breadcrumbs
cpython
/
Lib
/
asyncio
/
windows_utils.py
Copy path
Top
File metadata and controls
Code
Blame
181 lines (145 loc) · 5.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""Various Windows specific bits and pieces."""
import
sys
if
sys
.
platform
!=
'win32'
:
# pragma: no cover
raise
ImportError
(
'win32 only'
)
import
_winapi
import
itertools
import
msvcrt
import
os
import
subprocess
import
warnings
__all__
=
'pipe'
,
'Popen'
,
'PIPE'
,
'PipeHandle'
# Constants/globals
BUFSIZE
=
8192
PIPE
=
subprocess
.
PIPE
STDOUT
=
subprocess
.
STDOUT
_mmap_counter
=
itertools
.
count
()
_MAX_PIPE_ATTEMPTS
=
20
# Replacement for os.pipe() using handles instead of fds
def
pipe
(
*
,
duplex
=
False
,
overlapped
=
(
True
,
True
),
bufsize
=
BUFSIZE
):
"""Like os.pipe() but with overlapped support and using handles not fds."""
if
duplex
:
openmode
=
_winapi
.
PIPE_ACCESS_DUPLEX
access
=
_winapi
.
GENERIC_READ
|
_winapi
.
GENERIC_WRITE
obsize
,
ibsize
=
bufsize
,
bufsize
else
:
openmode
=
_winapi
.
PIPE_ACCESS_INBOUND
access
=
_winapi
.
GENERIC_WRITE
obsize
,
ibsize
=
0
,
bufsize
openmode
|=
_winapi
.
FILE_FLAG_FIRST_PIPE_INSTANCE
if
overlapped
[
0
]:
openmode
|=
_winapi
.
FILE_FLAG_OVERLAPPED
if
overlapped
[
1
]:
flags_and_attribs
=
_winapi
.
FILE_FLAG_OVERLAPPED
else
:
flags_and_attribs
=
0
h1
=
h2
=
None
try
:
for
attempts
in
itertools
.
count
():
address
=
r'\\.\pipe\python-pipe-{:d}-{:d}-{}'
.
format
(
os
.
getpid
(),
next
(
_mmap_counter
),
os
.
urandom
(
8
).
hex
())
try
:
h1
=
_winapi
.
CreateNamedPipe
(
address
,
openmode
,
_winapi
.
PIPE_WAIT
,
1
,
obsize
,
ibsize
,
_winapi
.
NMPWAIT_WAIT_FOREVER
,
_winapi
.
NULL
)
break
except
OSError
as
e
:
if
attempts
>=
_MAX_PIPE_ATTEMPTS
:
raise
if
e
.
winerror
not
in
(
_winapi
.
ERROR_PIPE_BUSY
,
_winapi
.
ERROR_ACCESS_DENIED
):
raise
h2
=
_winapi
.
CreateFile
(
address
,
access
,
0
,
_winapi
.
NULL
,
_winapi
.
OPEN_EXISTING
,
flags_and_attribs
,
_winapi
.
NULL
)
ov
=
_winapi
.
ConnectNamedPipe
(
h1
,
overlapped
=
True
)
ov
.
GetOverlappedResult
(
True
)
return
h1
,
h2
except
:
if
h1
is
not
None
:
_winapi
.
CloseHandle
(
h1
)
if
h2
is
not
None
:
_winapi
.
CloseHandle
(
h2
)
raise
# Wrapper for a pipe handle
class
PipeHandle
:
"""Wrapper for an overlapped pipe handle which is vaguely file-object like.
The IOCP event loop can use these instead of socket objects.
"""
def
__init__
(
self
,
handle
):
self
.
_handle
=
handle
def
__repr__
(
self
):
if
self
.
_handle
is
not
None
:
handle
=
f'handle=
{
self
.
_handle
!r
}
'
else
:
handle
=
'closed'
return
f'<
{
self
.
__class__
.
__name__
}
{
handle
}
>'
@
property
def
handle
(
self
):
return
self
.
_handle
def
fileno
(
self
):
if
self
.
_handle
is
None
:
raise
ValueError
(
"I/O operation on closed pipe"
)
return
self
.
_handle
def
close
(
self
,
*
,
CloseHandle
=
_winapi
.
CloseHandle
):
if
self
.
_handle
is
not
None
:
handle
=
self
.
_handle
self
.
_handle
=
None
CloseHandle
(
handle
)
def
__del__
(
self
,
_warn
=
warnings
.
warn
):
if
self
.
_handle
is
not
None
:
_warn
(
f"unclosed
{
self
!r
}
"
,
ResourceWarning
,
source
=
self
)
self
.
close
()
def
__enter__
(
self
):
return
self
def
__exit__
(
self
,
t
,
v
,
tb
):
self
.
close
()
# Replacement for subprocess.Popen using overlapped pipe handles
class
Popen
(
subprocess
.
Popen
):
"""Replacement for subprocess.Popen using overlapped pipe handles.
The stdin, stdout, stderr are None or instances of PipeHandle.
"""
def
__init__
(
self
,
args
,
stdin
=
None
,
stdout
=
None
,
stderr
=
None
,
**
kwds
):
assert
not
kwds
.
get
(
'universal_newlines'
)
assert
kwds
.
get
(
'bufsize'
,
0
)
==
0
stdin_rfd
=
stdout_wfd
=
stderr_wfd
=
None
stdin_wh
=
stdout_rh
=
stderr_rh
=
None
if
stdin
==
PIPE
:
stdin_rh
,
stdin_wh
=
pipe
(
overlapped
=
(
False
,
True
),
duplex
=
True
)
stdin_rfd
=
msvcrt
.
open_osfhandle
(
stdin_rh
,
os
.
O_RDONLY
)
else
:
stdin_rfd
=
stdin
if
stdout
==
PIPE
:
stdout_rh
,
stdout_wh
=
pipe
(
overlapped
=
(
True
,
False
))
stdout_wfd
=
msvcrt
.
open_osfhandle
(
stdout_wh
,
0
)
else
:
stdout_wfd
=
stdout
if
stderr
==
PIPE
:
stderr_rh
,
stderr_wh
=
pipe
(
overlapped
=
(
True
,
False
))
stderr_wfd
=
msvcrt
.
open_osfhandle
(
stderr_wh
,
0
)
elif
stderr
==
STDOUT
:
stderr_wfd
=
stdout_wfd
else
:
stderr_wfd
=
stderr
try
:
super
().
__init__
(
args
,
stdin
=
stdin_rfd
,
stdout
=
stdout_wfd
,
stderr
=
stderr_wfd
,
**
kwds
)
except
:
for
h
in
(
stdin_wh
,
stdout_rh
,
stderr_rh
):
if
h
is
not
None
:
_winapi
.
CloseHandle
(
h
)
raise
else
:
if
stdin_wh
is
not
None
:
self
.
stdin
=
PipeHandle
(
stdin_wh
)
if
stdout_rh
is
not
None
:
self
.
stdout
=
PipeHandle
(
stdout_rh
)
if
stderr_rh
is
not
None
:
self
.
stderr
=
PipeHandle
(
stderr_rh
)
finally
:
if
stdin
==
PIPE
:
os
.
close
(
stdin_rfd
)
if
stdout
==
PIPE
:
os
.
close
(
stdout_wfd
)
if
stderr
==
PIPE
:
os
.
close
(
stderr_wfd
)
You can’t perform that action at this time.