[Numpy-svn] r3322 - in trunk/numpy/f2py/lib: . parser

numpy-svn at scipy.org numpy-svn at scipy.org
Fri Oct 13 07:52:34 EDT 2006


Author: pearu
Date: 2006-10-13 06:52:25 -0500 (Fri, 13 Oct 2006)
New Revision: 3322

Added:
   trunk/numpy/f2py/lib/test_module_module.py
Modified:
   trunk/numpy/f2py/lib/parser/block_statements.py
   trunk/numpy/f2py/lib/py_wrap.py
   trunk/numpy/f2py/lib/py_wrap_subprogram.py
   trunk/numpy/f2py/lib/py_wrap_type.py
   trunk/numpy/f2py/lib/wrapper_base.py
Log:
F2PY: Impl. (suboptimal) solution for nested f90 module wrapping.

Modified: trunk/numpy/f2py/lib/parser/block_statements.py
===================================================================
--- trunk/numpy/f2py/lib/parser/block_statements.py	2006-10-13 00:41:44 UTC (rev 3321)
+++ trunk/numpy/f2py/lib/parser/block_statements.py	2006-10-13 11:52:25 UTC (rev 3322)
@@ -313,12 +313,12 @@
         if content:
             self.show_message('Not analyzed content: %s' % content)
 
-        module_provides = self.a.module_provides
-        for name, var in self.a.variables.items():
-            if var.is_public():
-                if module_provides.has_key(name):
-                    self.warning('module data object name conflict with %s, overriding.' % (name))
-                module_provides[name] = var
+        #module_provides = self.a.module_provides
+        #for name, var in self.a.variables.items():
+        #    if var.is_public():
+        #        if module_provides.has_key(name):
+        #            self.warning('module data object name conflict with %s, overriding.' % (name))
+        #        module_provides[name] = var
 
         return
 
@@ -1078,12 +1078,12 @@
 
         self.parent.a.type_decls[self.name] = self
 
-        parent_provides = self.parent.get_provides()
-        if parent_provides is not None:
-            if self.is_public():
-                if parent_provides.has_key(self.name):
-                    self.warning('type declaration name conflict with %s, overriding.' % (self.name))
-                parent_provides[self.name] = self
+        #parent_provides = self.parent.get_provides()
+        #if parent_provides is not None:
+        #    if self.is_public():
+        #        if parent_provides.has_key(self.name):
+        #            self.warning('type declaration name conflict with %s, overriding.' % (self.name))
+        #        parent_provides[self.name] = self
 
         return
 

Modified: trunk/numpy/f2py/lib/py_wrap.py
===================================================================
--- trunk/numpy/f2py/lib/py_wrap.py	2006-10-13 00:41:44 UTC (rev 3321)
+++ trunk/numpy/f2py/lib/py_wrap.py	2006-10-13 11:52:25 UTC (rev 3322)
@@ -57,11 +57,19 @@
     main_fortran_template = '''\
 %(fortran_code_list)s
 '''
+
+
+
     def __init__(self, modulename):
         WrapperBase.__init__(self)
         self.modulename = modulename
         self.cname = 'f2py_' + modulename
 
+        self.defined_cpp_code = []
+        self.defined_c_code = []
+        self.defined_types = []
+        self.defined_capi_codes = []
+
         
         self.header_list = []
         self.typedef_list = []

Modified: trunk/numpy/f2py/lib/py_wrap_subprogram.py
===================================================================
--- trunk/numpy/f2py/lib/py_wrap_subprogram.py	2006-10-13 00:41:44 UTC (rev 3321)
+++ trunk/numpy/f2py/lib/py_wrap_subprogram.py	2006-10-13 11:52:25 UTC (rev 3322)
@@ -57,6 +57,7 @@
 typedef void (*%(name)s_functype)();
 '''
     extern_template_module = '''\
+extern void %(init_func)s_f( %(name)s_functype);
 static %(name)s_functype %(name)s_func_ptr;
 '''
     objdecl_template_module = '''
@@ -77,14 +78,16 @@
 %(init_func)s_f(%(init_func)s_c);
 '''
 
-    _defined = []
     def __init__(self, parent, block):
         WrapperBase.__init__(self)
         self.name = name = pyname = block.name
         self.cname = cname = '%s_%s' % (parent.cname,name)
-        if cname in self._defined:
+
+        defined = parent.defined_capi_codes
+        if cname in defined:
             return
-        self._defined.append(cname)
+        defined.append(cname)
+        
         self.info('Generating interface for %s: %s' % (block.__class__, cname))
 
         if pyname.startswith('f2pywrap_'):
@@ -99,6 +102,8 @@
         self.objdecl_template =  ''
         self.fortran_code_template = ''
 
+        WrapperCPPMacro(parent, 'F_FUNC')
+        
         if isinstance(block.parent, Module):
             self.mname = block.parent.name
             self.init_func = '%s_init' % (name)
@@ -179,7 +184,7 @@
                     self.return_format_list.append('O&')
                     self.return_obj_list.append('\npyobj_from_%s, &%s' % (ti.ctype, argname))
 
-        WrapperCPPMacro(parent, 'F_FUNC')
+
         self.call_list.append('%s_f(%s);' % (name,', '.join(args_f+extra_args_f)))
 
         self.clean_pyobjfrom_list.reverse()

Modified: trunk/numpy/f2py/lib/py_wrap_type.py
===================================================================
--- trunk/numpy/f2py/lib/py_wrap_type.py	2006-10-13 00:41:44 UTC (rev 3321)
+++ trunk/numpy/f2py/lib/py_wrap_type.py	2006-10-13 11:52:25 UTC (rev 3322)
@@ -325,15 +325,17 @@
   return return_value;
 }
 ''' 
