[Numpy-svn] r5076 - trunk/numpy/distutils

numpy-svn@scip... numpy-svn@scip...
Thu Apr 24 00:14:44 CDT 2008


Author: charris
Date: 2008-04-24 00:14:41 -0500 (Thu, 24 Apr 2008)
New Revision: 5076

Modified:
   trunk/numpy/distutils/conv_template.py
Log:
Fix conv_template to correctly handle nested loops with surrounding code.
Add some template debugging aids.


Modified: trunk/numpy/distutils/conv_template.py
===================================================================
--- trunk/numpy/distutils/conv_template.py	2008-04-24 00:58:38 UTC (rev 5075)
+++ trunk/numpy/distutils/conv_template.py	2008-04-24 05:14:41 UTC (rev 5076)
@@ -160,9 +160,8 @@
         if nsub is None :
             nsub = size
         elif nsub != size :
-            print name
-            print vals
-            raise ValueError, "Mismatch in number to replace"
+            msg = "Mismatch in number: %s - %s" % (name, vals)
+            raise ValueError, msg
         names.append((name,vals))
 
     # generate list of dictionaries, one for each template iteration
@@ -176,12 +175,19 @@
 
 replace_re = re.compile(r"@([\w]+)@")
 def parse_string(astr, env, level, line) :
+    lineno = "#line %d\n" % line
+
     # local function for string replacement, uses env
     def replace(match):
         name = match.group(1)
-        return env[name]
+        try :
+            val = env[name]
+        except KeyError, e :
+            msg = '%s: %s'%(lineno, e)
+            raise KeyError, msg
+        return val
 
-    code = []
+    code = [lineno]
     struct = parse_structure(astr, level)
     if struct :
         # recurse over inner loops
@@ -193,19 +199,23 @@
             text = astr[sub[1]:sub[2]]
             oldend = sub[3]
             newline = line + sub[4]
-            envlist = parse_loop_header(head)
-            code.append(pref)
+            code.append(replace_re.sub(replace, pref))
+            try :
+                envlist = parse_loop_header(head)
+            except ValueError, e :
+                msg = "%s: %s" % (lineno, e)
+                raise ValueError, msg
             for newenv in envlist :
                 newenv.update(env)
                 newcode = parse_string(text, newenv, newlevel, newline)
                 code.extend(newcode)
-        code.append(astr[oldend:])       
+        suff = astr[oldend:]
+        code.append(replace_re.sub(replace, suff))
     else :
         # replace keys
-        lineno = "#line %d\n" % line
-        newcode = replace_re.sub(replace, astr)
-        code.append(''.join((lineno, newcode , '\n')))
-    return code
+        code.append(replace_re.sub(replace, astr))
+    code.append('\n')
+    return ''.join(code)
 
 def process_str(astr):
     code = [header]



More information about the Numpy-svn mailing list