Skip to content

Commit 3a04c52

Browse files
miss-islingtonasvetlov
authored andcommitted
bpo-31106: Fix handling of erros in posix_fallocate() and posix_fadvise() (GH-3000) (GH-3000) (#4101)
(cherry picked from commit d4b93e2)
1 parent 8cf7ebb commit 3a04c52

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

Lib/test/test_posix.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ def test_posix_fallocate(self):
235235
finally:
236236
os.close(fd)
237237

238+
# issue31106 - posix_fallocate() does not set error in errno.
239+
@unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
240+
"test needs posix.posix_fallocate()")
241+
def test_posix_fallocate_errno(self):
242+
try:
243+
posix.posix_fallocate(-42, 0, 10)
244+
except OSError as inst:
245+
if inst.errno != errno.EBADF:
246+
raise
247+
238248
@unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
239249
"test needs posix.posix_fadvise()")
240250
def test_posix_fadvise(self):
@@ -244,6 +254,15 @@ def test_posix_fadvise(self):
244254
finally:
245255
os.close(fd)
246256

257+
@unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
258+
"test needs posix.posix_fadvise()")
259+
def test_posix_fadvise_errno(self):
260+
try:
261+
posix.posix_fadvise(-42, 0, 0, posix.POSIX_FADV_WILLNEED)
262+
except OSError as inst:
263+
if inst.errno != errno.EBADF:
264+
raise
265+
247266
@unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime")
248267
def test_utime_with_fd(self):
249268
now = time.time()

Modules/posixmodule.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8792,11 +8792,16 @@ os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
87928792
Py_BEGIN_ALLOW_THREADS
87938793
result = posix_fallocate(fd, offset, length);
87948794
Py_END_ALLOW_THREADS
8795-
} while (result != 0 && errno == EINTR &&
8796-
!(async_err = PyErr_CheckSignals()));
8797-
if (result != 0)
8798-
return (!async_err) ? posix_error() : NULL;
8799-
Py_RETURN_NONE;
8795+
} while (result == EINTR && !(async_err = PyErr_CheckSignals()));
8796+
8797+
if (result == 0)
8798+
Py_RETURN_NONE;
8799+
8800+
if (async_err)
8801+
return NULL;
8802+
8803+
errno = result;
8804+
return posix_error();
88008805
}
88018806
#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
88028807

@@ -8834,11 +8839,16 @@ os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
88348839
Py_BEGIN_ALLOW_THREADS
88358840
result = posix_fadvise(fd, offset, length, advice);
88368841
Py_END_ALLOW_THREADS
8837-
} while (result != 0 && errno == EINTR &&
8838-
!(async_err = PyErr_CheckSignals()));
8839-
if (result != 0)
8840-
return (!async_err) ? posix_error() : NULL;
8841-
Py_RETURN_NONE;
8842+
} while (result == EINTR && !(async_err = PyErr_CheckSignals()));
8843+
8844+
if (result == 0)
8845+
Py_RETURN_NONE;
8846+
8847+
if (async_err)
8848+
return NULL;
8849+
8850+
errno = result;
8851+
return posix_error();
88428852
}
88438853
#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
88448854

0 commit comments

Comments
 (0)