[Numpy-svn] r3324 - trunk/numpy/f2py/lib

numpy-svn at scipy.org numpy-svn at scipy.org
Fri Oct 13 12:18:34 CDT 2006


Author: pearu
Date: 2006-10-13 12:18:28 -0500 (Fri, 13 Oct 2006)
New Revision: 3324

Modified:
   trunk/numpy/f2py/lib/py_wrap.py
   trunk/numpy/f2py/lib/py_wrap_type.py
   trunk/numpy/f2py/lib/test_module_module.py
Log:
F2PY G3: exposing f90 type information via _API variable for other modules.

Modified: trunk/numpy/f2py/lib/py_wrap.py
===================================================================
--- trunk/numpy/f2py/lib/py_wrap.py	2006-10-13 17:08:24 UTC (rev 3323)
+++ trunk/numpy/f2py/lib/py_wrap.py	2006-10-13 17:18:28 UTC (rev 3324)
@@ -43,11 +43,17 @@
 PyMODINIT_FUNC init%(modulename)s(void) {
   f2py_module = Py_InitModule("%(modulename)s", f2py_module_methods);
   import_array();
-  %(module_init_list)s
   if (PyErr_Occurred()) {
-    PyErr_SetString(PyExc_ImportError, "can\'t initialize module %(modulename)s");
+    PyErr_SetString(PyExc_ImportError, "failed to load array module.");
     return;
   }
+  %(module_init_list)s
+  return;
+capi_err:
+  if (!PyErr_Occurred()) {
+    PyErr_SetString(PyExc_RuntimeError, "failed to initialize %(modulename)s module.");
+  }
+  return;
 }
 #ifdef __cplusplus
 }

Modified: trunk/numpy/f2py/lib/py_wrap_type.py
===================================================================
--- trunk/numpy/f2py/lib/py_wrap_type.py	2006-10-13 17:08:24 UTC (rev 3323)
+++ trunk/numpy/f2py/lib/py_wrap_type.py	2006-10-13 17:18:28 UTC (rev 3324)
@@ -448,13 +448,24 @@
     0,                         /* tp_alloc */
     %(otype)s_new,                 /* tp_new */
 };
+
+void *F2PY_%(otype)s_API[] = {
+  (void *) &%(otype)sType,
+  (void *) pyobj_from_%(ctype)s,
+  (void *) pyobj_to_%(ctype)s_inplace
+};
 '''
-
+    
     module_init_template = '''\
 if (PyType_Ready(&%(otype)sType) < 0)
-  return;
+  goto capi_err;
 PyModule_AddObject(f2py_module, "%(name)s",
                                 (PyObject *)&%(otype)sType);
+{
+  PyObject* c_api = PyCObject_FromVoidPtr((void *)F2PY_%(otype)s_API, NULL);
+  PyModule_AddObject(f2py_module, "_%(name)s_API", c_api);
+  if (PyErr_Occurred()) goto capi_err;
+}
 '''
 
     c_code_template = '''\

Modified: trunk/numpy/f2py/lib/test_module_module.py
===================================================================
--- trunk/numpy/f2py/lib/test_module_module.py	2006-10-13 17:08:24 UTC (rev 3323)
+++ trunk/numpy/f2py/lib/test_module_module.py	2006-10-13 17:18:28 UTC (rev 3324)
@@ -16,7 +16,7 @@
 import sys
 from numpy.testing import *
 
-def build(fortran_code, rebuild=True):
+def build(fortran_code, rebuild=True, build_dir = 'tmp'):
     modulename = os.path.splitext(os.path.basename(__file__))[0] + '_ext'
     try:
         exec ('import %s as m' % (modulename))
@@ -25,27 +25,25 @@
             os.remove(m.__file__)
             raise ImportError,'%s is newer than %s' % (__file__, m.__file__)
     except ImportError,msg:
-        assert str(msg)==('No module named %s' % (modulename)),str(msg)
+        assert str(msg)==('No module named %s' % (modulename)) \
+               or str(msg).startswith('%s is newer than' % (__file__)),str(msg)
         print msg, ', recompiling %s.' % (modulename)
-        import tempfile
-        fname = tempfile.mktemp() + '.f90'
+        fname = os.path.join(build_dir, modulename + '_source.f90')
         f = open(fname,'w')
         f.write(fortran_code)
         f.close()
         sys_argv = []
-        sys_argv.extend(['--build-dir','tmp'])
+        sys_argv.extend(['--build-dir',build_dir])
         #sys_argv.extend(['-DF2PY_DEBUG_PYOBJ_TOFROM'])
         from main import build_extension
         sys_argv.extend(['-m',modulename, fname])
         build_extension(sys_argv)
-        os.remove(fname)
         status = os.system(' '.join([sys.executable] + sys.argv))
         sys.exit(status)
     return m
 
 fortran_code = '''
 module test_module_module_ext2
-
   type rat
     integer n,d
   end type rat



More information about the Numpy-svn mailing list