[pypy-svn] r59499 - in pypy/trunk/pypy/module/thread: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Tue Oct 28 18:47:21 CET 2008


Author: cfbolz
Date: Tue Oct 28 18:47:19 2008
New Revision: 59499

Modified:
   pypy/trunk/pypy/module/thread/os_lock.py
   pypy/trunk/pypy/module/thread/test/test_lock.py
Log:
add __enter__ and __exit__ methods to locks


Modified: pypy/trunk/pypy/module/thread/os_lock.py
==============================================================================
--- pypy/trunk/pypy/module/thread/os_lock.py	(original)
+++ pypy/trunk/pypy/module/thread/os_lock.py	Tue Oct 28 18:47:19 2008
@@ -5,7 +5,7 @@
 from pypy.module.thread import ll_thread as thread
 from pypy.module.thread.error import wrap_thread_error
 from pypy.interpreter.baseobjspace import Wrappable
-from pypy.interpreter.gateway import ObjSpace, interp2app
+from pypy.interpreter.gateway import ObjSpace, interp2app, Arguments
 from pypy.interpreter.typedef import TypeDef
 
 # Force the declaration of the type 'thread.LockType' for RPython
@@ -63,6 +63,12 @@
         else:
             return space.w_True
 
+    def descr__enter__(self, space):
+        self.descr_lock_acquire(space)
+        return self
+
+    def descr__exit__(self, space, __args__):
+        self.descr_lock_release(space)
 
 descr_acquire = interp2app(Lock.descr_lock_acquire,
                            unwrap_spec=['self', ObjSpace, int])
@@ -70,6 +76,11 @@
                            unwrap_spec=['self', ObjSpace])
 descr_locked  = interp2app(Lock.descr_lock_locked,
                            unwrap_spec=['self', ObjSpace])
+descr__enter__ = interp2app(Lock.descr__enter__,
+                            unwrap_spec=['self', ObjSpace])
+descr__exit__ = interp2app(Lock.descr__exit__,
+                            unwrap_spec=['self', ObjSpace, Arguments])
+
 
 Lock.typedef = TypeDef("thread.lock",
     __doc__ = """\
@@ -86,6 +97,8 @@
     acquire = descr_acquire,
     release = descr_release,
     locked  = descr_locked,
+    __enter__ = descr__enter__,
+    __exit__ = descr__exit__,
     # Obsolete synonyms
     acquire_lock = descr_acquire,
     release_lock = descr_release,

Modified: pypy/trunk/pypy/module/thread/test/test_lock.py
==============================================================================
--- pypy/trunk/pypy/module/thread/test/test_lock.py	(original)
+++ pypy/trunk/pypy/module/thread/test/test_lock.py	Tue Oct 28 18:47:19 2008
@@ -29,6 +29,25 @@
         assert lock.locked() is True
         assert feedback == [42]
 
+    def test_lock_in_with(self):
+        import thread
+        lock = thread.allocate_lock()
+        feedback = []
+        lock.acquire()
+        def f():
+            self.busywait(0.25)
+            feedback.append(42)
+            lock.release()
+        assert lock.locked() is True
+        thread.start_new_thread(f, ())
+        exec """
+from __future__ import with_statement
+if 1:
+        with lock:
+            assert lock.locked() is True
+            assert feedback == [42]
+""" in {"lock": lock, "feedback": feedback}
+        assert lock.locked() is False
 
 def test_compile_lock():
     from pypy.rlib import rgc



More information about the Pypy-commit mailing list