[py-svn] r35209 - in py/dist/py: io/test misc misc/testing

arigo at codespeak.net arigo at codespeak.net
Fri Dec 1 18:31:24 CET 2006


Author: arigo
Date: Fri Dec  1 18:31:22 2006
New Revision: 35209

Modified:
   py/dist/py/io/test/test_capture.py
   py/dist/py/misc/simplecapture.py
   py/dist/py/misc/testing/test_simplecapture.py
Log:
SimpleCapturing now sets sys.stdin to a stub class that complains
when reading (e.g. if there is a pdb.set_trace() left in the test)
instead of just appearing to hang.

Ideally when stdin is accessed, the capturing should be turned off, with
possibly all data captured so far sent to the screen.  This should be
configurable, though, because in automated test runs it is better to
crash than hang indefinitely.



Modified: py/dist/py/io/test/test_capture.py
==============================================================================
--- py/dist/py/io/test/test_capture.py	(original)
+++ py/dist/py/io/test/test_capture.py	Fri Dec  1 18:31:22 2006
@@ -24,7 +24,9 @@
         print >>f, "3"
         f.seek(0)
         cap = py.io.FDCapture(0, tmpfile=f)
-        x = raw_input()
+        # check with os.read() directly instead of raw_input(), because
+        # sys.stdin itself may be redirected (as py.test now does by default)
+        x = os.read(0, 100).strip()
         f = cap.done()
         assert x == "3"
 

Modified: py/dist/py/misc/simplecapture.py
==============================================================================
--- py/dist/py/misc/simplecapture.py	(original)
+++ py/dist/py/misc/simplecapture.py	Fri Dec  1 18:31:22 2006
@@ -14,8 +14,10 @@
     used by the unittest package to capture print-statements in tests.
     """
     def __init__(self):
+        self.oldin  = sys.stdin
         self.oldout = sys.stdout
         self.olderr = sys.stderr
+        sys.stdin  = self.newin  = DontReadFromInput()
         sys.stdout = self.newout = StringIO()
         sys.stderr = self.newerr = StringIO()
 
@@ -26,13 +28,27 @@
 
     def done(self): 
         o,e = sys.stdout, sys.stderr
-        sys.stdout, sys.stderr = self.oldout, self.olderr
-        del self.oldout, self.olderr
+        sys.stdin, sys.stdout, sys.stderr = (
+            self.oldin, self.oldout, self.olderr)
+        del self.oldin, self.oldout, self.olderr
         o, e = self.newout, self.newerr 
         o.seek(0)
         e.seek(0)
         return o,e 
 
+class DontReadFromInput:
+    """Temporary stub class.  Ideally when stdin is accessed, the
+    capturing should be turned off, with possibly all data captured
+    so far sent to the screen.  This should be configurable, though,
+    because in automated test runs it is better to crash than
+    hang indefinitely.
+    """
+    def read(self, *args):
+        raise IOError("reading from stdin while output is captured")
+    readline = read
+    readlines = read
+    __iter__ = read
+
 def callcapture(func, *args, **kwargs): 
     so = SimpleOutErrCapture()
     try: 

Modified: py/dist/py/misc/testing/test_simplecapture.py
==============================================================================
--- py/dist/py/misc/testing/test_simplecapture.py	(original)
+++ py/dist/py/misc/testing/test_simplecapture.py	Fri Dec  1 18:31:22 2006
@@ -66,6 +66,13 @@
         assert out1 == "cap1\n"
         assert out2 == "cap2\n"
 
+    def test_reading_stdin_while_captured_doesnt_hang(self):
+        cap = self.getcapture()
+        try:
+            py.test.raises(IOError, raw_input)
+        finally:
+            cap.reset()
+
 def test_callcapture(): 
     def func(x, y): 
         print x


More information about the py-svn mailing list