summaryrefslogtreecommitdiffstats
path: root/Python/fileutils.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-07-29 20:32:47 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2014-07-29 20:32:47 (GMT)
commit1db9e7bb19909ed56821b1580cbb024faccac041 (patch)
tree20043197ec08844340c9ac039fe26c630bd4189d /Python/fileutils.c
parent6aa4269ed25f7fdddd99fe1d7b09b8406ecb05ea (diff)
downloadcpython-1db9e7bb19909ed56821b1580cbb024faccac041.zip
cpython-1db9e7bb19909ed56821b1580cbb024faccac041.tar.gz
cpython-1db9e7bb19909ed56821b1580cbb024faccac041.tar.bz2
Issue #22054: Add os.get_blocking() and os.set_blocking() functions to get and
set the blocking mode of a file descriptor (False if the O_NONBLOCK flag is set, True otherwise). These functions are not available on Windows.
Diffstat (limited to 'Python/fileutils.c')
-rw-r--r--Python/fileutils.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c
index a55064f..065d3fd 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -1045,3 +1045,56 @@ _Py_dup(int fd)
return fd;
}
+#ifndef MS_WINDOWS
+/* Get the blocking mode of the file descriptor.
+ Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
+ raise an exception and return -1 on error. */
+int
+_Py_get_blocking(int fd)
+{
+ int flags = fcntl(fd, F_GETFL, 0);
+ if (flags < 0) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return -1;
+ }
+
+ return !(flags & O_NONBLOCK);
+}
+
+/* Set the blocking mode of the specified file descriptor.
+
+ Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
+ otherwise.
+
+ Return 0 on success, raise an exception and return -1 on error. */
+int
+_Py_set_blocking(int fd, int blocking)
+{
+#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
+ int arg = !blocking;
+ if (ioctl(fd, FIONBIO, &arg) < 0)
+ goto error;
+#else
+ int flags, res;
+
+ flags = fcntl(fd, F_GETFL, 0);
+ if (flags < 0)
+ goto error;
+
+ if (blocking)
+ flags = flags & (~O_NONBLOCK);
+ else
+ flags = flags | O_NONBLOCK;
+
+ res = fcntl(fd, F_SETFL, flags);
+ if (res < 0)
+ goto error;
+#endif
+ return 0;
+
+error:
+ PyErr_SetFromErrno(PyExc_OSError);
+ return -1;
+}
+#endif
+