Skip to content

Commit 4ee5bff

Browse files
committed
os: support blocking functions on Windows
Use the GetNamedPipeHandleState and SetNamedPipeHandleState Win32 API functions to add support for os.get_blocking and os.set_blocking.
1 parent 95cbb3d commit 4ee5bff

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

Include/internal/pycore_fileutils.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,10 @@ PyAPI_FUNC(int) _Py_set_inheritable_async_safe(int fd, int inheritable,
160160

161161
PyAPI_FUNC(int) _Py_dup(int fd);
162162

163-
#ifndef MS_WINDOWS
164163
PyAPI_FUNC(int) _Py_get_blocking(int fd);
165164

166165
PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
167-
#else /* MS_WINDOWS */
166+
#ifdef MS_WINDOWS
168167
PyAPI_FUNC(void*) _Py_get_osfhandle_noraise(int fd);
169168

170169
PyAPI_FUNC(void*) _Py_get_osfhandle(int fd);

Modules/clinic/posixmodule.c.h

-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

-2
Original file line numberDiff line numberDiff line change
@@ -13929,7 +13929,6 @@ os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
1392913929
}
1393013930
#endif /* MS_WINDOWS */
1393113931

13932-
#ifndef MS_WINDOWS
1393313932
/*[clinic input]
1393413933
os.get_blocking -> bool
1393513934
fd: int
@@ -13977,7 +13976,6 @@ os_set_blocking_impl(PyObject *module, int fd, int blocking)
1397713976
return NULL;
1397813977
Py_RETURN_NONE;
1397913978
}
13980-
#endif /* !MS_WINDOWS */
1398113979

1398213980

1398313981
/*[clinic input]

Python/fileutils.c

+35
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,41 @@ _Py_set_blocking(int fd, int blocking)
24502450
return -1;
24512451
}
24522452
#else /* MS_WINDOWS */
2453+
int
2454+
_Py_get_blocking(int fd)
2455+
{
2456+
HANDLE handle;
2457+
DWORD mode;
2458+
2459+
handle = _Py_get_osfhandle(fd);
2460+
if (handle == INVALID_HANDLE_VALUE)
2461+
return -1;
2462+
2463+
if (!GetNamedPipeHandleState(handle, &mode, NULL, NULL, NULL, NULL, NULL, NULL)) {
2464+
PyErr_SetFromWindowsErr(0);
2465+
return -1;
2466+
}
2467+
return mode == PIPE_WAIT;
2468+
}
2469+
2470+
int
2471+
_Py_set_blocking(int fd, int blocking)
2472+
{
2473+
HANDLE handle;
2474+
DWORD mode;
2475+
2476+
handle = _Py_get_osfhandle(fd);
2477+
if (handle == INVALID_HANDLE_VALUE)
2478+
return -1;
2479+
2480+
mode = blocking ? PIPE_WAIT : PIPE_NOWAIT;
2481+
if (!SetNamedPipeHandleState(handle, &mode, NULL, NULL)) {
2482+
PyErr_SetFromWindowsErr(0);
2483+
return -1;
2484+
}
2485+
return 0;
2486+
}
2487+
24532488
void*
24542489
_Py_get_osfhandle_noraise(int fd)
24552490
{

0 commit comments

Comments
 (0)