Skip to content

Commit 013d8ea

Browse files
committed
bpo-30555: Add _Py_{get,open}_osfhandle{,_noraise} and use them instead
1 parent 645dfa3 commit 013d8ea

File tree

4 files changed

+50
-66
lines changed

4 files changed

+50
-66
lines changed

Include/fileutils.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,15 @@ PyAPI_FUNC(int) _Py_dup(int fd);
117117
PyAPI_FUNC(int) _Py_get_blocking(int fd);
118118

119119
PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
120-
#endif /* !MS_WINDOWS */
120+
#else /* MS_WINDOWS */
121+
PyAPI_FUNC(intptr_t) _Py_get_osfhandle_noraise(int fd);
122+
123+
PyAPI_FUNC(intptr_t) _Py_get_osfhandle(int fd);
124+
125+
PyAPI_FUNC(int) _Py_open_osfhandle_noraise(intptr_t handle, int flags);
126+
127+
PyAPI_FUNC(int) _Py_open_osfhandle(intptr_t handle, int flags);
128+
#endif /* MS_WINDOWS */
121129

122130
#endif /* Py_LIMITED_API */
123131

Modules/_io/winconsoleio.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,6 @@ char _PyIO_get_console_type(PyObject *path_or_fd) {
133133
return m;
134134
}
135135

136-
static HANDLE py_get_osfhandle(int fd)
137-
{
138-
HANDLE handle;
139-
140-
_Py_BEGIN_SUPPRESS_IPH
141-
handle = (HANDLE)_get_osfhandle(fd);
142-
_Py_END_SUPPRESS_IPH
143-
if (handle == INVALID_HANDLE_VALUE) {
144-
PyErr_SetFromErrno(PyExc_OSError);
145-
return INVALID_HANDLE_VALUE;
146-
}
147-
148-
return handle;
149-
}
150-
151136

152137
/*[clinic input]
153138
module _io
@@ -674,7 +659,7 @@ readinto(winconsoleio *self, char *buf, Py_ssize_t len)
674659
return -1;
675660
}
676661

677-
HANDLE handle = py_get_osfhandle(self->fd);
662+
HANDLE handle = (HANDLE)_Py_get_osfhandle(self->fd);
678663
if (handle == INVALID_HANDLE_VALUE)
679664
return -1;
680665

@@ -810,7 +795,7 @@ _io__WindowsConsoleIO_readall_impl(winconsoleio *self)
810795
if (self->fd == -1)
811796
return err_closed();
812797

813-
handle = py_get_osfhandle(self->fd);
798+
handle = (HANDLE)_Py_get_osfhandle(self->fd);
814799
if (handle == INVALID_HANDLE_VALUE)
815800
return NULL;
816801

@@ -990,7 +975,7 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b)
990975
if (!self->writable)
991976
return err_mode("writing");
992977

993-
handle = py_get_osfhandle(self->fd);
978+
handle = (HANDLE)_Py_get_osfhandle(self->fd);
994979
if (handle == INVALID_HANDLE_VALUE)
995980
return NULL;
996981

PC/_testconsole.c

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,6 @@
1212
#include <windows.h>
1313
#include <fcntl.h>
1414

15-
/*
16-
* Macros to protect CRT calls against instant termination when passed an
17-
* invalid parameter (issue23524).
18-
*
19-
* It does not need to be defined when building using MSVC
20-
* earlier than 14.0 (_MSC_VER == 1900).
21-
*
22-
* Copied here since it's required to call _get_osfhandle safely and we don't
23-
* want to export py_get_osfhandle only because this test module needs it.
24-
*/
25-
#if defined _MSC_VER && _MSC_VER >= 1900
26-
27-
static void __cdecl _silent_invalid_parameter_handler(
28-
wchar_t const* expression,
29-
wchar_t const* function,
30-
wchar_t const* file,
31-
unsigned int line,
32-
uintptr_t pReserved) { }
33-
34-
#define _Py_BEGIN_SUPPRESS_IPH { _invalid_parameter_handler _Py_old_handler = \
35-
_set_thread_local_invalid_parameter_handler(_silent_invalid_parameter_handler);
36-
#define _Py_END_SUPPRESS_IPH _set_thread_local_invalid_parameter_handler(_Py_old_handler); }
37-
38-
#else
39-
40-
#define _Py_BEGIN_SUPPRESS_IPH
41-
#define _Py_END_SUPPRESS_IPH
42-
43-
#endif /* _MSC_VER >= 1900 */
44-
45-
static HANDLE py_get_osfhandle(int fd)
46-
{
47-
HANDLE handle;
48-
49-
_Py_BEGIN_SUPPRESS_IPH
50-
handle = (HANDLE)_get_osfhandle(fd);
51-
_Py_END_SUPPRESS_IPH
52-
if (handle == INVALID_HANDLE_VALUE) {
53-
PyErr_SetFromErrno(PyExc_OSError);
54-
return INVALID_HANDLE_VALUE;
55-
}
56-
57-
return handle;
58-
}
59-
6015
/* The full definition is in iomodule. We reproduce
6116
enough here to get the fd, which is all we want. */
6217
typedef struct {
@@ -113,7 +68,7 @@ _testconsole_write_input_impl(PyObject *module, PyObject *file,
11368
prec->Event.KeyEvent.uChar.UnicodeChar = *p;
11469
}
11570

116-
HANDLE hInput = py_get_osfhandle(((winconsoleio*)file)->fd);
71+
HANDLE hInput = (HANDLE)_Py_get_osfhandle(((winconsoleio*)file)->fd);
11772
if (hInput == INVALID_HANDLE_VALUE)
11873
goto error;
11974

Python/fileutils.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1585,4 +1585,40 @@ _Py_set_blocking(int fd, int blocking)
15851585
PyErr_SetFromErrno(PyExc_OSError);
15861586
return -1;
15871587
}
1588-
#endif
1588+
#else /* MS_WINDOWS */
1589+
intptr_t
1590+
_Py_get_osfhandle_noraise(int fd)
1591+
{
1592+
_Py_BEGIN_SUPPRESS_IPH
1593+
return _get_osfhandle(fd);
1594+
_Py_END_SUPPRESS_IPH
1595+
}
1596+
1597+
intptr_t
1598+
_Py_get_osfhandle(int fd)
1599+
{
1600+
intptr_t handle = _Py_get_osfhandle_noraise(fd);
1601+
if (handle == (intptr_t)INVALID_HANDLE_VALUE)
1602+
PyErr_SetFromErrno(PyExc_OSError);
1603+
1604+
return handle;
1605+
}
1606+
1607+
int
1608+
_Py_open_osfhandle_noraise(intptr_t handle, int flags)
1609+
{
1610+
_Py_BEGIN_SUPPRESS_IPH
1611+
return _open_osfhandle(handle, flags);
1612+
_Py_END_SUPPRESS_IPH
1613+
}
1614+
1615+
int
1616+
_Py_open_osfhandle(intptr_t handle, int flags)
1617+
{
1618+
int fd = _Py_open_osfhandle_noraise(handle, flags);
1619+
if (fd == -1)
1620+
PyErr_SetFromErrno(PyExc_OSError);
1621+
1622+
return fd;
1623+
}
1624+
#endif /* MS_WINDOWS */

0 commit comments

Comments
 (0)