diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-03-19 21:53:20 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-03-19 21:53:20 (GMT) |
commit | 66aab0c4b5b6b328aebc115f96275e7dcd114f8b (patch) | |
tree | c607e0019befcb199fb8f916038d01a0b3b9a5f3 /Modules/posixmodule.c | |
parent | 9eb57c5fa50ed2f57d9320bb575371868316b5f2 (diff) | |
download | cpython-66aab0c4b5b6b328aebc115f96275e7dcd114f8b.zip cpython-66aab0c4b5b6b328aebc115f96275e7dcd114f8b.tar.gz cpython-66aab0c4b5b6b328aebc115f96275e7dcd114f8b.tar.bz2 |
Issue #23708: Add _Py_read() and _Py_write() functions to factorize code handle
EINTR error and special cases for Windows.
These functions now truncate the length to PY_SSIZE_T_MAX to have a portable
and reliable behaviour. For example, read() result is undefined if counter is
greater than PY_SSIZE_T_MAX on Linux.
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r-- | Modules/posixmodule.c | 46 |
1 files changed, 5 insertions, 41 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index b8151c3..7aa8050 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -11207,37 +11207,27 @@ os_read_impl(PyModuleDef *module, int fd, Py_ssize_t length) /*[clinic end generated code: output=1f3bc27260a24968 input=1df2eaa27c0bf1d3]*/ { Py_ssize_t n; - int async_err = 0; PyObject *buffer; if (length < 0) { errno = EINVAL; return posix_error(); } - if (!_PyVerify_fd(fd)) - return posix_error(); #ifdef MS_WINDOWS - #define READ_CAST (int) + /* On Windows, the count parameter of read() is an int */ if (length > INT_MAX) length = INT_MAX; -#else - #define READ_CAST #endif buffer = PyBytes_FromStringAndSize((char *)NULL, length); if (buffer == NULL) return NULL; - do { - Py_BEGIN_ALLOW_THREADS - n = read(fd, PyBytes_AS_STRING(buffer), READ_CAST length); - Py_END_ALLOW_THREADS - } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); - - if (n < 0) { + n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); + if (n == -1) { Py_DECREF(buffer); - return (!async_err) ? posix_error() : NULL; + return NULL; } if (n != length) @@ -11541,33 +11531,7 @@ static Py_ssize_t os_write_impl(PyModuleDef *module, int fd, Py_buffer *data) /*[clinic end generated code: output=aeb96acfdd4d5112 input=3207e28963234f3c]*/ { - Py_ssize_t size; - int async_err = 0; - Py_ssize_t len = data->len; - - if (!_PyVerify_fd(fd)) { - posix_error(); - return -1; - } - - do { - Py_BEGIN_ALLOW_THREADS -#ifdef MS_WINDOWS - if (len > INT_MAX) - len = INT_MAX; - size = write(fd, data->buf, (int)len); -#else - size = write(fd, data->buf, len); -#endif - Py_END_ALLOW_THREADS - } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); - - if (size < 0) { - if (!async_err) - posix_error(); - return -1; - } - return size; + return _Py_write(fd, data->buf, data->len); } #ifdef HAVE_SENDFILE |