[pypy-svn] r78738 - in pypy/branch/signals/pypy: interpreter module/signal/test

arigo at codespeak.net arigo at codespeak.net
Fri Nov 5 14:08:25 CET 2010


Author: arigo
Date: Fri Nov  5 14:08:23 2010
New Revision: 78738

Modified:
   pypy/branch/signals/pypy/interpreter/baseobjspace.py
   pypy/branch/signals/pypy/interpreter/executioncontext.py
   pypy/branch/signals/pypy/module/signal/test/test_signal.py
Log:
Implement space.getexecutioncontext().checksignals().


Modified: pypy/branch/signals/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/signals/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/branch/signals/pypy/interpreter/baseobjspace.py	Fri Nov  5 14:08:23 2010
@@ -258,6 +258,7 @@
 
         self.interned_strings = {}
         self.actionflag = ActionFlag()    # changed by the signal module
+        self.check_signal_action = None   # changed by the signal module
         self.user_del_action = UserDelAction(self)
         self.frame_trace_action = FrameTraceAction(self)
 

Modified: pypy/branch/signals/pypy/interpreter/executioncontext.py
==============================================================================
--- pypy/branch/signals/pypy/interpreter/executioncontext.py	(original)
+++ pypy/branch/signals/pypy/interpreter/executioncontext.py	Fri Nov  5 14:08:23 2010
@@ -309,6 +309,13 @@
                 frame.last_exception = last_exception
                 self.is_tracing -= 1
 
+    def checksignals(self):
+        """Similar to PyErr_CheckSignals().  If called in the main thread,
+        and if signals are pending for the process, deliver them now
+        (i.e. call the signal handlers)."""
+        if self.space.check_signal_action is not None:
+            self.space.check_signal_action.perform(self, None)
+
     def _freeze_(self):
         raise Exception("ExecutionContext instances should not be seen during"
                         " translation.  Now is a good time to inspect the"

Modified: pypy/branch/signals/pypy/module/signal/test/test_signal.py
==============================================================================
--- pypy/branch/signals/pypy/module/signal/test/test_signal.py	(original)
+++ pypy/branch/signals/pypy/module/signal/test/test_signal.py	Fri Nov  5 14:08:23 2010
@@ -1,6 +1,38 @@
 import os, py
+import signal as cpy_signal
 from pypy.conftest import gettestobjspace
 
+
+class TestCheckSignals:
+
+    def setup_class(cls):
+        if not hasattr(os, 'kill') or not hasattr(os, 'getpid'):
+            py.test.skip("requires os.kill() and os.getpid()")
+        cls.space = gettestobjspace(usemodules=['signal'])
+
+    def test_checksignals(self):
+        space = self.space
+        w_received = space.appexec([], """():
+            import signal
+            received = []
+            def myhandler(signum, frame):
+                received.append(signum)
+            signal.signal(signal.SIGUSR1, myhandler)
+            return received""")
+        #
+        assert not space.is_true(w_received)
+        #
+        # send the signal now
+        os.kill(os.getpid(), cpy_signal.SIGUSR1)
+        #
+        # myhandler() should not be immediately called
+        assert not space.is_true(w_received)
+        #
+        # calling ec.checksignals() should call it
+        space.getexecutioncontext().checksignals()
+        assert space.is_true(w_received)
+
+
 class AppTestSignal:
 
     def setup_class(cls):
@@ -24,18 +56,12 @@
         signal.signal(signal.SIGUSR1, myhandler)
 
         posix.kill(posix.getpid(), signal.SIGUSR1)
-        for i in range(10000):
-             # wait a bit for the signal to be delivered to the handler
-            if received:
-                break
+        # the signal should be delivered to the handler immediately
         assert received == [signal.SIGUSR1]
         del received[:]
 
         posix.kill(posix.getpid(), signal.SIGUSR1)
-        for i in range(10000):
-             # wait a bit for the signal to be delivered to the handler
-            if received:
-                break
+        # the signal should be delivered to the handler immediately
         assert received == [signal.SIGUSR1]
         del received[:]
 



More information about the Pypy-commit mailing list