[Scipy-svn] r4503 - trunk/scipy/sandbox/mkufunc

scipy-svn@scip... scipy-svn@scip...
Sun Jun 29 22:19:30 CDT 2008


Author: ilan
Date: 2008-06-29 22:19:29 -0500 (Sun, 29 Jun 2008)
New Revision: 4503

Modified:
   trunk/scipy/sandbox/mkufunc/mkufunc.py
Log:
PyPy output source now also in support code; no more pypy.c

Modified: trunk/scipy/sandbox/mkufunc/mkufunc.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/mkufunc.py	2008-06-30 01:32:41 UTC (rev 4502)
+++ trunk/scipy/sandbox/mkufunc/mkufunc.py	2008-06-30 03:19:29 UTC (rev 4503)
@@ -105,17 +105,8 @@
         assert len(found) == 1
         res = found[0]
         res = res.replace(self._prefix + 'pypy_g_ll_math_ll_math_', '')
-        return res + '\n'
+        return 'inline ' + res + '\n'
     
-    def decl(self):
-        p = re.compile(r'^\w+[*\s\w]+' + self.cname +
-                       r'\s*\([^)]*\);',
-                       re.DOTALL | re.MULTILINE | re.VERBOSE)
-        
-        found = p.findall(self._allCsrc)
-        assert len(found) == 1
-        return found[0]
-    
     def ufunc_support_code(self):
         # Unfortunately the code in here is very hard to read.
         # In order to make the code clearer, one would need a real template
@@ -173,7 +164,7 @@
 		%(body1d_in)s
 		%(rettype)s *out = (%(rettype)s *)op;
 		
-		*out = f(%(ptrargs)s);
+		*out = (%(rettype)s) f(%(ptrargs)s);
 
                 %(body1d_add)s
                 op += os;
@@ -182,30 +173,21 @@
 ''' % locals()
 
 
-pypyc = os.path.join(weave.catalog.default_temp_dir(), 'pypy.c')
-
-def write_pypyc(cfuncs):
-    """ Given a list of Cfunc instances, write the C code containing the
-        functions into a file.
-    """
-    header = open(os.path.join(os.path.dirname(__file__),
-                               'pypy_head.h')).read()
-    fo = open(pypyc, 'w')
-    fo.write(header)
-    fo.write('/********************* end header ********************/\n\n')
-    for cf in cfuncs:
-        fo.write(cf.cfunc())
-    fo.close()
-
-
 def support_code(cfuncs):
     """ Given a list of Cfunc instances, return the support_code for weave.
     """
-    fname = cfuncs[0].f.__name__
+    acc = cStringIO.StringIO()
     
-    declarations = ''.join('\t%s\n' % cf.decl() for cf in cfuncs)
+    acc.write('/********************* start pypy_head.h  **************/\n\n')
+    acc.write(open(os.path.join(os.path.dirname(__file__),
+                                'pypy_head.h')).read())
+    acc.write('/********************** end pypy_head.h ****************/\n\n')
     
-    func_support = ''.join(cf.ufunc_support_code() for cf in cfuncs)
+    for cf in cfuncs:
+        acc.write(cf.cfunc())
+        acc.write(cf.ufunc_support_code())
+        
+    fname = cfuncs[0].f.__name__
     
     pyufuncs = ''.join('\tPyUFunc_%i,\n' % cf.n for cf in cfuncs)
     
@@ -214,12 +196,8 @@
     types = ''.join('\t%s  /* %i */\n' %
                     (''.join(typedict[t].npy + ', ' for t in cf.sig), cf.n)
                     for cf in cfuncs)
-    return '''
-extern "C" {
-%(declarations)s}
-
-%(func_support)s
-
+    
+    acc.write('''
 static PyUFuncGenericFunction %(fname)s_functions[] = {
 %(pyufuncs)s};
 
@@ -228,7 +206,9 @@
 
 static char %(fname)s_types[] = {
 %(types)s};
-''' % locals()
+''' % locals())
+    
+    return acc.getvalue()
 
 
 def code(f, signatures):
@@ -272,16 +252,13 @@
     
     cfuncs = [Cfunc(f, sig, n) for n, sig in enumerate(signatures)]
     
-    write_pypyc(cfuncs)
-    
     ufunc_info = weave.base_info.custom_info()
     ufunc_info.add_header('"numpy/ufuncobject.h"')
     
     return weave.inline(code(f, signatures),
                         verbose=0,
                         support_code=support_code(cfuncs),
-                        customize=ufunc_info,
-                        sources=[pypyc])
+                        customize=ufunc_info)
 
 
 def mkufunc(arg0=[float]):



More information about the Scipy-svn mailing list