[pypy-svn] r29965 - in pypy/dist/pypy/module/fcntl: . test

rhymes at codespeak.net rhymes at codespeak.net
Wed Jul 12 00:20:52 CEST 2006


Author: rhymes
Date: Wed Jul 12 00:20:48 2006
New Revision: 29965

Modified:
   pypy/dist/pypy/module/fcntl/__init__.py
   pypy/dist/pypy/module/fcntl/interp_fcntl.py
   pypy/dist/pypy/module/fcntl/test/test_fcntl.py
Log:
add fcntl.lockf(). it compiles fine in osx. some problems with function declaration in linux

Modified: pypy/dist/pypy/module/fcntl/__init__.py
==============================================================================
--- pypy/dist/pypy/module/fcntl/__init__.py	(original)
+++ pypy/dist/pypy/module/fcntl/__init__.py	Wed Jul 12 00:20:48 2006
@@ -3,7 +3,8 @@
 class Module(MixedModule):
     interpleveldefs = {
         'fcntl': 'interp_fcntl.fcntl',
-        'flock': 'interp_fcntl.flock'
+        'flock': 'interp_fcntl.flock',
+        'lockf': 'interp_fcntl.lockf'
     }
 
     appleveldefs = {

Modified: pypy/dist/pypy/module/fcntl/interp_fcntl.py
==============================================================================
--- pypy/dist/pypy/module/fcntl/interp_fcntl.py	(original)
+++ pypy/dist/pypy/module/fcntl/interp_fcntl.py	Wed Jul 12 00:20:48 2006
@@ -48,11 +48,12 @@
     cConfig.F_GETLEASE = 1025
     cConfig.F_SETLEASE = 1024
 
-# needed to export the constants outside. see __init__.py
+# needed to export the constants inside and outside. see __init__.py
 for name in constant_names:
     value = getattr(cConfig, name)
     if value is not None:
         constants[name] = value
+locals().update(constants)
 
 _flock = cConfig.flock
 libc.strerror.restype = c_char_p
@@ -157,6 +158,50 @@
     else:
         l = _check_flock_op(space, op)
         l.l_whence = l.l_start = l.l_len = 0
-        op = (F_SETLKW, F_SETLK)[op & LOCK_NB]
+        op = [F_SETLKW, F_SETLK][op & LOCK_NB]
         fcntl_flock(fd, op, byref(l))
 flock.unwrap_spec = [ObjSpace, W_Root, int]
+
+def lockf(space, w_fd, op, length=0, start=0, whence=0):
+    """lockf (fd, operation, length=0, start=0, whence=0)
+
+    This is essentially a wrapper around the fcntl() locking calls.  fd is the
+    file descriptor of the file to lock or unlock, and operation is one of the
+    following values:
+
+    LOCK_UN - unlock
+    LOCK_SH - acquire a shared lock
+    LOCK_EX - acquire an exclusive lock
+
+    When operation is LOCK_SH or LOCK_EX, it can also be bit-wise OR'd with
+    LOCK_NB to avoid blocking on lock acquisition.  If LOCK_NB is used and the
+    lock cannot be acquired, an IOError will be raised and the exception will
+    have an errno attribute set to EACCES or EAGAIN (depending on the
+    operating system -- for portability, check for either value).
+
+    length is the number of bytes to lock, with the default meaning to lock to
+    EOF.  start is the byte offset, relative to whence, to that the lock
+    starts.  whence is as with fileobj.seek(), specifically:
+
+    0 - relative to the start of the file (SEEK_SET)
+    1 - relative to the current buffer position (SEEK_CUR)
+    2 - relative to the end of the file (SEEK_END)"""
+
+    fd = _conv_descriptor(space, w_fd)
+
+    l = _check_flock_op(space, op)
+    l.l_start = l.l_len = 0
+
+    if start:
+        l.l_start = int(start)
+    if len:
+        l.l_len = int(length)
+    l.l_whence = whence
+
+    try:
+        op = [F_SETLKW, F_SETLK][op & LOCK_NB]
+    except IndexError:
+        raise OperationError(space.w_ValueError,
+            space.wrap("invalid value for operation"))
+    fcntl_flock(fd, op, byref(l))
+lockf.unwrap_spec = [ObjSpace, W_Root, int, int, int, int]

Modified: pypy/dist/pypy/module/fcntl/test/test_fcntl.py
==============================================================================
--- pypy/dist/pypy/module/fcntl/test/test_fcntl.py	(original)
+++ pypy/dist/pypy/module/fcntl/test/test_fcntl.py	Wed Jul 12 00:20:48 2006
@@ -119,12 +119,22 @@
         fcntl.flock(f, fcntl.LOCK_UN)
         
         f.close()
-# 
-# def test_lockf():
-#     py.test.raises(TypeError, cfcntl.lockf, f, "foo")
-#     py.test.raises(TypeError, cfcntl.lockf, f, cfcntl.LOCK_UN, "foo")
-#     py.test.raises(ValueError, cfcntl.lockf, f, 0)
-# 
+
+    def test_lockf(self):
+        import fcntl
+        
+        f = open("/tmp/lockf", "w")
+        
+        raises(TypeError, fcntl.lockf, f, "foo")
+        raises(TypeError, fcntl.lockf, f, fcntl.LOCK_UN, "foo")
+        raises(ValueError, fcntl.lockf, f, -1)
+        raises(ValueError, fcntl.lockf, f, 255)
+        
+        fcntl.lockf(f, fcntl.LOCK_SH)
+        fcntl.lockf(f, fcntl.LOCK_UN)
+        
+        f.close()
+
 # def test_ioctl():
 #     py.test.raises(TypeError, cfcntl.ioctl, "foo")
 #     py.test.raises(TypeError, cfcntl.ioctl, 0, "foo")



More information about the Pypy-commit mailing list