bpo-31368: Expose preadv and pwritev in the os module (#5239) · pythoncapi/cpython@4defba3 · GitHub
Skip to content

Commit 4defba3

Browse files
pablogsalvstinner
authored andcommitted
bpo-31368: Expose preadv and pwritev in the os module (python#5239)
1 parent 60da99b commit 4defba3

9 files changed

Lines changed: 470 additions & 79 deletions

File tree

Doc/library/os.rst

Lines changed: 84 additions & 0 deletions

Lib/test/test_posix.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,28 @@ def test_pread(self):
272272
finally:
273273
os.close(fd)
274274

275+
@unittest.skipUnless(hasattr(posix, 'preadv'), "test needs posix.preadv()")
276+
def test_preadv(self):
277+
fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
278+
try:
279+
os.write(fd, b'test1tt2t3t5t6t6t8')
280+
buf = [bytearray(i) for i in [5, 3, 2]]
281+
self.assertEqual(posix.preadv(fd, buf, 3), 10)
282+
self.assertEqual([b't1tt2', b't3t', b'5t'], list(buf))
283+
finally:
284+
os.close(fd)
285+
286+
@unittest.skipUnless(hasattr(posix, 'RWF_HIPRI'), "test needs posix.RWF_HIPRI")
287+
def test_preadv_flags(self):
288+
fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
289+
try:
290+
os.write(fd, b'test1tt2t3t5t6t6t8')
291+
buf = [bytearray(i) for i in [5, 3, 2]]
292+
self.assertEqual(posix.preadv(fd, buf, 3, os.RWF_HIPRI), 10)
293+
self.assertEqual([b't1tt2', b't3t', b'5t'], list(buf))
294+
finally:
295+
os.close(fd)
296+
275297
@unittest.skipUnless(hasattr(posix, 'pwrite'), "test needs posix.pwrite()")
276298
def test_pwrite(self):
277299
fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
@@ -283,6 +305,34 @@ def test_pwrite(self):
283305
finally:
284306
os.close(fd)
285307

308+
@unittest.skipUnless(hasattr(posix, 'pwritev'), "test needs posix.pwritev()")
309+
def test_pwritev(self):
310+
fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
311+
try:
312+
os.write(fd, b"xx")
313+
os.lseek(fd, 0, os.SEEK_SET)
314+
n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2)
315+
self.assertEqual(n, 10)
316+
317+
os.lseek(fd, 0, os.SEEK_SET)
318+
self.assertEqual(b'xxtest1tt2t3', posix.read(fd, 100))
319+
finally:
320+
os.close(fd)
321+
322+
@unittest.skipUnless(hasattr(posix, 'os.RWF_SYNC'), "test needs os.RWF_SYNC")
323+
def test_pwritev_flags(self):
324+
fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
325+
try:
326+
os.write(fd,b"xx")
327+
os.lseek(fd, 0, os.SEEK_SET)
328+
n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2, os.RWF_SYNC)
329+
self.assertEqual(n, 10)
330+
331+
os.lseek(fd, 0, os.SEEK_SET)
332+
self.assertEqual(b'xxtest1tt2', posix.read(fd, 100))
333+
finally:
334+
os.close(fd)
335+
286336
@unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
287337
"test needs posix.posix_fallocate()")
288338
def test_posix_fallocate(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Expose preadv and pwritev system calls in the os module. Patch by Pablo Galindo

Modules/clinic/posixmodule.c.h

Lines changed: 119 additions & 1 deletion

0 commit comments

Comments
 (0)