Skip to content

Commit aaf6020

Browse files
committed
bpo-30555: Swapped the rest of the uses of _{get,open}_osfhandle
1 parent 013d8ea commit aaf6020

File tree

6 files changed

+18
-58
lines changed

6 files changed

+18
-58
lines changed

Modules/_io/winconsoleio.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ char _PyIO_get_console_type(PyObject *path_or_fd) {
6363
int fd = PyLong_AsLong(path_or_fd);
6464
PyErr_Clear();
6565
if (fd >= 0) {
66-
HANDLE handle;
67-
_Py_BEGIN_SUPPRESS_IPH
68-
handle = (HANDLE)_get_osfhandle(fd);
69-
_Py_END_SUPPRESS_IPH
66+
HANDLE handle = (HANDLE)_Py_get_osfhandle_noraise(fd);
7067
if (handle == INVALID_HANDLE_VALUE)
7168
return '\0';
7269
return _get_console_type(handle);
@@ -348,9 +345,7 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
348345
goto bad_mode;
349346

350347
if (fd >= 0) {
351-
_Py_BEGIN_SUPPRESS_IPH
352-
handle = (HANDLE)_get_osfhandle(fd);
353-
_Py_END_SUPPRESS_IPH
348+
handle = (HANDLE)_Py_get_osfhandle_noraise(fd);
354349
self->closefd = 0;
355350
} else {
356351
DWORD access = GENERIC_READ;
@@ -382,12 +377,10 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
382377
goto error;
383378
}
384379

385-
_Py_BEGIN_SUPPRESS_IPH
386380
if (self->writable)
387-
self->fd = _open_osfhandle((intptr_t)handle, _O_WRONLY | _O_BINARY);
381+
self->fd = _Py_open_osfhandle_noraise((intptr_t)handle, _O_WRONLY | _O_BINARY);
388382
else
389-
self->fd = _open_osfhandle((intptr_t)handle, _O_RDONLY | _O_BINARY);
390-
_Py_END_SUPPRESS_IPH
383+
self->fd = _Py_open_osfhandle_noraise((intptr_t)handle, _O_RDONLY | _O_BINARY);
391384
if (self->fd < 0) {
392385
CloseHandle(handle);
393386
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);

Modules/mmapmodule.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,13 +1266,10 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
12661266
*/
12671267
if (fileno != -1 && fileno != 0) {
12681268
/* Ensure that fileno is within the CRT's valid range */
1269-
_Py_BEGIN_SUPPRESS_IPH
1270-
fh = (HANDLE)_get_osfhandle(fileno);
1271-
_Py_END_SUPPRESS_IPH
1272-
if (fh==(HANDLE)-1) {
1273-
PyErr_SetFromErrno(PyExc_OSError);
1269+
fh = (HANDLE)_Py_get_osfhandle(fileno);
1270+
if (fh == (HANDLE)INVALID_HANDLE_VALUE)
12741271
return NULL;
1275-
}
1272+
12761273
/* Win9x appears to need us seeked to zero */
12771274
lseek(fileno, 0, SEEK_SET);
12781275
}

Modules/posixmodule.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8452,18 +8452,16 @@ os_pipe_impl(PyObject *module)
84528452
attr.bInheritHandle = FALSE;
84538453

84548454
Py_BEGIN_ALLOW_THREADS
8455-
_Py_BEGIN_SUPPRESS_IPH
84568455
ok = CreatePipe(&read, &write, &attr, 0);
84578456
if (ok) {
8458-
fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
8459-
fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
8457+
fds[0] = _Py_open_osfhandle_noraise((intptr_t)read, _O_RDONLY);
8458+
fds[1] = _Py_open_osfhandle_noraise((intptr_t)write, _O_WRONLY);
84608459
if (fds[0] == -1 || fds[1] == -1) {
84618460
CloseHandle(read);
84628461
CloseHandle(write);
84638462
ok = 0;
84648463
}
84658464
}
8466-
_Py_END_SUPPRESS_IPH
84678465
Py_END_ALLOW_THREADS
84688466

84698467
if (!ok)

PC/msvcrtmodule.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,7 @@ static long
167167
msvcrt_open_osfhandle_impl(PyObject *module, intptr_t handle, int flags)
168168
/*[clinic end generated code: output=cede871bf939d6e3 input=cb2108bbea84514e]*/
169169
{
170-
int fd;
171-
172-
_Py_BEGIN_SUPPRESS_IPH
173-
fd = _open_osfhandle(handle, flags);
174-
_Py_END_SUPPRESS_IPH
175-
if (fd == -1)
176-
PyErr_SetFromErrno(PyExc_OSError);
177-
178-
return fd;
170+
return _Py_open_osfhandle(handle, flags);
179171
}
180172

181173
/*[clinic input]
@@ -193,15 +185,7 @@ static intptr_t
193185
msvcrt_get_osfhandle_impl(PyObject *module, int fd)
194186
/*[clinic end generated code: output=7ce761dd0de2b503 input=305900f4bfab76c7]*/
195187
{
196-
intptr_t handle = -1;
197-
198-
_Py_BEGIN_SUPPRESS_IPH
199-
handle = _get_osfhandle(fd);
200-
_Py_END_SUPPRESS_IPH
201-
if (handle == -1)
202-
PyErr_SetFromErrno(PyExc_OSError);
203-
204-
return handle;
188+
return _Py_get_osfhandle(fd);
205189
}
206190

207191
/* Console I/O */

Parser/myreadline.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,8 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
205205
if (!Py_LegacyWindowsStdioFlag && sys_stdin == stdin) {
206206
HANDLE hStdIn, hStdErr;
207207

208-
_Py_BEGIN_SUPPRESS_IPH
209-
hStdIn = (HANDLE)_get_osfhandle(fileno(sys_stdin));
210-
hStdErr = (HANDLE)_get_osfhandle(fileno(stderr));
211-
_Py_END_SUPPRESS_IPH
208+
hStdIn = (HANDLE)_Py_get_osfhandle_noraise(fileno(sys_stdin));
209+
hStdErr = (HANDLE)_Py_get_osfhandle_noraise(fileno(stderr));
212210

213211
if (_get_console_type(hStdIn) == 'r') {
214212
fflush(sys_stdout);

Python/fileutils.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,7 @@ _Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
614614
HANDLE h;
615615
int type;
616616

617-
_Py_BEGIN_SUPPRESS_IPH
618-
h = (HANDLE)_get_osfhandle(fd);
619-
_Py_END_SUPPRESS_IPH
617+
h = (HANDLE)_Py_get_osfhandle_noraise(fd);
620618

621619
if (h == INVALID_HANDLE_VALUE) {
622620
/* errno is already set by _get_osfhandle, but we also set
@@ -739,9 +737,7 @@ get_inheritable(int fd, int raise)
739737
HANDLE handle;
740738
DWORD flags;
741739

742-
_Py_BEGIN_SUPPRESS_IPH
743-
handle = (HANDLE)_get_osfhandle(fd);
744-
_Py_END_SUPPRESS_IPH
740+
handle = (HANDLE)_Py_get_osfhandle_noraise(fd);
745741
if (handle == INVALID_HANDLE_VALUE) {
746742
if (raise)
747743
PyErr_SetFromErrno(PyExc_OSError);
@@ -810,9 +806,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
810806
}
811807

812808
#ifdef MS_WINDOWS
813-
_Py_BEGIN_SUPPRESS_IPH
814-
handle = (HANDLE)_get_osfhandle(fd);
815-
_Py_END_SUPPRESS_IPH
809+
handle = (HANDLE)_Py_get_osfhandle_noraise(fd);
816810
if (handle == INVALID_HANDLE_VALUE) {
817811
if (raise)
818812
PyErr_SetFromErrno(PyExc_OSError);
@@ -1465,13 +1459,9 @@ _Py_dup(int fd)
14651459
#endif
14661460

14671461
#ifdef MS_WINDOWS
1468-
_Py_BEGIN_SUPPRESS_IPH
1469-
handle = (HANDLE)_get_osfhandle(fd);
1470-
_Py_END_SUPPRESS_IPH
1471-
if (handle == INVALID_HANDLE_VALUE) {
1472-
PyErr_SetFromErrno(PyExc_OSError);
1462+
handle = (HANDLE)_Py_get_osfhandle(fd);
1463+
if (handle == INVALID_HANDLE_VALUE)
14731464
return -1;
1474-
}
14751465

14761466
/* get the file type, ignore the error if it failed */
14771467
ftype = GetFileType(handle);

0 commit comments

Comments
 (0)