[pypy-svn] pypy default: move some code into model.py

antocuni commits-noreply at bitbucket.org
Fri Feb 18 13:54:38 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r42155:2bc86912a181
Date: 2011-02-18 10:26 +0100
http://bitbucket.org/pypy/pypy/changeset/2bc86912a181/

Log:	move some code into model.py

diff --git a/pypy/module/pypyjit/test_pypy_c/model.py b/pypy/module/pypyjit/test_pypy_c/model.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/pypyjit/test_pypy_c/model.py
@@ -0,0 +1,49 @@
+import py
+import re
+from lib_pypy import disassembler
+from pypy.jit.tool import oparser
+
+class Log(object):
+    def __init__(self, func, rawtraces):
+        traces = map(Trace, rawtraces)
+
+    @classmethod
+    def find_chunks_range(cls, func):
+        """
+        Parse the given function and return a dictionary mapping "chunk
+        names" to "line ranges".  Chunks are identified by comments with a
+        special syntax::
+
+            # the chunk "myid" corresponds to the whole line
+            print 'foo' # ID: myid
+        """
+        result = {}
+        start_lineno = func.func_code.co_firstlineno
+        for i, line in enumerate(py.code.Source(func)):
+            m = re.search('# ID: (\w+)', line)
+            if m:
+                name = m.group(1)
+                lineno = start_lineno+i
+                result[name] = xrange(lineno, lineno+1)
+        return result
+
+    @classmethod
+    def find_chunks(cls, func):
+        """
+        Parse the given function and return a dictionary mapping "chunk names"
+        to "opcodes".
+        """
+        chunks = {}
+        code = disassembler.dis(func)
+        ranges = cls.find_chunks_range(func)
+        for name, linerange in ranges.iteritems():
+            opcodes = [opcode for opcode in code.opcodes
+                       if opcode.lineno in linerange]
+            chunks[name] = opcodes
+        return chunks
+
+
+class Trace(object):
+    def __init__(self, rawtrace):
+        # "low level trace", i.e. an instance of history.TreeLoop
+        self.lltrace = oparser.parse(rawtrace, no_namespace=True)

diff --git a/pypy/module/pypyjit/test_pypy_c/test_pypy_c_new.py b/pypy/module/pypyjit/test_pypy_c/test_pypy_c_new.py
--- a/pypy/module/pypyjit/test_pypy_c/test_pypy_c_new.py
+++ b/pypy/module/pypyjit/test_pypy_c/test_pypy_c_new.py
@@ -3,16 +3,8 @@
 from lib_pypy import disassembler
 from pypy.tool.udir import udir
 from pypy.tool import logparser
-from pypy.jit.tool import oparser
+from pypy.module.pypyjit.test_pypy_c.model import Log
 
-class Log(object):
-    def __init__(self, func, rawtraces):
-        traces = map(Trace, rawtraces)
-
-class Trace(object):
-    def __init__(self, rawtrace):
-        # "low level trace", i.e. an instance of history.TreeLoop
-        self.lltrace = oparser.parse(rawtrace, no_namespace=True)
         
 
 
@@ -28,38 +20,6 @@
     def setup_method(self, meth):
         self.filepath = self.tmpdir.join(meth.im_func.func_name + '.py')
 
-    def find_chunks_range(self, func):
-        """
-        Parse the given function and return a dictionary mapping "chunk
-        names" to "line ranges".  Chunks are identified by comments with a
-        special syntax::
-
-            # the chunk "myid" corresponds to the whole line
-            print 'foo' # ID: myid
-        """
-        result = {}
-        start_lineno = func.func_code.co_firstlineno
-        for i, line in enumerate(py.code.Source(func)):
-            m = re.search('# ID: (\w+)', line)
-            if m:
-                name = m.group(1)
-                lineno = start_lineno+i
-                result[name] = xrange(lineno, lineno+1)
-        return result
-
-    def find_chunks(self, func):
-        """
-        Parse the given function and return a dictionary mapping "chunk names"
-        to "opcodes".
-        """
-        chunks = {}
-        code = disassembler.dis(func)
-        ranges = self.find_chunks_range(func)
-        for name, linerange in ranges.iteritems():
-            opcodes = [opcode for opcode in code.opcodes
-                       if opcode.lineno in linerange]
-            chunks[name] = opcodes
-        return chunks
     
     def run(self, func, threshold=1000):
         # write the snippet
@@ -83,7 +43,6 @@
         assert not stderr
         #
         # parse the JIT log
-        chunks = self.find_chunks(func)
         rawlog = logparser.parse_log_file(str(logfile))
         rawtraces = logparser.extract_category(rawlog, 'jit-log-opt-')
         log = Log(func, rawtraces)
@@ -91,30 +50,6 @@
 
 class TestInfrastructure(BaseTestPyPyC):
 
-    def test_find_chunks_range(self):
-        def f():
-            a = 0 # ID: myline
-            return a
-        #
-        start_lineno = f.func_code.co_firstlineno
-        ids = self.find_chunks_range(f)
-        assert len(ids) == 1
-        myline_range = ids['myline']
-        assert list(myline_range) == range(start_lineno+1, start_lineno+2)
-
-    def test_find_chunks(self):
-        def f():
-            i = 0
-            x = 0
-            z = x + 3 # ID: myline
-            return z
-        #
-        chunks = self.find_chunks(f)
-        assert len(chunks) == 1
-        myline = chunks['myline']
-        opcodes_names = [opcode.__class__.__name__ for opcode in myline]
-        assert opcodes_names == ['LOAD_FAST', 'LOAD_CONST', 'BINARY_ADD', 'STORE_FAST']
-
     def test_parse_jitlog(self):
         def f():
             i = 0

diff --git a/pypy/module/pypyjit/test_pypy_c/test_model.py b/pypy/module/pypyjit/test_pypy_c/test_model.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/pypyjit/test_pypy_c/test_model.py
@@ -0,0 +1,27 @@
+from pypy.module.pypyjit.test_pypy_c.model import Log
+
+class TestLog(object):
+
+    def test_find_chunks_range(self):
+        def f():
+            a = 0 # ID: myline
+            return a
+        #
+        start_lineno = f.func_code.co_firstlineno
+        ids = Log.find_chunks_range(f)
+        assert len(ids) == 1
+        myline_range = ids['myline']
+        assert list(myline_range) == range(start_lineno+1, start_lineno+2)
+
+    def test_find_chunks(self):
+        def f():
+            i = 0
+            x = 0
+            z = x + 3 # ID: myline
+            return z
+        #
+        chunks = Log.find_chunks(f)
+        assert len(chunks) == 1
+        myline = chunks['myline']
+        opcodes_names = [opcode.__class__.__name__ for opcode in myline]
+        assert opcodes_names == ['LOAD_FAST', 'LOAD_CONST', 'BINARY_ADD', 'STORE_FAST']


More information about the Pypy-commit mailing list