From d4b93e21c2664d6a78e0656e7a7be0807be1c352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D1=80=D0=B5=D0=BD=D0=B1=D0=B5=D1=80=D0=B3=20?= =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=BA?= Date: Mon, 14 Aug 2017 18:55:16 +0500 Subject: bpo-31106: Fix handling of erros in posix_fallocate() and posix_fadvise() (#3000) (#3000) --- Lib/test/test_posix.py | 19 +++++++++++++++++++ Modules/posixmodule.c | 30 ++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 148c064..dba50e0 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -298,6 +298,16 @@ class PosixTester(unittest.TestCase): finally: os.close(fd) + # issue31106 - posix_fallocate() does not set error in errno. + @unittest.skipUnless(hasattr(posix, 'posix_fallocate'), + "test needs posix.posix_fallocate()") + def test_posix_fallocate_errno(self): + try: + posix.posix_fallocate(-42, 0, 10) + except OSError as inst: + if inst.errno != errno.EBADF: + raise + @unittest.skipUnless(hasattr(posix, 'posix_fadvise'), "test needs posix.posix_fadvise()") def test_posix_fadvise(self): @@ -307,6 +317,15 @@ class PosixTester(unittest.TestCase): finally: os.close(fd) + @unittest.skipUnless(hasattr(posix, 'posix_fadvise'), + "test needs posix.posix_fadvise()") + def test_posix_fadvise_errno(self): + try: + posix.posix_fadvise(-42, 0, 0, posix.POSIX_FADV_WILLNEED) + except OSError as inst: + if inst.errno != errno.EBADF: + raise + @unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime") def test_utime_with_fd(self): now = time.time() diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 2e5e79a..f057787 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -8917,11 +8917,16 @@ os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, Py_BEGIN_ALLOW_THREADS result = posix_fallocate(fd, offset, length); Py_END_ALLOW_THREADS - } while (result != 0 && errno == EINTR && - !(async_err = PyErr_CheckSignals())); - if (result != 0) - return (!async_err) ? posix_error() : NULL; - Py_RETURN_NONE; + } while (result == EINTR && !(async_err = PyErr_CheckSignals())); + + if (result == 0) + Py_RETURN_NONE; + + if (async_err) + return NULL; + + errno = result; + return posix_error(); } #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ @@ -8959,11 +8964,16 @@ os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, Py_BEGIN_ALLOW_THREADS result = posix_fadvise(fd, offset, length, advice); Py_END_ALLOW_THREADS - } while (result != 0 && errno == EINTR && - !(async_err = PyErr_CheckSignals())); - if (result != 0) - return (!async_err) ? posix_error() : NULL; - Py_RETURN_NONE; + } while (result == EINTR && !(async_err = PyErr_CheckSignals())); + + if (result == 0) + Py_RETURN_NONE; + + if (async_err) + return NULL; + + errno = result; + return posix_error(); } #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ -- cgit v0.12