-    _defined = []
     def __init__(self, parent, typedecl):
         WrapperBase.__init__(self)
         self.name = name = typedecl.name
         ti = PyTypeInterface(typedecl)
         self.ctype = ctype = ti.ctype
-        if ctype in self._defined:
+
+        defined = parent.defined_types
+        if name in defined:
             return
-        self._defined.append(ctype)
+        defined.append(name)
+        
         self.info('Generating interface for %s: %s' % (typedecl.__class__, ctype))
 
         if isinstance(typedecl, (Integer,Byte,Real,DoublePrecision)):
@@ -580,14 +582,16 @@
     def __init__(self, parent, typedecl):
         WrapperBase.__init__(self)
         ti = PyTypeInterface(typedecl)
-        name = typedecl.name
-        if name in self._defined:
+        self.ctype = ctype = ti.ctype
+        defined = parent.defined_types
+        if ctype in defined:
             return
-        self._defined.append(name)
-        self.info('Generating interface for %s: %s' % (typedecl.__class__, name))
+        defined.append(ctype)
+
+        self.info('Generating interface for %s: %s' % (typedecl.__class__, ctype))
         parent.isf90 = True
 
-        self.name = name
+        self.name = name = typedecl.name
         self.otype = otype = ti.otype
         self.ctype = ctype = ti.ctype
         self.ctype_ptrs = self.ctype + '_ptrs'

Added: trunk/numpy/f2py/lib/test_module_module.py
===================================================================
--- trunk/numpy/f2py/lib/test_module_module.py	2006-10-13 00:41:44 UTC (rev 3321)
+++ trunk/numpy/f2py/lib/test_module_module.py	2006-10-13 11:52:25 UTC (rev 3322)
@@ -0,0 +1,84 @@
+#!/usr/bin/env python
+"""
+Tests for module with scalar derived types and subprograms.
+
+-----
+Permission to use, modify, and distribute this software is given under the
+terms of the NumPy License. See http://scipy.org.
+
+NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
+Author: Pearu Peterson <pearu at cens.ioc.ee>
+Created: Oct 2006
+-----
+"""
+
+import os
+import sys
+from numpy.testing import *
+
+def build(fortran_code, rebuild=True):
+    modulename = os.path.splitext(os.path.basename(__file__))[0] + '_ext'
+    try:
+        exec ('import %s as m' % (modulename))
+        if rebuild and os.stat(m.__file__)[8] < os.stat(__file__)[8]:
+            del sys.modules[m.__name__] # soft unload extension module
+            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)
+        print msg, ', recompiling %s.' % (modulename)
+        import tempfile
+        fname = tempfile.mktemp() + '.f90'
+        f = open(fname,'w')
+        f.write(fortran_code)
+        f.close()
+        sys_argv = []
+        sys_argv.extend(['--build-dir','tmp'])
+        #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
+  contains
+    subroutine foo2()
+      print*,"In foo2"
+    end subroutine foo2
+end module
+module test_module_module_ext
+  contains
+    subroutine foo
+      use test_module_module_ext2
+      print*,"In foo"
+      call foo2
+    end subroutine foo
+    subroutine bar(a)
+      use test_module_module_ext2
+      type(rat) a
+      print*,"In bar,a=",a
+    end subroutine bar
+end module test_module_module_ext
+'''
+
+# tester note: set rebuild=True when changing fortan_code and for SVN
+m = build(fortran_code, rebuild=True)
+
+from numpy import *
+
+class test_m(NumpyTestCase):
+
+    def check_foo_simple(self, level=1):
+        foo = m.foo
+        foo()
+
+if __name__ == "__main__":
+    NumpyTest().run()

Modified: trunk/numpy/f2py/lib/wrapper_base.py
===================================================================
--- trunk/numpy/f2py/lib/wrapper_base.py	2006-10-13 00:41:44 UTC (rev 3321)
+++ trunk/numpy/f2py/lib/wrapper_base.py	2006-10-13 11:52:25 UTC (rev 3322)
@@ -118,12 +118,12 @@
     """
     CPP macros
     """
-    _defined_macros = []
     def __init__(self, parent, name):
         WrapperBase.__init__(self)
-        if name in self._defined_macros:
+        defined = parent.defined_cpp_code
+        if name in defined:
             return
-        self._defined_macros.append(name)
+        defined.append(name)
 
         body = self.get_resource_content(name,'.cpp')
         if body is None:
@@ -137,12 +137,12 @@
     """
     C code
     """
-    _defined_codes = []
     def __init__(self, parent, name):
         WrapperBase.__init__(self)
-        if name in self._defined_codes:
+        defined = parent.defined_c_code
+        if name in defined:
             return
-        self._defined_codes.append(name)
+        defined.append(name)
 
         body = self.get_resource_content(name,'.c')
         if body is None:




More information about the Numpy-svn mailing list