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

numpy-svn at scipy.org numpy-svn at scipy.org
Sun Oct 1 16:48:43 EDT 2006


Author: pearu
Date: 2006-10-01 15:48:37 -0500 (Sun, 01 Oct 2006)
New Revision: 3239

Added:
   trunk/numpy/f2py/lib/src/SCALARS_IN_BITS.cpp
Modified:
   trunk/numpy/f2py/lib/generate_pyobj_tofrom_funcs.py
   trunk/numpy/f2py/lib/parser/typedecl_statements.py
   trunk/numpy/f2py/lib/python_wrapper.py
Log:
F2PY G3: fixed bugs, added float and complex scalars support.

Modified: trunk/numpy/f2py/lib/generate_pyobj_tofrom_funcs.py
===================================================================
--- trunk/numpy/f2py/lib/generate_pyobj_tofrom_funcs.py	2006-10-01 11:49:23 UTC (rev 3238)
+++ trunk/numpy/f2py/lib/generate_pyobj_tofrom_funcs.py	2006-10-01 20:48:37 UTC (rev 3239)
@@ -9,15 +9,44 @@
 from parser.api import CHAR_BIT
 
 def pyobj_from_npy_int(ctype):
-    ctype_bits = int(ctype[7:])
-    itemsize = ctype_bits/CHAR_BIT
     dtype = ctype.upper()
+    cls = 'Int'+ctype[7:]
     return '''\
+/* depends: SCALARS_IN_BITS.cpp */
 static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) {
-  return PyArray_Return(PyArray_SimpleNewFromData(0, NULL, %(dtype)s, (char*)value));
+  PyObject* obj = PyArrayScalar_New(%(cls)s);
+  if (obj==NULL) /* TODO: set exception */ return NULL;
+  PyArrayScalar_ASSIGN(obj,%(cls)s,*value);
+  return obj;
 }
 ''' % (locals())
 
+def pyobj_from_npy_float(ctype):
+    dtype = ctype.upper()
+    cls = 'Float'+ctype[9:]
+    return '''\
+/* depends: SCALARS_IN_BITS.cpp */
+static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) {
+  PyObject* obj = PyArrayScalar_New(%(cls)s);
+  if (obj==NULL) /* TODO: set exception */ return NULL;
+  PyArrayScalar_ASSIGN(obj,%(cls)s,*value);
+  return obj;
+}
+''' % (locals())
+
+def pyobj_from_npy_complex(ctype):
+    dtype = ctype.upper()
+    cls = 'Complex'+ctype[11:]
+    return '''\
+/* depends: SCALARS_IN_BITS.cpp */
+static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) {
+  PyObject* obj = PyArrayScalar_New(%(cls)s);
+  if (obj==NULL) /* TODO: set exception */ return NULL;
+  PyArrayScalar_ASSIGN(obj,%(cls)s,*value);
+  return obj;
+}
+''' % (locals())
+
 def pyobj_from_f2py_type(ctype):
     ctype_bits = int(ctype[10:])
     raise NotImplementedError,`ctype`
@@ -125,5 +154,9 @@
 def pyobj_from_npy_scalar(ctype):
     if ctype.startswith('npy_int'):
         return dict(c_code=pyobj_from_npy_int(ctype))
+    elif ctype.startswith('npy_float'):
+        return dict(c_code=pyobj_from_npy_float(ctype))
+    elif ctype.startswith('npy_complex'):
+        return dict(c_code=pyobj_from_npy_complex(ctype))
     raise NotImplementedError,`ctype`
 

Modified: trunk/numpy/f2py/lib/parser/typedecl_statements.py
===================================================================
--- trunk/numpy/f2py/lib/parser/typedecl_statements.py	2006-10-01 11:49:23 UTC (rev 3238)
+++ trunk/numpy/f2py/lib/parser/typedecl_statements.py	2006-10-01 20:48:37 UTC (rev 3239)
@@ -341,12 +341,15 @@
     def get_length(self):
         return self.selector[0] or 1
 
-    def get_bit_size(self):
-        return CHAR_BIT * int(self.get_kind())
-
     def get_byte_size(self):
-        return self.get_bit_size() / CHAR_BIT
+        length, kind = self.selector
+        if length: return int(length)
+        if kind: return int(kind)
+        return self.default_kind
 
+    def get_bit_size(self):
+        return CHAR_BIT * int(self.get_byte_size())
+
     def is_intrinsic(self): return not isinstance(self,(Type,Class))
     def is_derived(self): return isinstance(self,Type)
 
