summaryrefslogtreecommitdiff
path: root/numpy/f2py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/f2py')
-rw-r--r--numpy/f2py/__init__.py26
-rw-r--r--numpy/f2py/cfuncs.py4
-rwxr-xr-xnumpy/f2py/f2py2e.py4
-rw-r--r--numpy/f2py/src/fortranobject.c16
-rw-r--r--numpy/f2py/src/fortranobject.h5
-rw-r--r--numpy/f2py/tests/test_array_from_pyobj.py82
-rw-r--r--numpy/f2py/tests/test_callback.py24
7 files changed, 99 insertions, 62 deletions
diff --git a/numpy/f2py/__init__.py b/numpy/f2py/__init__.py
index ccdbd4e0b..fcfd1853e 100644
--- a/numpy/f2py/__init__.py
+++ b/numpy/f2py/__init__.py
@@ -28,20 +28,20 @@ def compile(source,
from numpy.distutils.exec_command import exec_command
import tempfile
if source_fn is None:
- fname = os.path.join(tempfile.mktemp()+'.f')
+ f = tempfile.NamedTemporaryFile(suffix='.f')
else:
- fname = source_fn
-
- f = open(fname, 'w')
- f.write(source)
- f.close()
-
- args = ' -c -m %s %s %s'%(modulename, fname, extra_args)
- c = '%s -c "import numpy.f2py as f2py2e;f2py2e.main()" %s' %(sys.executable, args)
- s, o = exec_command(c)
- if source_fn is None:
- try: os.remove(fname)
- except OSError: pass
+ f = open(source_fn, 'w')
+
+ try:
+ f.write(source)
+ f.flush()
+
+ args = ' -c -m %s %s %s'%(modulename, f.name, extra_args)
+ c = '%s -c "import numpy.f2py as f2py2e;f2py2e.main()" %s' % \
+ (sys.executable, args)
+ s, o = exec_command(c)
+ finally:
+ f.close()
return s
from numpy.testing import Tester
diff --git a/numpy/f2py/cfuncs.py b/numpy/f2py/cfuncs.py
index 5a5d2bf64..7fb630697 100644
--- a/numpy/f2py/cfuncs.py
+++ b/numpy/f2py/cfuncs.py
@@ -75,7 +75,7 @@ typedef long long long_long;
typedef unsigned long long unsigned_long_long;
#endif
"""
-typedefs['insinged_long_long']="""\
+typedefs['unsigned_long_long']="""\
#ifdef _WIN32
typedef __uint64 long_long;
#else
@@ -312,7 +312,7 @@ cppmacros['pyobj_from_complex_float1']='#define pyobj_from_complex_float1(v) (Py
needs['pyobj_from_string1']=['string']
cppmacros['pyobj_from_string1']='#define pyobj_from_string1(v) (PyString_FromString((char *)v))'
needs['pyobj_from_string1size']=['string']
-cppmacros['pyobj_from_string1size']='#define pyobj_from_string1size(v,len) (PyString_FromStringAndSize((char *)v, len))'
+cppmacros['pyobj_from_string1size']='#define pyobj_from_string1size(v,len) (PyUString_FromStringAndSize((char *)v, len))'
needs['TRYPYARRAYTEMPLATE']=['PRINTPYOBJERR']
cppmacros['TRYPYARRAYTEMPLATE']="""\
/* New SciPy */
diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py
index ff9d19e9d..25407d421 100755
--- a/numpy/f2py/f2py2e.py
+++ b/numpy/f2py/f2py2e.py
@@ -91,7 +91,7 @@ Options:
--lower is assumed with -h key, and --no-lower without -h key.
--build-dir <dirname> All f2py generated files are created in <dirname>.
- Default is tempfile.mktemp().
+ Default is tempfile.mkdtemp().
--overwrite-signature Overwrite existing signature file.
@@ -424,7 +424,7 @@ def run_compile():
del sys.argv[i]
else:
remove_build_dir = 1
- build_dir = os.path.join(tempfile.mktemp())
+ build_dir = tempfile.mkdtemp()
_reg1 = re.compile(r'[-][-]link[-]')
sysinfo_flags = [_m for _m in sys.argv[1:] if _reg1.match(_m)]
diff --git a/numpy/f2py/src/fortranobject.c b/numpy/f2py/src/fortranobject.c
index c07de99c0..9c96c1f46 100644
--- a/numpy/f2py/src/fortranobject.c
+++ b/numpy/f2py/src/fortranobject.c
@@ -335,7 +335,7 @@ fortran_call(PyFortranObject *fp, PyObject *arg, PyObject *kw) {
name=%s,func=%p,data=%p,%p\n",fp->defs[i].name,
fp->defs[i].func,fp->defs[i].data,&fp->defs[i].data); */
if (fp->defs[i].rank==-1) {/* is Fortran routine */
- if ((fp->defs[i].func==NULL)) {
+ if (fp->defs[i].func==NULL) {
PyErr_Format(PyExc_RuntimeError, "no function to call");
return NULL;
}
@@ -741,14 +741,12 @@ PyArrayObject* array_from_pyobj(const int type_num,
return arr;
}
- if ((intent & F2PY_INTENT_INOUT)
- || (intent & F2PY_INTENT_INPLACE)
- || (intent & F2PY_INTENT_CACHE)) {
- sprintf(mess,"failed to initialize intent(inout|inplace|cache) array"
- " -- input must be array but got %s",
- PyString_AsString(PyObject_Str(PyObject_Type(obj)))
- );
- PyErr_SetString(PyExc_TypeError,mess);
+ if ((intent & F2PY_INTENT_INOUT) ||
+ (intent & F2PY_INTENT_INPLACE) ||
+ (intent & F2PY_INTENT_CACHE)) {
+ PyErr_SetString(PyExc_TypeError,
+ "failed to initialize intent(inout|inplace|cache) "
+ "array, input not an array");
return NULL;
}
diff --git a/numpy/f2py/src/fortranobject.h b/numpy/f2py/src/fortranobject.h
index 76a357b5e..689f78c92 100644
--- a/numpy/f2py/src/fortranobject.h
+++ b/numpy/f2py/src/fortranobject.h
@@ -20,6 +20,7 @@ extern "C" {
#define PyString_GET_SIZE PyBytes_GET_SIZE
#define PyString_AS_STRING PyBytes_AS_STRING
#define PyString_FromString PyBytes_FromString
+#define PyUString_FromStringAndSize PyUnicode_FromStringAndSize
#define PyString_ConcatAndDel PyBytes_ConcatAndDel
#define PyString_AsString PyBytes_AsString
@@ -29,6 +30,10 @@ extern "C" {
#define PyInt_AsLong PyLong_AsLong
#define PyNumber_Int PyNumber_Long
+
+#else
+
+#define PyUString_FromStringAndSize PyString_FromStringAndSize
#endif
diff --git a/numpy/f2py/tests/test_array_from_pyobj.py b/numpy/f2py/tests/test_array_from_pyobj.py
index 09d613293..de954c374 100644
--- a/numpy/f2py/tests/test_array_from_pyobj.py
+++ b/numpy/f2py/tests/test_array_from_pyobj.py
@@ -4,6 +4,7 @@ import unittest
import os
import sys
import copy
+import platform
import nose
@@ -81,37 +82,45 @@ class Intent(object):
intent = Intent()
-class Type(object):
- _type_names = ['BOOL', 'BYTE', 'UBYTE', 'SHORT', 'USHORT', 'INT', 'UINT',
- 'LONG', 'ULONG', 'LONGLONG', 'ULONGLONG',
- 'FLOAT', 'DOUBLE', 'LONGDOUBLE', 'CFLOAT', 'CDOUBLE',
- 'CLONGDOUBLE']
- _type_cache = {}
-
- _cast_dict = {'BOOL':['BOOL']}
- _cast_dict['BYTE'] = _cast_dict['BOOL'] + ['BYTE']
- _cast_dict['UBYTE'] = _cast_dict['BOOL'] + ['UBYTE']
- _cast_dict['BYTE'] = ['BYTE']
- _cast_dict['UBYTE'] = ['UBYTE']
- _cast_dict['SHORT'] = _cast_dict['BYTE'] + ['UBYTE', 'SHORT']
- _cast_dict['USHORT'] = _cast_dict['UBYTE'] + ['BYTE', 'USHORT']
- _cast_dict['INT'] = _cast_dict['SHORT'] + ['USHORT', 'INT']
- _cast_dict['UINT'] = _cast_dict['USHORT'] + ['SHORT', 'UINT']
-
- _cast_dict['LONG'] = _cast_dict['INT'] + ['LONG']
- _cast_dict['ULONG'] = _cast_dict['UINT'] + ['ULONG']
-
- _cast_dict['LONGLONG'] = _cast_dict['LONG'] + ['LONGLONG']
- _cast_dict['ULONGLONG'] = _cast_dict['ULONG'] + ['ULONGLONG']
-
- _cast_dict['FLOAT'] = _cast_dict['SHORT'] + ['USHORT', 'FLOAT']
- _cast_dict['DOUBLE'] = _cast_dict['INT'] + ['UINT', 'FLOAT', 'DOUBLE']
- _cast_dict['LONGDOUBLE'] = _cast_dict['LONG'] + ['ULONG', 'FLOAT', 'DOUBLE', 'LONGDOUBLE']
-
- _cast_dict['CFLOAT'] = _cast_dict['FLOAT'] + ['CFLOAT']
+_type_names = ['BOOL', 'BYTE', 'UBYTE', 'SHORT', 'USHORT', 'INT', 'UINT',
+ 'LONG', 'ULONG', 'LONGLONG', 'ULONGLONG',
+ 'FLOAT', 'DOUBLE', 'CFLOAT']
+
+_cast_dict = {'BOOL':['BOOL']}
+_cast_dict['BYTE'] = _cast_dict['BOOL'] + ['BYTE']
+_cast_dict['UBYTE'] = _cast_dict['BOOL'] + ['UBYTE']
+_cast_dict['BYTE'] = ['BYTE']
+_cast_dict['UBYTE'] = ['UBYTE']
+_cast_dict['SHORT'] = _cast_dict['BYTE'] + ['UBYTE', 'SHORT']
+_cast_dict['USHORT'] = _cast_dict['UBYTE'] + ['BYTE', 'USHORT']
+_cast_dict['INT'] = _cast_dict['SHORT'] + ['USHORT', 'INT']
+_cast_dict['UINT'] = _cast_dict['USHORT'] + ['SHORT', 'UINT']
+
+_cast_dict['LONG'] = _cast_dict['INT'] + ['LONG']
+_cast_dict['ULONG'] = _cast_dict['UINT'] + ['ULONG']
+
+_cast_dict['LONGLONG'] = _cast_dict['LONG'] + ['LONGLONG']
+_cast_dict['ULONGLONG'] = _cast_dict['ULONG'] + ['ULONGLONG']
+
+_cast_dict['FLOAT'] = _cast_dict['SHORT'] + ['USHORT', 'FLOAT']
+_cast_dict['DOUBLE'] = _cast_dict['INT'] + ['UINT', 'FLOAT', 'DOUBLE']
+
+_cast_dict['CFLOAT'] = _cast_dict['FLOAT'] + ['CFLOAT']
+
+# (debian) sparc system malloc does not provide the alignment required by
+# 16 byte long double types this means the inout intent cannot be satisfied and
+# several tests fail as the alignment flag can be randomly true or fals
+# when numpy gains an aligned allocator the tests could be enabled again
+if 'sparc' not in platform.platform().lower():
+ _type_names.extend(['LONGDOUBLE', 'CDOUBLE', 'CLONGDOUBLE'])
+ _cast_dict['LONGDOUBLE'] = _cast_dict['LONG'] + \
+ ['ULONG', 'FLOAT', 'DOUBLE', 'LONGDOUBLE']
+ _cast_dict['CLONGDOUBLE'] = _cast_dict['LONGDOUBLE'] + \
+ ['CFLOAT', 'CDOUBLE', 'CLONGDOUBLE']
_cast_dict['CDOUBLE'] = _cast_dict['DOUBLE'] + ['CFLOAT', 'CDOUBLE']
- _cast_dict['CLONGDOUBLE'] = _cast_dict['LONGDOUBLE'] + ['CFLOAT', 'CDOUBLE', 'CLONGDOUBLE']
+class Type(object):
+ _type_cache = {}
def __new__(cls, name):
if isinstance(name, dtype):
@@ -138,15 +147,15 @@ class Type(object):
self.dtypechar = typeinfo[self.NAME][0]
def cast_types(self):
- return [self.__class__(_m) for _m in self._cast_dict[self.NAME]]
+ return [self.__class__(_m) for _m in _cast_dict[self.NAME]]
def all_types(self):
- return [self.__class__(_m) for _m in self._type_names]
+ return [self.__class__(_m) for _m in _type_names]
def smaller_types(self):
bits = typeinfo[self.NAME][3]
types = []
- for name in self._type_names:
+ for name in _type_names:
if typeinfo[name][3]<bits:
types.append(Type(name))
return types
@@ -154,7 +163,7 @@ class Type(object):
def equal_types(self):
bits = typeinfo[self.NAME][3]
types = []
- for name in self._type_names:
+ for name in _type_names:
if name==self.NAME: continue
if typeinfo[name][3]==bits:
types.append(Type(name))
@@ -163,7 +172,7 @@ class Type(object):
def larger_types(self):
bits = typeinfo[self.NAME][3]
types = []
- for name in self._type_names:
+ for name in _type_names:
if typeinfo[name][3]>bits:
types.append(Type(name))
return types
@@ -227,7 +236,8 @@ class Array(object):
assert_(self.arr_attr[2]==self.pyarr_attr[2]) # dimensions
if self.arr_attr[1]<=1:
assert_(self.arr_attr[3]==self.pyarr_attr[3],\
- repr((self.arr_attr[3], self.pyarr_attr[3], self.arr.tostring(), self.pyarr.tostring()))) # strides
+ repr((self.arr_attr[3], self.pyarr_attr[3],
+ self.arr.tobytes(), self.pyarr.tobytes()))) # strides
assert_(self.arr_attr[5][-2:]==self.pyarr_attr[5][-2:],\
repr((self.arr_attr[5], self.pyarr_attr[5]))) # descr
assert_(self.arr_attr[6]==self.pyarr_attr[6],\
@@ -531,7 +541,7 @@ class _test_shared_memory:
assert_(obj.dtype.type is self.type.dtype) # obj type is changed inplace!
-for t in Type._type_names:
+for t in _type_names:
exec('''\
class test_%s_gen(unittest.TestCase,
_test_shared_memory
diff --git a/numpy/f2py/tests/test_callback.py b/numpy/f2py/tests/test_callback.py
index 98a90a28c..16464140f 100644
--- a/numpy/f2py/tests/test_callback.py
+++ b/numpy/f2py/tests/test_callback.py
@@ -34,6 +34,17 @@ cf2py intent(out) a
external fun
call fun(a)
end
+
+ subroutine string_callback(callback, a)
+ external callback
+ double precision callback
+ double precision a
+ character*1 r
+cf2py intent(out) a
+ r = 'r'
+ a = callback(r)
+ end
+
"""
@dec.slow
@@ -103,6 +114,19 @@ cf2py intent(out) a
r = t(a.mth)
assert_( r==9, repr(r))
+ def test_string_callback(self):
+
+ def callback(code):
+ if code == 'r':
+ return 0
+ else:
+ return 1
+
+ f = getattr(self.module, 'string_callback')
+ r = f(callback)
+ assert_(r == 0, repr(r))
+
+
if __name__ == "__main__":
import nose
nose.runmodule()