Author: Joannah Nanjekye <[email protected]>
Branch: os_lockf
Changeset: r92917:287c9946859b
Date: 2017-10-28 22:59 +0300
http://bitbucket.org/pypy/pypy/changeset/287c9946859b/

Log:    lockf posixattributes

diff --git a/pypy/module/posix/__init__.py b/pypy/module/posix/__init__.py
--- a/pypy/module/posix/__init__.py
+++ b/pypy/module/posix/__init__.py
@@ -237,6 +237,12 @@
             if getattr(rposix, _name) is not None:
                 interpleveldefs[_name] = 'space.wrap(%d)' % getattr(rposix, 
_name)
 
+    if sys.platform.startswith('linux'):
+        interpleveldefs['lockf'] = 'interp_posix.lockf'
+        for _name in ['F_LOCK', 'F_TLOCK', 'F_ULOCK', 'F_TEST']:
+            if getattr(rposix, _name) is not None:
+                interpleveldefs[_name] = 'space.wrap(%d)' % getattr(rposix, 
_name)
+
     for _name in ["O_CLOEXEC"]:
         if getattr(rposix, _name) is not None:
             interpleveldefs[_name] = 'space.wrap(%d)' % getattr(rposix, _name)
diff --git a/pypy/module/posix/interp_posix.py 
b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -2468,3 +2468,16 @@
             wrap_oserror(space, e, eintr_retry=True)
         else:
            return space.newint(s)
+
+@unwrap_spec(fd=c_int, cmd=c_int, length=r_longlong)
+def lockf(space, fd, cmd, length):
+    """apply, test or remove a POSIX lock on an 
+    open file.
+    """
+    while True:
+        try:
+            s = rposix.lockf(fd, cmd, length)
+        except OSError as e:
+            wrap_oserror(space, e, eintr_retry=True)
+        else:
+           return space.newint(s)
diff --git a/pypy/module/posix/test/test_posix2.py 
b/pypy/module/posix/test/test_posix2.py
--- a/pypy/module/posix/test/test_posix2.py
+++ b/pypy/module/posix/test/test_posix2.py
@@ -1331,6 +1331,17 @@
             posix.close(fd)
             s2.close()
             s1.close()
+            
+        def test_os_lockf(self):
+            posix, os = self.posix, self.os
+            fd = os.open(self.path2 + 'test_os_lockf', os.O_WRONLY | 
os.O_CREAT)
+            try:
+                os.write(fd, b'test')
+                os.lseek(fd, 0, 0)
+                posix.lockf(fd, posix.F_LOCK, 4)
+                posix.lockf(fd, posix.F_ULOCK, 4)
+            finally:
+                os.close(fd)
 
     def test_urandom(self):
         os = self.posix
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -276,6 +276,10 @@
     SCHED_OTHER = rffi_platform.DefinedConstantInteger('SCHED_OTHER')
     SCHED_BATCH = rffi_platform.DefinedConstantInteger('SCHED_BATCH')
     O_NONBLOCK = rffi_platform.DefinedConstantInteger('O_NONBLOCK')
+    F_LOCK = rffi_platform.DefinedConstantInteger('F_LOCK')
+    F_TLOCK = rffi_platform.DefinedConstantInteger('F_TLOCK')
+    F_ULOCK = rffi_platform.DefinedConstantInteger('F_ULOCK')
+    F_TEST = rffi_platform.DefinedConstantInteger('F_TEST')
     OFF_T = rffi_platform.SimpleType('off_t')
     OFF_T_SIZE = rffi_platform.SizeOf('off_t')
 
@@ -548,6 +552,14 @@
             if error != 0:
                 raise OSError(error, 'posix_fadvise failed')
 
+    c_lockf = external('lockf',
+            [rffi.INT, rffi.INT , OFF_T], rffi.INT,
+            save_err=rffi.RFFI_SAVE_ERRNO)
+    @enforceargs(int, None, None)
+    def lockf(fd, cmd, length):
+        validate_fd(fd)
+        return handle_posix_error('lockf', c_lockf(fd, cmd, length))
+
 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
@@ -811,3 +811,14 @@
     assert isinstance(high, int) == True
     assert  high > low
 
+@rposix_requires('lockf')
+def test_os_lockf():
+    fname = str(udir.join('os_test.txt'))
+    fd = os.open(fname, os.O_WRONLY | os.O_CREAT, 0777)
+    try:
+        os.write(fd, b'test')
+        os.lseek(fd, 0, 0)
+        rposix.lockf(fd, rposix.F_LOCK, 4)
+        rposix.lockf(fd, rposix.F_ULOCK, 4)
+    finally:
+        os.close(fd)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to