[pypy-commit] pypy default: backport from f90ac97e3a04 the changes that go to rpython/

arigo pypy.commits at gmail.com
Mon Mar 13 12:36:16 EDT 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r90663:dd08f2c2fef2
Date: 2017-03-13 17:35 +0100
http://bitbucket.org/pypy/pypy/changeset/dd08f2c2fef2/

Log:	backport from f90ac97e3a04 the changes that go to rpython/

diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -464,6 +464,29 @@
             how = SEEK_END
     return handle_posix_error('lseek', c_lseek(fd, pos, how))
 
+c_pread = external('pread',
+                  [rffi.INT, rffi.VOIDP, rffi.SIZE_T , OFF_T], rffi.SSIZE_T,
+                  save_err=rffi.RFFI_SAVE_ERRNO)
+c_pwrite = external('pwrite',
+                   [rffi.INT, rffi.VOIDP, rffi.SIZE_T, OFF_T], rffi.SSIZE_T,
+                   save_err=rffi.RFFI_SAVE_ERRNO)
+
+ at enforceargs(int, int, None)
+def pread(fd, count, offset):
+    if count < 0:
+        raise OSError(errno.EINVAL, None)
+    validate_fd(fd)
+    with rffi.scoped_alloc_buffer(count) as buf:
+        void_buf = rffi.cast(rffi.VOIDP, buf.raw)
+        return buf.str(handle_posix_error('pread', c_pread(fd, void_buf, count, offset)))
+        
+ at enforceargs(int, None, None)
+def pwrite(fd, data, offset):
+    count = len(data)
+    validate_fd(fd)
+    with rffi.scoped_nonmovingbuffer(data) as buf:
+        return handle_posix_error('pwrite', c_pwrite(fd, buf, count, offset))
+
 c_ftruncate = external('ftruncate', [rffi.INT, rffi.LONGLONG], rffi.INT,
                        macro=_MACRO_ON_POSIX, save_err=rffi.RFFI_SAVE_ERRNO)
 c_fsync = external('fsync' if not _WIN32 else '_commit', [rffi.INT], rffi.INT,
diff --git a/rpython/rlib/test/test_rposix.py b/rpython/rlib/test/test_rposix.py
--- a/rpython/rlib/test/test_rposix.py
+++ b/rpython/rlib/test/test_rposix.py
@@ -676,7 +676,7 @@
     prio = rposix.getpriority(rposix.PRIO_PROCESS, 0)
     rposix.setpriority(rposix.PRIO_PROCESS, 0, prio)
     py.test.raises(OSError, rposix.getpriority, rposix.PRIO_PGRP, 123456789)
-
+    
 if sys.platform != 'win32':
     def test_sendfile():
         from rpython.rlib import rsocket
@@ -723,3 +723,30 @@
         os.close(fd)
         s2.close()
         s1.close()
+        
+ at rposix_requires('pread')
+def test_pread():
+    fname = str(udir.join('os_test.txt'))
+    fd = os.open(fname, os.O_RDWR | os.O_CREAT)
+    try:
+        assert fd >= 0
+        os.write(fd, b'Hello world')
+        os.lseek(fd, 0, 0)
+        assert rposix.pread(fd, 2, 1) == b'el'
+    finally:
+        os.close(fd)
+    py.test.raises(OSError, rposix.pread, fd, 2, 1)
+
+ at rposix_requires('pwrite')
+def test_pwrite():
+    fname = str(udir.join('os_test.txt'))
+    fd = os.open(fname, os.O_RDWR | os.O_CREAT, 0777)
+    try:
+        assert fd >= 0
+        os.write(fd, b'Hello world')
+        os.lseek(fd, 0, 0)
+        rposix.pwrite(fd, b'ea', 1)
+        assert os.read(fd, 4) == b'Heal'
+    finally:
+        os.close(fd)
+    py.test.raises(OSError, rposix.pwrite, fd, b'ea', 1)


More information about the pypy-commit mailing list