PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
#endif /* !MS_WINDOWS */
-#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900
-/* A routine to check if a file descriptor is valid on Windows. Returns 0
- * and sets errno to EBADF if it isn't. This is to avoid Assertions
- * from various functions in the Windows CRT beginning with
- * Visual Studio 2005
- */
-int _PyVerify_fd(int fd);
-
-#else
-#define _PyVerify_fd(A) (1) /* dummy */
-#endif
-
#endif /* Py_LIMITED_API */
#ifdef __cplusplus
int fd = self->fd;
self->fd = -1;
/* fd is accessible and someone else may have closed it */
- if (_PyVerify_fd(fd)) {
- Py_BEGIN_ALLOW_THREADS
- _Py_BEGIN_SUPPRESS_IPH
- err = close(fd);
- if (err < 0)
- save_errno = errno;
- _Py_END_SUPPRESS_IPH
- Py_END_ALLOW_THREADS
- } else {
+ Py_BEGIN_ALLOW_THREADS
+ _Py_BEGIN_SUPPRESS_IPH
+ err = close(fd);
+ if (err < 0)
save_errno = errno;
- err = -1;
- }
+ _Py_END_SUPPRESS_IPH
+ Py_END_ALLOW_THREADS
}
if (err < 0) {
errno = save_errno;
if (self->fd < 0)
return err_closed();
- if (!_PyVerify_fd(self->fd))
- return PyErr_SetFromErrno(PyExc_IOError);
_Py_BEGIN_SUPPRESS_IPH
#ifdef MS_WINDOWS
return NULL;
}
- if (_PyVerify_fd(fd)) {
- Py_BEGIN_ALLOW_THREADS
- _Py_BEGIN_SUPPRESS_IPH
+ Py_BEGIN_ALLOW_THREADS
+ _Py_BEGIN_SUPPRESS_IPH
#ifdef MS_WINDOWS
- res = _lseeki64(fd, pos, whence);
+ res = _lseeki64(fd, pos, whence);
#else
- res = lseek(fd, pos, whence);
+ res = lseek(fd, pos, whence);
#endif
- _Py_END_SUPPRESS_IPH
- Py_END_ALLOW_THREADS
- } else
- res = -1;
+ _Py_END_SUPPRESS_IPH
+ Py_END_ALLOW_THREADS
if (res < 0)
return PyErr_SetFromErrno(PyExc_IOError);
return err_closed();
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
- if (_PyVerify_fd(self->fd))
- res = isatty(self->fd);
- else
- res = 0;
+ res = isatty(self->fd);
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
return PyBool_FromLong(res);
fd = _PyLong_AsInt(file);
if (fd == -1 && PyErr_Occurred())
return -1;
- if (fd < 0 || !_PyVerify_fd(fd)) {
+ if (fd < 0) {
PyErr_SetString(PyExc_ValueError,
"file is not a valid file descripter");
return -1;
*/
if (fileno != -1 && fileno != 0) {
/* Ensure that fileno is within the CRT's valid range */
- if (!_PyVerify_fd(fileno)) {
- PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
- }
_Py_BEGIN_SUPPRESS_IPH
fh = (HANDLE)_get_osfhandle(fileno);
_Py_END_SUPPRESS_IPH
#endif
}
-
-#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900
-/* Legacy implementation of _PyVerify_fd_dup2 while transitioning to
- * MSVC 14.0. This should eventually be removed. (issue23524)
- */
-#define IOINFO_L2E 5
-#define IOINFO_ARRAYS 64
-#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
-#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
-#define _NO_CONSOLE_FILENO (intptr_t)-2
-
-/* the special case of checking dup2. The target fd must be in a sensible range */
-static int
-_PyVerify_fd_dup2(int fd1, int fd2)
-{
- if (!_PyVerify_fd(fd1))
- return 0;
- if (fd2 == _NO_CONSOLE_FILENO)
- return 0;
- if ((unsigned)fd2 < _NHANDLE_)
- return 1;
- else
- return 0;
-}
-#else
-#define _PyVerify_fd_dup2(fd1, fd2) (_PyVerify_fd(fd1) && (fd2) >= 0)
-#endif
-
#ifdef MS_WINDOWS
static int
int res;
int async_err = 0;
- if (!_PyVerify_fd(fd))
- return posix_error();
-
do {
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
{
int res;
- if (!_PyVerify_fd(fd))
- return posix_error();
/* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
* and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
* for more details.
int i;
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
- for (i = fd_low; i < fd_high; i++)
- if (_PyVerify_fd(i))
- close(i);
+ for (i = max(fd_low, 0); i < fd_high; i++)
+ close(i);
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
int dup3_works = -1;
#endif
- if (!_PyVerify_fd_dup2(fd, fd2))
+ if (fd < 0 || fd2 < 0)
return posix_error();
/* dup2() can fail with EINTR if the target FD is already open, because it
{
Py_off_t result;
- if (!_PyVerify_fd(fd)) {
- posix_error();
- return -1;
- }
#ifdef SEEK_SET
/* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
switch (how) {
if (PyErr_Occurred())
return -1;
- if (!_PyVerify_fd(fd)) {
- posix_error();
- return -1;
- }
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
#ifdef MS_WINDOWS
buffer = PyBytes_FromStringAndSize((char *)NULL, length);
if (buffer == NULL)
return NULL;
- if (!_PyVerify_fd(fd)) {
- Py_DECREF(buffer);
- return posix_error();
- }
do {
Py_BEGIN_ALLOW_THREADS
/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
{
int return_value;
- if (!_PyVerify_fd(fd))
- return 0;
_Py_BEGIN_SUPPRESS_IPH
return_value = isatty(fd);
_Py_END_SUPPRESS_IPH
Py_ssize_t size;
int async_err = 0;
- if (!_PyVerify_fd(fd)) {
- posix_error();
- return -1;
- }
-
do {
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
int result;
int async_err = 0;
- if (!_PyVerify_fd(fd))
- return posix_error();
-
do {
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
{
int return_value;
- if (!_PyVerify_fd(fd)) {
- posix_error();
- return -1;
- }
-
_Py_BEGIN_SUPPRESS_IPH
return_value = _Py_get_inheritable(fd);
_Py_END_SUPPRESS_IPH
/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
{
int result;
- if (!_PyVerify_fd(fd))
- return posix_error();
_Py_BEGIN_SUPPRESS_IPH
result = _Py_set_inheritable(fd, inheritable, NULL);
if (!PyArg_ParseTuple(args, "i:get_blocking", &fd))
return NULL;
- if (!_PyVerify_fd(fd))
- return posix_error();
-
_Py_BEGIN_SUPPRESS_IPH
blocking = _Py_get_blocking(fd);
_Py_END_SUPPRESS_IPH
if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking))
return NULL;
- if (!_PyVerify_fd(fd))
- return posix_error();
-
_Py_BEGIN_SUPPRESS_IPH
result = _Py_set_blocking(fd, blocking);
_Py_END_SUPPRESS_IPH
}
fd = (int)sockfd;
- if ((SOCKET_T)fd != sockfd || !_PyVerify_fd(fd)) {
+ if ((SOCKET_T)fd != sockfd) {
PyErr_SetString(PyExc_ValueError, "invalid fd");
return NULL;
}
if (fd != -1) {
int blocking;
- if (!_PyVerify_fd(fd)) {
- PyErr_SetString(PyExc_ValueError, "invalid fd");
- return NULL;
- }
-
if (_Py_fstat(fd, &status) != 0)
return NULL;
{
intptr_t handle = -1;
- if (!_PyVerify_fd(fd)) {
- PyErr_SetFromErrno(PyExc_IOError);
- }
- else {
_Py_BEGIN_SUPPRESS_IPH
- handle = _get_osfhandle(fd);
+ handle = _get_osfhandle(fd);
_Py_END_SUPPRESS_IPH
- if (handle == -1)
- PyErr_SetFromErrno(PyExc_IOError);
- }
+ if (handle == -1)
+ PyErr_SetFromErrno(PyExc_IOError);
return handle;
}
(void)(PyOS_InputHook)();
errno = 0;
clearerr(fp);
- if (_PyVerify_fd(fileno(fp)))
- p = fgets(buf, len, fp);
- else
- p = NULL;
+ p = fgets(buf, len, fp);
if (p != NULL)
return 0; /* No error */
err = errno;
#endif
int valid;
_Py_BEGIN_SUPPRESS_IPH
- valid = _PyVerify_fd(fd) && isatty(fd);
+ valid = isatty(fd);
_Py_END_SUPPRESS_IPH
if (!valid)
Py_RETURN_NONE;
HANDLE h;
int type;
- if (!_PyVerify_fd(fd))
- h = INVALID_HANDLE_VALUE;
- else {
- _Py_BEGIN_SUPPRESS_IPH
- h = (HANDLE)_get_osfhandle(fd);
- _Py_END_SUPPRESS_IPH
- }
+ _Py_BEGIN_SUPPRESS_IPH
+ h = (HANDLE)_get_osfhandle(fd);
+ _Py_END_SUPPRESS_IPH
if (h == INVALID_HANDLE_VALUE) {
/* errno is already set by _get_osfhandle, but we also set
HANDLE handle;
DWORD flags;
- if (!_PyVerify_fd(fd)) {
- if (raise)
- PyErr_SetFromErrno(PyExc_OSError);
- return -1;
- }
-
_Py_BEGIN_SUPPRESS_IPH
handle = (HANDLE)_get_osfhandle(fd);
_Py_END_SUPPRESS_IPH
}
#ifdef MS_WINDOWS
- if (!_PyVerify_fd(fd)) {
- if (raise)
- PyErr_SetFromErrno(PyExc_OSError);
- return -1;
- }
-
_Py_BEGIN_SUPPRESS_IPH
handle = (HANDLE)_get_osfhandle(fd);
_Py_END_SUPPRESS_IPH
* handler raised an exception. */
assert(!PyErr_Occurred());
- if (!_PyVerify_fd(fd)) {
- /* save/restore errno because PyErr_SetFromErrno() can modify it */
- err = errno;
- PyErr_SetFromErrno(PyExc_OSError);
- errno = err;
- return -1;
- }
-
#ifdef MS_WINDOWS
if (count > INT_MAX) {
/* On Windows, the count parameter of read() is an int */
int err;
int async_err = 0;
- if (!_PyVerify_fd(fd)) {
- if (gil_held) {
- /* save/restore errno because PyErr_SetFromErrno() can modify it */
- err = errno;
- PyErr_SetFromErrno(PyExc_OSError);
- errno = err;
- }
- return -1;
- }
-
_Py_BEGIN_SUPPRESS_IPH
#ifdef MS_WINDOWS
if (count > 32767 && isatty(fd)) {
assert(PyGILState_Check());
#endif
- if (!_PyVerify_fd(fd)) {
- PyErr_SetFromErrno(PyExc_OSError);
- return -1;
- }
-
#ifdef MS_WINDOWS
_Py_BEGIN_SUPPRESS_IPH
handle = (HANDLE)_get_osfhandle(fd);
return -1;
}
#endif
-
-#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900
-/* Legacy implementation of _PyVerify_fd while transitioning to
- * MSVC 14.0. This should eventually be removed. (issue23524)
- */
-
-/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
- * valid and raise an assertion if it isn't.
- * Normally, an invalid fd is likely to be a C program error and therefore
- * an assertion can be useful, but it does contradict the POSIX standard
- * which for write(2) states:
- * "Otherwise, -1 shall be returned and errno set to indicate the error."
- * "[EBADF] The fildes argument is not a valid file descriptor open for
- * writing."
- * Furthermore, python allows the user to enter any old integer
- * as a fd and should merely raise a python exception on error.
- * The Microsoft CRT doesn't provide an official way to check for the
- * validity of a file descriptor, but we can emulate its internal behaviour
- * by using the exported __pinfo data member and knowledge of the
- * internal structures involved.
- * The structures below must be updated for each version of visual studio
- * according to the file internal.h in the CRT source, until MS comes
- * up with a less hacky way to do this.
- * (all of this is to avoid globally modifying the CRT behaviour using
- * _set_invalid_parameter_handler() and _CrtSetReportMode())
- */
-/* The actual size of the structure is determined at runtime.
- * Only the first items must be present.
- */
-typedef struct {
- intptr_t osfhnd;
- char osfile;
-} my_ioinfo;
-
-extern __declspec(dllimport) char * __pioinfo[];
-#define IOINFO_L2E 5
-#define IOINFO_ARRAYS 64
-#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
-#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
-#define FOPEN 0x01
-#define _NO_CONSOLE_FILENO (intptr_t)-2
-
-/* This function emulates what the windows CRT does to validate file handles */
-int
-_PyVerify_fd(int fd)
-{
- const int i1 = fd >> IOINFO_L2E;
- const int i2 = fd & ((1 << IOINFO_L2E) - 1);
-
- static size_t sizeof_ioinfo = 0;
-
- /* Determine the actual size of the ioinfo structure,
- * as used by the CRT loaded in memory
- */
- if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
- sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
- }
- if (sizeof_ioinfo == 0) {
- /* This should not happen... */
- goto fail;
- }
-
- /* See that it isn't a special CLEAR fileno */
- if (fd != _NO_CONSOLE_FILENO) {
- /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
- * we check pointer validity and other info
- */
- if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
- /* finally, check that the file is open */
- my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
- if (info->osfile & FOPEN) {
- return 1;
- }
- }
- }
- fail:
- errno = EBADF;
- return 0;
-}
-
-#endif /* defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 */
is_valid_fd(int fd)
{
int fd2;
- if (fd < 0 || !_PyVerify_fd(fd))
+ if (fd < 0)
return 0;
_Py_BEGIN_SUPPRESS_IPH
/* Prefer dup() over fstat(). fstat() can require input/output whereas