[pypy-svn] r12258 - in pypy/dist/pypy: tool translator

arigo at codespeak.net arigo at codespeak.net
Sat May 14 12:20:49 CEST 2005


Author: arigo
Date: Sat May 14 12:20:49 2005
New Revision: 12258

Modified:
   pypy/dist/pypy/tool/template.py
   pypy/dist/pypy/translator/typer.py
Log:
- compile_template() can accept a plain string.
- the TyperError exception tries to show where the problem is.



Modified: pypy/dist/pypy/tool/template.py
==============================================================================
--- pypy/dist/pypy/tool/template.py	(original)
+++ pypy/dist/pypy/tool/template.py	Sat May 14 12:20:49 2005
@@ -4,19 +4,25 @@
 
 
 def compile_template(source, resultname):
-    """Compiles the source code (a py.code.Source or a list/generator of lines)
+    """Compiles the source code (a string or a list/generator of lines)
     which should be a definition for a function named 'resultname'.
     The caller's global dict and local variable bindings are captured.
     """
     if not isinstance(source, py.code.Source):
-        lines = list(source)
+        if isinstance(source, str):
+            lines = [source]
+        else:
+            lines = list(source)
         lines.append('')
         source = py.code.Source('\n'.join(lines))
 
     caller = sys._getframe(1)
     locals = caller.f_locals
-    localnames = locals.keys()
-    localnames.sort()
+    if locals is caller.f_globals:
+        localnames = []
+    else:
+        localnames = locals.keys()
+        localnames.sort()
     values = [locals[key] for key in localnames]
     
     source = source.putaround(

Modified: pypy/dist/pypy/translator/typer.py
==============================================================================
--- pypy/dist/pypy/translator/typer.py	(original)
+++ pypy/dist/pypy/translator/typer.py	Sat May 14 12:20:49 2005
@@ -6,7 +6,11 @@
 
 
 class TyperError(Exception):
-    pass
+    def __str__(self):
+        result = Exception.__str__(self)
+        if hasattr(self, 'where'):
+            result += '\n.. %r\n.. %r' % self.where
+        return result
 
 
 class Specializer:
@@ -120,7 +124,11 @@
 
             # make a specialized version of the current operation
             # (which may become several operations)
-            flatten_ops(self.specialized_op(op, bindings), newops)
+            try:
+                flatten_ops(self.specialized_op(op, bindings), newops)
+            except TyperError, e:
+                e.where = (block, op)
+                raise
 
         block.operations[:] = newops
         self.insert_link_conversions(block)
@@ -150,24 +158,28 @@
         # insert the needed conversions on the links
         can_insert_here = block.exitswitch is None and len(block.exits) == 1
         for link in block.exits:
-            for i in range(len(link.args)):
-                a1 = link.args[i]
-                if a1 in (link.last_exception, link.last_exc_value):# treated specially in gen_link
-                    continue
-                a2 = link.target.inputargs[i]
-                a2type = self.setbesttype(a2)
-                a1, convops = self.convertvar(a1, a2type)
-                if convops and not can_insert_here:
-                    # cannot insert conversion operations around a single
-                    # link, unless it is the only exit of this block.
-                    # create a new block along the link...
-                    newblock = insert_empty_block(self.annotator.translator,
-                                                  link)
-                    # ...and do the conversions there.
-                    self.insert_link_conversions(newblock)
-                    break   # done with this link
-                flatten_ops(convops, block.operations)
-                link.args[i] = a1
+            try:
+                for i in range(len(link.args)):
+                    a1 = link.args[i]
+                    if a1 in (link.last_exception, link.last_exc_value):# treated specially in gen_link
+                        continue
+                    a2 = link.target.inputargs[i]
+                    a2type = self.setbesttype(a2)
+                    a1, convops = self.convertvar(a1, a2type)
+                    if convops and not can_insert_here:
+                        # cannot insert conversion operations around a single
+                        # link, unless it is the only exit of this block.
+                        # create a new block along the link...
+                        newblock = insert_empty_block(self.annotator.translator,
+                                                      link)
+                        # ...and do the conversions there.
+                        self.insert_link_conversions(newblock)
+                        break   # done with this link
+                    flatten_ops(convops, block.operations)
+                    link.args[i] = a1
+            except TyperError, e:
+                e.where = (block, link)
+                raise
 
 
     def specialized_op(self, op, bindings):



More information about the Pypy-commit mailing list