[pypy-svn] r12246 - pypy/dist/pypy/rpython

arigo at codespeak.net arigo at codespeak.net
Fri May 13 19:36:58 CEST 2005


Author: arigo
Date: Fri May 13 19:36:58 2005
New Revision: 12246

Modified:
   pypy/dist/pypy/rpython/rlist.py
Log:
Looks slightly less obscure with compile_template().


Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Fri May 13 19:36:58 2005
@@ -1,6 +1,7 @@
 import py
 from pypy.annotation.model import *
 from pypy.rpython.lltypes import *
+from pypy.tool.template import compile_template
 
 
 def substitute_newlist(typer, op):
@@ -14,19 +15,19 @@
     except KeyError:
         # Make an implementation of newlist(x1,..,xn) which allocates
         # a list with n elements and initialize them.
- 
-        args = ', '.join(['arg%d' % i for i in range(n)])
-        lines = []
-        lines.append(    'def newlist(%s):' % args)
-        lines.append(    '    l = malloc(List_typ)')
-        lines.append(    '    l.items = malloc(List_typ.items.TO, %d)' % n)
-        for i in range(n):
-            lines.append('    l.items[%d].item = arg%d' % (i, i))
-        lines.append(    '    return l')
-        lines.append(    '')
-        miniglobal = {'List_typ': LIST.TO, 'malloc': malloc}
-        exec py.code.Source('\n'.join(lines)).compile() in miniglobal
-        newlist = typer.newlistcache[LIST, n] = miniglobal['newlist']
+        List_typ = LIST.TO
+
+        def template():
+            args = ', '.join(['arg%d' % i for i in range(n)])
+            yield     'def newlist(%s):' % args
+            yield     '    l = malloc(List_typ)'
+            yield     '    l.items = malloc(List_typ.items.TO, %d)' % n
+            for i in range(n):
+                yield '    l.items[%d].item = arg%d' % (i, i)
+            yield     '    return l'
+
+        newlist = compile_template(template(), 'newlist')
+        typer.newlistcache[LIST, n] = newlist
     return typer.substitute_op(op, (newlist,) + inputsignature +  (LIST,))
 
 def getlisttype(typer, s_list):



More information about the Pypy-commit mailing list