@@ -381,7 +384,10 @@
 class DoublePrecision(TypeDeclarationStatement):
     match = re.compile(r'double\s*precision\b',re.I).match
     default_kind = 8
-    
+
+    def get_byte_size(self):
+        return self.default_kind
+
     def get_zero_value(self):
         return '0.0D0'
 
@@ -392,20 +398,12 @@
     match = re.compile(r'complex\b',re.I).match
     default_kind = 4
 
-    def get_kind(self):
+    def get_byte_size(self):
         length, kind = self.selector
-        if kind:
-            return kind
-        if length:
-            return int(length)/2
-        return self.default_kind
+        if length: return 2*int(length)
+        if kind: return 2*int(kind)
+        return 2*self.default_kind
 
-    def get_length(self):
-        return 2 * int(self.get_kind())
-
-    def get_bit_size(self):
-        return CHAR_BIT * self.get_length()
-
     def get_zero_value(self):
         kind = self.get_kind()
         if kind==self.default_kind: return '(0.0, 0.0)'
@@ -419,10 +417,8 @@
     match = re.compile(r'double\s*complex\b',re.I).match
     default_kind = 8
 
-    def get_kind(self): return self.default_kind
-    def get_length(self): return 2 * self.get_kind()
-    def get_bit_size(self):
-        return CHAR_BIT * self.get_length()
+    def get_byte_size(self):
+        return 2 * self.default_kind
 
     def get_zero_value(self):
         return '(0.0D0,0.0D0)'

Modified: trunk/numpy/f2py/lib/python_wrapper.py
===================================================================
--- trunk/numpy/f2py/lib/python_wrapper.py	2006-10-01 11:49:23 UTC (rev 3238)
+++ trunk/numpy/f2py/lib/python_wrapper.py	2006-10-01 20:48:37 UTC (rev 3239)
@@ -23,6 +23,7 @@
 
 #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API
 #include "numpy/arrayobject.h"
+#include "numpy/arrayscalars.h"
 
 %(header_list)s
 
@@ -140,13 +141,7 @@
         self.ctype = ctype = typedecl.get_c_type()
 
         if ctype.startswith('npy_'):
-            from generate_pyobj_tofrom_funcs import pyobj_to_npy_scalar
-            d = pyobj_to_npy_scalar(ctype)
-            for v in d.values():
-                self.resolve_dependencies(parent, v)
-            for k,v in d.items():
-                l = getattr(parent, k+'_list')
-                l.append(v)
+            WrapperCCode(parent, 'pyobj_from_%s' % (ctype))
             return
         
         if not ctype.startswith('f2py_type_'):
@@ -598,38 +593,14 @@
 
 
 if __name__ == '__main__':
