[pypy-svn] r68878 - in pypy/branch/logging2/pypy/rlib: . test

arigo at codespeak.net arigo at codespeak.net
Sat Oct 31 12:04:20 CET 2009


Author: arigo
Date: Sat Oct 31 12:04:19 2009
New Revision: 68878

Modified:
   pypy/branch/logging2/pypy/rlib/debug.py
   pypy/branch/logging2/pypy/rlib/test/test_debug.py
Log:
A better way for tests to check that some debug_start/stop/prints
really occurred.


Modified: pypy/branch/logging2/pypy/rlib/debug.py
==============================================================================
--- pypy/branch/logging2/pypy/rlib/debug.py	(original)
+++ pypy/branch/logging2/pypy/rlib/debug.py	Sat Oct 31 12:04:19 2009
@@ -20,10 +20,23 @@
         hop.genop('debug_assert', vlist)
 
 
+class DebugLog(list):
+    def debug_print(self, *args):
+        self.append(('debug_print',) + args)
+    def debug_start(self, category):
+        self.append(('debug_start', category))
+    def debug_stop(self, category):
+        self.append(('debug_stop', category))
+
+_log = None       # patched from tests to be an object of class DebugLog
+                  # or compatible
+
 def debug_print(*args):
     for arg in args:
         print >> sys.stderr, arg,
     print >> sys.stderr
+    if _log is not None:
+        _log.debug_print(*args)
 
 class Entry(ExtRegistryEntry):
     _about_ = debug_print
@@ -51,10 +64,14 @@
 def debug_start(category):
     print >> sys.stderr, '%s[%s] {%s%s' % (_start_colors_1, time.clock(),
                                            category, _stop_colors)
+    if _log is not None:
+        _log.debug_start(category)
 
 def debug_stop(category):
     print >> sys.stderr, '%s[%s] %s}%s' % (_start_colors_2, time.clock(),
                                            category, _stop_colors)
+    if _log is not None:
+        _log.debug_stop(category)
 
 class Entry(ExtRegistryEntry):
     _about_ = debug_start, debug_stop

Modified: pypy/branch/logging2/pypy/rlib/test/test_debug.py
==============================================================================
--- pypy/branch/logging2/pypy/rlib/test/test_debug.py	(original)
+++ pypy/branch/logging2/pypy/rlib/test/test_debug.py	Sat Oct 31 12:04:19 2009
@@ -3,6 +3,7 @@
 from pypy.rlib.debug import check_annotation, make_sure_not_resized
 from pypy.rlib.debug import debug_print, debug_start, debug_stop
 from pypy.rlib.debug import have_debug_prints
+from pypy.rlib import debug
 from pypy.rpython.test.test_llinterp import interpret
 
 def test_check_annotation():
@@ -44,33 +45,31 @@
 class DebugTests:
 
     def test_debug_print_start_stop(self):
-        import sys
-        from cStringIO import StringIO
-
         def f(x):
             debug_start("mycat")
             debug_print("foo", 2, "bar", x)
             debug_stop("mycat")
             return have_debug_prints()
 
-        olderr = sys.stderr
         try:
-            sys.stderr = c = StringIO()
+            debug._log = dlog = debug.DebugLog()
             res = f(3)
             assert res == True
         finally:
-            sys.stderr = olderr
-        assert 'mycat' in c.getvalue()
-        assert 'foo 2 bar 3' in c.getvalue()
+            debug._log = None
+        assert ('debug_start', 'mycat') in dlog
+        assert ('debug_print', 'foo', 2, 'bar', 3) in dlog
+        assert ('debug_stop', 'mycat') in dlog
 
         try:
-            sys.stderr = c = StringIO()
+            debug._log = dlog = debug.DebugLog()
             res = self.interpret(f, [3])
             assert res == True
         finally:
-            sys.stderr = olderr
-        assert 'mycat' in c.getvalue()
-        assert 'foo 2 bar 3' in c.getvalue()
+            debug._log = None
+        assert ('debug_start', 'mycat') in dlog
+        assert ('debug_print', 'foo', 2, 'bar', 3) in dlog
+        assert ('debug_stop', 'mycat') in dlog
 
 
 class TestLLType(DebugTests):



More information about the Pypy-commit mailing list