[pypy-svn] r68855 - in pypy/branch/logging2/pypy: rlib rlib/test rpython rpython/lltypesystem

arigo at codespeak.net arigo at codespeak.net
Fri Oct 30 13:29:21 CET 2009


Author: arigo
Date: Fri Oct 30 13:29:20 2009
New Revision: 68855

Modified:
   pypy/branch/logging2/pypy/rlib/debug.py
   pypy/branch/logging2/pypy/rlib/test/test_debug.py
   pypy/branch/logging2/pypy/rpython/llinterp.py
   pypy/branch/logging2/pypy/rpython/lltypesystem/lloperation.py
   pypy/branch/logging2/pypy/rpython/lltypesystem/opimpl.py
Log:
Add the debug_start and debug_stop operations that mark the start and
the stop of some operation that we might want to profile and display.

Also rewrite a bit debug_print.


Modified: pypy/branch/logging2/pypy/rlib/debug.py
==============================================================================
--- pypy/branch/logging2/pypy/rlib/debug.py	(original)
+++ pypy/branch/logging2/pypy/rlib/debug.py	Fri Oct 30 13:29:20 2009
@@ -1,4 +1,4 @@
-
+import sys, time
 from pypy.rpython.extregistry import ExtRegistryEntry
 
 def ll_assert(x, msg):
@@ -21,7 +21,6 @@
 
 
 def debug_print(*args):
-    import sys
     for arg in args:
         print >> sys.stderr, arg,
     print >> sys.stderr
@@ -38,6 +37,26 @@
         hop.genop('debug_print', vlist)
 
 
+def debug_start(category):
+    print >> sys.stderr, '[%s] debug_start %s' % (time.clock(), category)
+
+def debug_stop(category):
+    print >> sys.stderr, '[%s] debug_stop  %s' % (time.clock(), category)
+
+class Entry(ExtRegistryEntry):
+    _about_ = debug_start, debug_stop
+
+    def compute_result_annotation(self, s_category):
+        return None
+
+    def specialize_call(self, hop):
+        fn = self.instance
+        string_repr = hop.rtyper.type_system.rstr.string_repr
+        vlist = hop.inputargs(string_repr)
+        hop.exception_cannot_occur()
+        hop.genop(fn.__name__, vlist)
+
+
 def llinterpcall(RESTYPE, pythonfunction, *args):
     """When running on the llinterp, this causes the llinterp to call to
     the provided Python function with the run-time value of the given args.

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	Fri Oct 30 13:29:20 2009
@@ -1,6 +1,7 @@
 
 import py
 from pypy.rlib.debug import check_annotation, make_sure_not_resized
+from pypy.rlib.debug import debug_print, debug_start, debug_stop
 from pypy.rpython.test.test_llinterp import interpret
 
 def test_check_annotation():
@@ -37,3 +38,41 @@
 
     py.test.raises(TooLateForChange, interpret, f, [], 
                    list_comprehension_operations=True)
+
+
+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")
+
+        olderr = sys.stderr
+        try:
+            sys.stderr = c = StringIO()
+            f(3)
+        finally:
+            sys.stderr = olderr
+        assert 'mycat' in c.getvalue()
+        assert 'foo 2 bar 3' in c.getvalue()
+
+        try:
+            sys.stderr = c = StringIO()
+            self.interpret(f, [3])
+        finally:
+            sys.stderr = olderr
+        assert 'mycat' in c.getvalue()
+        assert 'foo 2 bar 3' in c.getvalue()
+
+
+class TestLLType(DebugTests):
+    def interpret(self, f, args):
+        interpret(f, args, type_system='lltype')
+
+class TestOOType(DebugTests):
+    def interpret(self, f, args):
+        interpret(f, args, type_system='ootype')

Modified: pypy/branch/logging2/pypy/rpython/llinterp.py
==============================================================================
--- pypy/branch/logging2/pypy/rpython/llinterp.py	(original)
+++ pypy/branch/logging2/pypy/rpython/llinterp.py	Fri Oct 30 13:29:20 2009
@@ -522,20 +522,6 @@
         from pypy.translator.tool.lltracker import track
         track(*ll_objects)
 
-    def op_debug_print(self, *ll_args):
-        from pypy.rpython.lltypesystem.rstr import STR
-        line = []
-        for arg in ll_args:
-            T = lltype.typeOf(arg)
-            if T == lltype.Ptr(STR):
-                arg = ''.join(arg.chars)
-            line.append(str(arg))
-        line = ' '.join(line)
-        print line
-        tracer = self.llinterpreter.tracer
-        if tracer:
-            tracer.dump('\n[debug] %s\n' % (line,))
-
     def op_debug_pdb(self, *ll_args):
         if self.llinterpreter.tracer:
             self.llinterpreter.tracer.flush()

Modified: pypy/branch/logging2/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/branch/logging2/pypy/rpython/lltypesystem/lloperation.py	(original)
+++ pypy/branch/logging2/pypy/rpython/lltypesystem/lloperation.py	Fri Oct 30 13:29:20 2009
@@ -523,6 +523,8 @@
     # __________ debugging __________
     'debug_view':           LLOp(),
     'debug_print':          LLOp(canrun=True),
+    'debug_start':          LLOp(canrun=True),
+    'debug_stop':           LLOp(canrun=True),
     'debug_pdb':            LLOp(),
     'debug_assert':         LLOp(tryfold=True),
     'debug_fatalerror':     LLOp(),

Modified: pypy/branch/logging2/pypy/rpython/lltypesystem/opimpl.py
==============================================================================
--- pypy/branch/logging2/pypy/rpython/lltypesystem/opimpl.py	(original)
+++ pypy/branch/logging2/pypy/rpython/lltypesystem/opimpl.py	Fri Oct 30 13:29:20 2009
@@ -3,6 +3,7 @@
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.rpython.lltypesystem.lloperation import opimpls
+from pypy.rlib import debug
 
 # ____________________________________________________________
 # Implementation of the 'canfold' operations
@@ -411,10 +412,22 @@
         raise TypeError("cannot fold getfield on mutable array")
     return p[index]
 
+def _normalize(x):
+    TYPE = lltype.typeOf(x)
+    if (isinstance(TYPE, lltype.Ptr) and TYPE.TO._name == 'rpy_string'
+        or getattr(TYPE, '_name', '') == 'String'):    # ootype
+        from pypy.rpython.annlowlevel import hlstr
+        return hlstr(x)
+    return x
+
 def op_debug_print(*args):
-    for arg in args:
-        print arg,
-    print
+    debug.debug_print(*map(_normalize, args))
+
+def op_debug_start(category):
+    debug.debug_start(_normalize(category))
+
+def op_debug_stop(category):
+    debug.debug_stop(_normalize(category))
 
 def op_gc_stack_bottom():
     pass       # marker for trackgcroot.py



More information about the Pypy-commit mailing list