[pypy-svn] r41054 - in pypy/dist/pypy/translator/js/examples/console: . test

fijal at codespeak.net fijal at codespeak.net
Thu Mar 22 14:47:25 CET 2007


Author: fijal
Date: Thu Mar 22 14:47:24 2007
New Revision: 41054

Modified:
   pypy/dist/pypy/translator/js/examples/console/session.py
   pypy/dist/pypy/translator/js/examples/console/test/test_session.py
Log:
A new feature and a test


Modified: pypy/dist/pypy/translator/js/examples/console/session.py
==============================================================================
--- pypy/dist/pypy/translator/js/examples/console/session.py	(original)
+++ pypy/dist/pypy/translator/js/examples/console/session.py	Thu Mar 22 14:47:24 2007
@@ -3,6 +3,7 @@
 build around subprocess to run python console in it
 """
 
+KILL_TIMEOUT = 300
 TIMEOUT = 10
 
 """ The idea behind is that we create xmlhttprequest immediataly
@@ -15,9 +16,13 @@
 from Queue import Queue
 from py.__.green.greensock2 import autogreenlet, Timer, Interrupted
 from py.__.green.pipe.fd import FDInput
-    
+import time
+
+class Killed(Exception):
+    pass
+
 class Interpreter(object):
-    def __init__(self, python, timeout=TIMEOUT):
+    def __init__(self, python, timeout=TIMEOUT, kill_timeout=KILL_TIMEOUT):
         pipe = subprocess.Popen([python, "-u", "-i"], stdout=subprocess.PIPE,
                             stdin=subprocess.PIPE, stderr=subprocess.STDOUT,
                             close_fds=True, bufsize=0)
@@ -25,6 +30,8 @@
         self.read_fd = FDInput(self.pipe.stdout.fileno(), close=False)
         self.pid = pipe.pid
         self.timeout = timeout
+        self.kill_timeout = kill_timeout
+        self.last_activity = time.time()
 
     def timeout_read(self, fd, timeout):
         timer = Timer(timeout)
@@ -38,10 +45,14 @@
 
     def write_only(self, to_write):
         if to_write is not None:
+            self.last_activity = time.time()
             self.pipe.stdin.write(to_write)
 
     def interact(self, to_write=None):
         self.write_only(to_write)
+        if time.time() - self.last_activity > self.kill_timeout:
+            self.close()
+            raise Killed()
         return self.timeout_read(self.read_fd, self.timeout)
 
     def close(self):

Modified: pypy/dist/pypy/translator/js/examples/console/test/test_session.py
==============================================================================
--- pypy/dist/pypy/translator/js/examples/console/test/test_session.py	(original)
+++ pypy/dist/pypy/translator/js/examples/console/test/test_session.py	Thu Mar 22 14:47:24 2007
@@ -6,7 +6,7 @@
 
 from py.__.green.greensock2 import allof
 from py.__.green.pipe.fd import FDInput
-from pypy.translator.js.examples.console.session import Interpreter
+from pypy.translator.js.examples.console.session import Interpreter, Killed
 
 def test_greensock_reader_timeouter():
     i = Interpreter("python", timeout=3)
@@ -43,3 +43,9 @@
         print val
         val = i.interact()
 
+def test_kill_timeout():
+    i = Interpreter("python", kill_timeout=1, timeout=3)
+    while not i.interact().endswith(">>> "):
+        pass
+    i.interact()
+    py.test.raises(Killed, "i.interact()")



More information about the Pypy-commit mailing list