Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 940f33a

Browse files
committed
Issue #23524: Finish removing _PyVerify_fd from sources
1 parent dee6e25 commit 940f33a

10 files changed

Lines changed: 27 additions & 258 deletions

File tree

Include/fileutils.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,6 @@ PyAPI_FUNC(int) _Py_get_blocking(int fd);
121121
PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
122122
#endif /* !MS_WINDOWS */
123123

124-
#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900
125-
/* A routine to check if a file descriptor is valid on Windows. Returns 0
126-
* and sets errno to EBADF if it isn't. This is to avoid Assertions
127-
* from various functions in the Windows CRT beginning with
128-
* Visual Studio 2005
129-
*/
130-
int _PyVerify_fd(int fd);
131-
132-
#else
133-
#define _PyVerify_fd(A) (1) /* dummy */
134-
#endif
135-
136124
#endif /* Py_LIMITED_API */
137125

138126
#ifdef __cplusplus

Modules/_io/fileio.c

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,13 @@ internal_close(fileio *self)
117117
int fd = self->fd;
118118
self->fd = -1;
119119
/* fd is accessible and someone else may have closed it */
120-
if (_PyVerify_fd(fd)) {
121-
Py_BEGIN_ALLOW_THREADS
122-
_Py_BEGIN_SUPPRESS_IPH
123-
err = close(fd);
124-
if (err < 0)
125-
save_errno = errno;
126-
_Py_END_SUPPRESS_IPH
127-
Py_END_ALLOW_THREADS
128-
} else {
120+
Py_BEGIN_ALLOW_THREADS
121+
_Py_BEGIN_SUPPRESS_IPH
122+
err = close(fd);
123+
if (err < 0)
129124
save_errno = errno;
130-
err = -1;
131-
}
125+
_Py_END_SUPPRESS_IPH
126+
Py_END_ALLOW_THREADS
132127
}
133128
if (err < 0) {
134129
errno = save_errno;
@@ -700,8 +695,6 @@ _io_FileIO_readall_impl(fileio *self)
700695

701696
if (self->fd < 0)
702697
return err_closed();
703-
if (!_PyVerify_fd(self->fd))
704-
return PyErr_SetFromErrno(PyExc_IOError);
705698

706699
_Py_BEGIN_SUPPRESS_IPH
707700
#ifdef MS_WINDOWS
@@ -914,18 +907,15 @@ portable_lseek(int fd, PyObject *posobj, int whence)
914907
return NULL;
915908
}
916909

917-
if (_PyVerify_fd(fd)) {
918-
Py_BEGIN_ALLOW_THREADS
919-
_Py_BEGIN_SUPPRESS_IPH
910+
Py_BEGIN_ALLOW_THREADS
911+
_Py_BEGIN_SUPPRESS_IPH
920912
#ifdef MS_WINDOWS
921-
res = _lseeki64(fd, pos, whence);
913+
res = _lseeki64(fd, pos, whence);
922914
#else
923-
res = lseek(fd, pos, whence);
915+
res = lseek(fd, pos, whence);
924916
#endif
925-
_Py_END_SUPPRESS_IPH
926-
Py_END_ALLOW_THREADS
927-
} else
928-
res = -1;
917+
_Py_END_SUPPRESS_IPH
918+
Py_END_ALLOW_THREADS
929919
if (res < 0)
930920
return PyErr_SetFromErrno(PyExc_IOError);
931921

@@ -1116,10 +1106,7 @@ _io_FileIO_isatty_impl(fileio *self)
11161106
return err_closed();
11171107
Py_BEGIN_ALLOW_THREADS
11181108
_Py_BEGIN_SUPPRESS_IPH
1119-
if (_PyVerify_fd(self->fd))
1120-
res = isatty(self->fd);
1121-
else
1122-
res = 0;
1109+
res = isatty(self->fd);
11231110
_Py_END_SUPPRESS_IPH
11241111
Py_END_ALLOW_THREADS
11251112
return PyBool_FromLong(res);

