Skip to content

Commit d0d9677

Browse files
committed
Add tests
1 parent 5ae8927 commit d0d9677

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

Lib/test/test_fcntl.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ def test_bad_fd(self):
288288
with self.assertRaises(OSError):
289289
fcntl.fcntl(fd, fcntl.F_DUPFD, b'\0' * 2048)
290290

291+
@unittest.skipUnless(hasattr(fcntl, 'preallocate'), 'need fcntl.preallocate')
292+
def test_preallocate(self):
293+
self.f = open(TESTFN, 'wb+')
294+
fd = self.f.fileno()
295+
296+
result = fcntl.preallocate(fd, fcntl.F_ALLOCATECONTIG, fcntl.F_PEOFPOSMODE, 0, 1024)
297+
self.assertIsInstance(result, int)
298+
self.assertEqual(result, 1024)
299+
291300

292301
if __name__ == '__main__':
293302
unittest.main()

Modules/clinic/fcntlmodule.c.h

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/fcntlmodule.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj,
503503
}
504504

505505

506+
#ifdef F_PREALLOCATE
507+
506508
/*[clinic input]
507509
fcntl.preallocate
508510
@@ -521,9 +523,8 @@ This is a wrapper around the F_PREALLOCATE fcntl command.
521523
static PyObject *
522524
fcntl_preallocate_impl(PyObject *module, int fd, int flags, int posmode,
523525
long offset, long length)
524-
/*[clinic end generated code: output=4934b8a4dc1f5dc1 input=b8e76ad8be51da32]*/
526+
/*[clinic end generated code: output=4934b8a4dc1f5dc1 input=4c1a9d46551420ed]*/
525527
{
526-
#ifdef F_PREALLOCATE
527528
int ret;
528529
int async_err = 0;
529530

@@ -550,12 +551,10 @@ fcntl_preallocate_impl(PyObject *module, int fd, int flags, int posmode,
550551
}
551552

552553
return PyLong_FromLong((long)fstore.fst_bytesalloc);
553-
#else
554-
PyErr_SetString(PyExc_OSError, "F_PREALLOCATE not supported on this platform");
555-
return NULL;
556-
#endif
557554
}
558555

556+
#endif /* F_PREALLOCATE */
557+
559558
/* List of functions */
560559

561560
static PyMethodDef fcntl_methods[] = {
@@ -754,6 +753,12 @@ all_ins(PyObject* m)
754753
#ifdef F_ALLOCATEPERSIST
755754
if (PyModule_AddIntMacro(m, F_ALLOCATEPERSIST)) return -1;
756755
#endif
756+
#ifdef F_PEOFPOSMODE
757+
if (PyModule_AddIntMacro(m, F_PEOFPOSMODE)) return -1;
758+
#endif
759+
#ifdef F_VOLPOSMODE
760+
if (PyModule_AddIntMacro(m, F_VOLPOSMODE)) return -1;
761+
#endif
757762

758763
/* FreeBSD specifics */
759764
#ifdef F_DUP2FD

0 commit comments

Comments
 (0)