Skip to content

Commit d4b93e2

Browse files
socketpairlarryhastings
authored andcommitted
bpo-31106: Fix handling of erros in posix_fallocate() and posix_fadvise() (#3000) (#3000)
1 parent 48d9823 commit d4b93e2

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
@@ -298,6 +298,16 @@ def test_posix_fallocate(self):
298298
finally:
299299
os.close(fd)
300300

301+
# issue31106 - posix_fallocate() does not set error in errno.
302+
@unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
303+
"test needs posix.posix_fallocate()")
304+
def test_posix_fallocate_errno(self):
305+
try:
306+
posix.posix_fallocate(-42, 0, 10)
307+
except OSError as inst:
308+
if inst.errno != errno.EBADF:
309+
raise
310+
301311
@unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
302312
"test needs posix.posix_fadvise()")
303313
def test_posix_fadvise(self):
@@ -307,6 +317,15 @@ def test_posix_fadvise(self):
307317
finally:
308318
os.close(fd)
309319

320+
@unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
321+
"test needs posix.posix_fadvise()")
322+
def test_posix_fadvise_errno(self):
323+
try:
324+
posix.posix_fadvise(-42, 0, 0, posix.POSIX_FADV_WILLNEED)
325+
except OSError as inst:
326+
if inst.errno != errno.EBADF:
327+
raise
328+
310329
@unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime")
311330
def test_utime_with_fd(self):
312331
now = time.time()

Modules/posixmodule.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8917,11 +8917,16 @@ os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
89178917
Py_BEGIN_ALLOW_THREADS
89188918
result = posix_fallocate(fd, offset, length);
89198919
Py_END_ALLOW_THREADS
8920-
} while (result != 0 && errno == EINTR &&
8921-
!(async_err = PyErr_CheckSignals()));
8922-
if (result != 0)
8923-
return (!async_err) ? posix_error() : NULL;
8924-
Py_RETURN_NONE;
8920+
} while (result == EINTR && !(async_err = PyErr_CheckSignals()));
8921+
8922+
if (result == 0)
8923+
Py_RETURN_NONE;
8924+
8925+
if (async_err)
8926+
return NULL;
8927+
8928+
errno = result;
8929+
return posix_error();
89258930
}
89268931
#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
89278932

@@ -8959,11 +8964,16 @@ os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
89598964
Py_BEGIN_ALLOW_THREADS
89608965
result = posix_fadvise(fd, offset, length, advice);
89618966
Py_END_ALLOW_THREADS
8962-
} while (result != 0 && errno == EINTR &&
8963-
!(async_err = PyErr_CheckSignals()));
8964-
if (result != 0)
8965-
return (!async_err) ? posix_error() : NULL;
8966-
Py_RETURN_NONE;
8967+
} while (result == EINTR && !(async_err = PyErr_CheckSignals()));
8968+
8969+
if (result == 0)
8970+
Py_RETURN_NONE;
8971+
8972+
if (async_err)
8973+
return NULL;
8974+
8975+
errno = result;
8976+
return posix_error();
89678977
}
89688978
#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
89698979

0 commit comments

Comments
 (0)