Modules/faulthandler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ faulthandler_get_fileno(PyObject **file_ptr)
159159
fd = _PyLong_AsInt(file);
160160
if (fd == -1 && PyErr_Occurred())
161161
return -1;
162-
if (fd < 0 || !_PyVerify_fd(fd)) {
162+
if (fd < 0) {
163163
PyErr_SetString(PyExc_ValueError,
164164
"file is not a valid file descripter");
165165
return -1;

Modules/mmapmodule.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,10 +1326,6 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
13261326
*/
13271327
if (fileno != -1 && fileno != 0) {
13281328
/* Ensure that fileno is within the CRT's valid range */
1329-
if (!_PyVerify_fd(fileno)) {
1330-
PyErr_SetFromErrno(PyExc_OSError);
1331-
return NULL;
1332-
}
13331329
_Py_BEGIN_SUPPRESS_IPH
13341330
fh = (HANDLE)_get_osfhandle(fileno);
13351331
_Py_END_SUPPRESS_IPH

Modules/posixmodule.c

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,34 +1178,6 @@ PyLong_FromPy_off_t(Py_off_t offset)
11781178
#endif
11791179
}
11801180

1181-
1182-
#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900
1183-
/* Legacy implementation of _PyVerify_fd_dup2 while transitioning to
1184-
* MSVC 14.0. This should eventually be removed. (issue23524)
1185-
*/
1186-
#define IOINFO_L2E 5
1187-
#define IOINFO_ARRAYS 64
1188-
#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
1189-
#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
1190-
#define _NO_CONSOLE_FILENO (intptr_t)-2
1191-
1192-
/* the special case of checking dup2. The target fd must be in a sensible range */
1193-
static int
1194-
_PyVerify_fd_dup2(int fd1, int fd2)
1195-
{
1196-
if (!_PyVerify_fd(fd1))
1197-
return 0;
1198-
if (fd2 == _NO_CONSOLE_FILENO)
1199-
return 0;
1200-
if ((unsigned)fd2 < _NHANDLE_)
1201-
return 1;
1202-
else
1203-
return 0;
1204-
}
1205-
#else
1206-
#define _PyVerify_fd_dup2(fd1, fd2) (_PyVerify_fd(fd1) && (fd2) >= 0)
1207-
#endif
1208-
12091181
#ifdef MS_WINDOWS
12101182

12111183
static int
@@ -1409,9 +1381,6 @@ posix_fildes_fd(int fd, int (*func)(int))
14091381
int res;
14101382
int async_err = 0;
14111383

1412-
if (!_PyVerify_fd(fd))
1413-
return posix_error();
1414-
14151384
do {
14161385
Py_BEGIN_ALLOW_THREADS
14171386
_Py_BEGIN_SUPPRESS_IPH
@@ -7549,8 +7518,6 @@ os_close_impl(PyObject *module, int fd)
75497518
/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
75507519
{
75517520
int res;
7552-
if (!_PyVerify_fd(fd))
7553-
return posix_error();
75547521
/* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
75557522
* and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
75567523
* for more details.
@@ -7583,9 +7550,8 @@ os_closerange_impl(PyObject *module, int fd_low, int fd_high)
75837550
int i;
75847551
Py_BEGIN_ALLOW_THREADS
75857552
_Py_BEGIN_SUPPRESS_IPH
7586-
for (i = fd_low; i < fd_high; i++)
7587-
if (_PyVerify_fd(i))
7588-
close(i);
7553+
for (i = max(fd_low, 0); i < fd_high; i++)
7554+
close(i);
75897555
_Py_END_SUPPRESS_IPH
75907556
Py_END_ALLOW_THREADS
75917557
Py_RETURN_NONE;
@@ -7629,7 +7595,7 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
76297595
int dup3_works = -1;
76307596
#endif
76317597

7632-
if (!_PyVerify_fd_dup2(fd, fd2))
7598+
if (fd < 0 || fd2 < 0)
76337599
return posix_error();
76347600

76357601
/* dup2() can fail with EINTR if the target FD is already open, because it
@@ -7753,10 +7719,6 @@ os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
77537719
{
77547720
Py_off_t result;
77557721

7756-
if (!_PyVerify_fd(fd)) {
7757-
posix_error();
7758-
return -1;
7759-
}
77607722
#ifdef SEEK_SET
77617723
/* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
77627724
switch (how) {
@@ -7769,10 +7731,6 @@ os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
77697731
if (PyErr_Occurred())
77707732
return -1;
77717733

7772-
if (!_PyVerify_fd(fd)) {
7773-
posix_error();
7774-
return -1;
7775-
}
77767734
Py_BEGIN_ALLOW_THREADS
77777735
_Py_BEGIN_SUPPRESS_IPH
77787736
#ifdef MS_WINDOWS
@@ -7980,10 +7938,6 @@ os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
79807938
buffer = PyBytes_FromStringAndSize((char *)NULL, length);
79817939
if (buffer == NULL)
79827940
return NULL;
7983-
if (!_PyVerify_fd(fd)) {
7984-
Py_DECREF(buffer);
7985-
return posix_error();
7986-
}
79877941

79887942
do {
79897943
Py_BEGIN_ALLOW_THREADS
@@ -8226,8 +8180,6 @@ os_isatty_impl(PyObject *module, int fd)
82268180
/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
82278181
{
82288182
int return_value;
8229-
if (!_PyVerify_fd(fd))
8230-
return 0;
82318183
_Py_BEGIN_SUPPRESS_IPH
82328184
return_value = isatty(fd);
82338185
_Py_END_SUPPRESS_IPH
@@ -8419,11 +8371,6 @@ os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
84198371
Py_ssize_t size;
84208372
int async_err = 0;
84218373

8422-
if (!_PyVerify_fd(fd)) {
8423-
posix_error();
8424-
return -1;
8425-
}
8426-
84278374
do {
84288375
Py_BEGIN_ALLOW_THREADS
84298376
_Py_BEGIN_SUPPRESS_IPH
@@ -8606,9 +8553,6 @@ os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
86068553
int result;
86078554
int async_err = 0;
86088555

8609-
if (!_PyVerify_fd(fd))
8610-
return posix_error();
8611-
86128556
do {
86138557
Py_BEGIN_ALLOW_THREADS
86148558
_Py_BEGIN_SUPPRESS_IPH
@@ -10979,11 +10923,6 @@ os_get_inheritable_impl(PyObject *module, int fd)
1097910923
/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
1098010924
{
1098110925
int return_value;
10982-
if (!_PyVerify_fd(fd)) {
10983-
posix_error();
10984-
return -1;
10985-
}
10986-
1098710926
_Py_BEGIN_SUPPRESS_IPH
1098810927
return_value = _Py_get_inheritable(fd);
1098910928
_Py_END_SUPPRESS_IPH
@@ -11005,8 +10944,6 @@ os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
1100510944
/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
1100610945
{
1100710946
int result;
11008-
if (!_PyVerify_fd(fd))
11009-
return posix_error();
1101010947

1101110948
_Py_BEGIN_SUPPRESS_IPH
1101210949
result = _Py_set_inheritable(fd, inheritable, NULL);
@@ -11080,9 +11017,6 @@ posix_get_blocking(PyObject *self, PyObject *args)
1108011017
if (!PyArg_ParseTuple(args, "i:get_blocking", &fd))
1108111018
return NULL;
1108211019

11083-
if (!_PyVerify_fd(fd))
11084-
return posix_error();
11085-
1108611020
_Py_BEGIN_SUPPRESS_IPH
1108711021
blocking = _Py_get_blocking(fd);
1108811022
_Py_END_SUPPRESS_IPH
@@ -11106,9 +11040,6 @@ posix_set_blocking(PyObject *self, PyObject *args)
1110611040
if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking))
1110711041
return NULL;
1110811042

11109-
if (!_PyVerify_fd(fd))
11110-
return posix_error();
11111-
1111211043
_Py_BEGIN_SUPPRESS_IPH
1111311044
result = _Py_set_blocking(fd, blocking);
1111411045
_Py_END_SUPPRESS_IPH

Modules/signalmodule.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args)
579579
}
580580

581581
fd = (int)sockfd;
582-
if ((SOCKET_T)fd != sockfd || !_PyVerify_fd(fd)) {
582+
if ((SOCKET_T)fd != sockfd) {
583583
PyErr_SetString(PyExc_ValueError, "invalid fd");
584584
return NULL;
585585
}
@@ -609,11 +609,6 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args)
609609
if (fd != -1) {
610610
int blocking;
611611

612-
if (!_PyVerify_fd(fd)) {
613-
PyErr_SetString(PyExc_ValueError, "invalid fd");
614-
return NULL;
615-
}
616-
617612
if (_Py_fstat(fd, &status) != 0)
618613
return NULL;
619614

PC/msvcrtmodule.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,11 @@ msvcrt_get_osfhandle_impl(PyObject *module, int fd)
189189
{
190190
intptr_t handle = -1;
191191

192-
if (!_PyVerify_fd(fd)) {
193-
PyErr_SetFromErrno(PyExc_IOError);
194-
}
195-
else {
196192
_Py_BEGIN_SUPPRESS_IPH
197-
handle = _get_osfhandle(fd);
193+
handle = _get_osfhandle(fd);
198194
_Py_END_SUPPRESS_IPH
199-
if (handle == -1)
200-
PyErr_SetFromErrno(PyExc_IOError);
201-
}
195+
if (handle == -1)
196+
PyErr_SetFromErrno(PyExc_IOError);
202197

203198
return handle;
204199
}

Parser/myreadline.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ my_fgets(char *buf, int len, FILE *fp)
4141
(void)(PyOS_InputHook)();
4242
errno = 0;
4343
clearerr(fp);
44-
if (_PyVerify_fd(fileno(fp)))
45-
p = fgets(buf, len, fp);
46-
else
47-
p = NULL;
44+
p = fgets(buf, len, fp);
4845
if (p != NULL)
4946
return 0; /* No error */
5047
err = errno;

0 commit comments

Comments
 (0)