[pypy-svn] r77914 - in pypy/branch/fast-forward/pypy/module/_multiprocessing: . test

afa at codespeak.net afa at codespeak.net
Thu Oct 14 12:59:48 CEST 2010


Author: afa
Date: Thu Oct 14 12:59:47 2010
New Revision: 77914

Modified:
   pypy/branch/fast-forward/pypy/module/_multiprocessing/interp_semaphore.py
   pypy/branch/fast-forward/pypy/module/_multiprocessing/test/test_semaphore.py
Log:
Add context manager to _multiprocessing.SemLock


Modified: pypy/branch/fast-forward/pypy/module/_multiprocessing/interp_semaphore.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_multiprocessing/interp_semaphore.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_multiprocessing/interp_semaphore.py	Thu Oct 14 12:59:47 2010
@@ -1,6 +1,6 @@
 from pypy.interpreter.baseobjspace import ObjSpace, Wrappable, W_Root
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
-from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.interpreter.gateway import interp2app, Arguments, unwrap_spec
 from pypy.interpreter.error import wrap_oserror, OperationError
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.rlib.rarithmetic import r_uint
@@ -493,6 +493,14 @@
         self.__init__(handle_w(space, w_handle), kind, maxvalue)
         return space.wrap(self)
 
+    @unwrap_spec('self', ObjSpace)
+    def enter(self, space):
+        return self.acquire(space, w_timeout=space.w_None)
+
+    @unwrap_spec('self', ObjSpace, Arguments)
+    def exit(self, space, __args__):
+        self.release(space)
+
 @unwrap_spec(ObjSpace, W_Root, int, int, int)
 def descr_new(space, w_subtype, kind, value, maxvalue):
     if kind != RECURSIVE_MUTEX and kind != SEMAPHORE:
@@ -525,5 +533,7 @@
     acquire = interp2app(W_SemLock.acquire),
     release = interp2app(W_SemLock.release),
     _rebuild = interp2app(W_SemLock.rebuild.im_func, as_classmethod=True),
+    __enter__=interp2app(W_SemLock.enter),
+    __exit__=interp2app(W_SemLock.exit),
     SEM_VALUE_MAX=SEM_VALUE_MAX,
     )

Modified: pypy/branch/fast-forward/pypy/module/_multiprocessing/test/test_semaphore.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_multiprocessing/test/test_semaphore.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_multiprocessing/test/test_semaphore.py	Thu Oct 14 12:59:47 2010
@@ -71,3 +71,14 @@
 
         sem2 = SemLock._rebuild(sem.handle, kind, value)
         assert sem.handle == sem2.handle
+
+    def test_semaphore_contextmanager(self):
+        from _multiprocessing import SemLock
+        kind = self.SEMAPHORE
+        value = 1
+        maxvalue = 1
+        sem = SemLock(kind, value, maxvalue)
+
+        with sem:
+            assert sem._count() == 1
+        assert sem._count() == 0



More information about the Pypy-commit mailing list