summaryrefslogtreecommitdiffstats
path: root/Modules/posixmodule.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 /Modules/posixmodule.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 'Modules/posixmodule.c')
-rw-r--r--Modules/posixmodule.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index b7acbc3..0bee3c9 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -11146,6 +11146,56 @@ posix_set_handle_inheritable(PyObject *self, PyObject *args)
#endif /* MS_WINDOWS */
+#ifndef MS_WINDOWS
+PyDoc_STRVAR(get_blocking__doc__,
+ "get_blocking(fd) -> bool\n" \
+ "\n" \
+ "Get the blocking mode of the file descriptor:\n" \
+ "False if the O_NONBLOCK flag is set, True if the flag is cleared.");
+
+static PyObject*
+posix_get_blocking(PyObject *self, PyObject *args)
+{
+ int fd;
+ int blocking;
+
+ if (!PyArg_ParseTuple(args, "i:get_blocking", &fd))
+ return NULL;
+
+ if (!_PyVerify_fd(fd))
+ return posix_error();
+
+ blocking = _Py_get_blocking(fd);
+ if (blocking < 0)
+ return NULL;
+ return PyBool_FromLong(blocking);
+}
+
+PyDoc_STRVAR(set_blocking__doc__,
+ "set_blocking(fd, blocking)\n" \
+ "\n" \
+ "Set the blocking mode of the specified file descriptor.\n" \
+ "Set the O_NONBLOCK flag if blocking is False,\n" \
+ "clear the O_NONBLOCK flag otherwise.");
+
+static PyObject*
+posix_set_blocking(PyObject *self, PyObject *args)
+{
+ int fd, blocking;
+
+ if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking))
+ return NULL;
+
+ if (!_PyVerify_fd(fd))
+ return posix_error();
+
+ if (_Py_set_blocking(fd, blocking) < 0)
+ return NULL;
+ Py_RETURN_NONE;
+}
+#endif /* !MS_WINDOWS */
+
+
/*[clinic input]
dump buffer
[clinic start generated code]*/
@@ -11606,6 +11656,10 @@ static PyMethodDef posix_methods[] = {
{"set_handle_inheritable", posix_set_handle_inheritable,
METH_VARARGS, set_handle_inheritable__doc__},
#endif
+#ifndef MS_WINDOWS
+ {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__},
+ {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__},
+#endif
{NULL, NULL} /* Sentinel */
};