-    #from utils import str2stmt, get_char_bit
-    
-    stmt = parse("""
-    module rat
-      integer :: i
-      type info
-        integer flag
-      end type info
-      type rational
-        integer n
-        integer d
-        type(info) i
-      end type rational
-    end module rat
-    subroutine foo(a)
-    use rat
-    type(rational) a
-    end
-    """)
-    #stmt = stmt.content[-1].content[1]
-    #print stmt
-    #wrapgen = TypeWrapper(stmt)
-    #print wrapgen.fortran_code()
-    #print wrapgen.c_code()
 
     foo_code = """! -*- f90 -*-
       module rat
         type info
-          integer flag
+          complex flag
         end type info
         type rational
-          integer n,d
+          !integer n,d
           type(info) i
         end type rational
       end module rat
@@ -685,14 +656,15 @@
     #print foo.info.__doc__
     #print foo.rational.__doc__
     #print dir(foo.rational)
-    i = foo.info(7)
+    i = foo.info(70+3j)
+    print 'i=',i
     #print i #,i.as_tuple()
     #print 'i.flag=',i.flag
-    r = foo.rational(2,3,i)
+    r = foo.rational(i)
     print r
     j = r.i
     print 'r.i.flag=',(r.i).flag
-    print 'j.flag=',j.flag
+    print 'j.flag=',type(j.flag)
     #print 'r=',r
     sys.exit()
     n,d,ii = r.as_tuple()

Added: trunk/numpy/f2py/lib/src/SCALARS_IN_BITS.cpp
===================================================================
--- trunk/numpy/f2py/lib/src/SCALARS_IN_BITS.cpp	2006-10-01 11:49:23 UTC (rev 3238)
+++ trunk/numpy/f2py/lib/src/SCALARS_IN_BITS.cpp	2006-10-01 20:48:37 UTC (rev 3239)
@@ -0,0 +1,279 @@
+#if NPY_BITSOF_LONG == 8
+#  ifndef PyInt8ScalarObject
+#    define PyInt8ScalarObject PyLongScalarObject
+#    define PyInt8ArrType_Type PyLongArrType_Type
+#  endif
+#elif NPY_BITSOF_LONG == 16
+#  ifndef PyInt16ScalarObject
+#    define PyInt16ScalarObject PyLongScalarObject
+#    define PyInt16ArrType_Type PyLongArrType_Type
+#  endif
+#elif NPY_BITSOF_LONG == 32
+#  ifndef PyInt32ScalarObject
+#    define PyInt32ScalarObject PyLongScalarObject
+#    define PyInt32ArrType_Type PyLongArrType_Type
+#  endif
+#elif NPY_BITSOF_LONG == 64
+#  ifndef PyInt64ScalarObject
+#    define PyInt64ScalarObject PyLongScalarObject
+#    define PyInt64ArrType_Type PyLongArrType_Type
+#  endif
+#elif NPY_BITSOF_LONG == 128
+#  ifndef PyInt128ScalarObject
+#    define PyInt128ScalarObject PyLongScalarObject
+#    define PyInt128ArrType_Type PyLongArrType_Type
+#  endif
+#endif
+
+#if NPY_BITSOF_LONGLONG == 8
+#  ifndef PyInt8ScalarObject
+#    define PyInt8ScalarObject PyLongLongScalarObject
+#    define PyInt8ArrType_Type PyLongLongArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGLONG == 16
+#  ifndef PyInt16ScalarObject
+#    define PyInt16ScalarObject PyLongLongScalarObject
+#    define PyInt16ArrType_Type PyLongLongArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGLONG == 32
+#  ifndef PyInt32ScalarObject
+#    define PyInt32ScalarObject PyLongLongScalarObject
+#    define PyInt32ArrType_Type PyLongLongArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGLONG == 64
+#  ifndef PyInt64ScalarObject
+#    define PyInt64ScalarObject PyLongLongScalarObject
+#    define PyInt64ArrType_Type PyLongLongArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGLONG == 128
+#  ifndef PyInt128ScalarObject
+#    define PyInt128ScalarObject PyLongLongScalarObject
+#    define PyInt128ArrType_Type PyLongLongArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGLONG == 256
+#  ifndef PyInt256ScalarObject
+#    define PyInt256ScalarObject PyLongLongScalarObject
+#    define PyInt256ArrType_Type PyLongLongArrType_Type
+#  endif
+#endif
+
+#if NPY_BITSOF_INT == 8
+#  ifndef PyInt8ScalarObject
+#    define PyInt8ScalarObject PyIntScalarObject
+#    define PyInt8ArrType_Type PyIntArrType_Type
+#  endif
+#elif NPY_BITSOF_INT == 16
+#  ifndef PyInt16ScalarObject
+#    define PyInt16ScalarObject PyIntScalarObject
+#    define PyInt16ArrType_Type PyIntArrType_Type
+#  endif
+#elif NPY_BITSOF_INT == 32
+#  ifndef PyInt32ScalarObject
+#    define PyInt32ScalarObject PyIntScalarObject
+#    define PyInt32ArrType_Type PyIntArrType_Type
+#  endif
+#elif NPY_BITSOF_INT == 64
+#  ifndef PyInt64ScalarObject
+#    define PyInt64ScalarObject PyIntScalarObject
+#    define PyInt64ArrType_Type PyIntArrType_Type
+#  endif
+#elif NPY_BITSOF_INT == 128
+#  ifndef PyInt128ScalarObject
+#    define PyInt128ScalarObject PyIntScalarObject
+#    define PyInt128ArrType_Type PyIntArrType_Type
+#  endif
+#endif
+
+#if NPY_BITSOF_SHORT == 8
+#  ifndef PyInt8ScalarObject
+#    define PyInt8ScalarObject PyShortScalarObject
+#    define PyInt8ArrType_Type PyShortArrType_Type
+#  endif
+#elif NPY_BITSOF_SHORT == 16
+#  ifndef PyInt16ScalarObject
+#    define PyInt16ScalarObject PyShortScalarObject
+#    define PyInt16ArrType_Type PyShortArrType_Type
+#  endif
+#elif NPY_BITSOF_SHORT == 32
+#  ifndef PyInt32ScalarObject
+#    define PyInt32ScalarObject PyShortScalarObject
+#    define PyInt32ArrType_Type PyShortArrType_Type
+#  endif
+#elif NPY_BITSOF_SHORT == 64
+#  ifndef PyInt64ScalarObject
+#    define PyInt64ScalarObject PyShortScalarObject
+#    define PyInt64ArrType_Type PyShortArrType_Type
+#  endif
+#elif NPY_BITSOF_SHORT == 128
+#  ifndef PyInt128ScalarObject
+#    define PyInt128ScalarObject PyShortScalarObject
+#    define PyInt128ArrType_Type PyShortArrType_Type
+#  endif
+#endif
+
+#if NPY_BITSOF_CHAR == 8
+#  ifndef PyInt8ScalarObject
+#    define PyInt8ScalarObject PyByteScalarObject
+#    define PyInt8ArrType_Type PyByteArrType_Type
+#  endif
+#elif NPY_BITSOF_CHAR == 16
+#  ifndef PyInt16ScalarObject
+#    define PyInt16ScalarObject PyByteScalarObject
+#    define PyInt16ArrType_Type PyByteArrType_Type
+#  endif
+#elif NPY_BITSOF_CHAR == 32
+#  ifndef PyInt32ScalarObject
+#    define PyInt32ScalarObject PyByteScalarObject
+#    define PyInt32ArrType_Type PyByteArrType_Type
+#  endif
+#elif NPY_BITSOF_CHAR == 64
+#  ifndef PyInt64ScalarObject
+#    define PyInt64ScalarObject PyByteScalarObject
+#    define PyInt64ArrType_Type PyByteArrType_Type
+#  endif
+#elif NPY_BITSOF_CHAR == 128
+#  ifndef PyInt128ScalarObject
+#    define PyInt128ScalarObject PyByteScalarObject
+#    define PyInt128ArrType_Type PyByteArrType_Type
+#  endif
+#endif
+
+#if NPY_BITSOF_DOUBLE == 16
+#  ifndef PyFloat16ScalarObject
+#    define PyFloat16ScalarObject PyDoubleScalarObject
+#    define PyComplex32ScalarObject PyCDoubleScalarObject
+#    define PyFloat16ArrType_Type PyDoubleArrType_Type
+#    define PyComplex32ArrType_Type PyCDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_DOUBLE == 32
+#  ifndef PyFloat32ScalarObject
+#    define PyFloat32ScalarObject PyDoubleScalarObject
+#    define PyComplex64ScalarObject PyCDoubleScalarObject
+#    define PyFloat32ArrType_Type PyDoubleArrType_Type
+#    define PyComplex64ArrType_Type PyCDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_DOUBLE == 64
+#  ifndef PyFloat64ScalarObject
+#    define PyFloat64ScalarObject PyDoubleScalarObject
+#    define PyComplex128ScalarObject PyCDoubleScalarObject
+#    define PyFloat64ArrType_Type PyDoubleArrType_Type
+#    define PyComplex128ArrType_Type PyCDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_DOUBLE == 80
+#  ifndef PyFloat80ScalarObject
+#    define PyFloat80ScalarObject PyDoubleScalarObject
+#    define PyComplex160ScalarObject PyCDoubleScalarObject
+#    define PyFloat80ArrType_Type PyDoubleArrType_Type
+#    define PyComplex160ArrType_Type PyCDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_DOUBLE == 96
+#  ifndef PyFloat96ScalarObject
+#    define PyFloat96ScalarObject PyDoubleScalarObject
+#    define PyComplex192ScalarObject PyCDoubleScalarObject
+#    define PyFloat96ArrType_Type PyDoubleArrType_Type
+#    define PyComplex192ArrType_Type PyCDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_DOUBLE == 128
+#  ifndef PyFloat128ScalarObject
+#    define PyFloat128ScalarObject PyDoubleScalarObject
+#    define PyComplex256ScalarObject PyCDoubleScalarObject
+#    define PyFloat128ArrType_Type PyDoubleArrType_Type
+#    define PyComplex256ArrType_Type PyCDoubleArrType_Type
+#  endif
+#endif
+
+#if NPY_BITSOF_FLOAT == 16
+#  ifndef PyFloat16ScalarObject
+#    define PyFloat16ScalarObject PyFloatScalarObject
+#    define PyComplex32ScalarObject PyCFloatScalarObject
+#    define PyFloat16ArrType_Type PyFloatArrType_Type
+#    define PyComplex32ArrType_Type PyCFloatArrType_Type
+#  endif
+#elif NPY_BITSOF_FLOAT == 32
+#  ifndef PyFloat32ScalarObject
+#    define PyFloat32ScalarObject PyFloatScalarObject
+#    define PyComplex64ScalarObject PyCFloatScalarObject
+#    define PyFloat32ArrType_Type PyFloatArrType_Type
+#    define PyComplex64ArrType_Type PyCFloatArrType_Type
+#  endif
+#elif NPY_BITSOF_FLOAT == 64
+#  ifndef PyFloat64ScalarObject
+#    define PyFloat64ScalarObject PyFloatScalarObject
+#    define PyComplex128ScalarObject PyCFloatScalarObject
+#    define PyFloat64ArrType_Type PyFloatArrType_Type
+#    define PyComplex128ArrType_Type PyCFloatArrType_Type
+#  endif
+#elif NPY_BITSOF_FLOAT == 80
+#  ifndef PyFloat80ScalarObject
+#    define PyFloat80ScalarObject PyFloatScalarObject
+#    define PyComplex160ScalarObject PyCFloatScalarObject
+#    define PyFloat80ArrType_Type PyFloatArrType_Type
+#    define PyComplex160ArrType_Type PyCFloatArrType_Type
+#  endif
+#elif NPY_BITSOF_FLOAT == 96
+#  ifndef PyFloat96ScalarObject
+#    define PyFloat96ScalarObject PyFloatScalarObject
+#    define PyComplex192ScalarObject PyCFloatScalarObject
+#    define PyFloat96ArrType_Type PyFloatArrType_Type
+#    define PyComplex192ArrType_Type PyCFloatArrType_Type
+#  endif
+#elif NPY_BITSOF_FLOAT == 128
+#  ifndef PyFloat128ScalarObject
+#    define PyFloat128ScalarObject PyFloatScalarObject
+#    define PyComplex256ScalarObject PyCFloatScalarObject
+#    define PyFloat128ArrType_Type PyFloatArrType_Type
+#    define PyComplex256ArrType_Type PyCFloatArrType_Type
+#  endif
+#endif
+
+#if NPY_BITSOF_LONGDOUBLE == 16
+#  ifndef PyFloat16ScalarObject
+#    define PyFloat16ScalarObject PyLongDoubleScalarObject
+#    define PyComplex32ScalarObject PyCLongDoubleScalarObject
+#    define PyFloat16ArrType_Type PyLongDoubleArrType_Type
+#    define PyComplex32ArrType_Type PyCLongDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGDOUBLE == 32
+#  ifndef PyFloat32ScalarObject
+#    define PyFloat32ScalarObject PyLongDoubleScalarObject
+#    define PyComplex64ScalarObject PyCLongDoubleScalarObject
+#    define PyFloat32ArrType_Type PyLongDoubleArrType_Type
+#    define PyComplex64ArrType_Type PyCLongDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGDOUBLE == 64
+#  ifndef PyFloat64ScalarObject
+#    define PyFloat64ScalarObject PyLongDoubleScalarObject
+#    define PyComplex128ScalarObject PyCLongDoubleScalarObject
+#    define PyFloat64ArrType_Type PyLongDoubleArrType_Type
+#    define PyComplex128ArrType_Type PyCLongDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGDOUBLE == 80
+#  ifndef PyFloat80ScalarObject
+#    define PyFloat80ScalarObject PyLongDoubleScalarObject
+#    define PyComplex160ScalarObject PyCLongDoubleScalarObject
+#    define PyFloat80ArrType_Type PyLongDoubleArrType_Type
+#    define PyComplex160ArrType_Type PyCLongDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGDOUBLE == 96
+#  ifndef PyFloat96ScalarObject
+#    define PyFloat96ScalarObject PyLongDoubleScalarObject
+#    define PyComplex192ScalarObject PyCLongDoubleScalarObject
+#    define PyFloat96ArrType_Type PyLongDoubleArrType_Type
+#    define PyComplex192ArrType_Type PyCLongDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGDOUBLE == 128
+#  ifndef PyFloat128ScalarObject
+#    define PyFloat128ScalarObject PyLongDoubleScalarObject
+#    define PyComplex256ScalarObject PyCLongDoubleScalarObject
+#    define PyFloat128ArrType_Type PyLongDoubleArrType_Type
+#    define PyComplex256ArrType_Type PyCLongDoubleArrType_Type
+#  endif
+#elif NPY_BITSOF_LONGDOUBLE == 256
+#  ifndef PyFloat256ScalarObject
+#    define PyFloat256ScalarObject PyLongDoubleScalarObject
+#    define PyComplex512ScalarObject PyCLongDoubleScalarObject
+#    define PyFloat256ArrType_Type PyLongDoubleArrType_Type
+#    define PyComplex512ArrType_Type PyCLongDoubleArrType_Type
+#  endif
+#endif
+




More information about the Numpy-svn mailing list