Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/__init__.pyi +42 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/cb_rules.py +644 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/cfuncs.py +1536 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/common_rules.py +146 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/f2py2e.py +768 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/f90mod_rules.py +264 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/setup.cfg +3 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/accesstype.f90 +13 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/data_common.f +8 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/data_multiplier.f +5 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/data_stmts.f90 +20 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh15035.f +16 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh17859.f +12 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh22648.pyf +7 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23533.f +5 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23598.f90 +4 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23879.f90 +20 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh2848.f90 +13 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/operators.f90 +49 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/privatemod.f90 +11 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/pubprivmod.f90 +10 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/parameter/constant_both.f90 +57 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/parameter/constant_compound.f90 +15 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/parameter/constant_integer.f90 +22 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/parameter/constant_non_compound.f90 +23 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/parameter/constant_real.f90 +23 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/return_character/foo77.f +45 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/return_character/foo90.f90 +48 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_abstract_interface.py +25 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_array_from_pyobj.py +686 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_assumed_shape.py +49 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_block_docstring.py +17 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_callback.py +243 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_character.py +636 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_common.py +27 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_crackfortran.py +350 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_data.py +70 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_docs.py +55 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_f2cmap.py +15 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_kind.py +47 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_parameter.py +112 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_quoted_character.py +16 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_regression.py +77 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_return_character.py +45 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_return_complex.py +65 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_return_integer.py +53 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_return_logical.py +64 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_return_real.py +107 -0
- llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_semicolon_split.py +74 -0
llmeval-env/lib/python3.10/site-packages/numpy/f2py/__init__.pyi
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
from collections.abc import Iterable
|
| 4 |
+
from typing import Literal as L, Any, overload, TypedDict
|
| 5 |
+
|
| 6 |
+
from numpy._pytesttester import PytestTester
|
| 7 |
+
|
| 8 |
+
class _F2PyDictBase(TypedDict):
|
| 9 |
+
csrc: list[str]
|
| 10 |
+
h: list[str]
|
| 11 |
+
|
| 12 |
+
class _F2PyDict(_F2PyDictBase, total=False):
|
| 13 |
+
fsrc: list[str]
|
| 14 |
+
ltx: list[str]
|
| 15 |
+
|
| 16 |
+
__all__: list[str]
|
| 17 |
+
test: PytestTester
|
| 18 |
+
|
| 19 |
+
def run_main(comline_list: Iterable[str]) -> dict[str, _F2PyDict]: ...
|
| 20 |
+
|
| 21 |
+
@overload
|
| 22 |
+
def compile( # type: ignore[misc]
|
| 23 |
+
source: str | bytes,
|
| 24 |
+
modulename: str = ...,
|
| 25 |
+
extra_args: str | list[str] = ...,
|
| 26 |
+
verbose: bool = ...,
|
| 27 |
+
source_fn: None | str | bytes | os.PathLike[Any] = ...,
|
| 28 |
+
extension: L[".f", ".f90"] = ...,
|
| 29 |
+
full_output: L[False] = ...,
|
| 30 |
+
) -> int: ...
|
| 31 |
+
@overload
|
| 32 |
+
def compile(
|
| 33 |
+
source: str | bytes,
|
| 34 |
+
modulename: str = ...,
|
| 35 |
+
extra_args: str | list[str] = ...,
|
| 36 |
+
verbose: bool = ...,
|
| 37 |
+
source_fn: None | str | bytes | os.PathLike[Any] = ...,
|
| 38 |
+
extension: L[".f", ".f90"] = ...,
|
| 39 |
+
full_output: L[True] = ...,
|
| 40 |
+
) -> subprocess.CompletedProcess[bytes]: ...
|
| 41 |
+
|
| 42 |
+
def get_include() -> str: ...
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/cb_rules.py
ADDED
|
@@ -0,0 +1,644 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Build call-back mechanism for f2py2e.
|
| 3 |
+
|
| 4 |
+
Copyright 1999 -- 2011 Pearu Peterson all rights reserved.
|
| 5 |
+
Copyright 2011 -- present NumPy Developers.
|
| 6 |
+
Permission to use, modify, and distribute this software is given under the
|
| 7 |
+
terms of the NumPy License.
|
| 8 |
+
|
| 9 |
+
NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
| 10 |
+
"""
|
| 11 |
+
from . import __version__
|
| 12 |
+
from .auxfuncs import (
|
| 13 |
+
applyrules, debugcapi, dictappend, errmess, getargs, hasnote, isarray,
|
| 14 |
+
iscomplex, iscomplexarray, iscomplexfunction, isfunction, isintent_c,
|
| 15 |
+
isintent_hide, isintent_in, isintent_inout, isintent_nothide,
|
| 16 |
+
isintent_out, isoptional, isrequired, isscalar, isstring,
|
| 17 |
+
isstringfunction, issubroutine, l_and, l_not, l_or, outmess, replace,
|
| 18 |
+
stripcomma, throw_error
|
| 19 |
+
)
|
| 20 |
+
from . import cfuncs
|
| 21 |
+
|
| 22 |
+
f2py_version = __version__.version
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
################## Rules for callback function ##############
|
| 26 |
+
|
| 27 |
+
cb_routine_rules = {
|
| 28 |
+
'cbtypedefs': 'typedef #rctype#(*#name#_typedef)(#optargs_td##args_td##strarglens_td##noargs#);',
|
| 29 |
+
'body': """
|
| 30 |
+
#begintitle#
|
| 31 |
+
typedef struct {
|
| 32 |
+
PyObject *capi;
|
| 33 |
+
PyTupleObject *args_capi;
|
| 34 |
+
int nofargs;
|
| 35 |
+
jmp_buf jmpbuf;
|
| 36 |
+
} #name#_t;
|
| 37 |
+
|
| 38 |
+
#if defined(F2PY_THREAD_LOCAL_DECL) && !defined(F2PY_USE_PYTHON_TLS)
|
| 39 |
+
|
| 40 |
+
static F2PY_THREAD_LOCAL_DECL #name#_t *_active_#name# = NULL;
|
| 41 |
+
|
| 42 |
+
static #name#_t *swap_active_#name#(#name#_t *ptr) {
|
| 43 |
+
#name#_t *prev = _active_#name#;
|
| 44 |
+
_active_#name# = ptr;
|
| 45 |
+
return prev;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
static #name#_t *get_active_#name#(void) {
|
| 49 |
+
return _active_#name#;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
#else
|
| 53 |
+
|
| 54 |
+
static #name#_t *swap_active_#name#(#name#_t *ptr) {
|
| 55 |
+
char *key = "__f2py_cb_#name#";
|
| 56 |
+
return (#name#_t *)F2PySwapThreadLocalCallbackPtr(key, ptr);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
static #name#_t *get_active_#name#(void) {
|
| 60 |
+
char *key = "__f2py_cb_#name#";
|
| 61 |
+
return (#name#_t *)F2PyGetThreadLocalCallbackPtr(key);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
#endif
|
| 65 |
+
|
| 66 |
+
/*typedef #rctype#(*#name#_typedef)(#optargs_td##args_td##strarglens_td##noargs#);*/
|
| 67 |
+
#static# #rctype# #callbackname# (#optargs##args##strarglens##noargs#) {
|
| 68 |
+
#name#_t cb_local = { NULL, NULL, 0 };
|
| 69 |
+
#name#_t *cb = NULL;
|
| 70 |
+
PyTupleObject *capi_arglist = NULL;
|
| 71 |
+
PyObject *capi_return = NULL;
|
| 72 |
+
PyObject *capi_tmp = NULL;
|
| 73 |
+
PyObject *capi_arglist_list = NULL;
|
| 74 |
+
int capi_j,capi_i = 0;
|
| 75 |
+
int capi_longjmp_ok = 1;
|
| 76 |
+
#decl#
|
| 77 |
+
#ifdef F2PY_REPORT_ATEXIT
|
| 78 |
+
f2py_cb_start_clock();
|
| 79 |
+
#endif
|
| 80 |
+
cb = get_active_#name#();
|
| 81 |
+
if (cb == NULL) {
|
| 82 |
+
capi_longjmp_ok = 0;
|
| 83 |
+
cb = &cb_local;
|
| 84 |
+
}
|
| 85 |
+
capi_arglist = cb->args_capi;
|
| 86 |
+
CFUNCSMESS(\"cb:Call-back function #name# (maxnofargs=#maxnofargs#(-#nofoptargs#))\\n\");
|
| 87 |
+
CFUNCSMESSPY(\"cb:#name#_capi=\",cb->capi);
|
| 88 |
+
if (cb->capi==NULL) {
|
| 89 |
+
capi_longjmp_ok = 0;
|
| 90 |
+
cb->capi = PyObject_GetAttrString(#modulename#_module,\"#argname#\");
|
| 91 |
+
CFUNCSMESSPY(\"cb:#name#_capi=\",cb->capi);
|
| 92 |
+
}
|
| 93 |
+
if (cb->capi==NULL) {
|
| 94 |
+
PyErr_SetString(#modulename#_error,\"cb: Callback #argname# not defined (as an argument or module #modulename# attribute).\\n\");
|
| 95 |
+
goto capi_fail;
|
| 96 |
+
}
|
| 97 |
+
if (F2PyCapsule_Check(cb->capi)) {
|
| 98 |
+
#name#_typedef #name#_cptr;
|
| 99 |
+
#name#_cptr = F2PyCapsule_AsVoidPtr(cb->capi);
|
| 100 |
+
#returncptr#(*#name#_cptr)(#optargs_nm##args_nm##strarglens_nm#);
|
| 101 |
+
#return#
|
| 102 |
+
}
|
| 103 |
+
if (capi_arglist==NULL) {
|
| 104 |
+
capi_longjmp_ok = 0;
|
| 105 |
+
capi_tmp = PyObject_GetAttrString(#modulename#_module,\"#argname#_extra_args\");
|
| 106 |
+
if (capi_tmp) {
|
| 107 |
+
capi_arglist = (PyTupleObject *)PySequence_Tuple(capi_tmp);
|
| 108 |
+
Py_DECREF(capi_tmp);
|
| 109 |
+
if (capi_arglist==NULL) {
|
| 110 |
+
PyErr_SetString(#modulename#_error,\"Failed to convert #modulename#.#argname#_extra_args to tuple.\\n\");
|
| 111 |
+
goto capi_fail;
|
| 112 |
+
}
|
| 113 |
+
} else {
|
| 114 |
+
PyErr_Clear();
|
| 115 |
+
capi_arglist = (PyTupleObject *)Py_BuildValue(\"()\");
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
if (capi_arglist == NULL) {
|
| 119 |
+
PyErr_SetString(#modulename#_error,\"Callback #argname# argument list is not set.\\n\");
|
| 120 |
+
goto capi_fail;
|
| 121 |
+
}
|
| 122 |
+
#setdims#
|
| 123 |
+
#ifdef PYPY_VERSION
|
| 124 |
+
#define CAPI_ARGLIST_SETITEM(idx, value) PyList_SetItem((PyObject *)capi_arglist_list, idx, value)
|
| 125 |
+
capi_arglist_list = PySequence_List(capi_arglist);
|
| 126 |
+
if (capi_arglist_list == NULL) goto capi_fail;
|
| 127 |
+
#else
|
| 128 |
+
#define CAPI_ARGLIST_SETITEM(idx, value) PyTuple_SetItem((PyObject *)capi_arglist, idx, value)
|
| 129 |
+
#endif
|
| 130 |
+
#pyobjfrom#
|
| 131 |
+
#undef CAPI_ARGLIST_SETITEM
|
| 132 |
+
#ifdef PYPY_VERSION
|
| 133 |
+
CFUNCSMESSPY(\"cb:capi_arglist=\",capi_arglist_list);
|
| 134 |
+
#else
|
| 135 |
+
CFUNCSMESSPY(\"cb:capi_arglist=\",capi_arglist);
|
| 136 |
+
#endif
|
| 137 |
+
CFUNCSMESS(\"cb:Call-back calling Python function #argname#.\\n\");
|
| 138 |
+
#ifdef F2PY_REPORT_ATEXIT
|
| 139 |
+
f2py_cb_start_call_clock();
|
| 140 |
+
#endif
|
| 141 |
+
#ifdef PYPY_VERSION
|
| 142 |
+
capi_return = PyObject_CallObject(cb->capi,(PyObject *)capi_arglist_list);
|
| 143 |
+
Py_DECREF(capi_arglist_list);
|
| 144 |
+
capi_arglist_list = NULL;
|
| 145 |
+
#else
|
| 146 |
+
capi_return = PyObject_CallObject(cb->capi,(PyObject *)capi_arglist);
|
| 147 |
+
#endif
|
| 148 |
+
#ifdef F2PY_REPORT_ATEXIT
|
| 149 |
+
f2py_cb_stop_call_clock();
|
| 150 |
+
#endif
|
| 151 |
+
CFUNCSMESSPY(\"cb:capi_return=\",capi_return);
|
| 152 |
+
if (capi_return == NULL) {
|
| 153 |
+
fprintf(stderr,\"capi_return is NULL\\n\");
|
| 154 |
+
goto capi_fail;
|
| 155 |
+
}
|
| 156 |
+
if (capi_return == Py_None) {
|
| 157 |
+
Py_DECREF(capi_return);
|
| 158 |
+
capi_return = Py_BuildValue(\"()\");
|
| 159 |
+
}
|
| 160 |
+
else if (!PyTuple_Check(capi_return)) {
|
| 161 |
+
capi_return = Py_BuildValue(\"(N)\",capi_return);
|
| 162 |
+
}
|
| 163 |
+
capi_j = PyTuple_Size(capi_return);
|
| 164 |
+
capi_i = 0;
|
| 165 |
+
#frompyobj#
|
| 166 |
+
CFUNCSMESS(\"cb:#name#:successful\\n\");
|
| 167 |
+
Py_DECREF(capi_return);
|
| 168 |
+
#ifdef F2PY_REPORT_ATEXIT
|
| 169 |
+
f2py_cb_stop_clock();
|
| 170 |
+
#endif
|
| 171 |
+
goto capi_return_pt;
|
| 172 |
+
capi_fail:
|
| 173 |
+
fprintf(stderr,\"Call-back #name# failed.\\n\");
|
| 174 |
+
Py_XDECREF(capi_return);
|
| 175 |
+
Py_XDECREF(capi_arglist_list);
|
| 176 |
+
if (capi_longjmp_ok) {
|
| 177 |
+
longjmp(cb->jmpbuf,-1);
|
| 178 |
+
}
|
| 179 |
+
capi_return_pt:
|
| 180 |
+
;
|
| 181 |
+
#return#
|
| 182 |
+
}
|
| 183 |
+
#endtitle#
|
| 184 |
+
""",
|
| 185 |
+
'need': ['setjmp.h', 'CFUNCSMESS', 'F2PY_THREAD_LOCAL_DECL'],
|
| 186 |
+
'maxnofargs': '#maxnofargs#',
|
| 187 |
+
'nofoptargs': '#nofoptargs#',
|
| 188 |
+
'docstr': """\
|
| 189 |
+
def #argname#(#docsignature#): return #docreturn#\\n\\
|
| 190 |
+
#docstrsigns#""",
|
| 191 |
+
'latexdocstr': """
|
| 192 |
+
{{}\\verb@def #argname#(#latexdocsignature#): return #docreturn#@{}}
|
| 193 |
+
#routnote#
|
| 194 |
+
|
| 195 |
+
#latexdocstrsigns#""",
|
| 196 |
+
'docstrshort': 'def #argname#(#docsignature#): return #docreturn#'
|
| 197 |
+
}
|
| 198 |
+
cb_rout_rules = [
|
| 199 |
+
{ # Init
|
| 200 |
+
'separatorsfor': {'decl': '\n',
|
| 201 |
+
'args': ',', 'optargs': '', 'pyobjfrom': '\n', 'freemem': '\n',
|
| 202 |
+
'args_td': ',', 'optargs_td': '',
|
| 203 |
+
'args_nm': ',', 'optargs_nm': '',
|
| 204 |
+
'frompyobj': '\n', 'setdims': '\n',
|
| 205 |
+
'docstrsigns': '\\n"\n"',
|
| 206 |
+
'latexdocstrsigns': '\n',
|
| 207 |
+
'latexdocstrreq': '\n', 'latexdocstropt': '\n',
|
| 208 |
+
'latexdocstrout': '\n', 'latexdocstrcbs': '\n',
|
| 209 |
+
},
|
| 210 |
+
'decl': '/*decl*/', 'pyobjfrom': '/*pyobjfrom*/', 'frompyobj': '/*frompyobj*/',
|
| 211 |
+
'args': [], 'optargs': '', 'return': '', 'strarglens': '', 'freemem': '/*freemem*/',
|
| 212 |
+
'args_td': [], 'optargs_td': '', 'strarglens_td': '',
|
| 213 |
+
'args_nm': [], 'optargs_nm': '', 'strarglens_nm': '',
|
| 214 |
+
'noargs': '',
|
| 215 |
+
'setdims': '/*setdims*/',
|
| 216 |
+
'docstrsigns': '', 'latexdocstrsigns': '',
|
| 217 |
+
'docstrreq': ' Required arguments:',
|
| 218 |
+
'docstropt': ' Optional arguments:',
|
| 219 |
+
'docstrout': ' Return objects:',
|
| 220 |
+
'docstrcbs': ' Call-back functions:',
|
| 221 |
+
'docreturn': '', 'docsign': '', 'docsignopt': '',
|
| 222 |
+
'latexdocstrreq': '\\noindent Required arguments:',
|
| 223 |
+
'latexdocstropt': '\\noindent Optional arguments:',
|
| 224 |
+
'latexdocstrout': '\\noindent Return objects:',
|
| 225 |
+
'latexdocstrcbs': '\\noindent Call-back functions:',
|
| 226 |
+
'routnote': {hasnote: '--- #note#', l_not(hasnote): ''},
|
| 227 |
+
}, { # Function
|
| 228 |
+
'decl': ' #ctype# return_value = 0;',
|
| 229 |
+
'frompyobj': [
|
| 230 |
+
{debugcapi: ' CFUNCSMESS("cb:Getting return_value->");'},
|
| 231 |
+
'''\
|
| 232 |
+
if (capi_j>capi_i) {
|
| 233 |
+
GETSCALARFROMPYTUPLE(capi_return,capi_i++,&return_value,#ctype#,
|
| 234 |
+
"#ctype#_from_pyobj failed in converting return_value of"
|
| 235 |
+
" call-back function #name# to C #ctype#\\n");
|
| 236 |
+
} else {
|
| 237 |
+
fprintf(stderr,"Warning: call-back function #name# did not provide"
|
| 238 |
+
" return value (index=%d, type=#ctype#)\\n",capi_i);
|
| 239 |
+
}''',
|
| 240 |
+
{debugcapi:
|
| 241 |
+
' fprintf(stderr,"#showvalueformat#.\\n",return_value);'}
|
| 242 |
+
],
|
| 243 |
+
'need': ['#ctype#_from_pyobj', {debugcapi: 'CFUNCSMESS'}, 'GETSCALARFROMPYTUPLE'],
|
| 244 |
+
'return': ' return return_value;',
|
| 245 |
+
'_check': l_and(isfunction, l_not(isstringfunction), l_not(iscomplexfunction))
|
| 246 |
+
},
|
| 247 |
+
{ # String function
|
| 248 |
+
'pyobjfrom': {debugcapi: ' fprintf(stderr,"debug-capi:cb:#name#:%d:\\n",return_value_len);'},
|
| 249 |
+
'args': '#ctype# return_value,int return_value_len',
|
| 250 |
+
'args_nm': 'return_value,&return_value_len',
|
| 251 |
+
'args_td': '#ctype# ,int',
|
| 252 |
+
'frompyobj': [
|
| 253 |
+
{debugcapi: ' CFUNCSMESS("cb:Getting return_value->\\"");'},
|
| 254 |
+
"""\
|
| 255 |
+
if (capi_j>capi_i) {
|
| 256 |
+
GETSTRFROMPYTUPLE(capi_return,capi_i++,return_value,return_value_len);
|
| 257 |
+
} else {
|
| 258 |
+
fprintf(stderr,"Warning: call-back function #name# did not provide"
|
| 259 |
+
" return value (index=%d, type=#ctype#)\\n",capi_i);
|
| 260 |
+
}""",
|
| 261 |
+
{debugcapi:
|
| 262 |
+
' fprintf(stderr,"#showvalueformat#\\".\\n",return_value);'}
|
| 263 |
+
],
|
| 264 |
+
'need': ['#ctype#_from_pyobj', {debugcapi: 'CFUNCSMESS'},
|
| 265 |
+
'string.h', 'GETSTRFROMPYTUPLE'],
|
| 266 |
+
'return': 'return;',
|
| 267 |
+
'_check': isstringfunction
|
| 268 |
+
},
|
| 269 |
+
{ # Complex function
|
| 270 |
+
'optargs': """
|
| 271 |
+
#ifndef F2PY_CB_RETURNCOMPLEX
|
| 272 |
+
#ctype# *return_value
|
| 273 |
+
#endif
|
| 274 |
+
""",
|
| 275 |
+
'optargs_nm': """
|
| 276 |
+
#ifndef F2PY_CB_RETURNCOMPLEX
|
| 277 |
+
return_value
|
| 278 |
+
#endif
|
| 279 |
+
""",
|
| 280 |
+
'optargs_td': """
|
| 281 |
+
#ifndef F2PY_CB_RETURNCOMPLEX
|
| 282 |
+
#ctype# *
|
| 283 |
+
#endif
|
| 284 |
+
""",
|
| 285 |
+
'decl': """
|
| 286 |
+
#ifdef F2PY_CB_RETURNCOMPLEX
|
| 287 |
+
#ctype# return_value = {0, 0};
|
| 288 |
+
#endif
|
| 289 |
+
""",
|
| 290 |
+
'frompyobj': [
|
| 291 |
+
{debugcapi: ' CFUNCSMESS("cb:Getting return_value->");'},
|
| 292 |
+
"""\
|
| 293 |
+
if (capi_j>capi_i) {
|
| 294 |
+
#ifdef F2PY_CB_RETURNCOMPLEX
|
| 295 |
+
GETSCALARFROMPYTUPLE(capi_return,capi_i++,&return_value,#ctype#,
|
| 296 |
+
\"#ctype#_from_pyobj failed in converting return_value of call-back\"
|
| 297 |
+
\" function #name# to C #ctype#\\n\");
|
| 298 |
+
#else
|
| 299 |
+
GETSCALARFROMPYTUPLE(capi_return,capi_i++,return_value,#ctype#,
|
| 300 |
+
\"#ctype#_from_pyobj failed in converting return_value of call-back\"
|
| 301 |
+
\" function #name# to C #ctype#\\n\");
|
| 302 |
+
#endif
|
| 303 |
+
} else {
|
| 304 |
+
fprintf(stderr,
|
| 305 |
+
\"Warning: call-back function #name# did not provide\"
|
| 306 |
+
\" return value (index=%d, type=#ctype#)\\n\",capi_i);
|
| 307 |
+
}""",
|
| 308 |
+
{debugcapi: """\
|
| 309 |
+
#ifdef F2PY_CB_RETURNCOMPLEX
|
| 310 |
+
fprintf(stderr,\"#showvalueformat#.\\n\",(return_value).r,(return_value).i);
|
| 311 |
+
#else
|
| 312 |
+
fprintf(stderr,\"#showvalueformat#.\\n\",(*return_value).r,(*return_value).i);
|
| 313 |
+
#endif
|
| 314 |
+
"""}
|
| 315 |
+
],
|
| 316 |
+
'return': """
|
| 317 |
+
#ifdef F2PY_CB_RETURNCOMPLEX
|
| 318 |
+
return return_value;
|
| 319 |
+
#else
|
| 320 |
+
return;
|
| 321 |
+
#endif
|
| 322 |
+
""",
|
| 323 |
+
'need': ['#ctype#_from_pyobj', {debugcapi: 'CFUNCSMESS'},
|
| 324 |
+
'string.h', 'GETSCALARFROMPYTUPLE', '#ctype#'],
|
| 325 |
+
'_check': iscomplexfunction
|
| 326 |
+
},
|
| 327 |
+
{'docstrout': ' #pydocsignout#',
|
| 328 |
+
'latexdocstrout': ['\\item[]{{}\\verb@#pydocsignout#@{}}',
|
| 329 |
+
{hasnote: '--- #note#'}],
|
| 330 |
+
'docreturn': '#rname#,',
|
| 331 |
+
'_check': isfunction},
|
| 332 |
+
{'_check': issubroutine, 'return': 'return;'}
|
| 333 |
+
]
|
| 334 |
+
|
| 335 |
+
cb_arg_rules = [
|
| 336 |
+
{ # Doc
|
| 337 |
+
'docstropt': {l_and(isoptional, isintent_nothide): ' #pydocsign#'},
|
| 338 |
+
'docstrreq': {l_and(isrequired, isintent_nothide): ' #pydocsign#'},
|
| 339 |
+
'docstrout': {isintent_out: ' #pydocsignout#'},
|
| 340 |
+
'latexdocstropt': {l_and(isoptional, isintent_nothide): ['\\item[]{{}\\verb@#pydocsign#@{}}',
|
| 341 |
+
{hasnote: '--- #note#'}]},
|
| 342 |
+
'latexdocstrreq': {l_and(isrequired, isintent_nothide): ['\\item[]{{}\\verb@#pydocsign#@{}}',
|
| 343 |
+
{hasnote: '--- #note#'}]},
|
| 344 |
+
'latexdocstrout': {isintent_out: ['\\item[]{{}\\verb@#pydocsignout#@{}}',
|
| 345 |
+
{l_and(hasnote, isintent_hide): '--- #note#',
|
| 346 |
+
l_and(hasnote, isintent_nothide): '--- See above.'}]},
|
| 347 |
+
'docsign': {l_and(isrequired, isintent_nothide): '#varname#,'},
|
| 348 |
+
'docsignopt': {l_and(isoptional, isintent_nothide): '#varname#,'},
|
| 349 |
+
'depend': ''
|
| 350 |
+
},
|
| 351 |
+
{
|
| 352 |
+
'args': {
|
| 353 |
+
l_and(isscalar, isintent_c): '#ctype# #varname_i#',
|
| 354 |
+
l_and(isscalar, l_not(isintent_c)): '#ctype# *#varname_i#_cb_capi',
|
| 355 |
+
isarray: '#ctype# *#varname_i#',
|
| 356 |
+
isstring: '#ctype# #varname_i#'
|
| 357 |
+
},
|
| 358 |
+
'args_nm': {
|
| 359 |
+
l_and(isscalar, isintent_c): '#varname_i#',
|
| 360 |
+
l_and(isscalar, l_not(isintent_c)): '#varname_i#_cb_capi',
|
| 361 |
+
isarray: '#varname_i#',
|
| 362 |
+
isstring: '#varname_i#'
|
| 363 |
+
},
|
| 364 |
+
'args_td': {
|
| 365 |
+
l_and(isscalar, isintent_c): '#ctype#',
|
| 366 |
+
l_and(isscalar, l_not(isintent_c)): '#ctype# *',
|
| 367 |
+
isarray: '#ctype# *',
|
| 368 |
+
isstring: '#ctype#'
|
| 369 |
+
},
|
| 370 |
+
'need': {l_or(isscalar, isarray, isstring): '#ctype#'},
|
| 371 |
+
# untested with multiple args
|
| 372 |
+
'strarglens': {isstring: ',int #varname_i#_cb_len'},
|
| 373 |
+
'strarglens_td': {isstring: ',int'}, # untested with multiple args
|
| 374 |
+
# untested with multiple args
|
| 375 |
+
'strarglens_nm': {isstring: ',#varname_i#_cb_len'},
|
| 376 |
+
},
|
| 377 |
+
{ # Scalars
|
| 378 |
+
'decl': {l_not(isintent_c): ' #ctype# #varname_i#=(*#varname_i#_cb_capi);'},
|
| 379 |
+
'error': {l_and(isintent_c, isintent_out,
|
| 380 |
+
throw_error('intent(c,out) is forbidden for callback scalar arguments')):
|
| 381 |
+
''},
|
| 382 |
+
'frompyobj': [{debugcapi: ' CFUNCSMESS("cb:Getting #varname#->");'},
|
| 383 |
+
{isintent_out:
|
| 384 |
+
' if (capi_j>capi_i)\n GETSCALARFROMPYTUPLE(capi_return,capi_i++,#varname_i#_cb_capi,#ctype#,"#ctype#_from_pyobj failed in converting argument #varname# of call-back function #name# to C #ctype#\\n");'},
|
| 385 |
+
{l_and(debugcapi, l_and(l_not(iscomplex), isintent_c)):
|
| 386 |
+
' fprintf(stderr,"#showvalueformat#.\\n",#varname_i#);'},
|
| 387 |
+
{l_and(debugcapi, l_and(l_not(iscomplex), l_not( isintent_c))):
|
| 388 |
+
' fprintf(stderr,"#showvalueformat#.\\n",*#varname_i#_cb_capi);'},
|
| 389 |
+
{l_and(debugcapi, l_and(iscomplex, isintent_c)):
|
| 390 |
+
' fprintf(stderr,"#showvalueformat#.\\n",(#varname_i#).r,(#varname_i#).i);'},
|
| 391 |
+
{l_and(debugcapi, l_and(iscomplex, l_not( isintent_c))):
|
| 392 |
+
' fprintf(stderr,"#showvalueformat#.\\n",(*#varname_i#_cb_capi).r,(*#varname_i#_cb_capi).i);'},
|
| 393 |
+
],
|
| 394 |
+
'need': [{isintent_out: ['#ctype#_from_pyobj', 'GETSCALARFROMPYTUPLE']},
|
| 395 |
+
{debugcapi: 'CFUNCSMESS'}],
|
| 396 |
+
'_check': isscalar
|
| 397 |
+
}, {
|
| 398 |
+
'pyobjfrom': [{isintent_in: """\
|
| 399 |
+
if (cb->nofargs>capi_i)
|
| 400 |
+
if (CAPI_ARGLIST_SETITEM(capi_i++,pyobj_from_#ctype#1(#varname_i#)))
|
| 401 |
+
goto capi_fail;"""},
|
| 402 |
+
{isintent_inout: """\
|
| 403 |
+
if (cb->nofargs>capi_i)
|
| 404 |
+
if (CAPI_ARGLIST_SETITEM(capi_i++,pyarr_from_p_#ctype#1(#varname_i#_cb_capi)))
|
| 405 |
+
goto capi_fail;"""}],
|
| 406 |
+
'need': [{isintent_in: 'pyobj_from_#ctype#1'},
|
| 407 |
+
{isintent_inout: 'pyarr_from_p_#ctype#1'},
|
| 408 |
+
{iscomplex: '#ctype#'}],
|
| 409 |
+
'_check': l_and(isscalar, isintent_nothide),
|
| 410 |
+
'_optional': ''
|
| 411 |
+
}, { # String
|
| 412 |
+
'frompyobj': [{debugcapi: ' CFUNCSMESS("cb:Getting #varname#->\\"");'},
|
| 413 |
+
""" if (capi_j>capi_i)
|
| 414 |
+
GETSTRFROMPYTUPLE(capi_return,capi_i++,#varname_i#,#varname_i#_cb_len);""",
|
| 415 |
+
{debugcapi:
|
| 416 |
+
' fprintf(stderr,"#showvalueformat#\\":%d:.\\n",#varname_i#,#varname_i#_cb_len);'},
|
| 417 |
+
],
|
| 418 |
+
'need': ['#ctype#', 'GETSTRFROMPYTUPLE',
|
| 419 |
+
{debugcapi: 'CFUNCSMESS'}, 'string.h'],
|
| 420 |
+
'_check': l_and(isstring, isintent_out)
|
| 421 |
+
}, {
|
| 422 |
+
'pyobjfrom': [
|
| 423 |
+
{debugcapi:
|
| 424 |
+
(' fprintf(stderr,"debug-capi:cb:#varname#=#showvalueformat#:'
|
| 425 |
+
'%d:\\n",#varname_i#,#varname_i#_cb_len);')},
|
| 426 |
+
{isintent_in: """\
|
| 427 |
+
if (cb->nofargs>capi_i)
|
| 428 |
+
if (CAPI_ARGLIST_SETITEM(capi_i++,pyobj_from_#ctype#1size(#varname_i#,#varname_i#_cb_len)))
|
| 429 |
+
goto capi_fail;"""},
|
| 430 |
+
{isintent_inout: """\
|
| 431 |
+
if (cb->nofargs>capi_i) {
|
| 432 |
+
int #varname_i#_cb_dims[] = {#varname_i#_cb_len};
|
| 433 |
+
if (CAPI_ARGLIST_SETITEM(capi_i++,pyarr_from_p_#ctype#1(#varname_i#,#varname_i#_cb_dims)))
|
| 434 |
+
goto capi_fail;
|
| 435 |
+
}"""}],
|
| 436 |
+
'need': [{isintent_in: 'pyobj_from_#ctype#1size'},
|
| 437 |
+
{isintent_inout: 'pyarr_from_p_#ctype#1'}],
|
| 438 |
+
'_check': l_and(isstring, isintent_nothide),
|
| 439 |
+
'_optional': ''
|
| 440 |
+
},
|
| 441 |
+
# Array ...
|
| 442 |
+
{
|
| 443 |
+
'decl': ' npy_intp #varname_i#_Dims[#rank#] = {#rank*[-1]#};',
|
| 444 |
+
'setdims': ' #cbsetdims#;',
|
| 445 |
+
'_check': isarray,
|
| 446 |
+
'_depend': ''
|
| 447 |
+
},
|
| 448 |
+
{
|
| 449 |
+
'pyobjfrom': [{debugcapi: ' fprintf(stderr,"debug-capi:cb:#varname#\\n");'},
|
| 450 |
+
{isintent_c: """\
|
| 451 |
+
if (cb->nofargs>capi_i) {
|
| 452 |
+
/* tmp_arr will be inserted to capi_arglist_list that will be
|
| 453 |
+
destroyed when leaving callback function wrapper together
|
| 454 |
+
with tmp_arr. */
|
| 455 |
+
PyArrayObject *tmp_arr = (PyArrayObject *)PyArray_New(&PyArray_Type,
|
| 456 |
+
#rank#,#varname_i#_Dims,#atype#,NULL,(char*)#varname_i#,#elsize#,
|
| 457 |
+
NPY_ARRAY_CARRAY,NULL);
|
| 458 |
+
""",
|
| 459 |
+
l_not(isintent_c): """\
|
| 460 |
+
if (cb->nofargs>capi_i) {
|
| 461 |
+
/* tmp_arr will be inserted to capi_arglist_list that will be
|
| 462 |
+
destroyed when leaving callback function wrapper together
|
| 463 |
+
with tmp_arr. */
|
| 464 |
+
PyArrayObject *tmp_arr = (PyArrayObject *)PyArray_New(&PyArray_Type,
|
| 465 |
+
#rank#,#varname_i#_Dims,#atype#,NULL,(char*)#varname_i#,#elsize#,
|
| 466 |
+
NPY_ARRAY_FARRAY,NULL);
|
| 467 |
+
""",
|
| 468 |
+
},
|
| 469 |
+
"""
|
| 470 |
+
if (tmp_arr==NULL)
|
| 471 |
+
goto capi_fail;
|
| 472 |
+
if (CAPI_ARGLIST_SETITEM(capi_i++,(PyObject *)tmp_arr))
|
| 473 |
+
goto capi_fail;
|
| 474 |
+
}"""],
|
| 475 |
+
'_check': l_and(isarray, isintent_nothide, l_or(isintent_in, isintent_inout)),
|
| 476 |
+
'_optional': '',
|
| 477 |
+
}, {
|
| 478 |
+
'frompyobj': [{debugcapi: ' CFUNCSMESS("cb:Getting #varname#->");'},
|
| 479 |
+
""" if (capi_j>capi_i) {
|
| 480 |
+
PyArrayObject *rv_cb_arr = NULL;
|
| 481 |
+
if ((capi_tmp = PyTuple_GetItem(capi_return,capi_i++))==NULL) goto capi_fail;
|
| 482 |
+
rv_cb_arr = array_from_pyobj(#atype#,#varname_i#_Dims,#rank#,F2PY_INTENT_IN""",
|
| 483 |
+
{isintent_c: '|F2PY_INTENT_C'},
|
| 484 |
+
""",capi_tmp);
|
| 485 |
+
if (rv_cb_arr == NULL) {
|
| 486 |
+
fprintf(stderr,\"rv_cb_arr is NULL\\n\");
|
| 487 |
+
goto capi_fail;
|
| 488 |
+
}
|
| 489 |
+
MEMCOPY(#varname_i#,PyArray_DATA(rv_cb_arr),PyArray_NBYTES(rv_cb_arr));
|
| 490 |
+
if (capi_tmp != (PyObject *)rv_cb_arr) {
|
| 491 |
+
Py_DECREF(rv_cb_arr);
|
| 492 |
+
}
|
| 493 |
+
}""",
|
| 494 |
+
{debugcapi: ' fprintf(stderr,"<-.\\n");'},
|
| 495 |
+
],
|
| 496 |
+
'need': ['MEMCOPY', {iscomplexarray: '#ctype#'}],
|
| 497 |
+
'_check': l_and(isarray, isintent_out)
|
| 498 |
+
}, {
|
| 499 |
+
'docreturn': '#varname#,',
|
| 500 |
+
'_check': isintent_out
|
| 501 |
+
}
|
| 502 |
+
]
|
| 503 |
+
|
| 504 |
+
################## Build call-back module #############
|
| 505 |
+
cb_map = {}
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
def buildcallbacks(m):
|
| 509 |
+
cb_map[m['name']] = []
|
| 510 |
+
for bi in m['body']:
|
| 511 |
+
if bi['block'] == 'interface':
|
| 512 |
+
for b in bi['body']:
|
| 513 |
+
if b:
|
| 514 |
+
buildcallback(b, m['name'])
|
| 515 |
+
else:
|
| 516 |
+
errmess('warning: empty body for %s\n' % (m['name']))
|
| 517 |
+
|
| 518 |
+
|
| 519 |
+
def buildcallback(rout, um):
|
| 520 |
+
from . import capi_maps
|
| 521 |
+
|
| 522 |
+
outmess(' Constructing call-back function "cb_%s_in_%s"\n' %
|
| 523 |
+
(rout['name'], um))
|
| 524 |
+
args, depargs = getargs(rout)
|
| 525 |
+
capi_maps.depargs = depargs
|
| 526 |
+
var = rout['vars']
|
| 527 |
+
vrd = capi_maps.cb_routsign2map(rout, um)
|
| 528 |
+
rd = dictappend({}, vrd)
|
| 529 |
+
cb_map[um].append([rout['name'], rd['name']])
|
| 530 |
+
for r in cb_rout_rules:
|
| 531 |
+
if ('_check' in r and r['_check'](rout)) or ('_check' not in r):
|
| 532 |
+
ar = applyrules(r, vrd, rout)
|
| 533 |
+
rd = dictappend(rd, ar)
|
| 534 |
+
savevrd = {}
|
| 535 |
+
for i, a in enumerate(args):
|
| 536 |
+
vrd = capi_maps.cb_sign2map(a, var[a], index=i)
|
| 537 |
+
savevrd[a] = vrd
|
| 538 |
+
for r in cb_arg_rules:
|
| 539 |
+
if '_depend' in r:
|
| 540 |
+
continue
|
| 541 |
+
if '_optional' in r and isoptional(var[a]):
|
| 542 |
+
continue
|
| 543 |
+
if ('_check' in r and r['_check'](var[a])) or ('_check' not in r):
|
| 544 |
+
ar = applyrules(r, vrd, var[a])
|
| 545 |
+
rd = dictappend(rd, ar)
|
| 546 |
+
if '_break' in r:
|
| 547 |
+
break
|
| 548 |
+
for a in args:
|
| 549 |
+
vrd = savevrd[a]
|
| 550 |
+
for r in cb_arg_rules:
|
| 551 |
+
if '_depend' in r:
|
| 552 |
+
continue
|
| 553 |
+
if ('_optional' not in r) or ('_optional' in r and isrequired(var[a])):
|
| 554 |
+
continue
|
| 555 |
+
if ('_check' in r and r['_check'](var[a])) or ('_check' not in r):
|
| 556 |
+
ar = applyrules(r, vrd, var[a])
|
| 557 |
+
rd = dictappend(rd, ar)
|
| 558 |
+
if '_break' in r:
|
| 559 |
+
break
|
| 560 |
+
for a in depargs:
|
| 561 |
+
vrd = savevrd[a]
|
| 562 |
+
for r in cb_arg_rules:
|
| 563 |
+
if '_depend' not in r:
|
| 564 |
+
continue
|
| 565 |
+
if '_optional' in r:
|
| 566 |
+
continue
|
| 567 |
+
if ('_check' in r and r['_check'](var[a])) or ('_check' not in r):
|
| 568 |
+
ar = applyrules(r, vrd, var[a])
|
| 569 |
+
rd = dictappend(rd, ar)
|
| 570 |
+
if '_break' in r:
|
| 571 |
+
break
|
| 572 |
+
if 'args' in rd and 'optargs' in rd:
|
| 573 |
+
if isinstance(rd['optargs'], list):
|
| 574 |
+
rd['optargs'] = rd['optargs'] + ["""
|
| 575 |
+
#ifndef F2PY_CB_RETURNCOMPLEX
|
| 576 |
+
,
|
| 577 |
+
#endif
|
| 578 |
+
"""]
|
| 579 |
+
rd['optargs_nm'] = rd['optargs_nm'] + ["""
|
| 580 |
+
#ifndef F2PY_CB_RETURNCOMPLEX
|
| 581 |
+
,
|
| 582 |
+
#endif
|
| 583 |
+
"""]
|
| 584 |
+
rd['optargs_td'] = rd['optargs_td'] + ["""
|
| 585 |
+
#ifndef F2PY_CB_RETURNCOMPLEX
|
| 586 |
+
,
|
| 587 |
+
#endif
|
| 588 |
+
"""]
|
| 589 |
+
if isinstance(rd['docreturn'], list):
|
| 590 |
+
rd['docreturn'] = stripcomma(
|
| 591 |
+
replace('#docreturn#', {'docreturn': rd['docreturn']}))
|
| 592 |
+
optargs = stripcomma(replace('#docsignopt#',
|
| 593 |
+
{'docsignopt': rd['docsignopt']}
|
| 594 |
+
))
|
| 595 |
+
if optargs == '':
|
| 596 |
+
rd['docsignature'] = stripcomma(
|
| 597 |
+
replace('#docsign#', {'docsign': rd['docsign']}))
|
| 598 |
+
else:
|
| 599 |
+
rd['docsignature'] = replace('#docsign#[#docsignopt#]',
|
| 600 |
+
{'docsign': rd['docsign'],
|
| 601 |
+
'docsignopt': optargs,
|
| 602 |
+
})
|
| 603 |
+
rd['latexdocsignature'] = rd['docsignature'].replace('_', '\\_')
|
| 604 |
+
rd['latexdocsignature'] = rd['latexdocsignature'].replace(',', ', ')
|
| 605 |
+
rd['docstrsigns'] = []
|
| 606 |
+
rd['latexdocstrsigns'] = []
|
| 607 |
+
for k in ['docstrreq', 'docstropt', 'docstrout', 'docstrcbs']:
|
| 608 |
+
if k in rd and isinstance(rd[k], list):
|
| 609 |
+
rd['docstrsigns'] = rd['docstrsigns'] + rd[k]
|
| 610 |
+
k = 'latex' + k
|
| 611 |
+
if k in rd and isinstance(rd[k], list):
|
| 612 |
+
rd['latexdocstrsigns'] = rd['latexdocstrsigns'] + rd[k][0:1] +\
|
| 613 |
+
['\\begin{description}'] + rd[k][1:] +\
|
| 614 |
+
['\\end{description}']
|
| 615 |
+
if 'args' not in rd:
|
| 616 |
+
rd['args'] = ''
|
| 617 |
+
rd['args_td'] = ''
|
| 618 |
+
rd['args_nm'] = ''
|
| 619 |
+
if not (rd.get('args') or rd.get('optargs') or rd.get('strarglens')):
|
| 620 |
+
rd['noargs'] = 'void'
|
| 621 |
+
|
| 622 |
+
ar = applyrules(cb_routine_rules, rd)
|
| 623 |
+
cfuncs.callbacks[rd['name']] = ar['body']
|
| 624 |
+
if isinstance(ar['need'], str):
|
| 625 |
+
ar['need'] = [ar['need']]
|
| 626 |
+
|
| 627 |
+
if 'need' in rd:
|
| 628 |
+
for t in cfuncs.typedefs.keys():
|
| 629 |
+
if t in rd['need']:
|
| 630 |
+
ar['need'].append(t)
|
| 631 |
+
|
| 632 |
+
cfuncs.typedefs_generated[rd['name'] + '_typedef'] = ar['cbtypedefs']
|
| 633 |
+
ar['need'].append(rd['name'] + '_typedef')
|
| 634 |
+
cfuncs.needs[rd['name']] = ar['need']
|
| 635 |
+
|
| 636 |
+
capi_maps.lcb2_map[rd['name']] = {'maxnofargs': ar['maxnofargs'],
|
| 637 |
+
'nofoptargs': ar['nofoptargs'],
|
| 638 |
+
'docstr': ar['docstr'],
|
| 639 |
+
'latexdocstr': ar['latexdocstr'],
|
| 640 |
+
'argname': rd['argname']
|
| 641 |
+
}
|
| 642 |
+
outmess(' %s\n' % (ar['docstrshort']))
|
| 643 |
+
return
|
| 644 |
+
################## Build call-back function #############
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/cfuncs.py
ADDED
|
@@ -0,0 +1,1536 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
C declarations, CPP macros, and C functions for f2py2e.
|
| 4 |
+
Only required declarations/macros/functions will be used.
|
| 5 |
+
|
| 6 |
+
Copyright 1999 -- 2011 Pearu Peterson all rights reserved.
|
| 7 |
+
Copyright 2011 -- present NumPy Developers.
|
| 8 |
+
Permission to use, modify, and distribute this software is given under the
|
| 9 |
+
terms of the NumPy License.
|
| 10 |
+
|
| 11 |
+
NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
| 12 |
+
"""
|
| 13 |
+
import sys
|
| 14 |
+
import copy
|
| 15 |
+
|
| 16 |
+
from . import __version__
|
| 17 |
+
|
| 18 |
+
f2py_version = __version__.version
|
| 19 |
+
errmess = sys.stderr.write
|
| 20 |
+
|
| 21 |
+
##################### Definitions ##################
|
| 22 |
+
|
| 23 |
+
outneeds = {'includes0': [], 'includes': [], 'typedefs': [], 'typedefs_generated': [],
|
| 24 |
+
'userincludes': [],
|
| 25 |
+
'cppmacros': [], 'cfuncs': [], 'callbacks': [], 'f90modhooks': [],
|
| 26 |
+
'commonhooks': []}
|
| 27 |
+
needs = {}
|
| 28 |
+
includes0 = {'includes0': '/*need_includes0*/'}
|
| 29 |
+
includes = {'includes': '/*need_includes*/'}
|
| 30 |
+
userincludes = {'userincludes': '/*need_userincludes*/'}
|
| 31 |
+
typedefs = {'typedefs': '/*need_typedefs*/'}
|
| 32 |
+
typedefs_generated = {'typedefs_generated': '/*need_typedefs_generated*/'}
|
| 33 |
+
cppmacros = {'cppmacros': '/*need_cppmacros*/'}
|
| 34 |
+
cfuncs = {'cfuncs': '/*need_cfuncs*/'}
|
| 35 |
+
callbacks = {'callbacks': '/*need_callbacks*/'}
|
| 36 |
+
f90modhooks = {'f90modhooks': '/*need_f90modhooks*/',
|
| 37 |
+
'initf90modhooksstatic': '/*initf90modhooksstatic*/',
|
| 38 |
+
'initf90modhooksdynamic': '/*initf90modhooksdynamic*/',
|
| 39 |
+
}
|
| 40 |
+
commonhooks = {'commonhooks': '/*need_commonhooks*/',
|
| 41 |
+
'initcommonhooks': '/*need_initcommonhooks*/',
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
############ Includes ###################
|
| 45 |
+
|
| 46 |
+
includes0['math.h'] = '#include <math.h>'
|
| 47 |
+
includes0['string.h'] = '#include <string.h>'
|
| 48 |
+
includes0['setjmp.h'] = '#include <setjmp.h>'
|
| 49 |
+
|
| 50 |
+
includes['arrayobject.h'] = '''#define PY_ARRAY_UNIQUE_SYMBOL PyArray_API
|
| 51 |
+
#include "arrayobject.h"'''
|
| 52 |
+
includes['npy_math.h'] = '#include "numpy/npy_math.h"'
|
| 53 |
+
|
| 54 |
+
includes['arrayobject.h'] = '#include "fortranobject.h"'
|
| 55 |
+
includes['stdarg.h'] = '#include <stdarg.h>'
|
| 56 |
+
|
| 57 |
+
############# Type definitions ###############
|
| 58 |
+
|
| 59 |
+
typedefs['unsigned_char'] = 'typedef unsigned char unsigned_char;'
|
| 60 |
+
typedefs['unsigned_short'] = 'typedef unsigned short unsigned_short;'
|
| 61 |
+
typedefs['unsigned_long'] = 'typedef unsigned long unsigned_long;'
|
| 62 |
+
typedefs['signed_char'] = 'typedef signed char signed_char;'
|
| 63 |
+
typedefs['long_long'] = """
|
| 64 |
+
#if defined(NPY_OS_WIN32)
|
| 65 |
+
typedef __int64 long_long;
|
| 66 |
+
#else
|
| 67 |
+
typedef long long long_long;
|
| 68 |
+
typedef unsigned long long unsigned_long_long;
|
| 69 |
+
#endif
|
| 70 |
+
"""
|
| 71 |
+
typedefs['unsigned_long_long'] = """
|
| 72 |
+
#if defined(NPY_OS_WIN32)
|
| 73 |
+
typedef __uint64 long_long;
|
| 74 |
+
#else
|
| 75 |
+
typedef unsigned long long unsigned_long_long;
|
| 76 |
+
#endif
|
| 77 |
+
"""
|
| 78 |
+
typedefs['long_double'] = """
|
| 79 |
+
#ifndef _LONG_DOUBLE
|
| 80 |
+
typedef long double long_double;
|
| 81 |
+
#endif
|
| 82 |
+
"""
|
| 83 |
+
typedefs[
|
| 84 |
+
'complex_long_double'] = 'typedef struct {long double r,i;} complex_long_double;'
|
| 85 |
+
typedefs['complex_float'] = 'typedef struct {float r,i;} complex_float;'
|
| 86 |
+
typedefs['complex_double'] = 'typedef struct {double r,i;} complex_double;'
|
| 87 |
+
typedefs['string'] = """typedef char * string;"""
|
| 88 |
+
typedefs['character'] = """typedef char character;"""
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
############### CPP macros ####################
|
| 92 |
+
cppmacros['CFUNCSMESS'] = """
|
| 93 |
+
#ifdef DEBUGCFUNCS
|
| 94 |
+
#define CFUNCSMESS(mess) fprintf(stderr,\"debug-capi:\"mess);
|
| 95 |
+
#define CFUNCSMESSPY(mess,obj) CFUNCSMESS(mess) \\
|
| 96 |
+
PyObject_Print((PyObject *)obj,stderr,Py_PRINT_RAW);\\
|
| 97 |
+
fprintf(stderr,\"\\n\");
|
| 98 |
+
#else
|
| 99 |
+
#define CFUNCSMESS(mess)
|
| 100 |
+
#define CFUNCSMESSPY(mess,obj)
|
| 101 |
+
#endif
|
| 102 |
+
"""
|
| 103 |
+
cppmacros['F_FUNC'] = """
|
| 104 |
+
#if defined(PREPEND_FORTRAN)
|
| 105 |
+
#if defined(NO_APPEND_FORTRAN)
|
| 106 |
+
#if defined(UPPERCASE_FORTRAN)
|
| 107 |
+
#define F_FUNC(f,F) _##F
|
| 108 |
+
#else
|
| 109 |
+
#define F_FUNC(f,F) _##f
|
| 110 |
+
#endif
|
| 111 |
+
#else
|
| 112 |
+
#if defined(UPPERCASE_FORTRAN)
|
| 113 |
+
#define F_FUNC(f,F) _##F##_
|
| 114 |
+
#else
|
| 115 |
+
#define F_FUNC(f,F) _##f##_
|
| 116 |
+
#endif
|
| 117 |
+
#endif
|
| 118 |
+
#else
|
| 119 |
+
#if defined(NO_APPEND_FORTRAN)
|
| 120 |
+
#if defined(UPPERCASE_FORTRAN)
|
| 121 |
+
#define F_FUNC(f,F) F
|
| 122 |
+
#else
|
| 123 |
+
#define F_FUNC(f,F) f
|
| 124 |
+
#endif
|
| 125 |
+
#else
|
| 126 |
+
#if defined(UPPERCASE_FORTRAN)
|
| 127 |
+
#define F_FUNC(f,F) F##_
|
| 128 |
+
#else
|
| 129 |
+
#define F_FUNC(f,F) f##_
|
| 130 |
+
#endif
|
| 131 |
+
#endif
|
| 132 |
+
#endif
|
| 133 |
+
#if defined(UNDERSCORE_G77)
|
| 134 |
+
#define F_FUNC_US(f,F) F_FUNC(f##_,F##_)
|
| 135 |
+
#else
|
| 136 |
+
#define F_FUNC_US(f,F) F_FUNC(f,F)
|
| 137 |
+
#endif
|
| 138 |
+
"""
|
| 139 |
+
cppmacros['F_WRAPPEDFUNC'] = """
|
| 140 |
+
#if defined(PREPEND_FORTRAN)
|
| 141 |
+
#if defined(NO_APPEND_FORTRAN)
|
| 142 |
+
#if defined(UPPERCASE_FORTRAN)
|
| 143 |
+
#define F_WRAPPEDFUNC(f,F) _F2PYWRAP##F
|
| 144 |
+
#else
|
| 145 |
+
#define F_WRAPPEDFUNC(f,F) _f2pywrap##f
|
| 146 |
+
#endif
|
| 147 |
+
#else
|
| 148 |
+
#if defined(UPPERCASE_FORTRAN)
|
| 149 |
+
#define F_WRAPPEDFUNC(f,F) _F2PYWRAP##F##_
|
| 150 |
+
#else
|
| 151 |
+
#define F_WRAPPEDFUNC(f,F) _f2pywrap##f##_
|
| 152 |
+
#endif
|
| 153 |
+
#endif
|
| 154 |
+
#else
|
| 155 |
+
#if defined(NO_APPEND_FORTRAN)
|
| 156 |
+
#if defined(UPPERCASE_FORTRAN)
|
| 157 |
+
#define F_WRAPPEDFUNC(f,F) F2PYWRAP##F
|
| 158 |
+
#else
|
| 159 |
+
#define F_WRAPPEDFUNC(f,F) f2pywrap##f
|
| 160 |
+
#endif
|
| 161 |
+
#else
|
| 162 |
+
#if defined(UPPERCASE_FORTRAN)
|
| 163 |
+
#define F_WRAPPEDFUNC(f,F) F2PYWRAP##F##_
|
| 164 |
+
#else
|
| 165 |
+
#define F_WRAPPEDFUNC(f,F) f2pywrap##f##_
|
| 166 |
+
#endif
|
| 167 |
+
#endif
|
| 168 |
+
#endif
|
| 169 |
+
#if defined(UNDERSCORE_G77)
|
| 170 |
+
#define F_WRAPPEDFUNC_US(f,F) F_WRAPPEDFUNC(f##_,F##_)
|
| 171 |
+
#else
|
| 172 |
+
#define F_WRAPPEDFUNC_US(f,F) F_WRAPPEDFUNC(f,F)
|
| 173 |
+
#endif
|
| 174 |
+
"""
|
| 175 |
+
cppmacros['F_MODFUNC'] = """
|
| 176 |
+
#if defined(F90MOD2CCONV1) /*E.g. Compaq Fortran */
|
| 177 |
+
#if defined(NO_APPEND_FORTRAN)
|
| 178 |
+
#define F_MODFUNCNAME(m,f) $ ## m ## $ ## f
|
| 179 |
+
#else
|
| 180 |
+
#define F_MODFUNCNAME(m,f) $ ## m ## $ ## f ## _
|
| 181 |
+
#endif
|
| 182 |
+
#endif
|
| 183 |
+
|
| 184 |
+
#if defined(F90MOD2CCONV2) /*E.g. IBM XL Fortran, not tested though */
|
| 185 |
+
#if defined(NO_APPEND_FORTRAN)
|
| 186 |
+
#define F_MODFUNCNAME(m,f) __ ## m ## _MOD_ ## f
|
| 187 |
+
#else
|
| 188 |
+
#define F_MODFUNCNAME(m,f) __ ## m ## _MOD_ ## f ## _
|
| 189 |
+
#endif
|
| 190 |
+
#endif
|
| 191 |
+
|
| 192 |
+
#if defined(F90MOD2CCONV3) /*E.g. MIPSPro Compilers */
|
| 193 |
+
#if defined(NO_APPEND_FORTRAN)
|
| 194 |
+
#define F_MODFUNCNAME(m,f) f ## .in. ## m
|
| 195 |
+
#else
|
| 196 |
+
#define F_MODFUNCNAME(m,f) f ## .in. ## m ## _
|
| 197 |
+
#endif
|
| 198 |
+
#endif
|
| 199 |
+
/*
|
| 200 |
+
#if defined(UPPERCASE_FORTRAN)
|
| 201 |
+
#define F_MODFUNC(m,M,f,F) F_MODFUNCNAME(M,F)
|
| 202 |
+
#else
|
| 203 |
+
#define F_MODFUNC(m,M,f,F) F_MODFUNCNAME(m,f)
|
| 204 |
+
#endif
|
| 205 |
+
*/
|
| 206 |
+
|
| 207 |
+
#define F_MODFUNC(m,f) (*(f2pymodstruct##m##.##f))
|
| 208 |
+
"""
|
| 209 |
+
cppmacros['SWAPUNSAFE'] = """
|
| 210 |
+
#define SWAP(a,b) (size_t)(a) = ((size_t)(a) ^ (size_t)(b));\\
|
| 211 |
+
(size_t)(b) = ((size_t)(a) ^ (size_t)(b));\\
|
| 212 |
+
(size_t)(a) = ((size_t)(a) ^ (size_t)(b))
|
| 213 |
+
"""
|
| 214 |
+
cppmacros['SWAP'] = """
|
| 215 |
+
#define SWAP(a,b,t) {\\
|
| 216 |
+
t *c;\\
|
| 217 |
+
c = a;\\
|
| 218 |
+
a = b;\\
|
| 219 |
+
b = c;}
|
| 220 |
+
"""
|
| 221 |
+
# cppmacros['ISCONTIGUOUS']='#define ISCONTIGUOUS(m) (PyArray_FLAGS(m) &
|
| 222 |
+
# NPY_ARRAY_C_CONTIGUOUS)'
|
| 223 |
+
cppmacros['PRINTPYOBJERR'] = """
|
| 224 |
+
#define PRINTPYOBJERR(obj)\\
|
| 225 |
+
fprintf(stderr,\"#modulename#.error is related to \");\\
|
| 226 |
+
PyObject_Print((PyObject *)obj,stderr,Py_PRINT_RAW);\\
|
| 227 |
+
fprintf(stderr,\"\\n\");
|
| 228 |
+
"""
|
| 229 |
+
cppmacros['MINMAX'] = """
|
| 230 |
+
#ifndef max
|
| 231 |
+
#define max(a,b) ((a > b) ? (a) : (b))
|
| 232 |
+
#endif
|
| 233 |
+
#ifndef min
|
| 234 |
+
#define min(a,b) ((a < b) ? (a) : (b))
|
| 235 |
+
#endif
|
| 236 |
+
#ifndef MAX
|
| 237 |
+
#define MAX(a,b) ((a > b) ? (a) : (b))
|
| 238 |
+
#endif
|
| 239 |
+
#ifndef MIN
|
| 240 |
+
#define MIN(a,b) ((a < b) ? (a) : (b))
|
| 241 |
+
#endif
|
| 242 |
+
"""
|
| 243 |
+
cppmacros['len..'] = """
|
| 244 |
+
/* See fortranobject.h for definitions. The macros here are provided for BC. */
|
| 245 |
+
#define rank f2py_rank
|
| 246 |
+
#define shape f2py_shape
|
| 247 |
+
#define fshape f2py_shape
|
| 248 |
+
#define len f2py_len
|
| 249 |
+
#define flen f2py_flen
|
| 250 |
+
#define slen f2py_slen
|
| 251 |
+
#define size f2py_size
|
| 252 |
+
"""
|
| 253 |
+
cppmacros['pyobj_from_char1'] = r"""
|
| 254 |
+
#define pyobj_from_char1(v) (PyLong_FromLong(v))
|
| 255 |
+
"""
|
| 256 |
+
cppmacros['pyobj_from_short1'] = r"""
|
| 257 |
+
#define pyobj_from_short1(v) (PyLong_FromLong(v))
|
| 258 |
+
"""
|
| 259 |
+
needs['pyobj_from_int1'] = ['signed_char']
|
| 260 |
+
cppmacros['pyobj_from_int1'] = r"""
|
| 261 |
+
#define pyobj_from_int1(v) (PyLong_FromLong(v))
|
| 262 |
+
"""
|
| 263 |
+
cppmacros['pyobj_from_long1'] = r"""
|
| 264 |
+
#define pyobj_from_long1(v) (PyLong_FromLong(v))
|
| 265 |
+
"""
|
| 266 |
+
needs['pyobj_from_long_long1'] = ['long_long']
|
| 267 |
+
cppmacros['pyobj_from_long_long1'] = """
|
| 268 |
+
#ifdef HAVE_LONG_LONG
|
| 269 |
+
#define pyobj_from_long_long1(v) (PyLong_FromLongLong(v))
|
| 270 |
+
#else
|
| 271 |
+
#warning HAVE_LONG_LONG is not available. Redefining pyobj_from_long_long.
|
| 272 |
+
#define pyobj_from_long_long1(v) (PyLong_FromLong(v))
|
| 273 |
+
#endif
|
| 274 |
+
"""
|
| 275 |
+
needs['pyobj_from_long_double1'] = ['long_double']
|
| 276 |
+
cppmacros['pyobj_from_long_double1'] = """
|
| 277 |
+
#define pyobj_from_long_double1(v) (PyFloat_FromDouble(v))"""
|
| 278 |
+
cppmacros['pyobj_from_double1'] = """
|
| 279 |
+
#define pyobj_from_double1(v) (PyFloat_FromDouble(v))"""
|
| 280 |
+
cppmacros['pyobj_from_float1'] = """
|
| 281 |
+
#define pyobj_from_float1(v) (PyFloat_FromDouble(v))"""
|
| 282 |
+
needs['pyobj_from_complex_long_double1'] = ['complex_long_double']
|
| 283 |
+
cppmacros['pyobj_from_complex_long_double1'] = """
|
| 284 |
+
#define pyobj_from_complex_long_double1(v) (PyComplex_FromDoubles(v.r,v.i))"""
|
| 285 |
+
needs['pyobj_from_complex_double1'] = ['complex_double']
|
| 286 |
+
cppmacros['pyobj_from_complex_double1'] = """
|
| 287 |
+
#define pyobj_from_complex_double1(v) (PyComplex_FromDoubles(v.r,v.i))"""
|
| 288 |
+
needs['pyobj_from_complex_float1'] = ['complex_float']
|
| 289 |
+
cppmacros['pyobj_from_complex_float1'] = """
|
| 290 |
+
#define pyobj_from_complex_float1(v) (PyComplex_FromDoubles(v.r,v.i))"""
|
| 291 |
+
needs['pyobj_from_string1'] = ['string']
|
| 292 |
+
cppmacros['pyobj_from_string1'] = """
|
| 293 |
+
#define pyobj_from_string1(v) (PyUnicode_FromString((char *)v))"""
|
| 294 |
+
needs['pyobj_from_string1size'] = ['string']
|
| 295 |
+
cppmacros['pyobj_from_string1size'] = """
|
| 296 |
+
#define pyobj_from_string1size(v,len) (PyUnicode_FromStringAndSize((char *)v, len))"""
|
| 297 |
+
needs['TRYPYARRAYTEMPLATE'] = ['PRINTPYOBJERR']
|
| 298 |
+
cppmacros['TRYPYARRAYTEMPLATE'] = """
|
| 299 |
+
/* New SciPy */
|
| 300 |
+
#define TRYPYARRAYTEMPLATECHAR case NPY_STRING: *(char *)(PyArray_DATA(arr))=*v; break;
|
| 301 |
+
#define TRYPYARRAYTEMPLATELONG case NPY_LONG: *(long *)(PyArray_DATA(arr))=*v; break;
|
| 302 |
+
#define TRYPYARRAYTEMPLATEOBJECT case NPY_OBJECT: PyArray_SETITEM(arr,PyArray_DATA(arr),pyobj_from_ ## ctype ## 1(*v)); break;
|
| 303 |
+
|
| 304 |
+
#define TRYPYARRAYTEMPLATE(ctype,typecode) \\
|
| 305 |
+
PyArrayObject *arr = NULL;\\
|
| 306 |
+
if (!obj) return -2;\\
|
| 307 |
+
if (!PyArray_Check(obj)) return -1;\\
|
| 308 |
+
if (!(arr=(PyArrayObject *)obj)) {fprintf(stderr,\"TRYPYARRAYTEMPLATE:\");PRINTPYOBJERR(obj);return 0;}\\
|
| 309 |
+
if (PyArray_DESCR(arr)->type==typecode) {*(ctype *)(PyArray_DATA(arr))=*v; return 1;}\\
|
| 310 |
+
switch (PyArray_TYPE(arr)) {\\
|
| 311 |
+
case NPY_DOUBLE: *(npy_double *)(PyArray_DATA(arr))=*v; break;\\
|
| 312 |
+
case NPY_INT: *(npy_int *)(PyArray_DATA(arr))=*v; break;\\
|
| 313 |
+
case NPY_LONG: *(npy_long *)(PyArray_DATA(arr))=*v; break;\\
|
| 314 |
+
case NPY_FLOAT: *(npy_float *)(PyArray_DATA(arr))=*v; break;\\
|
| 315 |
+
case NPY_CDOUBLE: *(npy_double *)(PyArray_DATA(arr))=*v; break;\\
|
| 316 |
+
case NPY_CFLOAT: *(npy_float *)(PyArray_DATA(arr))=*v; break;\\
|
| 317 |
+
case NPY_BOOL: *(npy_bool *)(PyArray_DATA(arr))=(*v!=0); break;\\
|
| 318 |
+
case NPY_UBYTE: *(npy_ubyte *)(PyArray_DATA(arr))=*v; break;\\
|
| 319 |
+
case NPY_BYTE: *(npy_byte *)(PyArray_DATA(arr))=*v; break;\\
|
| 320 |
+
case NPY_SHORT: *(npy_short *)(PyArray_DATA(arr))=*v; break;\\
|
| 321 |
+
case NPY_USHORT: *(npy_ushort *)(PyArray_DATA(arr))=*v; break;\\
|
| 322 |
+
case NPY_UINT: *(npy_uint *)(PyArray_DATA(arr))=*v; break;\\
|
| 323 |
+
case NPY_ULONG: *(npy_ulong *)(PyArray_DATA(arr))=*v; break;\\
|
| 324 |
+
case NPY_LONGLONG: *(npy_longlong *)(PyArray_DATA(arr))=*v; break;\\
|
| 325 |
+
case NPY_ULONGLONG: *(npy_ulonglong *)(PyArray_DATA(arr))=*v; break;\\
|
| 326 |
+
case NPY_LONGDOUBLE: *(npy_longdouble *)(PyArray_DATA(arr))=*v; break;\\
|
| 327 |
+
case NPY_CLONGDOUBLE: *(npy_longdouble *)(PyArray_DATA(arr))=*v; break;\\
|
| 328 |
+
case NPY_OBJECT: PyArray_SETITEM(arr, PyArray_DATA(arr), pyobj_from_ ## ctype ## 1(*v)); break;\\
|
| 329 |
+
default: return -2;\\
|
| 330 |
+
};\\
|
| 331 |
+
return 1
|
| 332 |
+
"""
|
| 333 |
+
|
| 334 |
+
needs['TRYCOMPLEXPYARRAYTEMPLATE'] = ['PRINTPYOBJERR']
|
| 335 |
+
cppmacros['TRYCOMPLEXPYARRAYTEMPLATE'] = """
|
| 336 |
+
#define TRYCOMPLEXPYARRAYTEMPLATEOBJECT case NPY_OBJECT: PyArray_SETITEM(arr, PyArray_DATA(arr), pyobj_from_complex_ ## ctype ## 1((*v))); break;
|
| 337 |
+
#define TRYCOMPLEXPYARRAYTEMPLATE(ctype,typecode)\\
|
| 338 |
+
PyArrayObject *arr = NULL;\\
|
| 339 |
+
if (!obj) return -2;\\
|
| 340 |
+
if (!PyArray_Check(obj)) return -1;\\
|
| 341 |
+
if (!(arr=(PyArrayObject *)obj)) {fprintf(stderr,\"TRYCOMPLEXPYARRAYTEMPLATE:\");PRINTPYOBJERR(obj);return 0;}\\
|
| 342 |
+
if (PyArray_DESCR(arr)->type==typecode) {\\
|
| 343 |
+
*(ctype *)(PyArray_DATA(arr))=(*v).r;\\
|
| 344 |
+
*(ctype *)(PyArray_DATA(arr)+sizeof(ctype))=(*v).i;\\
|
| 345 |
+
return 1;\\
|
| 346 |
+
}\\
|
| 347 |
+
switch (PyArray_TYPE(arr)) {\\
|
| 348 |
+
case NPY_CDOUBLE: *(npy_double *)(PyArray_DATA(arr))=(*v).r;\\
|
| 349 |
+
*(npy_double *)(PyArray_DATA(arr)+sizeof(npy_double))=(*v).i;\\
|
| 350 |
+
break;\\
|
| 351 |
+
case NPY_CFLOAT: *(npy_float *)(PyArray_DATA(arr))=(*v).r;\\
|
| 352 |
+
*(npy_float *)(PyArray_DATA(arr)+sizeof(npy_float))=(*v).i;\\
|
| 353 |
+
break;\\
|
| 354 |
+
case NPY_DOUBLE: *(npy_double *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 355 |
+
case NPY_LONG: *(npy_long *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 356 |
+
case NPY_FLOAT: *(npy_float *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 357 |
+
case NPY_INT: *(npy_int *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 358 |
+
case NPY_SHORT: *(npy_short *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 359 |
+
case NPY_UBYTE: *(npy_ubyte *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 360 |
+
case NPY_BYTE: *(npy_byte *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 361 |
+
case NPY_BOOL: *(npy_bool *)(PyArray_DATA(arr))=((*v).r!=0 && (*v).i!=0); break;\\
|
| 362 |
+
case NPY_USHORT: *(npy_ushort *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 363 |
+
case NPY_UINT: *(npy_uint *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 364 |
+
case NPY_ULONG: *(npy_ulong *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 365 |
+
case NPY_LONGLONG: *(npy_longlong *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 366 |
+
case NPY_ULONGLONG: *(npy_ulonglong *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 367 |
+
case NPY_LONGDOUBLE: *(npy_longdouble *)(PyArray_DATA(arr))=(*v).r; break;\\
|
| 368 |
+
case NPY_CLONGDOUBLE: *(npy_longdouble *)(PyArray_DATA(arr))=(*v).r;\\
|
| 369 |
+
*(npy_longdouble *)(PyArray_DATA(arr)+sizeof(npy_longdouble))=(*v).i;\\
|
| 370 |
+
break;\\
|
| 371 |
+
case NPY_OBJECT: PyArray_SETITEM(arr, PyArray_DATA(arr), pyobj_from_complex_ ## ctype ## 1((*v))); break;\\
|
| 372 |
+
default: return -2;\\
|
| 373 |
+
};\\
|
| 374 |
+
return -1;
|
| 375 |
+
"""
|
| 376 |
+
# cppmacros['NUMFROMARROBJ']="""
|
| 377 |
+
# define NUMFROMARROBJ(typenum,ctype) \\
|
| 378 |
+
# if (PyArray_Check(obj)) arr = (PyArrayObject *)obj;\\
|
| 379 |
+
# else arr = (PyArrayObject *)PyArray_ContiguousFromObject(obj,typenum,0,0);\\
|
| 380 |
+
# if (arr) {\\
|
| 381 |
+
# if (PyArray_TYPE(arr)==NPY_OBJECT) {\\
|
| 382 |
+
# if (!ctype ## _from_pyobj(v,(PyArray_DESCR(arr)->getitem)(PyArray_DATA(arr)),\"\"))\\
|
| 383 |
+
# goto capi_fail;\\
|
| 384 |
+
# } else {\\
|
| 385 |
+
# (PyArray_DESCR(arr)->cast[typenum])(PyArray_DATA(arr),1,(char*)v,1,1);\\
|
| 386 |
+
# }\\
|
| 387 |
+
# if ((PyObject *)arr != obj) { Py_DECREF(arr); }\\
|
| 388 |
+
# return 1;\\
|
| 389 |
+
# }
|
| 390 |
+
# """
|
| 391 |
+
# XXX: Note that CNUMFROMARROBJ is identical with NUMFROMARROBJ
|
| 392 |
+
# cppmacros['CNUMFROMARROBJ']="""
|
| 393 |
+
# define CNUMFROMARROBJ(typenum,ctype) \\
|
| 394 |
+
# if (PyArray_Check(obj)) arr = (PyArrayObject *)obj;\\
|
| 395 |
+
# else arr = (PyArrayObject *)PyArray_ContiguousFromObject(obj,typenum,0,0);\\
|
| 396 |
+
# if (arr) {\\
|
| 397 |
+
# if (PyArray_TYPE(arr)==NPY_OBJECT) {\\
|
| 398 |
+
# if (!ctype ## _from_pyobj(v,(PyArray_DESCR(arr)->getitem)(PyArray_DATA(arr)),\"\"))\\
|
| 399 |
+
# goto capi_fail;\\
|
| 400 |
+
# } else {\\
|
| 401 |
+
# (PyArray_DESCR(arr)->cast[typenum])((void *)(PyArray_DATA(arr)),1,(void *)(v),1,1);\\
|
| 402 |
+
# }\\
|
| 403 |
+
# if ((PyObject *)arr != obj) { Py_DECREF(arr); }\\
|
| 404 |
+
# return 1;\\
|
| 405 |
+
# }
|
| 406 |
+
# """
|
| 407 |
+
|
| 408 |
+
|
| 409 |
+
needs['GETSTRFROMPYTUPLE'] = ['STRINGCOPYN', 'PRINTPYOBJERR']
|
| 410 |
+
cppmacros['GETSTRFROMPYTUPLE'] = """
|
| 411 |
+
#define GETSTRFROMPYTUPLE(tuple,index,str,len) {\\
|
| 412 |
+
PyObject *rv_cb_str = PyTuple_GetItem((tuple),(index));\\
|
| 413 |
+
if (rv_cb_str == NULL)\\
|
| 414 |
+
goto capi_fail;\\
|
| 415 |
+
if (PyBytes_Check(rv_cb_str)) {\\
|
| 416 |
+
str[len-1]='\\0';\\
|
| 417 |
+
STRINGCOPYN((str),PyBytes_AS_STRING((PyBytesObject*)rv_cb_str),(len));\\
|
| 418 |
+
} else {\\
|
| 419 |
+
PRINTPYOBJERR(rv_cb_str);\\
|
| 420 |
+
PyErr_SetString(#modulename#_error,\"string object expected\");\\
|
| 421 |
+
goto capi_fail;\\
|
| 422 |
+
}\\
|
| 423 |
+
}
|
| 424 |
+
"""
|
| 425 |
+
cppmacros['GETSCALARFROMPYTUPLE'] = """
|
| 426 |
+
#define GETSCALARFROMPYTUPLE(tuple,index,var,ctype,mess) {\\
|
| 427 |
+
if ((capi_tmp = PyTuple_GetItem((tuple),(index)))==NULL) goto capi_fail;\\
|
| 428 |
+
if (!(ctype ## _from_pyobj((var),capi_tmp,mess)))\\
|
| 429 |
+
goto capi_fail;\\
|
| 430 |
+
}
|
| 431 |
+
"""
|
| 432 |
+
|
| 433 |
+
cppmacros['FAILNULL'] = """\
|
| 434 |
+
#define FAILNULL(p) do { \\
|
| 435 |
+
if ((p) == NULL) { \\
|
| 436 |
+
PyErr_SetString(PyExc_MemoryError, "NULL pointer found"); \\
|
| 437 |
+
goto capi_fail; \\
|
| 438 |
+
} \\
|
| 439 |
+
} while (0)
|
| 440 |
+
"""
|
| 441 |
+
needs['MEMCOPY'] = ['string.h', 'FAILNULL']
|
| 442 |
+
cppmacros['MEMCOPY'] = """
|
| 443 |
+
#define MEMCOPY(to,from,n)\\
|
| 444 |
+
do { FAILNULL(to); FAILNULL(from); (void)memcpy(to,from,n); } while (0)
|
| 445 |
+
"""
|
| 446 |
+
cppmacros['STRINGMALLOC'] = """
|
| 447 |
+
#define STRINGMALLOC(str,len)\\
|
| 448 |
+
if ((str = (string)malloc(len+1)) == NULL) {\\
|
| 449 |
+
PyErr_SetString(PyExc_MemoryError, \"out of memory\");\\
|
| 450 |
+
goto capi_fail;\\
|
| 451 |
+
} else {\\
|
| 452 |
+
(str)[len] = '\\0';\\
|
| 453 |
+
}
|
| 454 |
+
"""
|
| 455 |
+
cppmacros['STRINGFREE'] = """
|
| 456 |
+
#define STRINGFREE(str) do {if (!(str == NULL)) free(str);} while (0)
|
| 457 |
+
"""
|
| 458 |
+
needs['STRINGPADN'] = ['string.h']
|
| 459 |
+
cppmacros['STRINGPADN'] = """
|
| 460 |
+
/*
|
| 461 |
+
STRINGPADN replaces null values with padding values from the right.
|
| 462 |
+
|
| 463 |
+
`to` must have size of at least N bytes.
|
| 464 |
+
|
| 465 |
+
If the `to[N-1]` has null value, then replace it and all the
|
| 466 |
+
preceding, nulls with the given padding.
|
| 467 |
+
|
| 468 |
+
STRINGPADN(to, N, PADDING, NULLVALUE) is an inverse operation.
|
| 469 |
+
*/
|
| 470 |
+
#define STRINGPADN(to, N, NULLVALUE, PADDING) \\
|
| 471 |
+
do { \\
|
| 472 |
+
int _m = (N); \\
|
| 473 |
+
char *_to = (to); \\
|
| 474 |
+
for (_m -= 1; _m >= 0 && _to[_m] == NULLVALUE; _m--) { \\
|
| 475 |
+
_to[_m] = PADDING; \\
|
| 476 |
+
} \\
|
| 477 |
+
} while (0)
|
| 478 |
+
"""
|
| 479 |
+
needs['STRINGCOPYN'] = ['string.h', 'FAILNULL']
|
| 480 |
+
cppmacros['STRINGCOPYN'] = """
|
| 481 |
+
/*
|
| 482 |
+
STRINGCOPYN copies N bytes.
|
| 483 |
+
|
| 484 |
+
`to` and `from` buffers must have sizes of at least N bytes.
|
| 485 |
+
*/
|
| 486 |
+
#define STRINGCOPYN(to,from,N) \\
|
| 487 |
+
do { \\
|
| 488 |
+
int _m = (N); \\
|
| 489 |
+
char *_to = (to); \\
|
| 490 |
+
char *_from = (from); \\
|
| 491 |
+
FAILNULL(_to); FAILNULL(_from); \\
|
| 492 |
+
(void)strncpy(_to, _from, _m); \\
|
| 493 |
+
} while (0)
|
| 494 |
+
"""
|
| 495 |
+
needs['STRINGCOPY'] = ['string.h', 'FAILNULL']
|
| 496 |
+
cppmacros['STRINGCOPY'] = """
|
| 497 |
+
#define STRINGCOPY(to,from)\\
|
| 498 |
+
do { FAILNULL(to); FAILNULL(from); (void)strcpy(to,from); } while (0)
|
| 499 |
+
"""
|
| 500 |
+
cppmacros['CHECKGENERIC'] = """
|
| 501 |
+
#define CHECKGENERIC(check,tcheck,name) \\
|
| 502 |
+
if (!(check)) {\\
|
| 503 |
+
PyErr_SetString(#modulename#_error,\"(\"tcheck\") failed for \"name);\\
|
| 504 |
+
/*goto capi_fail;*/\\
|
| 505 |
+
} else """
|
| 506 |
+
cppmacros['CHECKARRAY'] = """
|
| 507 |
+
#define CHECKARRAY(check,tcheck,name) \\
|
| 508 |
+
if (!(check)) {\\
|
| 509 |
+
PyErr_SetString(#modulename#_error,\"(\"tcheck\") failed for \"name);\\
|
| 510 |
+
/*goto capi_fail;*/\\
|
| 511 |
+
} else """
|
| 512 |
+
cppmacros['CHECKSTRING'] = """
|
| 513 |
+
#define CHECKSTRING(check,tcheck,name,show,var)\\
|
| 514 |
+
if (!(check)) {\\
|
| 515 |
+
char errstring[256];\\
|
| 516 |
+
sprintf(errstring, \"%s: \"show, \"(\"tcheck\") failed for \"name, slen(var), var);\\
|
| 517 |
+
PyErr_SetString(#modulename#_error, errstring);\\
|
| 518 |
+
/*goto capi_fail;*/\\
|
| 519 |
+
} else """
|
| 520 |
+
cppmacros['CHECKSCALAR'] = """
|
| 521 |
+
#define CHECKSCALAR(check,tcheck,name,show,var)\\
|
| 522 |
+
if (!(check)) {\\
|
| 523 |
+
char errstring[256];\\
|
| 524 |
+
sprintf(errstring, \"%s: \"show, \"(\"tcheck\") failed for \"name, var);\\
|
| 525 |
+
PyErr_SetString(#modulename#_error,errstring);\\
|
| 526 |
+
/*goto capi_fail;*/\\
|
| 527 |
+
} else """
|
| 528 |
+
# cppmacros['CHECKDIMS']="""
|
| 529 |
+
# define CHECKDIMS(dims,rank) \\
|
| 530 |
+
# for (int i=0;i<(rank);i++)\\
|
| 531 |
+
# if (dims[i]<0) {\\
|
| 532 |
+
# fprintf(stderr,\"Unspecified array argument requires a complete dimension specification.\\n\");\\
|
| 533 |
+
# goto capi_fail;\\
|
| 534 |
+
# }
|
| 535 |
+
# """
|
| 536 |
+
cppmacros[
|
| 537 |
+
'ARRSIZE'] = '#define ARRSIZE(dims,rank) (_PyArray_multiply_list(dims,rank))'
|
| 538 |
+
cppmacros['OLDPYNUM'] = """
|
| 539 |
+
#ifdef OLDPYNUM
|
| 540 |
+
#error You need to install NumPy version 0.13 or higher. See https://scipy.org/install.html
|
| 541 |
+
#endif
|
| 542 |
+
"""
|
| 543 |
+
cppmacros["F2PY_THREAD_LOCAL_DECL"] = """
|
| 544 |
+
#ifndef F2PY_THREAD_LOCAL_DECL
|
| 545 |
+
#if defined(_MSC_VER)
|
| 546 |
+
#define F2PY_THREAD_LOCAL_DECL __declspec(thread)
|
| 547 |
+
#elif defined(NPY_OS_MINGW)
|
| 548 |
+
#define F2PY_THREAD_LOCAL_DECL __thread
|
| 549 |
+
#elif defined(__STDC_VERSION__) \\
|
| 550 |
+
&& (__STDC_VERSION__ >= 201112L) \\
|
| 551 |
+
&& !defined(__STDC_NO_THREADS__) \\
|
| 552 |
+
&& (!defined(__GLIBC__) || __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 12)) \\
|
| 553 |
+
&& !defined(NPY_OS_OPENBSD) && !defined(NPY_OS_HAIKU)
|
| 554 |
+
/* __STDC_NO_THREADS__ was first defined in a maintenance release of glibc 2.12,
|
| 555 |
+
see https://lists.gnu.org/archive/html/commit-hurd/2012-07/msg00180.html,
|
| 556 |
+
so `!defined(__STDC_NO_THREADS__)` may give false positive for the existence
|
| 557 |
+
of `threads.h` when using an older release of glibc 2.12
|
| 558 |
+
See gh-19437 for details on OpenBSD */
|
| 559 |
+
#include <threads.h>
|
| 560 |
+
#define F2PY_THREAD_LOCAL_DECL thread_local
|
| 561 |
+
#elif defined(__GNUC__) \\
|
| 562 |
+
&& (__GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 4)))
|
| 563 |
+
#define F2PY_THREAD_LOCAL_DECL __thread
|
| 564 |
+
#endif
|
| 565 |
+
#endif
|
| 566 |
+
"""
|
| 567 |
+
################# C functions ###############
|
| 568 |
+
|
| 569 |
+
cfuncs['calcarrindex'] = """
|
| 570 |
+
static int calcarrindex(int *i,PyArrayObject *arr) {
|
| 571 |
+
int k,ii = i[0];
|
| 572 |
+
for (k=1; k < PyArray_NDIM(arr); k++)
|
| 573 |
+
ii += (ii*(PyArray_DIM(arr,k) - 1)+i[k]); /* assuming contiguous arr */
|
| 574 |
+
return ii;
|
| 575 |
+
}"""
|
| 576 |
+
cfuncs['calcarrindextr'] = """
|
| 577 |
+
static int calcarrindextr(int *i,PyArrayObject *arr) {
|
| 578 |
+
int k,ii = i[PyArray_NDIM(arr)-1];
|
| 579 |
+
for (k=1; k < PyArray_NDIM(arr); k++)
|
| 580 |
+
ii += (ii*(PyArray_DIM(arr,PyArray_NDIM(arr)-k-1) - 1)+i[PyArray_NDIM(arr)-k-1]); /* assuming contiguous arr */
|
| 581 |
+
return ii;
|
| 582 |
+
}"""
|
| 583 |
+
cfuncs['forcomb'] = """
|
| 584 |
+
static struct { int nd;npy_intp *d;int *i,*i_tr,tr; } forcombcache;
|
| 585 |
+
static int initforcomb(npy_intp *dims,int nd,int tr) {
|
| 586 |
+
int k;
|
| 587 |
+
if (dims==NULL) return 0;
|
| 588 |
+
if (nd<0) return 0;
|
| 589 |
+
forcombcache.nd = nd;
|
| 590 |
+
forcombcache.d = dims;
|
| 591 |
+
forcombcache.tr = tr;
|
| 592 |
+
if ((forcombcache.i = (int *)malloc(sizeof(int)*nd))==NULL) return 0;
|
| 593 |
+
if ((forcombcache.i_tr = (int *)malloc(sizeof(int)*nd))==NULL) return 0;
|
| 594 |
+
for (k=1;k<nd;k++) {
|
| 595 |
+
forcombcache.i[k] = forcombcache.i_tr[nd-k-1] = 0;
|
| 596 |
+
}
|
| 597 |
+
forcombcache.i[0] = forcombcache.i_tr[nd-1] = -1;
|
| 598 |
+
return 1;
|
| 599 |
+
}
|
| 600 |
+
static int *nextforcomb(void) {
|
| 601 |
+
int j,*i,*i_tr,k;
|
| 602 |
+
int nd=forcombcache.nd;
|
| 603 |
+
if ((i=forcombcache.i) == NULL) return NULL;
|
| 604 |
+
if ((i_tr=forcombcache.i_tr) == NULL) return NULL;
|
| 605 |
+
if (forcombcache.d == NULL) return NULL;
|
| 606 |
+
i[0]++;
|
| 607 |
+
if (i[0]==forcombcache.d[0]) {
|
| 608 |
+
j=1;
|
| 609 |
+
while ((j<nd) && (i[j]==forcombcache.d[j]-1)) j++;
|
| 610 |
+
if (j==nd) {
|
| 611 |
+
free(i);
|
| 612 |
+
free(i_tr);
|
| 613 |
+
return NULL;
|
| 614 |
+
}
|
| 615 |
+
for (k=0;k<j;k++) i[k] = i_tr[nd-k-1] = 0;
|
| 616 |
+
i[j]++;
|
| 617 |
+
i_tr[nd-j-1]++;
|
| 618 |
+
} else
|
| 619 |
+
i_tr[nd-1]++;
|
| 620 |
+
if (forcombcache.tr) return i_tr;
|
| 621 |
+
return i;
|
| 622 |
+
}"""
|
| 623 |
+
needs['try_pyarr_from_string'] = ['STRINGCOPYN', 'PRINTPYOBJERR', 'string']
|
| 624 |
+
cfuncs['try_pyarr_from_string'] = """
|
| 625 |
+
/*
|
| 626 |
+
try_pyarr_from_string copies str[:len(obj)] to the data of an `ndarray`.
|
| 627 |
+
|
| 628 |
+
If obj is an `ndarray`, it is assumed to be contiguous.
|
| 629 |
+
|
| 630 |
+
If the specified len==-1, str must be null-terminated.
|
| 631 |
+
*/
|
| 632 |
+
static int try_pyarr_from_string(PyObject *obj,
|
| 633 |
+
const string str, const int len) {
|
| 634 |
+
#ifdef DEBUGCFUNCS
|
| 635 |
+
fprintf(stderr, "try_pyarr_from_string(str='%s', len=%d, obj=%p)\\n",
|
| 636 |
+
(char*)str,len, obj);
|
| 637 |
+
#endif
|
| 638 |
+
if (!obj) return -2; /* Object missing */
|
| 639 |
+
if (obj == Py_None) return -1; /* None */
|
| 640 |
+
if (!PyArray_Check(obj)) goto capi_fail; /* not an ndarray */
|
| 641 |
+
if (PyArray_Check(obj)) {
|
| 642 |
+
PyArrayObject *arr = (PyArrayObject *)obj;
|
| 643 |
+
assert(ISCONTIGUOUS(arr));
|
| 644 |
+
string buf = PyArray_DATA(arr);
|
| 645 |
+
npy_intp n = len;
|
| 646 |
+
if (n == -1) {
|
| 647 |
+
/* Assuming null-terminated str. */
|
| 648 |
+
n = strlen(str);
|
| 649 |
+
}
|
| 650 |
+
if (n > PyArray_NBYTES(arr)) {
|
| 651 |
+
n = PyArray_NBYTES(arr);
|
| 652 |
+
}
|
| 653 |
+
STRINGCOPYN(buf, str, n);
|
| 654 |
+
return 1;
|
| 655 |
+
}
|
| 656 |
+
capi_fail:
|
| 657 |
+
PRINTPYOBJERR(obj);
|
| 658 |
+
PyErr_SetString(#modulename#_error, \"try_pyarr_from_string failed\");
|
| 659 |
+
return 0;
|
| 660 |
+
}
|
| 661 |
+
"""
|
| 662 |
+
needs['string_from_pyobj'] = ['string', 'STRINGMALLOC', 'STRINGCOPYN']
|
| 663 |
+
cfuncs['string_from_pyobj'] = """
|
| 664 |
+
/*
|
| 665 |
+
Create a new string buffer `str` of at most length `len` from a
|
| 666 |
+
Python string-like object `obj`.
|
| 667 |
+
|
| 668 |
+
The string buffer has given size (len) or the size of inistr when len==-1.
|
| 669 |
+
|
| 670 |
+
The string buffer is padded with blanks: in Fortran, trailing blanks
|
| 671 |
+
are insignificant contrary to C nulls.
|
| 672 |
+
*/
|
| 673 |
+
static int
|
| 674 |
+
string_from_pyobj(string *str, int *len, const string inistr, PyObject *obj,
|
| 675 |
+
const char *errmess)
|
| 676 |
+
{
|
| 677 |
+
PyObject *tmp = NULL;
|
| 678 |
+
string buf = NULL;
|
| 679 |
+
npy_intp n = -1;
|
| 680 |
+
#ifdef DEBUGCFUNCS
|
| 681 |
+
fprintf(stderr,\"string_from_pyobj(str='%s',len=%d,inistr='%s',obj=%p)\\n\",
|
| 682 |
+
(char*)str, *len, (char *)inistr, obj);
|
| 683 |
+
#endif
|
| 684 |
+
if (obj == Py_None) {
|
| 685 |
+
n = strlen(inistr);
|
| 686 |
+
buf = inistr;
|
| 687 |
+
}
|
| 688 |
+
else if (PyArray_Check(obj)) {
|
| 689 |
+
PyArrayObject *arr = (PyArrayObject *)obj;
|
| 690 |
+
if (!ISCONTIGUOUS(arr)) {
|
| 691 |
+
PyErr_SetString(PyExc_ValueError,
|
| 692 |
+
\"array object is non-contiguous.\");
|
| 693 |
+
goto capi_fail;
|
| 694 |
+
}
|
| 695 |
+
n = PyArray_NBYTES(arr);
|
| 696 |
+
buf = PyArray_DATA(arr);
|
| 697 |
+
n = strnlen(buf, n);
|
| 698 |
+
}
|
| 699 |
+
else {
|
| 700 |
+
if (PyBytes_Check(obj)) {
|
| 701 |
+
tmp = obj;
|
| 702 |
+
Py_INCREF(tmp);
|
| 703 |
+
}
|
| 704 |
+
else if (PyUnicode_Check(obj)) {
|
| 705 |
+
tmp = PyUnicode_AsASCIIString(obj);
|
| 706 |
+
}
|
| 707 |
+
else {
|
| 708 |
+
PyObject *tmp2;
|
| 709 |
+
tmp2 = PyObject_Str(obj);
|
| 710 |
+
if (tmp2) {
|
| 711 |
+
tmp = PyUnicode_AsASCIIString(tmp2);
|
| 712 |
+
Py_DECREF(tmp2);
|
| 713 |
+
}
|
| 714 |
+
else {
|
| 715 |
+
tmp = NULL;
|
| 716 |
+
}
|
| 717 |
+
}
|
| 718 |
+
if (tmp == NULL) goto capi_fail;
|
| 719 |
+
n = PyBytes_GET_SIZE(tmp);
|
| 720 |
+
buf = PyBytes_AS_STRING(tmp);
|
| 721 |
+
}
|
| 722 |
+
if (*len == -1) {
|
| 723 |
+
/* TODO: change the type of `len` so that we can remove this */
|
| 724 |
+
if (n > NPY_MAX_INT) {
|
| 725 |
+
PyErr_SetString(PyExc_OverflowError,
|
| 726 |
+
"object too large for a 32-bit int");
|
| 727 |
+
goto capi_fail;
|
| 728 |
+
}
|
| 729 |
+
*len = n;
|
| 730 |
+
}
|
| 731 |
+
else if (*len < n) {
|
| 732 |
+
/* discard the last (len-n) bytes of input buf */
|
| 733 |
+
n = *len;
|
| 734 |
+
}
|
| 735 |
+
if (n < 0 || *len < 0 || buf == NULL) {
|
| 736 |
+
goto capi_fail;
|
| 737 |
+
}
|
| 738 |
+
STRINGMALLOC(*str, *len); // *str is allocated with size (*len + 1)
|
| 739 |
+
if (n < *len) {
|
| 740 |
+
/*
|
| 741 |
+
Pad fixed-width string with nulls. The caller will replace
|
| 742 |
+
nulls with blanks when the corresponding argument is not
|
| 743 |
+
intent(c).
|
| 744 |
+
*/
|
| 745 |
+
memset(*str + n, '\\0', *len - n);
|
| 746 |
+
}
|
| 747 |
+
STRINGCOPYN(*str, buf, n);
|
| 748 |
+
Py_XDECREF(tmp);
|
| 749 |
+
return 1;
|
| 750 |
+
capi_fail:
|
| 751 |
+
Py_XDECREF(tmp);
|
| 752 |
+
{
|
| 753 |
+
PyObject* err = PyErr_Occurred();
|
| 754 |
+
if (err == NULL) {
|
| 755 |
+
err = #modulename#_error;
|
| 756 |
+
}
|
| 757 |
+
PyErr_SetString(err, errmess);
|
| 758 |
+
}
|
| 759 |
+
return 0;
|
| 760 |
+
}
|
| 761 |
+
"""
|
| 762 |
+
|
| 763 |
+
cfuncs['character_from_pyobj'] = """
|
| 764 |
+
static int
|
| 765 |
+
character_from_pyobj(character* v, PyObject *obj, const char *errmess) {
|
| 766 |
+
if (PyBytes_Check(obj)) {
|
| 767 |
+
/* empty bytes has trailing null, so dereferencing is always safe */
|
| 768 |
+
*v = PyBytes_AS_STRING(obj)[0];
|
| 769 |
+
return 1;
|
| 770 |
+
} else if (PyUnicode_Check(obj)) {
|
| 771 |
+
PyObject* tmp = PyUnicode_AsASCIIString(obj);
|
| 772 |
+
if (tmp != NULL) {
|
| 773 |
+
*v = PyBytes_AS_STRING(tmp)[0];
|
| 774 |
+
Py_DECREF(tmp);
|
| 775 |
+
return 1;
|
| 776 |
+
}
|
| 777 |
+
} else if (PyArray_Check(obj)) {
|
| 778 |
+
PyArrayObject* arr = (PyArrayObject*)obj;
|
| 779 |
+
if (F2PY_ARRAY_IS_CHARACTER_COMPATIBLE(arr)) {
|
| 780 |
+
*v = PyArray_BYTES(arr)[0];
|
| 781 |
+
return 1;
|
| 782 |
+
} else if (F2PY_IS_UNICODE_ARRAY(arr)) {
|
| 783 |
+
// TODO: update when numpy will support 1-byte and
|
| 784 |
+
// 2-byte unicode dtypes
|
| 785 |
+
PyObject* tmp = PyUnicode_FromKindAndData(
|
| 786 |
+
PyUnicode_4BYTE_KIND,
|
| 787 |
+
PyArray_BYTES(arr),
|
| 788 |
+
(PyArray_NBYTES(arr)>0?1:0));
|
| 789 |
+
if (tmp != NULL) {
|
| 790 |
+
if (character_from_pyobj(v, tmp, errmess)) {
|
| 791 |
+
Py_DECREF(tmp);
|
| 792 |
+
return 1;
|
| 793 |
+
}
|
| 794 |
+
Py_DECREF(tmp);
|
| 795 |
+
}
|
| 796 |
+
}
|
| 797 |
+
} else if (PySequence_Check(obj)) {
|
| 798 |
+
PyObject* tmp = PySequence_GetItem(obj,0);
|
| 799 |
+
if (tmp != NULL) {
|
| 800 |
+
if (character_from_pyobj(v, tmp, errmess)) {
|
| 801 |
+
Py_DECREF(tmp);
|
| 802 |
+
return 1;
|
| 803 |
+
}
|
| 804 |
+
Py_DECREF(tmp);
|
| 805 |
+
}
|
| 806 |
+
}
|
| 807 |
+
{
|
| 808 |
+
/* TODO: This error (and most other) error handling needs cleaning. */
|
| 809 |
+
char mess[F2PY_MESSAGE_BUFFER_SIZE];
|
| 810 |
+
strcpy(mess, errmess);
|
| 811 |
+
PyObject* err = PyErr_Occurred();
|
| 812 |
+
if (err == NULL) {
|
| 813 |
+
err = PyExc_TypeError;
|
| 814 |
+
Py_INCREF(err);
|
| 815 |
+
}
|
| 816 |
+
else {
|
| 817 |
+
Py_INCREF(err);
|
| 818 |
+
PyErr_Clear();
|
| 819 |
+
}
|
| 820 |
+
sprintf(mess + strlen(mess),
|
| 821 |
+
" -- expected str|bytes|sequence-of-str-or-bytes, got ");
|
| 822 |
+
f2py_describe(obj, mess + strlen(mess));
|
| 823 |
+
PyErr_SetString(err, mess);
|
| 824 |
+
Py_DECREF(err);
|
| 825 |
+
}
|
| 826 |
+
return 0;
|
| 827 |
+
}
|
| 828 |
+
"""
|
| 829 |
+
|
| 830 |
+
# TODO: These should be dynamically generated, too many mapped to int things,
|
| 831 |
+
# see note in _isocbind.py
|
| 832 |
+
needs['char_from_pyobj'] = ['int_from_pyobj']
|
| 833 |
+
cfuncs['char_from_pyobj'] = """
|
| 834 |
+
static int
|
| 835 |
+
char_from_pyobj(char* v, PyObject *obj, const char *errmess) {
|
| 836 |
+
int i = 0;
|
| 837 |
+
if (int_from_pyobj(&i, obj, errmess)) {
|
| 838 |
+
*v = (char)i;
|
| 839 |
+
return 1;
|
| 840 |
+
}
|
| 841 |
+
return 0;
|
| 842 |
+
}
|
| 843 |
+
"""
|
| 844 |
+
|
| 845 |
+
|
| 846 |
+
needs['signed_char_from_pyobj'] = ['int_from_pyobj', 'signed_char']
|
| 847 |
+
cfuncs['signed_char_from_pyobj'] = """
|
| 848 |
+
static int
|
| 849 |
+
signed_char_from_pyobj(signed_char* v, PyObject *obj, const char *errmess) {
|
| 850 |
+
int i = 0;
|
| 851 |
+
if (int_from_pyobj(&i, obj, errmess)) {
|
| 852 |
+
*v = (signed_char)i;
|
| 853 |
+
return 1;
|
| 854 |
+
}
|
| 855 |
+
return 0;
|
| 856 |
+
}
|
| 857 |
+
"""
|
| 858 |
+
|
| 859 |
+
|
| 860 |
+
needs['short_from_pyobj'] = ['int_from_pyobj']
|
| 861 |
+
cfuncs['short_from_pyobj'] = """
|
| 862 |
+
static int
|
| 863 |
+
short_from_pyobj(short* v, PyObject *obj, const char *errmess) {
|
| 864 |
+
int i = 0;
|
| 865 |
+
if (int_from_pyobj(&i, obj, errmess)) {
|
| 866 |
+
*v = (short)i;
|
| 867 |
+
return 1;
|
| 868 |
+
}
|
| 869 |
+
return 0;
|
| 870 |
+
}
|
| 871 |
+
"""
|
| 872 |
+
|
| 873 |
+
|
| 874 |
+
cfuncs['int_from_pyobj'] = """
|
| 875 |
+
static int
|
| 876 |
+
int_from_pyobj(int* v, PyObject *obj, const char *errmess)
|
| 877 |
+
{
|
| 878 |
+
PyObject* tmp = NULL;
|
| 879 |
+
|
| 880 |
+
if (PyLong_Check(obj)) {
|
| 881 |
+
*v = Npy__PyLong_AsInt(obj);
|
| 882 |
+
return !(*v == -1 && PyErr_Occurred());
|
| 883 |
+
}
|
| 884 |
+
|
| 885 |
+
tmp = PyNumber_Long(obj);
|
| 886 |
+
if (tmp) {
|
| 887 |
+
*v = Npy__PyLong_AsInt(tmp);
|
| 888 |
+
Py_DECREF(tmp);
|
| 889 |
+
return !(*v == -1 && PyErr_Occurred());
|
| 890 |
+
}
|
| 891 |
+
|
| 892 |
+
if (PyComplex_Check(obj)) {
|
| 893 |
+
PyErr_Clear();
|
| 894 |
+
tmp = PyObject_GetAttrString(obj,\"real\");
|
| 895 |
+
}
|
| 896 |
+
else if (PyBytes_Check(obj) || PyUnicode_Check(obj)) {
|
| 897 |
+
/*pass*/;
|
| 898 |
+
}
|
| 899 |
+
else if (PySequence_Check(obj)) {
|
| 900 |
+
PyErr_Clear();
|
| 901 |
+
tmp = PySequence_GetItem(obj, 0);
|
| 902 |
+
}
|
| 903 |
+
|
| 904 |
+
if (tmp) {
|
| 905 |
+
if (int_from_pyobj(v, tmp, errmess)) {
|
| 906 |
+
Py_DECREF(tmp);
|
| 907 |
+
return 1;
|
| 908 |
+
}
|
| 909 |
+
Py_DECREF(tmp);
|
| 910 |
+
}
|
| 911 |
+
|
| 912 |
+
{
|
| 913 |
+
PyObject* err = PyErr_Occurred();
|
| 914 |
+
if (err == NULL) {
|
| 915 |
+
err = #modulename#_error;
|
| 916 |
+
}
|
| 917 |
+
PyErr_SetString(err, errmess);
|
| 918 |
+
}
|
| 919 |
+
return 0;
|
| 920 |
+
}
|
| 921 |
+
"""
|
| 922 |
+
|
| 923 |
+
|
| 924 |
+
cfuncs['long_from_pyobj'] = """
|
| 925 |
+
static int
|
| 926 |
+
long_from_pyobj(long* v, PyObject *obj, const char *errmess) {
|
| 927 |
+
PyObject* tmp = NULL;
|
| 928 |
+
|
| 929 |
+
if (PyLong_Check(obj)) {
|
| 930 |
+
*v = PyLong_AsLong(obj);
|
| 931 |
+
return !(*v == -1 && PyErr_Occurred());
|
| 932 |
+
}
|
| 933 |
+
|
| 934 |
+
tmp = PyNumber_Long(obj);
|
| 935 |
+
if (tmp) {
|
| 936 |
+
*v = PyLong_AsLong(tmp);
|
| 937 |
+
Py_DECREF(tmp);
|
| 938 |
+
return !(*v == -1 && PyErr_Occurred());
|
| 939 |
+
}
|
| 940 |
+
|
| 941 |
+
if (PyComplex_Check(obj)) {
|
| 942 |
+
PyErr_Clear();
|
| 943 |
+
tmp = PyObject_GetAttrString(obj,\"real\");
|
| 944 |
+
}
|
| 945 |
+
else if (PyBytes_Check(obj) || PyUnicode_Check(obj)) {
|
| 946 |
+
/*pass*/;
|
| 947 |
+
}
|
| 948 |
+
else if (PySequence_Check(obj)) {
|
| 949 |
+
PyErr_Clear();
|
| 950 |
+
tmp = PySequence_GetItem(obj, 0);
|
| 951 |
+
}
|
| 952 |
+
|
| 953 |
+
if (tmp) {
|
| 954 |
+
if (long_from_pyobj(v, tmp, errmess)) {
|
| 955 |
+
Py_DECREF(tmp);
|
| 956 |
+
return 1;
|
| 957 |
+
}
|
| 958 |
+
Py_DECREF(tmp);
|
| 959 |
+
}
|
| 960 |
+
{
|
| 961 |
+
PyObject* err = PyErr_Occurred();
|
| 962 |
+
if (err == NULL) {
|
| 963 |
+
err = #modulename#_error;
|
| 964 |
+
}
|
| 965 |
+
PyErr_SetString(err, errmess);
|
| 966 |
+
}
|
| 967 |
+
return 0;
|
| 968 |
+
}
|
| 969 |
+
"""
|
| 970 |
+
|
| 971 |
+
|
| 972 |
+
needs['long_long_from_pyobj'] = ['long_long']
|
| 973 |
+
cfuncs['long_long_from_pyobj'] = """
|
| 974 |
+
static int
|
| 975 |
+
long_long_from_pyobj(long_long* v, PyObject *obj, const char *errmess)
|
| 976 |
+
{
|
| 977 |
+
PyObject* tmp = NULL;
|
| 978 |
+
|
| 979 |
+
if (PyLong_Check(obj)) {
|
| 980 |
+
*v = PyLong_AsLongLong(obj);
|
| 981 |
+
return !(*v == -1 && PyErr_Occurred());
|
| 982 |
+
}
|
| 983 |
+
|
| 984 |
+
tmp = PyNumber_Long(obj);
|
| 985 |
+
if (tmp) {
|
| 986 |
+
*v = PyLong_AsLongLong(tmp);
|
| 987 |
+
Py_DECREF(tmp);
|
| 988 |
+
return !(*v == -1 && PyErr_Occurred());
|
| 989 |
+
}
|
| 990 |
+
|
| 991 |
+
if (PyComplex_Check(obj)) {
|
| 992 |
+
PyErr_Clear();
|
| 993 |
+
tmp = PyObject_GetAttrString(obj,\"real\");
|
| 994 |
+
}
|
| 995 |
+
else if (PyBytes_Check(obj) || PyUnicode_Check(obj)) {
|
| 996 |
+
/*pass*/;
|
| 997 |
+
}
|
| 998 |
+
else if (PySequence_Check(obj)) {
|
| 999 |
+
PyErr_Clear();
|
| 1000 |
+
tmp = PySequence_GetItem(obj, 0);
|
| 1001 |
+
}
|
| 1002 |
+
|
| 1003 |
+
if (tmp) {
|
| 1004 |
+
if (long_long_from_pyobj(v, tmp, errmess)) {
|
| 1005 |
+
Py_DECREF(tmp);
|
| 1006 |
+
return 1;
|
| 1007 |
+
}
|
| 1008 |
+
Py_DECREF(tmp);
|
| 1009 |
+
}
|
| 1010 |
+
{
|
| 1011 |
+
PyObject* err = PyErr_Occurred();
|
| 1012 |
+
if (err == NULL) {
|
| 1013 |
+
err = #modulename#_error;
|
| 1014 |
+
}
|
| 1015 |
+
PyErr_SetString(err,errmess);
|
| 1016 |
+
}
|
| 1017 |
+
return 0;
|
| 1018 |
+
}
|
| 1019 |
+
"""
|
| 1020 |
+
|
| 1021 |
+
|
| 1022 |
+
needs['long_double_from_pyobj'] = ['double_from_pyobj', 'long_double']
|
| 1023 |
+
cfuncs['long_double_from_pyobj'] = """
|
| 1024 |
+
static int
|
| 1025 |
+
long_double_from_pyobj(long_double* v, PyObject *obj, const char *errmess)
|
| 1026 |
+
{
|
| 1027 |
+
double d=0;
|
| 1028 |
+
if (PyArray_CheckScalar(obj)){
|
| 1029 |
+
if PyArray_IsScalar(obj, LongDouble) {
|
| 1030 |
+
PyArray_ScalarAsCtype(obj, v);
|
| 1031 |
+
return 1;
|
| 1032 |
+
}
|
| 1033 |
+
else if (PyArray_Check(obj) && PyArray_TYPE(obj) == NPY_LONGDOUBLE) {
|
| 1034 |
+
(*v) = *((npy_longdouble *)PyArray_DATA(obj));
|
| 1035 |
+
return 1;
|
| 1036 |
+
}
|
| 1037 |
+
}
|
| 1038 |
+
if (double_from_pyobj(&d, obj, errmess)) {
|
| 1039 |
+
*v = (long_double)d;
|
| 1040 |
+
return 1;
|
| 1041 |
+
}
|
| 1042 |
+
return 0;
|
| 1043 |
+
}
|
| 1044 |
+
"""
|
| 1045 |
+
|
| 1046 |
+
|
| 1047 |
+
cfuncs['double_from_pyobj'] = """
|
| 1048 |
+
static int
|
| 1049 |
+
double_from_pyobj(double* v, PyObject *obj, const char *errmess)
|
| 1050 |
+
{
|
| 1051 |
+
PyObject* tmp = NULL;
|
| 1052 |
+
if (PyFloat_Check(obj)) {
|
| 1053 |
+
*v = PyFloat_AsDouble(obj);
|
| 1054 |
+
return !(*v == -1.0 && PyErr_Occurred());
|
| 1055 |
+
}
|
| 1056 |
+
|
| 1057 |
+
tmp = PyNumber_Float(obj);
|
| 1058 |
+
if (tmp) {
|
| 1059 |
+
*v = PyFloat_AsDouble(tmp);
|
| 1060 |
+
Py_DECREF(tmp);
|
| 1061 |
+
return !(*v == -1.0 && PyErr_Occurred());
|
| 1062 |
+
}
|
| 1063 |
+
|
| 1064 |
+
if (PyComplex_Check(obj)) {
|
| 1065 |
+
PyErr_Clear();
|
| 1066 |
+
tmp = PyObject_GetAttrString(obj,\"real\");
|
| 1067 |
+
}
|
| 1068 |
+
else if (PyBytes_Check(obj) || PyUnicode_Check(obj)) {
|
| 1069 |
+
/*pass*/;
|
| 1070 |
+
}
|
| 1071 |
+
else if (PySequence_Check(obj)) {
|
| 1072 |
+
PyErr_Clear();
|
| 1073 |
+
tmp = PySequence_GetItem(obj, 0);
|
| 1074 |
+
}
|
| 1075 |
+
|
| 1076 |
+
if (tmp) {
|
| 1077 |
+
if (double_from_pyobj(v,tmp,errmess)) {Py_DECREF(tmp); return 1;}
|
| 1078 |
+
Py_DECREF(tmp);
|
| 1079 |
+
}
|
| 1080 |
+
{
|
| 1081 |
+
PyObject* err = PyErr_Occurred();
|
| 1082 |
+
if (err==NULL) err = #modulename#_error;
|
| 1083 |
+
PyErr_SetString(err,errmess);
|
| 1084 |
+
}
|
| 1085 |
+
return 0;
|
| 1086 |
+
}
|
| 1087 |
+
"""
|
| 1088 |
+
|
| 1089 |
+
|
| 1090 |
+
needs['float_from_pyobj'] = ['double_from_pyobj']
|
| 1091 |
+
cfuncs['float_from_pyobj'] = """
|
| 1092 |
+
static int
|
| 1093 |
+
float_from_pyobj(float* v, PyObject *obj, const char *errmess)
|
| 1094 |
+
{
|
| 1095 |
+
double d=0.0;
|
| 1096 |
+
if (double_from_pyobj(&d,obj,errmess)) {
|
| 1097 |
+
*v = (float)d;
|
| 1098 |
+
return 1;
|
| 1099 |
+
}
|
| 1100 |
+
return 0;
|
| 1101 |
+
}
|
| 1102 |
+
"""
|
| 1103 |
+
|
| 1104 |
+
|
| 1105 |
+
needs['complex_long_double_from_pyobj'] = ['complex_long_double', 'long_double',
|
| 1106 |
+
'complex_double_from_pyobj', 'npy_math.h']
|
| 1107 |
+
cfuncs['complex_long_double_from_pyobj'] = """
|
| 1108 |
+
static int
|
| 1109 |
+
complex_long_double_from_pyobj(complex_long_double* v, PyObject *obj, const char *errmess)
|
| 1110 |
+
{
|
| 1111 |
+
complex_double cd = {0.0,0.0};
|
| 1112 |
+
if (PyArray_CheckScalar(obj)){
|
| 1113 |
+
if PyArray_IsScalar(obj, CLongDouble) {
|
| 1114 |
+
PyArray_ScalarAsCtype(obj, v);
|
| 1115 |
+
return 1;
|
| 1116 |
+
}
|
| 1117 |
+
else if (PyArray_Check(obj) && PyArray_TYPE(obj)==NPY_CLONGDOUBLE) {
|
| 1118 |
+
(*v).r = npy_creall(*(((npy_clongdouble *)PyArray_DATA(obj))));
|
| 1119 |
+
(*v).i = npy_cimagl(*(((npy_clongdouble *)PyArray_DATA(obj))));
|
| 1120 |
+
return 1;
|
| 1121 |
+
}
|
| 1122 |
+
}
|
| 1123 |
+
if (complex_double_from_pyobj(&cd,obj,errmess)) {
|
| 1124 |
+
(*v).r = (long_double)cd.r;
|
| 1125 |
+
(*v).i = (long_double)cd.i;
|
| 1126 |
+
return 1;
|
| 1127 |
+
}
|
| 1128 |
+
return 0;
|
| 1129 |
+
}
|
| 1130 |
+
"""
|
| 1131 |
+
|
| 1132 |
+
|
| 1133 |
+
needs['complex_double_from_pyobj'] = ['complex_double', 'npy_math.h']
|
| 1134 |
+
cfuncs['complex_double_from_pyobj'] = """
|
| 1135 |
+
static int
|
| 1136 |
+
complex_double_from_pyobj(complex_double* v, PyObject *obj, const char *errmess) {
|
| 1137 |
+
Py_complex c;
|
| 1138 |
+
if (PyComplex_Check(obj)) {
|
| 1139 |
+
c = PyComplex_AsCComplex(obj);
|
| 1140 |
+
(*v).r = c.real;
|
| 1141 |
+
(*v).i = c.imag;
|
| 1142 |
+
return 1;
|
| 1143 |
+
}
|
| 1144 |
+
if (PyArray_IsScalar(obj, ComplexFloating)) {
|
| 1145 |
+
if (PyArray_IsScalar(obj, CFloat)) {
|
| 1146 |
+
npy_cfloat new;
|
| 1147 |
+
PyArray_ScalarAsCtype(obj, &new);
|
| 1148 |
+
(*v).r = (double)npy_crealf(new);
|
| 1149 |
+
(*v).i = (double)npy_cimagf(new);
|
| 1150 |
+
}
|
| 1151 |
+
else if (PyArray_IsScalar(obj, CLongDouble)) {
|
| 1152 |
+
npy_clongdouble new;
|
| 1153 |
+
PyArray_ScalarAsCtype(obj, &new);
|
| 1154 |
+
(*v).r = (double)npy_creall(new);
|
| 1155 |
+
(*v).i = (double)npy_cimagl(new);
|
| 1156 |
+
}
|
| 1157 |
+
else { /* if (PyArray_IsScalar(obj, CDouble)) */
|
| 1158 |
+
PyArray_ScalarAsCtype(obj, v);
|
| 1159 |
+
}
|
| 1160 |
+
return 1;
|
| 1161 |
+
}
|
| 1162 |
+
if (PyArray_CheckScalar(obj)) { /* 0-dim array or still array scalar */
|
| 1163 |
+
PyArrayObject *arr;
|
| 1164 |
+
if (PyArray_Check(obj)) {
|
| 1165 |
+
arr = (PyArrayObject *)PyArray_Cast((PyArrayObject *)obj, NPY_CDOUBLE);
|
| 1166 |
+
}
|
| 1167 |
+
else {
|
| 1168 |
+
arr = (PyArrayObject *)PyArray_FromScalar(obj, PyArray_DescrFromType(NPY_CDOUBLE));
|
| 1169 |
+
}
|
| 1170 |
+
if (arr == NULL) {
|
| 1171 |
+
return 0;
|
| 1172 |
+
}
|
| 1173 |
+
(*v).r = npy_creal(*(((npy_cdouble *)PyArray_DATA(arr))));
|
| 1174 |
+
(*v).i = npy_cimag(*(((npy_cdouble *)PyArray_DATA(arr))));
|
| 1175 |
+
Py_DECREF(arr);
|
| 1176 |
+
return 1;
|
| 1177 |
+
}
|
| 1178 |
+
/* Python does not provide PyNumber_Complex function :-( */
|
| 1179 |
+
(*v).i = 0.0;
|
| 1180 |
+
if (PyFloat_Check(obj)) {
|
| 1181 |
+
(*v).r = PyFloat_AsDouble(obj);
|
| 1182 |
+
return !((*v).r == -1.0 && PyErr_Occurred());
|
| 1183 |
+
}
|
| 1184 |
+
if (PyLong_Check(obj)) {
|
| 1185 |
+
(*v).r = PyLong_AsDouble(obj);
|
| 1186 |
+
return !((*v).r == -1.0 && PyErr_Occurred());
|
| 1187 |
+
}
|
| 1188 |
+
if (PySequence_Check(obj) && !(PyBytes_Check(obj) || PyUnicode_Check(obj))) {
|
| 1189 |
+
PyObject *tmp = PySequence_GetItem(obj,0);
|
| 1190 |
+
if (tmp) {
|
| 1191 |
+
if (complex_double_from_pyobj(v,tmp,errmess)) {
|
| 1192 |
+
Py_DECREF(tmp);
|
| 1193 |
+
return 1;
|
| 1194 |
+
}
|
| 1195 |
+
Py_DECREF(tmp);
|
| 1196 |
+
}
|
| 1197 |
+
}
|
| 1198 |
+
{
|
| 1199 |
+
PyObject* err = PyErr_Occurred();
|
| 1200 |
+
if (err==NULL)
|
| 1201 |
+
err = PyExc_TypeError;
|
| 1202 |
+
PyErr_SetString(err,errmess);
|
| 1203 |
+
}
|
| 1204 |
+
return 0;
|
| 1205 |
+
}
|
| 1206 |
+
"""
|
| 1207 |
+
|
| 1208 |
+
|
| 1209 |
+
needs['complex_float_from_pyobj'] = [
|
| 1210 |
+
'complex_float', 'complex_double_from_pyobj']
|
| 1211 |
+
cfuncs['complex_float_from_pyobj'] = """
|
| 1212 |
+
static int
|
| 1213 |
+
complex_float_from_pyobj(complex_float* v,PyObject *obj,const char *errmess)
|
| 1214 |
+
{
|
| 1215 |
+
complex_double cd={0.0,0.0};
|
| 1216 |
+
if (complex_double_from_pyobj(&cd,obj,errmess)) {
|
| 1217 |
+
(*v).r = (float)cd.r;
|
| 1218 |
+
(*v).i = (float)cd.i;
|
| 1219 |
+
return 1;
|
| 1220 |
+
}
|
| 1221 |
+
return 0;
|
| 1222 |
+
}
|
| 1223 |
+
"""
|
| 1224 |
+
|
| 1225 |
+
|
| 1226 |
+
cfuncs['try_pyarr_from_character'] = """
|
| 1227 |
+
static int try_pyarr_from_character(PyObject* obj, character* v) {
|
| 1228 |
+
PyArrayObject *arr = (PyArrayObject*)obj;
|
| 1229 |
+
if (!obj) return -2;
|
| 1230 |
+
if (PyArray_Check(obj)) {
|
| 1231 |
+
if (F2PY_ARRAY_IS_CHARACTER_COMPATIBLE(arr)) {
|
| 1232 |
+
*(character *)(PyArray_DATA(arr)) = *v;
|
| 1233 |
+
return 1;
|
| 1234 |
+
}
|
| 1235 |
+
}
|
| 1236 |
+
{
|
| 1237 |
+
char mess[F2PY_MESSAGE_BUFFER_SIZE];
|
| 1238 |
+
PyObject* err = PyErr_Occurred();
|
| 1239 |
+
if (err == NULL) {
|
| 1240 |
+
err = PyExc_ValueError;
|
| 1241 |
+
strcpy(mess, "try_pyarr_from_character failed"
|
| 1242 |
+
" -- expected bytes array-scalar|array, got ");
|
| 1243 |
+
f2py_describe(obj, mess + strlen(mess));
|
| 1244 |
+
PyErr_SetString(err, mess);
|
| 1245 |
+
}
|
| 1246 |
+
}
|
| 1247 |
+
return 0;
|
| 1248 |
+
}
|
| 1249 |
+
"""
|
| 1250 |
+
|
| 1251 |
+
needs['try_pyarr_from_char'] = ['pyobj_from_char1', 'TRYPYARRAYTEMPLATE']
|
| 1252 |
+
cfuncs[
|
| 1253 |
+
'try_pyarr_from_char'] = 'static int try_pyarr_from_char(PyObject* obj,char* v) {\n TRYPYARRAYTEMPLATE(char,\'c\');\n}\n'
|
| 1254 |
+
needs['try_pyarr_from_signed_char'] = ['TRYPYARRAYTEMPLATE', 'unsigned_char']
|
| 1255 |
+
cfuncs[
|
| 1256 |
+
'try_pyarr_from_unsigned_char'] = 'static int try_pyarr_from_unsigned_char(PyObject* obj,unsigned_char* v) {\n TRYPYARRAYTEMPLATE(unsigned_char,\'b\');\n}\n'
|
| 1257 |
+
needs['try_pyarr_from_signed_char'] = ['TRYPYARRAYTEMPLATE', 'signed_char']
|
| 1258 |
+
cfuncs[
|
| 1259 |
+
'try_pyarr_from_signed_char'] = 'static int try_pyarr_from_signed_char(PyObject* obj,signed_char* v) {\n TRYPYARRAYTEMPLATE(signed_char,\'1\');\n}\n'
|
| 1260 |
+
needs['try_pyarr_from_short'] = ['pyobj_from_short1', 'TRYPYARRAYTEMPLATE']
|
| 1261 |
+
cfuncs[
|
| 1262 |
+
'try_pyarr_from_short'] = 'static int try_pyarr_from_short(PyObject* obj,short* v) {\n TRYPYARRAYTEMPLATE(short,\'s\');\n}\n'
|
| 1263 |
+
needs['try_pyarr_from_int'] = ['pyobj_from_int1', 'TRYPYARRAYTEMPLATE']
|
| 1264 |
+
cfuncs[
|
| 1265 |
+
'try_pyarr_from_int'] = 'static int try_pyarr_from_int(PyObject* obj,int* v) {\n TRYPYARRAYTEMPLATE(int,\'i\');\n}\n'
|
| 1266 |
+
needs['try_pyarr_from_long'] = ['pyobj_from_long1', 'TRYPYARRAYTEMPLATE']
|
| 1267 |
+
cfuncs[
|
| 1268 |
+
'try_pyarr_from_long'] = 'static int try_pyarr_from_long(PyObject* obj,long* v) {\n TRYPYARRAYTEMPLATE(long,\'l\');\n}\n'
|
| 1269 |
+
needs['try_pyarr_from_long_long'] = [
|
| 1270 |
+
'pyobj_from_long_long1', 'TRYPYARRAYTEMPLATE', 'long_long']
|
| 1271 |
+
cfuncs[
|
| 1272 |
+
'try_pyarr_from_long_long'] = 'static int try_pyarr_from_long_long(PyObject* obj,long_long* v) {\n TRYPYARRAYTEMPLATE(long_long,\'L\');\n}\n'
|
| 1273 |
+
needs['try_pyarr_from_float'] = ['pyobj_from_float1', 'TRYPYARRAYTEMPLATE']
|
| 1274 |
+
cfuncs[
|
| 1275 |
+
'try_pyarr_from_float'] = 'static int try_pyarr_from_float(PyObject* obj,float* v) {\n TRYPYARRAYTEMPLATE(float,\'f\');\n}\n'
|
| 1276 |
+
needs['try_pyarr_from_double'] = ['pyobj_from_double1', 'TRYPYARRAYTEMPLATE']
|
| 1277 |
+
cfuncs[
|
| 1278 |
+
'try_pyarr_from_double'] = 'static int try_pyarr_from_double(PyObject* obj,double* v) {\n TRYPYARRAYTEMPLATE(double,\'d\');\n}\n'
|
| 1279 |
+
needs['try_pyarr_from_complex_float'] = [
|
| 1280 |
+
'pyobj_from_complex_float1', 'TRYCOMPLEXPYARRAYTEMPLATE', 'complex_float']
|
| 1281 |
+
cfuncs[
|
| 1282 |
+
'try_pyarr_from_complex_float'] = 'static int try_pyarr_from_complex_float(PyObject* obj,complex_float* v) {\n TRYCOMPLEXPYARRAYTEMPLATE(float,\'F\');\n}\n'
|
| 1283 |
+
needs['try_pyarr_from_complex_double'] = [
|
| 1284 |
+
'pyobj_from_complex_double1', 'TRYCOMPLEXPYARRAYTEMPLATE', 'complex_double']
|
| 1285 |
+
cfuncs[
|
| 1286 |
+
'try_pyarr_from_complex_double'] = 'static int try_pyarr_from_complex_double(PyObject* obj,complex_double* v) {\n TRYCOMPLEXPYARRAYTEMPLATE(double,\'D\');\n}\n'
|
| 1287 |
+
|
| 1288 |
+
|
| 1289 |
+
needs['create_cb_arglist'] = ['CFUNCSMESS', 'PRINTPYOBJERR', 'MINMAX']
|
| 1290 |
+
# create the list of arguments to be used when calling back to python
|
| 1291 |
+
cfuncs['create_cb_arglist'] = """
|
| 1292 |
+
static int
|
| 1293 |
+
create_cb_arglist(PyObject* fun, PyTupleObject* xa , const int maxnofargs,
|
| 1294 |
+
const int nofoptargs, int *nofargs, PyTupleObject **args,
|
| 1295 |
+
const char *errmess)
|
| 1296 |
+
{
|
| 1297 |
+
PyObject *tmp = NULL;
|
| 1298 |
+
PyObject *tmp_fun = NULL;
|
| 1299 |
+
Py_ssize_t tot, opt, ext, siz, i, di = 0;
|
| 1300 |
+
CFUNCSMESS(\"create_cb_arglist\\n\");
|
| 1301 |
+
tot=opt=ext=siz=0;
|
| 1302 |
+
/* Get the total number of arguments */
|
| 1303 |
+
if (PyFunction_Check(fun)) {
|
| 1304 |
+
tmp_fun = fun;
|
| 1305 |
+
Py_INCREF(tmp_fun);
|
| 1306 |
+
}
|
| 1307 |
+
else {
|
| 1308 |
+
di = 1;
|
| 1309 |
+
if (PyObject_HasAttrString(fun,\"im_func\")) {
|
| 1310 |
+
tmp_fun = PyObject_GetAttrString(fun,\"im_func\");
|
| 1311 |
+
}
|
| 1312 |
+
else if (PyObject_HasAttrString(fun,\"__call__\")) {
|
| 1313 |
+
tmp = PyObject_GetAttrString(fun,\"__call__\");
|
| 1314 |
+
if (PyObject_HasAttrString(tmp,\"im_func\"))
|
| 1315 |
+
tmp_fun = PyObject_GetAttrString(tmp,\"im_func\");
|
| 1316 |
+
else {
|
| 1317 |
+
tmp_fun = fun; /* built-in function */
|
| 1318 |
+
Py_INCREF(tmp_fun);
|
| 1319 |
+
tot = maxnofargs;
|
| 1320 |
+
if (PyCFunction_Check(fun)) {
|
| 1321 |
+
/* In case the function has a co_argcount (like on PyPy) */
|
| 1322 |
+
di = 0;
|
| 1323 |
+
}
|
| 1324 |
+
if (xa != NULL)
|
| 1325 |
+
tot += PyTuple_Size((PyObject *)xa);
|
| 1326 |
+
}
|
| 1327 |
+
Py_XDECREF(tmp);
|
| 1328 |
+
}
|
| 1329 |
+
else if (PyFortran_Check(fun) || PyFortran_Check1(fun)) {
|
| 1330 |
+
tot = maxnofargs;
|
| 1331 |
+
if (xa != NULL)
|
| 1332 |
+
tot += PyTuple_Size((PyObject *)xa);
|
| 1333 |
+
tmp_fun = fun;
|
| 1334 |
+
Py_INCREF(tmp_fun);
|
| 1335 |
+
}
|
| 1336 |
+
else if (F2PyCapsule_Check(fun)) {
|
| 1337 |
+
tot = maxnofargs;
|
| 1338 |
+
if (xa != NULL)
|
| 1339 |
+
ext = PyTuple_Size((PyObject *)xa);
|
| 1340 |
+
if(ext>0) {
|
| 1341 |
+
fprintf(stderr,\"extra arguments tuple cannot be used with PyCapsule call-back\\n\");
|
| 1342 |
+
goto capi_fail;
|
| 1343 |
+
}
|
| 1344 |
+
tmp_fun = fun;
|
| 1345 |
+
Py_INCREF(tmp_fun);
|
| 1346 |
+
}
|
| 1347 |
+
}
|
| 1348 |
+
|
| 1349 |
+
if (tmp_fun == NULL) {
|
| 1350 |
+
fprintf(stderr,
|
| 1351 |
+
\"Call-back argument must be function|instance|instance.__call__|f2py-function \"
|
| 1352 |
+
\"but got %s.\\n\",
|
| 1353 |
+
((fun == NULL) ? \"NULL\" : Py_TYPE(fun)->tp_name));
|
| 1354 |
+
goto capi_fail;
|
| 1355 |
+
}
|
| 1356 |
+
|
| 1357 |
+
if (PyObject_HasAttrString(tmp_fun,\"__code__\")) {
|
| 1358 |
+
if (PyObject_HasAttrString(tmp = PyObject_GetAttrString(tmp_fun,\"__code__\"),\"co_argcount\")) {
|
| 1359 |
+
PyObject *tmp_argcount = PyObject_GetAttrString(tmp,\"co_argcount\");
|
| 1360 |
+
Py_DECREF(tmp);
|
| 1361 |
+
if (tmp_argcount == NULL) {
|
| 1362 |
+
goto capi_fail;
|
| 1363 |
+
}
|
| 1364 |
+
tot = PyLong_AsSsize_t(tmp_argcount) - di;
|
| 1365 |
+
Py_DECREF(tmp_argcount);
|
| 1366 |
+
}
|
| 1367 |
+
}
|
| 1368 |
+
/* Get the number of optional arguments */
|
| 1369 |
+
if (PyObject_HasAttrString(tmp_fun,\"__defaults__\")) {
|
| 1370 |
+
if (PyTuple_Check(tmp = PyObject_GetAttrString(tmp_fun,\"__defaults__\")))
|
| 1371 |
+
opt = PyTuple_Size(tmp);
|
| 1372 |
+
Py_XDECREF(tmp);
|
| 1373 |
+
}
|
| 1374 |
+
/* Get the number of extra arguments */
|
| 1375 |
+
if (xa != NULL)
|
| 1376 |
+
ext = PyTuple_Size((PyObject *)xa);
|
| 1377 |
+
/* Calculate the size of call-backs argument list */
|
| 1378 |
+
siz = MIN(maxnofargs+ext,tot);
|
| 1379 |
+
*nofargs = MAX(0,siz-ext);
|
| 1380 |
+
|
| 1381 |
+
#ifdef DEBUGCFUNCS
|
| 1382 |
+
fprintf(stderr,
|
| 1383 |
+
\"debug-capi:create_cb_arglist:maxnofargs(-nofoptargs),\"
|
| 1384 |
+
\"tot,opt,ext,siz,nofargs = %d(-%d), %zd, %zd, %zd, %zd, %d\\n\",
|
| 1385 |
+
maxnofargs, nofoptargs, tot, opt, ext, siz, *nofargs);
|
| 1386 |
+
#endif
|
| 1387 |
+
|
| 1388 |
+
if (siz < tot-opt) {
|
| 1389 |
+
fprintf(stderr,
|
| 1390 |
+
\"create_cb_arglist: Failed to build argument list \"
|
| 1391 |
+
\"(siz) with enough arguments (tot-opt) required by \"
|
| 1392 |
+
\"user-supplied function (siz,tot,opt=%zd, %zd, %zd).\\n\",
|
| 1393 |
+
siz, tot, opt);
|
| 1394 |
+
goto capi_fail;
|
| 1395 |
+
}
|
| 1396 |
+
|
| 1397 |
+
/* Initialize argument list */
|
| 1398 |
+
*args = (PyTupleObject *)PyTuple_New(siz);
|
| 1399 |
+
for (i=0;i<*nofargs;i++) {
|
| 1400 |
+
Py_INCREF(Py_None);
|
| 1401 |
+
PyTuple_SET_ITEM((PyObject *)(*args),i,Py_None);
|
| 1402 |
+
}
|
| 1403 |
+
if (xa != NULL)
|
| 1404 |
+
for (i=(*nofargs);i<siz;i++) {
|
| 1405 |
+
tmp = PyTuple_GetItem((PyObject *)xa,i-(*nofargs));
|
| 1406 |
+
Py_INCREF(tmp);
|
| 1407 |
+
PyTuple_SET_ITEM(*args,i,tmp);
|
| 1408 |
+
}
|
| 1409 |
+
CFUNCSMESS(\"create_cb_arglist-end\\n\");
|
| 1410 |
+
Py_DECREF(tmp_fun);
|
| 1411 |
+
return 1;
|
| 1412 |
+
|
| 1413 |
+
capi_fail:
|
| 1414 |
+
if (PyErr_Occurred() == NULL)
|
| 1415 |
+
PyErr_SetString(#modulename#_error, errmess);
|
| 1416 |
+
Py_XDECREF(tmp_fun);
|
| 1417 |
+
return 0;
|
| 1418 |
+
}
|
| 1419 |
+
"""
|
| 1420 |
+
|
| 1421 |
+
|
| 1422 |
+
def buildcfuncs():
|
| 1423 |
+
from .capi_maps import c2capi_map
|
| 1424 |
+
for k in c2capi_map.keys():
|
| 1425 |
+
m = 'pyarr_from_p_%s1' % k
|
| 1426 |
+
cppmacros[
|
| 1427 |
+
m] = '#define %s(v) (PyArray_SimpleNewFromData(0,NULL,%s,(char *)v))' % (m, c2capi_map[k])
|
| 1428 |
+
k = 'string'
|
| 1429 |
+
m = 'pyarr_from_p_%s1' % k
|
| 1430 |
+
# NPY_CHAR compatibility, NPY_STRING with itemsize 1
|
| 1431 |
+
cppmacros[
|
| 1432 |
+
m] = '#define %s(v,dims) (PyArray_New(&PyArray_Type, 1, dims, NPY_STRING, NULL, v, 1, NPY_ARRAY_CARRAY, NULL))' % (m)
|
| 1433 |
+
|
| 1434 |
+
|
| 1435 |
+
############ Auxiliary functions for sorting needs ###################
|
| 1436 |
+
|
| 1437 |
+
def append_needs(need, flag=1):
|
| 1438 |
+
# This function modifies the contents of the global `outneeds` dict.
|
| 1439 |
+
if isinstance(need, list):
|
| 1440 |
+
for n in need:
|
| 1441 |
+
append_needs(n, flag)
|
| 1442 |
+
elif isinstance(need, str):
|
| 1443 |
+
if not need:
|
| 1444 |
+
return
|
| 1445 |
+
if need in includes0:
|
| 1446 |
+
n = 'includes0'
|
| 1447 |
+
elif need in includes:
|
| 1448 |
+
n = 'includes'
|
| 1449 |
+
elif need in typedefs:
|
| 1450 |
+
n = 'typedefs'
|
| 1451 |
+
elif need in typedefs_generated:
|
| 1452 |
+
n = 'typedefs_generated'
|
| 1453 |
+
elif need in cppmacros:
|
| 1454 |
+
n = 'cppmacros'
|
| 1455 |
+
elif need in cfuncs:
|
| 1456 |
+
n = 'cfuncs'
|
| 1457 |
+
elif need in callbacks:
|
| 1458 |
+
n = 'callbacks'
|
| 1459 |
+
elif need in f90modhooks:
|
| 1460 |
+
n = 'f90modhooks'
|
| 1461 |
+
elif need in commonhooks:
|
| 1462 |
+
n = 'commonhooks'
|
| 1463 |
+
else:
|
| 1464 |
+
errmess('append_needs: unknown need %s\n' % (repr(need)))
|
| 1465 |
+
return
|
| 1466 |
+
if need in outneeds[n]:
|
| 1467 |
+
return
|
| 1468 |
+
if flag:
|
| 1469 |
+
tmp = {}
|
| 1470 |
+
if need in needs:
|
| 1471 |
+
for nn in needs[need]:
|
| 1472 |
+
t = append_needs(nn, 0)
|
| 1473 |
+
if isinstance(t, dict):
|
| 1474 |
+
for nnn in t.keys():
|
| 1475 |
+
if nnn in tmp:
|
| 1476 |
+
tmp[nnn] = tmp[nnn] + t[nnn]
|
| 1477 |
+
else:
|
| 1478 |
+
tmp[nnn] = t[nnn]
|
| 1479 |
+
for nn in tmp.keys():
|
| 1480 |
+
for nnn in tmp[nn]:
|
| 1481 |
+
if nnn not in outneeds[nn]:
|
| 1482 |
+
outneeds[nn] = [nnn] + outneeds[nn]
|
| 1483 |
+
outneeds[n].append(need)
|
| 1484 |
+
else:
|
| 1485 |
+
tmp = {}
|
| 1486 |
+
if need in needs:
|
| 1487 |
+
for nn in needs[need]:
|
| 1488 |
+
t = append_needs(nn, flag)
|
| 1489 |
+
if isinstance(t, dict):
|
| 1490 |
+
for nnn in t.keys():
|
| 1491 |
+
if nnn in tmp:
|
| 1492 |
+
tmp[nnn] = t[nnn] + tmp[nnn]
|
| 1493 |
+
else:
|
| 1494 |
+
tmp[nnn] = t[nnn]
|
| 1495 |
+
if n not in tmp:
|
| 1496 |
+
tmp[n] = []
|
| 1497 |
+
tmp[n].append(need)
|
| 1498 |
+
return tmp
|
| 1499 |
+
else:
|
| 1500 |
+
errmess('append_needs: expected list or string but got :%s\n' %
|
| 1501 |
+
(repr(need)))
|
| 1502 |
+
|
| 1503 |
+
|
| 1504 |
+
def get_needs():
|
| 1505 |
+
# This function modifies the contents of the global `outneeds` dict.
|
| 1506 |
+
res = {}
|
| 1507 |
+
for n in outneeds.keys():
|
| 1508 |
+
out = []
|
| 1509 |
+
saveout = copy.copy(outneeds[n])
|
| 1510 |
+
while len(outneeds[n]) > 0:
|
| 1511 |
+
if outneeds[n][0] not in needs:
|
| 1512 |
+
out.append(outneeds[n][0])
|
| 1513 |
+
del outneeds[n][0]
|
| 1514 |
+
else:
|
| 1515 |
+
flag = 0
|
| 1516 |
+
for k in outneeds[n][1:]:
|
| 1517 |
+
if k in needs[outneeds[n][0]]:
|
| 1518 |
+
flag = 1
|
| 1519 |
+
break
|
| 1520 |
+
if flag:
|
| 1521 |
+
outneeds[n] = outneeds[n][1:] + [outneeds[n][0]]
|
| 1522 |
+
else:
|
| 1523 |
+
out.append(outneeds[n][0])
|
| 1524 |
+
del outneeds[n][0]
|
| 1525 |
+
if saveout and (0 not in map(lambda x, y: x == y, saveout, outneeds[n])) \
|
| 1526 |
+
and outneeds[n] != []:
|
| 1527 |
+
print(n, saveout)
|
| 1528 |
+
errmess(
|
| 1529 |
+
'get_needs: no progress in sorting needs, probably circular dependence, skipping.\n')
|
| 1530 |
+
out = out + saveout
|
| 1531 |
+
break
|
| 1532 |
+
saveout = copy.copy(outneeds[n])
|
| 1533 |
+
if out == []:
|
| 1534 |
+
out = [n]
|
| 1535 |
+
res[n] = out
|
| 1536 |
+
return res
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/common_rules.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Build common block mechanism for f2py2e.
|
| 3 |
+
|
| 4 |
+
Copyright 1999 -- 2011 Pearu Peterson all rights reserved.
|
| 5 |
+
Copyright 2011 -- present NumPy Developers.
|
| 6 |
+
Permission to use, modify, and distribute this software is given under the
|
| 7 |
+
terms of the NumPy License
|
| 8 |
+
|
| 9 |
+
NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
| 10 |
+
"""
|
| 11 |
+
from . import __version__
|
| 12 |
+
f2py_version = __version__.version
|
| 13 |
+
|
| 14 |
+
from .auxfuncs import (
|
| 15 |
+
hasbody, hascommon, hasnote, isintent_hide, outmess, getuseblocks
|
| 16 |
+
)
|
| 17 |
+
from . import capi_maps
|
| 18 |
+
from . import func2subr
|
| 19 |
+
from .crackfortran import rmbadname
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def findcommonblocks(block, top=1):
|
| 23 |
+
ret = []
|
| 24 |
+
if hascommon(block):
|
| 25 |
+
for key, value in block['common'].items():
|
| 26 |
+
vars_ = {v: block['vars'][v] for v in value}
|
| 27 |
+
ret.append((key, value, vars_))
|
| 28 |
+
elif hasbody(block):
|
| 29 |
+
for b in block['body']:
|
| 30 |
+
ret = ret + findcommonblocks(b, 0)
|
| 31 |
+
if top:
|
| 32 |
+
tret = []
|
| 33 |
+
names = []
|
| 34 |
+
for t in ret:
|
| 35 |
+
if t[0] not in names:
|
| 36 |
+
names.append(t[0])
|
| 37 |
+
tret.append(t)
|
| 38 |
+
return tret
|
| 39 |
+
return ret
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def buildhooks(m):
|
| 43 |
+
ret = {'commonhooks': [], 'initcommonhooks': [],
|
| 44 |
+
'docs': ['"COMMON blocks:\\n"']}
|
| 45 |
+
fwrap = ['']
|
| 46 |
+
|
| 47 |
+
def fadd(line, s=fwrap):
|
| 48 |
+
s[0] = '%s\n %s' % (s[0], line)
|
| 49 |
+
chooks = ['']
|
| 50 |
+
|
| 51 |
+
def cadd(line, s=chooks):
|
| 52 |
+
s[0] = '%s\n%s' % (s[0], line)
|
| 53 |
+
ihooks = ['']
|
| 54 |
+
|
| 55 |
+
def iadd(line, s=ihooks):
|
| 56 |
+
s[0] = '%s\n%s' % (s[0], line)
|
| 57 |
+
doc = ['']
|
| 58 |
+
|
| 59 |
+
def dadd(line, s=doc):
|
| 60 |
+
s[0] = '%s\n%s' % (s[0], line)
|
| 61 |
+
for (name, vnames, vars) in findcommonblocks(m):
|
| 62 |
+
lower_name = name.lower()
|
| 63 |
+
hnames, inames = [], []
|
| 64 |
+
for n in vnames:
|
| 65 |
+
if isintent_hide(vars[n]):
|
| 66 |
+
hnames.append(n)
|
| 67 |
+
else:
|
| 68 |
+
inames.append(n)
|
| 69 |
+
if hnames:
|
| 70 |
+
outmess('\t\tConstructing COMMON block support for "%s"...\n\t\t %s\n\t\t Hidden: %s\n' % (
|
| 71 |
+
name, ','.join(inames), ','.join(hnames)))
|
| 72 |
+
else:
|
| 73 |
+
outmess('\t\tConstructing COMMON block support for "%s"...\n\t\t %s\n' % (
|
| 74 |
+
name, ','.join(inames)))
|
| 75 |
+
fadd('subroutine f2pyinit%s(setupfunc)' % name)
|
| 76 |
+
for usename in getuseblocks(m):
|
| 77 |
+
fadd(f'use {usename}')
|
| 78 |
+
fadd('external setupfunc')
|
| 79 |
+
for n in vnames:
|
| 80 |
+
fadd(func2subr.var2fixfortran(vars, n))
|
| 81 |
+
if name == '_BLNK_':
|
| 82 |
+
fadd('common %s' % (','.join(vnames)))
|
| 83 |
+
else:
|
| 84 |
+
fadd('common /%s/ %s' % (name, ','.join(vnames)))
|
| 85 |
+
fadd('call setupfunc(%s)' % (','.join(inames)))
|
| 86 |
+
fadd('end\n')
|
| 87 |
+
cadd('static FortranDataDef f2py_%s_def[] = {' % (name))
|
| 88 |
+
idims = []
|
| 89 |
+
for n in inames:
|
| 90 |
+
ct = capi_maps.getctype(vars[n])
|
| 91 |
+
elsize = capi_maps.get_elsize(vars[n])
|
| 92 |
+
at = capi_maps.c2capi_map[ct]
|
| 93 |
+
dm = capi_maps.getarrdims(n, vars[n])
|
| 94 |
+
if dm['dims']:
|
| 95 |
+
idims.append('(%s)' % (dm['dims']))
|
| 96 |
+
else:
|
| 97 |
+
idims.append('')
|
| 98 |
+
dms = dm['dims'].strip()
|
| 99 |
+
if not dms:
|
| 100 |
+
dms = '-1'
|
| 101 |
+
cadd('\t{\"%s\",%s,{{%s}},%s, %s},'
|
| 102 |
+
% (n, dm['rank'], dms, at, elsize))
|
| 103 |
+
cadd('\t{NULL}\n};')
|
| 104 |
+
inames1 = rmbadname(inames)
|
| 105 |
+
inames1_tps = ','.join(['char *' + s for s in inames1])
|
| 106 |
+
cadd('static void f2py_setup_%s(%s) {' % (name, inames1_tps))
|
| 107 |
+
cadd('\tint i_f2py=0;')
|
| 108 |
+
for n in inames1:
|
| 109 |
+
cadd('\tf2py_%s_def[i_f2py++].data = %s;' % (name, n))
|
| 110 |
+
cadd('}')
|
| 111 |
+
if '_' in lower_name:
|
| 112 |
+
F_FUNC = 'F_FUNC_US'
|
| 113 |
+
else:
|
| 114 |
+
F_FUNC = 'F_FUNC'
|
| 115 |
+
cadd('extern void %s(f2pyinit%s,F2PYINIT%s)(void(*)(%s));'
|
| 116 |
+
% (F_FUNC, lower_name, name.upper(),
|
| 117 |
+
','.join(['char*'] * len(inames1))))
|
| 118 |
+
cadd('static void f2py_init_%s(void) {' % name)
|
| 119 |
+
cadd('\t%s(f2pyinit%s,F2PYINIT%s)(f2py_setup_%s);'
|
| 120 |
+
% (F_FUNC, lower_name, name.upper(), name))
|
| 121 |
+
cadd('}\n')
|
| 122 |
+
iadd('\ttmp = PyFortranObject_New(f2py_%s_def,f2py_init_%s);' % (name, name))
|
| 123 |
+
iadd('\tif (tmp == NULL) return NULL;')
|
| 124 |
+
iadd('\tif (F2PyDict_SetItemString(d, \"%s\", tmp) == -1) return NULL;'
|
| 125 |
+
% name)
|
| 126 |
+
iadd('\tPy_DECREF(tmp);')
|
| 127 |
+
tname = name.replace('_', '\\_')
|
| 128 |
+
dadd('\\subsection{Common block \\texttt{%s}}\n' % (tname))
|
| 129 |
+
dadd('\\begin{description}')
|
| 130 |
+
for n in inames:
|
| 131 |
+
dadd('\\item[]{{}\\verb@%s@{}}' %
|
| 132 |
+
(capi_maps.getarrdocsign(n, vars[n])))
|
| 133 |
+
if hasnote(vars[n]):
|
| 134 |
+
note = vars[n]['note']
|
| 135 |
+
if isinstance(note, list):
|
| 136 |
+
note = '\n'.join(note)
|
| 137 |
+
dadd('--- %s' % (note))
|
| 138 |
+
dadd('\\end{description}')
|
| 139 |
+
ret['docs'].append(
|
| 140 |
+
'"\t/%s/ %s\\n"' % (name, ','.join(map(lambda v, d: v + d, inames, idims))))
|
| 141 |
+
ret['commonhooks'] = chooks
|
| 142 |
+
ret['initcommonhooks'] = ihooks
|
| 143 |
+
ret['latexdoc'] = doc[0]
|
| 144 |
+
if len(ret['docs']) <= 1:
|
| 145 |
+
ret['docs'] = ''
|
| 146 |
+
return ret, fwrap[0]
|
llmeval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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
f2py2e - Fortran to Python C/API generator. 2nd Edition.
|
| 5 |
+
See __usage__ below.
|
| 6 |
+
|
| 7 |
+
Copyright 1999 -- 2011 Pearu Peterson all rights reserved.
|
| 8 |
+
Copyright 2011 -- present NumPy Developers.
|
| 9 |
+
Permission to use, modify, and distribute this software is given under the
|
| 10 |
+
terms of the NumPy License.
|
| 11 |
+
|
| 12 |
+
NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
| 13 |
+
"""
|
| 14 |
+
import sys
|
| 15 |
+
import os
|
| 16 |
+
import pprint
|
| 17 |
+
import re
|
| 18 |
+
from pathlib import Path
|
| 19 |
+
from itertools import dropwhile
|
| 20 |
+
import argparse
|
| 21 |
+
import copy
|
| 22 |
+
|
| 23 |
+
from . import crackfortran
|
| 24 |
+
from . import rules
|
| 25 |
+
from . import cb_rules
|
| 26 |
+
from . import auxfuncs
|
| 27 |
+
from . import cfuncs
|
| 28 |
+
from . import f90mod_rules
|
| 29 |
+
from . import __version__
|
| 30 |
+
from . import capi_maps
|
| 31 |
+
from numpy.f2py._backends import f2py_build_generator
|
| 32 |
+
|
| 33 |
+
f2py_version = __version__.version
|
| 34 |
+
numpy_version = __version__.version
|
| 35 |
+
errmess = sys.stderr.write
|
| 36 |
+
# outmess=sys.stdout.write
|
| 37 |
+
show = pprint.pprint
|
| 38 |
+
outmess = auxfuncs.outmess
|
| 39 |
+
MESON_ONLY_VER = (sys.version_info >= (3, 12))
|
| 40 |
+
|
| 41 |
+
__usage__ =\
|
| 42 |
+
f"""Usage:
|
| 43 |
+
|
| 44 |
+
1) To construct extension module sources:
|
| 45 |
+
|
| 46 |
+
f2py [<options>] <fortran files> [[[only:]||[skip:]] \\
|
| 47 |
+
<fortran functions> ] \\
|
| 48 |
+
[: <fortran files> ...]
|
| 49 |
+
|
| 50 |
+
2) To compile fortran files and build extension modules:
|
| 51 |
+
|
| 52 |
+
f2py -c [<options>, <build_flib options>, <extra options>] <fortran files>
|
| 53 |
+
|
| 54 |
+
3) To generate signature files:
|
| 55 |
+
|
| 56 |
+
f2py -h <filename.pyf> ...< same options as in (1) >
|
| 57 |
+
|
| 58 |
+
Description: This program generates a Python C/API file (<modulename>module.c)
|
| 59 |
+
that contains wrappers for given fortran functions so that they
|
| 60 |
+
can be called from Python. With the -c option the corresponding
|
| 61 |
+
extension modules are built.
|
| 62 |
+
|
| 63 |
+
Options:
|
| 64 |
+
|
| 65 |
+
-h <filename> Write signatures of the fortran routines to file <filename>
|
| 66 |
+
and exit. You can then edit <filename> and use it instead
|
| 67 |
+
of <fortran files>. If <filename>==stdout then the
|
| 68 |
+
signatures are printed to stdout.
|
| 69 |
+
<fortran functions> Names of fortran routines for which Python C/API
|
| 70 |
+
functions will be generated. Default is all that are found
|
| 71 |
+
in <fortran files>.
|
| 72 |
+
<fortran files> Paths to fortran/signature files that will be scanned for
|
| 73 |
+
<fortran functions> in order to determine their signatures.
|
| 74 |
+
skip: Ignore fortran functions that follow until `:'.
|
| 75 |
+
only: Use only fortran functions that follow until `:'.
|
| 76 |
+
: Get back to <fortran files> mode.
|
| 77 |
+
|
| 78 |
+
-m <modulename> Name of the module; f2py generates a Python/C API
|
| 79 |
+
file <modulename>module.c or extension module <modulename>.
|
| 80 |
+
Default is 'untitled'.
|
| 81 |
+
|
| 82 |
+
'-include<header>' Writes additional headers in the C wrapper, can be passed
|
| 83 |
+
multiple times, generates #include <header> each time.
|
| 84 |
+
|
| 85 |
+
--[no-]lower Do [not] lower the cases in <fortran files>. By default,
|
| 86 |
+
--lower is assumed with -h key, and --no-lower without -h key.
|
| 87 |
+
|
| 88 |
+
--build-dir <dirname> All f2py generated files are created in <dirname>.
|
| 89 |
+
Default is tempfile.mkdtemp().
|
| 90 |
+
|
| 91 |
+
--overwrite-signature Overwrite existing signature file.
|
| 92 |
+
|
| 93 |
+
--[no-]latex-doc Create (or not) <modulename>module.tex.
|
| 94 |
+
Default is --no-latex-doc.
|
| 95 |
+
--short-latex Create 'incomplete' LaTeX document (without commands
|
| 96 |
+
\\documentclass, \\tableofcontents, and \\begin{{document}},
|
| 97 |
+
\\end{{document}}).
|
| 98 |
+
|
| 99 |
+
--[no-]rest-doc Create (or not) <modulename>module.rst.
|
| 100 |
+
Default is --no-rest-doc.
|
| 101 |
+
|
| 102 |
+
--debug-capi Create C/API code that reports the state of the wrappers
|
| 103 |
+
during runtime. Useful for debugging.
|
| 104 |
+
|
| 105 |
+
--[no-]wrap-functions Create Fortran subroutine wrappers to Fortran 77
|
| 106 |
+
functions. --wrap-functions is default because it ensures
|
| 107 |
+
maximum portability/compiler independence.
|
| 108 |
+
|
| 109 |
+
--include-paths <path1>:<path2>:... Search include files from the given
|
| 110 |
+
directories.
|
| 111 |
+
|
| 112 |
+
--help-link [..] List system resources found by system_info.py. See also
|
| 113 |
+
--link-<resource> switch below. [..] is optional list
|
| 114 |
+
of resources names. E.g. try 'f2py --help-link lapack_opt'.
|
| 115 |
+
|
| 116 |
+
--f2cmap <filename> Load Fortran-to-Python KIND specification from the given
|
| 117 |
+
file. Default: .f2py_f2cmap in current directory.
|
| 118 |
+
|
| 119 |
+
--quiet Run quietly.
|
| 120 |
+
--verbose Run with extra verbosity.
|
| 121 |
+
--skip-empty-wrappers Only generate wrapper files when needed.
|
| 122 |
+
-v Print f2py version ID and exit.
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
build backend options (only effective with -c)
|
| 126 |
+
[NO_MESON] is used to indicate an option not meant to be used
|
| 127 |
+
with the meson backend or above Python 3.12:
|
| 128 |
+
|
| 129 |
+
--fcompiler= Specify Fortran compiler type by vendor [NO_MESON]
|
| 130 |
+
--compiler= Specify distutils C compiler type [NO_MESON]
|
| 131 |
+
|
| 132 |
+
--help-fcompiler List available Fortran compilers and exit [NO_MESON]
|
| 133 |
+
--f77exec= Specify the path to F77 compiler [NO_MESON]
|
| 134 |
+
--f90exec= Specify the path to F90 compiler [NO_MESON]
|
| 135 |
+
--f77flags= Specify F77 compiler flags
|
| 136 |
+
--f90flags= Specify F90 compiler flags
|
| 137 |
+
--opt= Specify optimization flags [NO_MESON]
|
| 138 |
+
--arch= Specify architecture specific optimization flags [NO_MESON]
|
| 139 |
+
--noopt Compile without optimization [NO_MESON]
|
| 140 |
+
--noarch Compile without arch-dependent optimization [NO_MESON]
|
| 141 |
+
--debug Compile with debugging information
|
| 142 |
+
|
| 143 |
+
--dep <dependency>
|
| 144 |
+
Specify a meson dependency for the module. This may
|
| 145 |
+
be passed multiple times for multiple dependencies.
|
| 146 |
+
Dependencies are stored in a list for further processing.
|
| 147 |
+
|
| 148 |
+
Example: --dep lapack --dep scalapack
|
| 149 |
+
This will identify "lapack" and "scalapack" as dependencies
|
| 150 |
+
and remove them from argv, leaving a dependencies list
|
| 151 |
+
containing ["lapack", "scalapack"].
|
| 152 |
+
|
| 153 |
+
--backend <backend_type>
|
| 154 |
+
Specify the build backend for the compilation process.
|
| 155 |
+
The supported backends are 'meson' and 'distutils'.
|
| 156 |
+
If not specified, defaults to 'distutils'. On
|
| 157 |
+
Python 3.12 or higher, the default is 'meson'.
|
| 158 |
+
|
| 159 |
+
Extra options (only effective with -c):
|
| 160 |
+
|
| 161 |
+
--link-<resource> Link extension module with <resource> as defined
|
| 162 |
+
by numpy.distutils/system_info.py. E.g. to link
|
| 163 |
+
with optimized LAPACK libraries (vecLib on MacOSX,
|
| 164 |
+
ATLAS elsewhere), use --link-lapack_opt.
|
| 165 |
+
See also --help-link switch. [NO_MESON]
|
| 166 |
+
|
| 167 |
+
-L/path/to/lib/ -l<libname>
|
| 168 |
+
-D<define> -U<name>
|
| 169 |
+
-I/path/to/include/
|
| 170 |
+
<filename>.o <filename>.so <filename>.a
|
| 171 |
+
|
| 172 |
+
Using the following macros may be required with non-gcc Fortran
|
| 173 |
+
compilers:
|
| 174 |
+
-DPREPEND_FORTRAN -DNO_APPEND_FORTRAN -DUPPERCASE_FORTRAN
|
| 175 |
+
-DUNDERSCORE_G77
|
| 176 |
+
|
| 177 |
+
When using -DF2PY_REPORT_ATEXIT, a performance report of F2PY
|
| 178 |
+
interface is printed out at exit (platforms: Linux).
|
| 179 |
+
|
| 180 |
+
When using -DF2PY_REPORT_ON_ARRAY_COPY=<int>, a message is
|
| 181 |
+
sent to stderr whenever F2PY interface makes a copy of an
|
| 182 |
+
array. Integer <int> sets the threshold for array sizes when
|
| 183 |
+
a message should be shown.
|
| 184 |
+
|
| 185 |
+
Version: {f2py_version}
|
| 186 |
+
numpy Version: {numpy_version}
|
| 187 |
+
License: NumPy license (see LICENSE.txt in the NumPy source code)
|
| 188 |
+
Copyright 1999 -- 2011 Pearu Peterson all rights reserved.
|
| 189 |
+
Copyright 2011 -- present NumPy Developers.
|
| 190 |
+
https://numpy.org/doc/stable/f2py/index.html\n"""
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
def scaninputline(inputline):
|
| 194 |
+
files, skipfuncs, onlyfuncs, debug = [], [], [], []
|
| 195 |
+
f, f2, f3, f5, f6, f8, f9, f10 = 1, 0, 0, 0, 0, 0, 0, 0
|
| 196 |
+
verbose = 1
|
| 197 |
+
emptygen = True
|
| 198 |
+
dolc = -1
|
| 199 |
+
dolatexdoc = 0
|
| 200 |
+
dorestdoc = 0
|
| 201 |
+
wrapfuncs = 1
|
| 202 |
+
buildpath = '.'
|
| 203 |
+
include_paths, inputline = get_includes(inputline)
|
| 204 |
+
signsfile, modulename = None, None
|
| 205 |
+
options = {'buildpath': buildpath,
|
| 206 |
+
'coutput': None,
|
| 207 |
+
'f2py_wrapper_output': None}
|
| 208 |
+
for l in inputline:
|
| 209 |
+
if l == '':
|
| 210 |
+
pass
|
| 211 |
+
elif l == 'only:':
|
| 212 |
+
f = 0
|
| 213 |
+
elif l == 'skip:':
|
| 214 |
+
f = -1
|
| 215 |
+
elif l == ':':
|
| 216 |
+
f = 1
|
| 217 |
+
elif l[:8] == '--debug-':
|
| 218 |
+
debug.append(l[8:])
|
| 219 |
+
elif l == '--lower':
|
| 220 |
+
dolc = 1
|
| 221 |
+
elif l == '--build-dir':
|
| 222 |
+
f6 = 1
|
| 223 |
+
elif l == '--no-lower':
|
| 224 |
+
dolc = 0
|
| 225 |
+
elif l == '--quiet':
|
| 226 |
+
verbose = 0
|
| 227 |
+
elif l == '--verbose':
|
| 228 |
+
verbose += 1
|
| 229 |
+
elif l == '--latex-doc':
|
| 230 |
+
dolatexdoc = 1
|
| 231 |
+
elif l == '--no-latex-doc':
|
| 232 |
+
dolatexdoc = 0
|
| 233 |
+
elif l == '--rest-doc':
|
| 234 |
+
dorestdoc = 1
|
| 235 |
+
elif l == '--no-rest-doc':
|
| 236 |
+
dorestdoc = 0
|
| 237 |
+
elif l == '--wrap-functions':
|
| 238 |
+
wrapfuncs = 1
|
| 239 |
+
elif l == '--no-wrap-functions':
|
| 240 |
+
wrapfuncs = 0
|
| 241 |
+
elif l == '--short-latex':
|
| 242 |
+
options['shortlatex'] = 1
|
| 243 |
+
elif l == '--coutput':
|
| 244 |
+
f8 = 1
|
| 245 |
+
elif l == '--f2py-wrapper-output':
|
| 246 |
+
f9 = 1
|
| 247 |
+
elif l == '--f2cmap':
|
| 248 |
+
f10 = 1
|
| 249 |
+
elif l == '--overwrite-signature':
|
| 250 |
+
options['h-overwrite'] = 1
|
| 251 |
+
elif l == '-h':
|
| 252 |
+
f2 = 1
|
| 253 |
+
elif l == '-m':
|
| 254 |
+
f3 = 1
|
| 255 |
+
elif l[:2] == '-v':
|
| 256 |
+
print(f2py_version)
|
| 257 |
+
sys.exit()
|
| 258 |
+
elif l == '--show-compilers':
|
| 259 |
+
f5 = 1
|
| 260 |
+
elif l[:8] == '-include':
|
| 261 |
+
cfuncs.outneeds['userincludes'].append(l[9:-1])
|
| 262 |
+
cfuncs.userincludes[l[9:-1]] = '#include ' + l[8:]
|
| 263 |
+
elif l == '--skip-empty-wrappers':
|
| 264 |
+
emptygen = False
|
| 265 |
+
elif l[0] == '-':
|
| 266 |
+
errmess('Unknown option %s\n' % repr(l))
|
| 267 |
+
sys.exit()
|
| 268 |
+
elif f2:
|
| 269 |
+
f2 = 0
|
| 270 |
+
signsfile = l
|
| 271 |
+
elif f3:
|
| 272 |
+
f3 = 0
|
| 273 |
+
modulename = l
|
| 274 |
+
elif f6:
|
| 275 |
+
f6 = 0
|
| 276 |
+
buildpath = l
|
| 277 |
+
elif f8:
|
| 278 |
+
f8 = 0
|
| 279 |
+
options["coutput"] = l
|
| 280 |
+
elif f9:
|
| 281 |
+
f9 = 0
|
| 282 |
+
options["f2py_wrapper_output"] = l
|
| 283 |
+
elif f10:
|
| 284 |
+
f10 = 0
|
| 285 |
+
options["f2cmap_file"] = l
|
| 286 |
+
elif f == 1:
|
| 287 |
+
try:
|
| 288 |
+
with open(l):
|
| 289 |
+
pass
|
| 290 |
+
files.append(l)
|
| 291 |
+
except OSError as detail:
|
| 292 |
+
errmess(f'OSError: {detail!s}. Skipping file "{l!s}".\n')
|
| 293 |
+
elif f == -1:
|
| 294 |
+
skipfuncs.append(l)
|
| 295 |
+
elif f == 0:
|
| 296 |
+
onlyfuncs.append(l)
|
| 297 |
+
if not f5 and not files and not modulename:
|
| 298 |
+
print(__usage__)
|
| 299 |
+
sys.exit()
|
| 300 |
+
if not os.path.isdir(buildpath):
|
| 301 |
+
if not verbose:
|
| 302 |
+
outmess('Creating build directory %s\n' % (buildpath))
|
| 303 |
+
os.mkdir(buildpath)
|
| 304 |
+
if signsfile:
|
| 305 |
+
signsfile = os.path.join(buildpath, signsfile)
|
| 306 |
+
if signsfile and os.path.isfile(signsfile) and 'h-overwrite' not in options:
|
| 307 |
+
errmess(
|
| 308 |
+
'Signature file "%s" exists!!! Use --overwrite-signature to overwrite.\n' % (signsfile))
|
| 309 |
+
sys.exit()
|
| 310 |
+
|
| 311 |
+
options['emptygen'] = emptygen
|
| 312 |
+
options['debug'] = debug
|
| 313 |
+
options['verbose'] = verbose
|
| 314 |
+
if dolc == -1 and not signsfile:
|
| 315 |
+
options['do-lower'] = 0
|
| 316 |
+
else:
|
| 317 |
+
options['do-lower'] = dolc
|
| 318 |
+
if modulename:
|
| 319 |
+
options['module'] = modulename
|
| 320 |
+
if signsfile:
|
| 321 |
+
options['signsfile'] = signsfile
|
| 322 |
+
if onlyfuncs:
|
| 323 |
+
options['onlyfuncs'] = onlyfuncs
|
| 324 |
+
if skipfuncs:
|
| 325 |
+
options['skipfuncs'] = skipfuncs
|
| 326 |
+
options['dolatexdoc'] = dolatexdoc
|
| 327 |
+
options['dorestdoc'] = dorestdoc
|
| 328 |
+
options['wrapfuncs'] = wrapfuncs
|
| 329 |
+
options['buildpath'] = buildpath
|
| 330 |
+
options['include_paths'] = include_paths
|
| 331 |
+
options.setdefault('f2cmap_file', None)
|
| 332 |
+
return files, options
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
def callcrackfortran(files, options):
|
| 336 |
+
rules.options = options
|
| 337 |
+
crackfortran.debug = options['debug']
|
| 338 |
+
crackfortran.verbose = options['verbose']
|
| 339 |
+
if 'module' in options:
|
| 340 |
+
crackfortran.f77modulename = options['module']
|
| 341 |
+
if 'skipfuncs' in options:
|
| 342 |
+
crackfortran.skipfuncs = options['skipfuncs']
|
| 343 |
+
if 'onlyfuncs' in options:
|
| 344 |
+
crackfortran.onlyfuncs = options['onlyfuncs']
|
| 345 |
+
crackfortran.include_paths[:] = options['include_paths']
|
| 346 |
+
crackfortran.dolowercase = options['do-lower']
|
| 347 |
+
postlist = crackfortran.crackfortran(files)
|
| 348 |
+
if 'signsfile' in options:
|
| 349 |
+
outmess('Saving signatures to file "%s"\n' % (options['signsfile']))
|
| 350 |
+
pyf = crackfortran.crack2fortran(postlist)
|
| 351 |
+
if options['signsfile'][-6:] == 'stdout':
|
| 352 |
+
sys.stdout.write(pyf)
|
| 353 |
+
else:
|
| 354 |
+
with open(options['signsfile'], 'w') as f:
|
| 355 |
+
f.write(pyf)
|
| 356 |
+
if options["coutput"] is None:
|
| 357 |
+
for mod in postlist:
|
| 358 |
+
mod["coutput"] = "%smodule.c" % mod["name"]
|
| 359 |
+
else:
|
| 360 |
+
for mod in postlist:
|
| 361 |
+
mod["coutput"] = options["coutput"]
|
| 362 |
+
if options["f2py_wrapper_output"] is None:
|
| 363 |
+
for mod in postlist:
|
| 364 |
+
mod["f2py_wrapper_output"] = "%s-f2pywrappers.f" % mod["name"]
|
| 365 |
+
else:
|
| 366 |
+
for mod in postlist:
|
| 367 |
+
mod["f2py_wrapper_output"] = options["f2py_wrapper_output"]
|
| 368 |
+
return postlist
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
def buildmodules(lst):
|
| 372 |
+
cfuncs.buildcfuncs()
|
| 373 |
+
outmess('Building modules...\n')
|
| 374 |
+
modules, mnames, isusedby = [], [], {}
|
| 375 |
+
for item in lst:
|
| 376 |
+
if '__user__' in item['name']:
|
| 377 |
+
cb_rules.buildcallbacks(item)
|
| 378 |
+
else:
|
| 379 |
+
if 'use' in item:
|
| 380 |
+
for u in item['use'].keys():
|
| 381 |
+
if u not in isusedby:
|
| 382 |
+
isusedby[u] = []
|
| 383 |
+
isusedby[u].append(item['name'])
|
| 384 |
+
modules.append(item)
|
| 385 |
+
mnames.append(item['name'])
|
| 386 |
+
ret = {}
|
| 387 |
+
for module, name in zip(modules, mnames):
|
| 388 |
+
if name in isusedby:
|
| 389 |
+
outmess('\tSkipping module "%s" which is used by %s.\n' % (
|
| 390 |
+
name, ','.join('"%s"' % s for s in isusedby[name])))
|
| 391 |
+
else:
|
| 392 |
+
um = []
|
| 393 |
+
if 'use' in module:
|
| 394 |
+
for u in module['use'].keys():
|
| 395 |
+
if u in isusedby and u in mnames:
|
| 396 |
+
um.append(modules[mnames.index(u)])
|
| 397 |
+
else:
|
| 398 |
+
outmess(
|
| 399 |
+
f'\tModule "{name}" uses nonexisting "{u}" '
|
| 400 |
+
'which will be ignored.\n')
|
| 401 |
+
ret[name] = {}
|
| 402 |
+
dict_append(ret[name], rules.buildmodule(module, um))
|
| 403 |
+
return ret
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
def dict_append(d_out, d_in):
|
| 407 |
+
for (k, v) in d_in.items():
|
| 408 |
+
if k not in d_out:
|
| 409 |
+
d_out[k] = []
|
| 410 |
+
if isinstance(v, list):
|
| 411 |
+
d_out[k] = d_out[k] + v
|
| 412 |
+
else:
|
| 413 |
+
d_out[k].append(v)
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
def run_main(comline_list):
|
| 417 |
+
"""
|
| 418 |
+
Equivalent to running::
|
| 419 |
+
|
| 420 |
+
f2py <args>
|
| 421 |
+
|
| 422 |
+
where ``<args>=string.join(<list>,' ')``, but in Python. Unless
|
| 423 |
+
``-h`` is used, this function returns a dictionary containing
|
| 424 |
+
information on generated modules and their dependencies on source
|
| 425 |
+
files.
|
| 426 |
+
|
| 427 |
+
You cannot build extension modules with this function, that is,
|
| 428 |
+
using ``-c`` is not allowed. Use the ``compile`` command instead.
|
| 429 |
+
|
| 430 |
+
Examples
|
| 431 |
+
--------
|
| 432 |
+
The command ``f2py -m scalar scalar.f`` can be executed from Python as
|
| 433 |
+
follows.
|
| 434 |
+
|
| 435 |
+
.. literalinclude:: ../../source/f2py/code/results/run_main_session.dat
|
| 436 |
+
:language: python
|
| 437 |
+
|
| 438 |
+
"""
|
| 439 |
+
crackfortran.reset_global_f2py_vars()
|
| 440 |
+
f2pydir = os.path.dirname(os.path.abspath(cfuncs.__file__))
|
| 441 |
+
fobjhsrc = os.path.join(f2pydir, 'src', 'fortranobject.h')
|
| 442 |
+
fobjcsrc = os.path.join(f2pydir, 'src', 'fortranobject.c')
|
| 443 |
+
# gh-22819 -- begin
|
| 444 |
+
parser = make_f2py_compile_parser()
|
| 445 |
+
args, comline_list = parser.parse_known_args(comline_list)
|
| 446 |
+
pyf_files, _ = filter_files("", "[.]pyf([.]src|)", comline_list)
|
| 447 |
+
# Checks that no existing modulename is defined in a pyf file
|
| 448 |
+
# TODO: Remove all this when scaninputline is replaced
|
| 449 |
+
if args.module_name:
|
| 450 |
+
if "-h" in comline_list:
|
| 451 |
+
modname = (
|
| 452 |
+
args.module_name
|
| 453 |
+
) # Directly use from args when -h is present
|
| 454 |
+
else:
|
| 455 |
+
modname = validate_modulename(
|
| 456 |
+
pyf_files, args.module_name
|
| 457 |
+
) # Validate modname when -h is not present
|
| 458 |
+
comline_list += ['-m', modname] # needed for the rest of scaninputline
|
| 459 |
+
# gh-22819 -- end
|
| 460 |
+
files, options = scaninputline(comline_list)
|
| 461 |
+
auxfuncs.options = options
|
| 462 |
+
capi_maps.load_f2cmap_file(options['f2cmap_file'])
|
| 463 |
+
postlist = callcrackfortran(files, options)
|
| 464 |
+
isusedby = {}
|
| 465 |
+
for plist in postlist:
|
| 466 |
+
if 'use' in plist:
|
| 467 |
+
for u in plist['use'].keys():
|
| 468 |
+
if u not in isusedby:
|
| 469 |
+
isusedby[u] = []
|
| 470 |
+
isusedby[u].append(plist['name'])
|
| 471 |
+
for plist in postlist:
|
| 472 |
+
if plist['block'] == 'python module' and '__user__' in plist['name']:
|
| 473 |
+
if plist['name'] in isusedby:
|
| 474 |
+
# if not quiet:
|
| 475 |
+
outmess(
|
| 476 |
+
f'Skipping Makefile build for module "{plist["name"]}" '
|
| 477 |
+
'which is used by {}\n'.format(
|
| 478 |
+
','.join(f'"{s}"' for s in isusedby[plist['name']])))
|
| 479 |
+
if 'signsfile' in options:
|
| 480 |
+
if options['verbose'] > 1:
|
| 481 |
+
outmess(
|
| 482 |
+
'Stopping. Edit the signature file and then run f2py on the signature file: ')
|
| 483 |
+
outmess('%s %s\n' %
|
| 484 |
+
(os.path.basename(sys.argv[0]), options['signsfile']))
|
| 485 |
+
return
|
| 486 |
+
for plist in postlist:
|
| 487 |
+
if plist['block'] != 'python module':
|
| 488 |
+
if 'python module' not in options:
|
| 489 |
+
errmess(
|
| 490 |
+
'Tip: If your original code is Fortran source then you must use -m option.\n')
|
| 491 |
+
raise TypeError('All blocks must be python module blocks but got %s' % (
|
| 492 |
+
repr(plist['block'])))
|
| 493 |
+
auxfuncs.debugoptions = options['debug']
|
| 494 |
+
f90mod_rules.options = options
|
| 495 |
+
auxfuncs.wrapfuncs = options['wrapfuncs']
|
| 496 |
+
|
| 497 |
+
ret = buildmodules(postlist)
|
| 498 |
+
|
| 499 |
+
for mn in ret.keys():
|
| 500 |
+
dict_append(ret[mn], {'csrc': fobjcsrc, 'h': fobjhsrc})
|
| 501 |
+
return ret
|
| 502 |
+
|
| 503 |
+
|
| 504 |
+
def filter_files(prefix, suffix, files, remove_prefix=None):
|
| 505 |
+
"""
|
| 506 |
+
Filter files by prefix and suffix.
|
| 507 |
+
"""
|
| 508 |
+
filtered, rest = [], []
|
| 509 |
+
match = re.compile(prefix + r'.*' + suffix + r'\Z').match
|
| 510 |
+
if remove_prefix:
|
| 511 |
+
ind = len(prefix)
|
| 512 |
+
else:
|
| 513 |
+
ind = 0
|
| 514 |
+
for file in [x.strip() for x in files]:
|
| 515 |
+
if match(file):
|
| 516 |
+
filtered.append(file[ind:])
|
| 517 |
+
else:
|
| 518 |
+
rest.append(file)
|
| 519 |
+
return filtered, rest
|
| 520 |
+
|
| 521 |
+
|
| 522 |
+
def get_prefix(module):
|
| 523 |
+
p = os.path.dirname(os.path.dirname(module.__file__))
|
| 524 |
+
return p
|
| 525 |
+
|
| 526 |
+
|
| 527 |
+
class CombineIncludePaths(argparse.Action):
|
| 528 |
+
def __call__(self, parser, namespace, values, option_string=None):
|
| 529 |
+
include_paths_set = set(getattr(namespace, 'include_paths', []) or [])
|
| 530 |
+
if option_string == "--include_paths":
|
| 531 |
+
outmess("Use --include-paths or -I instead of --include_paths which will be removed")
|
| 532 |
+
if option_string == "--include-paths" or option_string == "--include_paths":
|
| 533 |
+
include_paths_set.update(values.split(':'))
|
| 534 |
+
else:
|
| 535 |
+
include_paths_set.add(values)
|
| 536 |
+
setattr(namespace, 'include_paths', list(include_paths_set))
|
| 537 |
+
|
| 538 |
+
def include_parser():
|
| 539 |
+
parser = argparse.ArgumentParser(add_help=False)
|
| 540 |
+
parser.add_argument("-I", dest="include_paths", action=CombineIncludePaths)
|
| 541 |
+
parser.add_argument("--include-paths", dest="include_paths", action=CombineIncludePaths)
|
| 542 |
+
parser.add_argument("--include_paths", dest="include_paths", action=CombineIncludePaths)
|
| 543 |
+
return parser
|
| 544 |
+
|
| 545 |
+
def get_includes(iline):
|
| 546 |
+
iline = (' '.join(iline)).split()
|
| 547 |
+
parser = include_parser()
|
| 548 |
+
args, remain = parser.parse_known_args(iline)
|
| 549 |
+
ipaths = args.include_paths
|
| 550 |
+
if args.include_paths is None:
|
| 551 |
+
ipaths = []
|
| 552 |
+
return ipaths, remain
|
| 553 |
+
|
| 554 |
+
def make_f2py_compile_parser():
|
| 555 |
+
parser = argparse.ArgumentParser(add_help=False)
|
| 556 |
+
parser.add_argument("--dep", action="append", dest="dependencies")
|
| 557 |
+
parser.add_argument("--backend", choices=['meson', 'distutils'], default='distutils')
|
| 558 |
+
parser.add_argument("-m", dest="module_name")
|
| 559 |
+
return parser
|
| 560 |
+
|
| 561 |
+
def preparse_sysargv():
|
| 562 |
+
# To keep backwards bug compatibility, newer flags are handled by argparse,
|
| 563 |
+
# and `sys.argv` is passed to the rest of `f2py` as is.
|
| 564 |
+
parser = make_f2py_compile_parser()
|
| 565 |
+
|
| 566 |
+
args, remaining_argv = parser.parse_known_args()
|
| 567 |
+
sys.argv = [sys.argv[0]] + remaining_argv
|
| 568 |
+
|
| 569 |
+
backend_key = args.backend
|
| 570 |
+
if MESON_ONLY_VER and backend_key == 'distutils':
|
| 571 |
+
outmess("Cannot use distutils backend with Python>=3.12,"
|
| 572 |
+
" using meson backend instead.\n")
|
| 573 |
+
backend_key = "meson"
|
| 574 |
+
|
| 575 |
+
return {
|
| 576 |
+
"dependencies": args.dependencies or [],
|
| 577 |
+
"backend": backend_key,
|
| 578 |
+
"modulename": args.module_name,
|
| 579 |
+
}
|
| 580 |
+
|
| 581 |
+
def run_compile():
|
| 582 |
+
"""
|
| 583 |
+
Do it all in one call!
|
| 584 |
+
"""
|
| 585 |
+
import tempfile
|
| 586 |
+
|
| 587 |
+
# Collect dependency flags, preprocess sys.argv
|
| 588 |
+
argy = preparse_sysargv()
|
| 589 |
+
modulename = argy["modulename"]
|
| 590 |
+
if modulename is None:
|
| 591 |
+
modulename = 'untitled'
|
| 592 |
+
dependencies = argy["dependencies"]
|
| 593 |
+
backend_key = argy["backend"]
|
| 594 |
+
build_backend = f2py_build_generator(backend_key)
|
| 595 |
+
|
| 596 |
+
i = sys.argv.index('-c')
|
| 597 |
+
del sys.argv[i]
|
| 598 |
+
|
| 599 |
+
remove_build_dir = 0
|
| 600 |
+
try:
|
| 601 |
+
i = sys.argv.index('--build-dir')
|
| 602 |
+
except ValueError:
|
| 603 |
+
i = None
|
| 604 |
+
if i is not None:
|
| 605 |
+
build_dir = sys.argv[i + 1]
|
| 606 |
+
del sys.argv[i + 1]
|
| 607 |
+
del sys.argv[i]
|
| 608 |
+
else:
|
| 609 |
+
remove_build_dir = 1
|
| 610 |
+
build_dir = tempfile.mkdtemp()
|
| 611 |
+
|
| 612 |
+
_reg1 = re.compile(r'--link-')
|
| 613 |
+
sysinfo_flags = [_m for _m in sys.argv[1:] if _reg1.match(_m)]
|
| 614 |
+
sys.argv = [_m for _m in sys.argv if _m not in sysinfo_flags]
|
| 615 |
+
if sysinfo_flags:
|
| 616 |
+
sysinfo_flags = [f[7:] for f in sysinfo_flags]
|
| 617 |
+
|
| 618 |
+
_reg2 = re.compile(
|
| 619 |
+
r'--((no-|)(wrap-functions|lower)|debug-capi|quiet|skip-empty-wrappers)|-include')
|
| 620 |
+
f2py_flags = [_m for _m in sys.argv[1:] if _reg2.match(_m)]
|
| 621 |
+
sys.argv = [_m for _m in sys.argv if _m not in f2py_flags]
|
| 622 |
+
f2py_flags2 = []
|
| 623 |
+
fl = 0
|
| 624 |
+
for a in sys.argv[1:]:
|
| 625 |
+
if a in ['only:', 'skip:']:
|
| 626 |
+
fl = 1
|
| 627 |
+
elif a == ':':
|
| 628 |
+
fl = 0
|
| 629 |
+
if fl or a == ':':
|
| 630 |
+
f2py_flags2.append(a)
|
| 631 |
+
if f2py_flags2 and f2py_flags2[-1] != ':':
|
| 632 |
+
f2py_flags2.append(':')
|
| 633 |
+
f2py_flags.extend(f2py_flags2)
|
| 634 |
+
sys.argv = [_m for _m in sys.argv if _m not in f2py_flags2]
|
| 635 |
+
_reg3 = re.compile(
|
| 636 |
+
r'--((f(90)?compiler(-exec|)|compiler)=|help-compiler)')
|
| 637 |
+
flib_flags = [_m for _m in sys.argv[1:] if _reg3.match(_m)]
|
| 638 |
+
sys.argv = [_m for _m in sys.argv if _m not in flib_flags]
|
| 639 |
+
_reg4 = re.compile(
|
| 640 |
+
r'--((f(77|90)(flags|exec)|opt|arch)=|(debug|noopt|noarch|help-fcompiler))')
|
| 641 |
+
fc_flags = [_m for _m in sys.argv[1:] if _reg4.match(_m)]
|
| 642 |
+
sys.argv = [_m for _m in sys.argv if _m not in fc_flags]
|
| 643 |
+
|
| 644 |
+
del_list = []
|
| 645 |
+
for s in flib_flags:
|
| 646 |
+
v = '--fcompiler='
|
| 647 |
+
if s[:len(v)] == v:
|
| 648 |
+
if MESON_ONLY_VER or backend_key == 'meson':
|
| 649 |
+
outmess(
|
| 650 |
+
"--fcompiler cannot be used with meson,"
|
| 651 |
+
"set compiler with the FC environment variable\n"
|
| 652 |
+
)
|
| 653 |
+
else:
|
| 654 |
+
from numpy.distutils import fcompiler
|
| 655 |
+
fcompiler.load_all_fcompiler_classes()
|
| 656 |
+
allowed_keys = list(fcompiler.fcompiler_class.keys())
|
| 657 |
+
nv = ov = s[len(v):].lower()
|
| 658 |
+
if ov not in allowed_keys:
|
| 659 |
+
vmap = {} # XXX
|
| 660 |
+
try:
|
| 661 |
+
nv = vmap[ov]
|
| 662 |
+
except KeyError:
|
| 663 |
+
if ov not in vmap.values():
|
| 664 |
+
print('Unknown vendor: "%s"' % (s[len(v):]))
|
| 665 |
+
nv = ov
|
| 666 |
+
i = flib_flags.index(s)
|
| 667 |
+
flib_flags[i] = '--fcompiler=' + nv
|
| 668 |
+
continue
|
| 669 |
+
for s in del_list:
|
| 670 |
+
i = flib_flags.index(s)
|
| 671 |
+
del flib_flags[i]
|
| 672 |
+
assert len(flib_flags) <= 2, repr(flib_flags)
|
| 673 |
+
|
| 674 |
+
_reg5 = re.compile(r'--(verbose)')
|
| 675 |
+
setup_flags = [_m for _m in sys.argv[1:] if _reg5.match(_m)]
|
| 676 |
+
sys.argv = [_m for _m in sys.argv if _m not in setup_flags]
|
| 677 |
+
|
| 678 |
+
if '--quiet' in f2py_flags:
|
| 679 |
+
setup_flags.append('--quiet')
|
| 680 |
+
|
| 681 |
+
# Ugly filter to remove everything but sources
|
| 682 |
+
sources = sys.argv[1:]
|
| 683 |
+
f2cmapopt = '--f2cmap'
|
| 684 |
+
if f2cmapopt in sys.argv:
|
| 685 |
+
i = sys.argv.index(f2cmapopt)
|
| 686 |
+
f2py_flags.extend(sys.argv[i:i + 2])
|
| 687 |
+
del sys.argv[i + 1], sys.argv[i]
|
| 688 |
+
sources = sys.argv[1:]
|
| 689 |
+
|
| 690 |
+
pyf_files, _sources = filter_files("", "[.]pyf([.]src|)", sources)
|
| 691 |
+
sources = pyf_files + _sources
|
| 692 |
+
modulename = validate_modulename(pyf_files, modulename)
|
| 693 |
+
extra_objects, sources = filter_files('', '[.](o|a|so|dylib)', sources)
|
| 694 |
+
library_dirs, sources = filter_files('-L', '', sources, remove_prefix=1)
|
| 695 |
+
libraries, sources = filter_files('-l', '', sources, remove_prefix=1)
|
| 696 |
+
undef_macros, sources = filter_files('-U', '', sources, remove_prefix=1)
|
| 697 |
+
define_macros, sources = filter_files('-D', '', sources, remove_prefix=1)
|
| 698 |
+
for i in range(len(define_macros)):
|
| 699 |
+
name_value = define_macros[i].split('=', 1)
|
| 700 |
+
if len(name_value) == 1:
|
| 701 |
+
name_value.append(None)
|
| 702 |
+
if len(name_value) == 2:
|
| 703 |
+
define_macros[i] = tuple(name_value)
|
| 704 |
+
else:
|
| 705 |
+
print('Invalid use of -D:', name_value)
|
| 706 |
+
|
| 707 |
+
# Construct wrappers / signatures / things
|
| 708 |
+
if backend_key == 'meson':
|
| 709 |
+
if not pyf_files:
|
| 710 |
+
outmess('Using meson backend\nWill pass --lower to f2py\nSee https://numpy.org/doc/stable/f2py/buildtools/meson.html\n')
|
| 711 |
+
f2py_flags.append('--lower')
|
| 712 |
+
run_main(f" {' '.join(f2py_flags)} -m {modulename} {' '.join(sources)}".split())
|
| 713 |
+
else:
|
| 714 |
+
run_main(f" {' '.join(f2py_flags)} {' '.join(pyf_files)}".split())
|
| 715 |
+
|
| 716 |
+
# Order matters here, includes are needed for run_main above
|
| 717 |
+
include_dirs, sources = get_includes(sources)
|
| 718 |
+
# Now use the builder
|
| 719 |
+
builder = build_backend(
|
| 720 |
+
modulename,
|
| 721 |
+
sources,
|
| 722 |
+
extra_objects,
|
| 723 |
+
build_dir,
|
| 724 |
+
include_dirs,
|
| 725 |
+
library_dirs,
|
| 726 |
+
libraries,
|
| 727 |
+
define_macros,
|
| 728 |
+
undef_macros,
|
| 729 |
+
f2py_flags,
|
| 730 |
+
sysinfo_flags,
|
| 731 |
+
fc_flags,
|
| 732 |
+
flib_flags,
|
| 733 |
+
setup_flags,
|
| 734 |
+
remove_build_dir,
|
| 735 |
+
{"dependencies": dependencies},
|
| 736 |
+
)
|
| 737 |
+
|
| 738 |
+
builder.compile()
|
| 739 |
+
|
| 740 |
+
|
| 741 |
+
def validate_modulename(pyf_files, modulename='untitled'):
|
| 742 |
+
if len(pyf_files) > 1:
|
| 743 |
+
raise ValueError("Only one .pyf file per call")
|
| 744 |
+
if pyf_files:
|
| 745 |
+
pyff = pyf_files[0]
|
| 746 |
+
pyf_modname = auxfuncs.get_f2py_modulename(pyff)
|
| 747 |
+
if modulename != pyf_modname:
|
| 748 |
+
outmess(
|
| 749 |
+
f"Ignoring -m {modulename}.\n"
|
| 750 |
+
f"{pyff} defines {pyf_modname} to be the modulename.\n"
|
| 751 |
+
)
|
| 752 |
+
modulename = pyf_modname
|
| 753 |
+
return modulename
|
| 754 |
+
|
| 755 |
+
def main():
|
| 756 |
+
if '--help-link' in sys.argv[1:]:
|
| 757 |
+
sys.argv.remove('--help-link')
|
| 758 |
+
if MESON_ONLY_VER:
|
| 759 |
+
outmess("Use --dep for meson builds\n")
|
| 760 |
+
else:
|
| 761 |
+
from numpy.distutils.system_info import show_all
|
| 762 |
+
show_all()
|
| 763 |
+
return
|
| 764 |
+
|
| 765 |
+
if '-c' in sys.argv[1:]:
|
| 766 |
+
run_compile()
|
| 767 |
+
else:
|
| 768 |
+
run_main(sys.argv[1:])
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/f90mod_rules.py
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Build F90 module support for f2py2e.
|
| 3 |
+
|
| 4 |
+
Copyright 1999 -- 2011 Pearu Peterson all rights reserved.
|
| 5 |
+
Copyright 2011 -- present NumPy Developers.
|
| 6 |
+
Permission to use, modify, and distribute this software is given under the
|
| 7 |
+
terms of the NumPy License.
|
| 8 |
+
|
| 9 |
+
NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
| 10 |
+
"""
|
| 11 |
+
__version__ = "$Revision: 1.27 $"[10:-1]
|
| 12 |
+
|
| 13 |
+
f2py_version = 'See `f2py -v`'
|
| 14 |
+
|
| 15 |
+
import numpy as np
|
| 16 |
+
|
| 17 |
+
from . import capi_maps
|
| 18 |
+
from . import func2subr
|
| 19 |
+
from .crackfortran import undo_rmbadname, undo_rmbadname1
|
| 20 |
+
|
| 21 |
+
# The environment provided by auxfuncs.py is needed for some calls to eval.
|
| 22 |
+
# As the needed functions cannot be determined by static inspection of the
|
| 23 |
+
# code, it is safest to use import * pending a major refactoring of f2py.
|
| 24 |
+
from .auxfuncs import *
|
| 25 |
+
|
| 26 |
+
options = {}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def findf90modules(m):
|
| 30 |
+
if ismodule(m):
|
| 31 |
+
return [m]
|
| 32 |
+
if not hasbody(m):
|
| 33 |
+
return []
|
| 34 |
+
ret = []
|
| 35 |
+
for b in m['body']:
|
| 36 |
+
if ismodule(b):
|
| 37 |
+
ret.append(b)
|
| 38 |
+
else:
|
| 39 |
+
ret = ret + findf90modules(b)
|
| 40 |
+
return ret
|
| 41 |
+
|
| 42 |
+
fgetdims1 = """\
|
| 43 |
+
external f2pysetdata
|
| 44 |
+
logical ns
|
| 45 |
+
integer r,i
|
| 46 |
+
integer(%d) s(*)
|
| 47 |
+
ns = .FALSE.
|
| 48 |
+
if (allocated(d)) then
|
| 49 |
+
do i=1,r
|
| 50 |
+
if ((size(d,i).ne.s(i)).and.(s(i).ge.0)) then
|
| 51 |
+
ns = .TRUE.
|
| 52 |
+
end if
|
| 53 |
+
end do
|
| 54 |
+
if (ns) then
|
| 55 |
+
deallocate(d)
|
| 56 |
+
end if
|
| 57 |
+
end if
|
| 58 |
+
if ((.not.allocated(d)).and.(s(1).ge.1)) then""" % np.intp().itemsize
|
| 59 |
+
|
| 60 |
+
fgetdims2 = """\
|
| 61 |
+
end if
|
| 62 |
+
if (allocated(d)) then
|
| 63 |
+
do i=1,r
|
| 64 |
+
s(i) = size(d,i)
|
| 65 |
+
end do
|
| 66 |
+
end if
|
| 67 |
+
flag = 1
|
| 68 |
+
call f2pysetdata(d,allocated(d))"""
|
| 69 |
+
|
| 70 |
+
fgetdims2_sa = """\
|
| 71 |
+
end if
|
| 72 |
+
if (allocated(d)) then
|
| 73 |
+
do i=1,r
|
| 74 |
+
s(i) = size(d,i)
|
| 75 |
+
end do
|
| 76 |
+
!s(r) must be equal to len(d(1))
|
| 77 |
+
end if
|
| 78 |
+
flag = 2
|
| 79 |
+
call f2pysetdata(d,allocated(d))"""
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def buildhooks(pymod):
|
| 83 |
+
from . import rules
|
| 84 |
+
ret = {'f90modhooks': [], 'initf90modhooks': [], 'body': [],
|
| 85 |
+
'need': ['F_FUNC', 'arrayobject.h'],
|
| 86 |
+
'separatorsfor': {'includes0': '\n', 'includes': '\n'},
|
| 87 |
+
'docs': ['"Fortran 90/95 modules:\\n"'],
|
| 88 |
+
'latexdoc': []}
|
| 89 |
+
fhooks = ['']
|
| 90 |
+
|
| 91 |
+
def fadd(line, s=fhooks):
|
| 92 |
+
s[0] = '%s\n %s' % (s[0], line)
|
| 93 |
+
doc = ['']
|
| 94 |
+
|
| 95 |
+
def dadd(line, s=doc):
|
| 96 |
+
s[0] = '%s\n%s' % (s[0], line)
|
| 97 |
+
|
| 98 |
+
usenames = getuseblocks(pymod)
|
| 99 |
+
for m in findf90modules(pymod):
|
| 100 |
+
sargs, fargs, efargs, modobjs, notvars, onlyvars = [], [], [], [], [
|
| 101 |
+
m['name']], []
|
| 102 |
+
sargsp = []
|
| 103 |
+
ifargs = []
|
| 104 |
+
mfargs = []
|
| 105 |
+
if hasbody(m):
|
| 106 |
+
for b in m['body']:
|
| 107 |
+
notvars.append(b['name'])
|
| 108 |
+
for n in m['vars'].keys():
|
| 109 |
+
var = m['vars'][n]
|
| 110 |
+
if (n not in notvars) and (not l_or(isintent_hide, isprivate)(var)):
|
| 111 |
+
onlyvars.append(n)
|
| 112 |
+
mfargs.append(n)
|
| 113 |
+
outmess('\t\tConstructing F90 module support for "%s"...\n' %
|
| 114 |
+
(m['name']))
|
| 115 |
+
if m['name'] in usenames and not onlyvars:
|
| 116 |
+
outmess(f"\t\t\tSkipping {m['name']} since it is in 'use'...\n")
|
| 117 |
+
continue
|
| 118 |
+
if onlyvars:
|
| 119 |
+
outmess('\t\t Variables: %s\n' % (' '.join(onlyvars)))
|
| 120 |
+
chooks = ['']
|
| 121 |
+
|
| 122 |
+
def cadd(line, s=chooks):
|
| 123 |
+
s[0] = '%s\n%s' % (s[0], line)
|
| 124 |
+
ihooks = ['']
|
| 125 |
+
|
| 126 |
+
def iadd(line, s=ihooks):
|
| 127 |
+
s[0] = '%s\n%s' % (s[0], line)
|
| 128 |
+
|
| 129 |
+
vrd = capi_maps.modsign2map(m)
|
| 130 |
+
cadd('static FortranDataDef f2py_%s_def[] = {' % (m['name']))
|
| 131 |
+
dadd('\\subsection{Fortran 90/95 module \\texttt{%s}}\n' % (m['name']))
|
| 132 |
+
if hasnote(m):
|
| 133 |
+
note = m['note']
|
| 134 |
+
if isinstance(note, list):
|
| 135 |
+
note = '\n'.join(note)
|
| 136 |
+
dadd(note)
|
| 137 |
+
if onlyvars:
|
| 138 |
+
dadd('\\begin{description}')
|
| 139 |
+
for n in onlyvars:
|
| 140 |
+
var = m['vars'][n]
|
| 141 |
+
modobjs.append(n)
|
| 142 |
+
ct = capi_maps.getctype(var)
|
| 143 |
+
at = capi_maps.c2capi_map[ct]
|
| 144 |
+
dm = capi_maps.getarrdims(n, var)
|
| 145 |
+
dms = dm['dims'].replace('*', '-1').strip()
|
| 146 |
+
dms = dms.replace(':', '-1').strip()
|
| 147 |
+
if not dms:
|
| 148 |
+
dms = '-1'
|
| 149 |
+
use_fgetdims2 = fgetdims2
|
| 150 |
+
cadd('\t{"%s",%s,{{%s}},%s, %s},' %
|
| 151 |
+
(undo_rmbadname1(n), dm['rank'], dms, at,
|
| 152 |
+
capi_maps.get_elsize(var)))
|
| 153 |
+
dadd('\\item[]{{}\\verb@%s@{}}' %
|
| 154 |
+
(capi_maps.getarrdocsign(n, var)))
|
| 155 |
+
if hasnote(var):
|
| 156 |
+
note = var['note']
|
| 157 |
+
if isinstance(note, list):
|
| 158 |
+
note = '\n'.join(note)
|
| 159 |
+
dadd('--- %s' % (note))
|
| 160 |
+
if isallocatable(var):
|
| 161 |
+
fargs.append('f2py_%s_getdims_%s' % (m['name'], n))
|
| 162 |
+
efargs.append(fargs[-1])
|
| 163 |
+
sargs.append(
|
| 164 |
+
'void (*%s)(int*,npy_intp*,void(*)(char*,npy_intp*),int*)' % (n))
|
| 165 |
+
sargsp.append('void (*)(int*,npy_intp*,void(*)(char*,npy_intp*),int*)')
|
| 166 |
+
iadd('\tf2py_%s_def[i_f2py++].func = %s;' % (m['name'], n))
|
| 167 |
+
fadd('subroutine %s(r,s,f2pysetdata,flag)' % (fargs[-1]))
|
| 168 |
+
fadd('use %s, only: d => %s\n' %
|
| 169 |
+
(m['name'], undo_rmbadname1(n)))
|
| 170 |
+
fadd('integer flag\n')
|
| 171 |
+
fhooks[0] = fhooks[0] + fgetdims1
|
| 172 |
+
dms = range(1, int(dm['rank']) + 1)
|
| 173 |
+
fadd(' allocate(d(%s))\n' %
|
| 174 |
+
(','.join(['s(%s)' % i for i in dms])))
|
| 175 |
+
fhooks[0] = fhooks[0] + use_fgetdims2
|
| 176 |
+
fadd('end subroutine %s' % (fargs[-1]))
|
| 177 |
+
else:
|
| 178 |
+
fargs.append(n)
|
| 179 |
+
sargs.append('char *%s' % (n))
|
| 180 |
+
sargsp.append('char*')
|
| 181 |
+
iadd('\tf2py_%s_def[i_f2py++].data = %s;' % (m['name'], n))
|
| 182 |
+
if onlyvars:
|
| 183 |
+
dadd('\\end{description}')
|
| 184 |
+
if hasbody(m):
|
| 185 |
+
for b in m['body']:
|
| 186 |
+
if not isroutine(b):
|
| 187 |
+
outmess("f90mod_rules.buildhooks:"
|
| 188 |
+
f" skipping {b['block']} {b['name']}\n")
|
| 189 |
+
continue
|
| 190 |
+
modobjs.append('%s()' % (b['name']))
|
| 191 |
+
b['modulename'] = m['name']
|
| 192 |
+
api, wrap = rules.buildapi(b)
|
| 193 |
+
if isfunction(b):
|
| 194 |
+
fhooks[0] = fhooks[0] + wrap
|
| 195 |
+
fargs.append('f2pywrap_%s_%s' % (m['name'], b['name']))
|
| 196 |
+
ifargs.append(func2subr.createfuncwrapper(b, signature=1))
|
| 197 |
+
else:
|
| 198 |
+
if wrap:
|
| 199 |
+
fhooks[0] = fhooks[0] + wrap
|
| 200 |
+
fargs.append('f2pywrap_%s_%s' % (m['name'], b['name']))
|
| 201 |
+
ifargs.append(
|
| 202 |
+
func2subr.createsubrwrapper(b, signature=1))
|
| 203 |
+
else:
|
| 204 |
+
fargs.append(b['name'])
|
| 205 |
+
mfargs.append(fargs[-1])
|
| 206 |
+
api['externroutines'] = []
|
| 207 |
+
ar = applyrules(api, vrd)
|
| 208 |
+
ar['docs'] = []
|
| 209 |
+
ar['docshort'] = []
|
| 210 |
+
ret = dictappend(ret, ar)
|
| 211 |
+
cadd(('\t{"%s",-1,{{-1}},0,0,NULL,(void *)'
|
| 212 |
+
'f2py_rout_#modulename#_%s_%s,'
|
| 213 |
+
'doc_f2py_rout_#modulename#_%s_%s},')
|
| 214 |
+
% (b['name'], m['name'], b['name'], m['name'], b['name']))
|
| 215 |
+
sargs.append('char *%s' % (b['name']))
|
| 216 |
+
sargsp.append('char *')
|
| 217 |
+
iadd('\tf2py_%s_def[i_f2py++].data = %s;' %
|
| 218 |
+
(m['name'], b['name']))
|
| 219 |
+
cadd('\t{NULL}\n};\n')
|
| 220 |
+
iadd('}')
|
| 221 |
+
ihooks[0] = 'static void f2py_setup_%s(%s) {\n\tint i_f2py=0;%s' % (
|
| 222 |
+
m['name'], ','.join(sargs), ihooks[0])
|
| 223 |
+
if '_' in m['name']:
|
| 224 |
+
F_FUNC = 'F_FUNC_US'
|
| 225 |
+
else:
|
| 226 |
+
F_FUNC = 'F_FUNC'
|
| 227 |
+
iadd('extern void %s(f2pyinit%s,F2PYINIT%s)(void (*)(%s));'
|
| 228 |
+
% (F_FUNC, m['name'], m['name'].upper(), ','.join(sargsp)))
|
| 229 |
+
iadd('static void f2py_init_%s(void) {' % (m['name']))
|
| 230 |
+
iadd('\t%s(f2pyinit%s,F2PYINIT%s)(f2py_setup_%s);'
|
| 231 |
+
% (F_FUNC, m['name'], m['name'].upper(), m['name']))
|
| 232 |
+
iadd('}\n')
|
| 233 |
+
ret['f90modhooks'] = ret['f90modhooks'] + chooks + ihooks
|
| 234 |
+
ret['initf90modhooks'] = ['\tPyDict_SetItemString(d, "%s", PyFortranObject_New(f2py_%s_def,f2py_init_%s));' % (
|
| 235 |
+
m['name'], m['name'], m['name'])] + ret['initf90modhooks']
|
| 236 |
+
fadd('')
|
| 237 |
+
fadd('subroutine f2pyinit%s(f2pysetupfunc)' % (m['name']))
|
| 238 |
+
if mfargs:
|
| 239 |
+
for a in undo_rmbadname(mfargs):
|
| 240 |
+
fadd('use %s, only : %s' % (m['name'], a))
|
| 241 |
+
if ifargs:
|
| 242 |
+
fadd(' '.join(['interface'] + ifargs))
|
| 243 |
+
fadd('end interface')
|
| 244 |
+
fadd('external f2pysetupfunc')
|
| 245 |
+
if efargs:
|
| 246 |
+
for a in undo_rmbadname(efargs):
|
| 247 |
+
fadd('external %s' % (a))
|
| 248 |
+
fadd('call f2pysetupfunc(%s)' % (','.join(undo_rmbadname(fargs))))
|
| 249 |
+
fadd('end subroutine f2pyinit%s\n' % (m['name']))
|
| 250 |
+
|
| 251 |
+
dadd('\n'.join(ret['latexdoc']).replace(
|
| 252 |
+
r'\subsection{', r'\subsubsection{'))
|
| 253 |
+
|
| 254 |
+
ret['latexdoc'] = []
|
| 255 |
+
ret['docs'].append('"\t%s --- %s"' % (m['name'],
|
| 256 |
+
','.join(undo_rmbadname(modobjs))))
|
| 257 |
+
|
| 258 |
+
ret['routine_defs'] = ''
|
| 259 |
+
ret['doc'] = []
|
| 260 |
+
ret['docshort'] = []
|
| 261 |
+
ret['latexdoc'] = doc[0]
|
| 262 |
+
if len(ret['docs']) <= 1:
|
| 263 |
+
ret['docs'] = ''
|
| 264 |
+
return ret, fhooks[0]
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/setup.cfg
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[bdist_rpm]
|
| 2 |
+
doc_files = docs/
|
| 3 |
+
tests/
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/__init__.py
ADDED
|
File without changes
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/accesstype.f90
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module foo
|
| 2 |
+
public
|
| 3 |
+
type, private, bind(c) :: a
|
| 4 |
+
integer :: i
|
| 5 |
+
end type a
|
| 6 |
+
type, bind(c) :: b_
|
| 7 |
+
integer :: j
|
| 8 |
+
end type b_
|
| 9 |
+
public :: b_
|
| 10 |
+
type :: c
|
| 11 |
+
integer :: k
|
| 12 |
+
end type c
|
| 13 |
+
end module foo
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/data_common.f
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BLOCK DATA PARAM_INI
|
| 2 |
+
COMMON /MYCOM/ MYDATA
|
| 3 |
+
DATA MYDATA /0/
|
| 4 |
+
END
|
| 5 |
+
SUBROUTINE SUB1
|
| 6 |
+
COMMON /MYCOM/ MYDATA
|
| 7 |
+
MYDATA = MYDATA + 1
|
| 8 |
+
END
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/data_multiplier.f
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BLOCK DATA MYBLK
|
| 2 |
+
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
|
| 3 |
+
COMMON /MYCOM/ IVAR1, IVAR2, IVAR3, IVAR4, EVAR5
|
| 4 |
+
DATA IVAR1, IVAR2, IVAR3, IVAR4, EVAR5 /2*3,2*2,0.0D0/
|
| 5 |
+
END
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/data_stmts.f90
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
! gh-23276
|
| 2 |
+
module cmplxdat
|
| 3 |
+
implicit none
|
| 4 |
+
integer :: i, j
|
| 5 |
+
real :: x, y
|
| 6 |
+
real, dimension(2) :: z
|
| 7 |
+
real(kind=8) :: pi
|
| 8 |
+
complex(kind=8), target :: medium_ref_index
|
| 9 |
+
complex(kind=8), target :: ref_index_one, ref_index_two
|
| 10 |
+
complex(kind=8), dimension(2) :: my_array
|
| 11 |
+
real(kind=8), dimension(3) :: my_real_array = (/1.0d0, 2.0d0, 3.0d0/)
|
| 12 |
+
|
| 13 |
+
data i, j / 2, 3 /
|
| 14 |
+
data x, y / 1.5, 2.0 /
|
| 15 |
+
data z / 3.5, 7.0 /
|
| 16 |
+
data medium_ref_index / (1.d0, 0.d0) /
|
| 17 |
+
data ref_index_one, ref_index_two / (13.0d0, 21.0d0), (-30.0d0, 43.0d0) /
|
| 18 |
+
data my_array / (1.0d0, 2.0d0), (-3.0d0, 4.0d0) /
|
| 19 |
+
data pi / 3.1415926535897932384626433832795028841971693993751058209749445923078164062d0 /
|
| 20 |
+
end module cmplxdat
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh15035.f
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
subroutine subb(k)
|
| 2 |
+
real(8), intent(inout) :: k(:)
|
| 3 |
+
k=k+1
|
| 4 |
+
endsubroutine
|
| 5 |
+
|
| 6 |
+
subroutine subc(w,k)
|
| 7 |
+
real(8), intent(in) :: w(:)
|
| 8 |
+
real(8), intent(out) :: k(size(w))
|
| 9 |
+
k=w+1
|
| 10 |
+
endsubroutine
|
| 11 |
+
|
| 12 |
+
function t0(value)
|
| 13 |
+
character value
|
| 14 |
+
character t0
|
| 15 |
+
t0 = value
|
| 16 |
+
endfunction
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh17859.f
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
integer(8) function external_as_statement(fcn)
|
| 2 |
+
implicit none
|
| 3 |
+
external fcn
|
| 4 |
+
integer(8) :: fcn
|
| 5 |
+
external_as_statement = fcn(0)
|
| 6 |
+
end
|
| 7 |
+
|
| 8 |
+
integer(8) function external_as_attribute(fcn)
|
| 9 |
+
implicit none
|
| 10 |
+
integer(8), external :: fcn
|
| 11 |
+
external_as_attribute = fcn(0)
|
| 12 |
+
end
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh22648.pyf
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python module iri16py ! in
|
| 2 |
+
interface ! in :iri16py
|
| 3 |
+
block data ! in :iri16py:iridreg_modified.for
|
| 4 |
+
COMMON /fircom/ eden,tabhe,tabla,tabmo,tabza,tabfl
|
| 5 |
+
end block data
|
| 6 |
+
end interface
|
| 7 |
+
end python module iri16py
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23533.f
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SUBROUTINE EXAMPLE( )
|
| 2 |
+
IF( .TRUE. ) THEN
|
| 3 |
+
CALL DO_SOMETHING()
|
| 4 |
+
END IF ! ** .TRUE. **
|
| 5 |
+
END
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23598.f90
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
integer function intproduct(a, b) result(res)
|
| 2 |
+
integer, intent(in) :: a, b
|
| 3 |
+
res = a*b
|
| 4 |
+
end function
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23879.f90
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module gh23879
|
| 2 |
+
implicit none
|
| 3 |
+
private
|
| 4 |
+
public :: foo
|
| 5 |
+
|
| 6 |
+
contains
|
| 7 |
+
|
| 8 |
+
subroutine foo(a, b)
|
| 9 |
+
integer, intent(in) :: a
|
| 10 |
+
integer, intent(out) :: b
|
| 11 |
+
b = a
|
| 12 |
+
call bar(b)
|
| 13 |
+
end subroutine
|
| 14 |
+
|
| 15 |
+
subroutine bar(x)
|
| 16 |
+
integer, intent(inout) :: x
|
| 17 |
+
x = 2*x
|
| 18 |
+
end subroutine
|
| 19 |
+
|
| 20 |
+
end module gh23879
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh2848.f90
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
subroutine gh2848( &
|
| 2 |
+
! first 2 parameters
|
| 3 |
+
par1, par2,&
|
| 4 |
+
! last 2 parameters
|
| 5 |
+
par3, par4)
|
| 6 |
+
|
| 7 |
+
integer, intent(in) :: par1, par2
|
| 8 |
+
integer, intent(out) :: par3, par4
|
| 9 |
+
|
| 10 |
+
par3 = par1
|
| 11 |
+
par4 = par2
|
| 12 |
+
|
| 13 |
+
end subroutine gh2848
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/operators.f90
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module foo
|
| 2 |
+
type bar
|
| 3 |
+
character(len = 32) :: item
|
| 4 |
+
end type bar
|
| 5 |
+
interface operator(.item.)
|
| 6 |
+
module procedure item_int, item_real
|
| 7 |
+
end interface operator(.item.)
|
| 8 |
+
interface operator(==)
|
| 9 |
+
module procedure items_are_equal
|
| 10 |
+
end interface operator(==)
|
| 11 |
+
interface assignment(=)
|
| 12 |
+
module procedure get_int, get_real
|
| 13 |
+
end interface assignment(=)
|
| 14 |
+
contains
|
| 15 |
+
function item_int(val) result(elem)
|
| 16 |
+
integer, intent(in) :: val
|
| 17 |
+
type(bar) :: elem
|
| 18 |
+
|
| 19 |
+
write(elem%item, "(I32)") val
|
| 20 |
+
end function item_int
|
| 21 |
+
|
| 22 |
+
function item_real(val) result(elem)
|
| 23 |
+
real, intent(in) :: val
|
| 24 |
+
type(bar) :: elem
|
| 25 |
+
|
| 26 |
+
write(elem%item, "(1PE32.12)") val
|
| 27 |
+
end function item_real
|
| 28 |
+
|
| 29 |
+
function items_are_equal(val1, val2) result(equal)
|
| 30 |
+
type(bar), intent(in) :: val1, val2
|
| 31 |
+
logical :: equal
|
| 32 |
+
|
| 33 |
+
equal = (val1%item == val2%item)
|
| 34 |
+
end function items_are_equal
|
| 35 |
+
|
| 36 |
+
subroutine get_real(rval, item)
|
| 37 |
+
real, intent(out) :: rval
|
| 38 |
+
type(bar), intent(in) :: item
|
| 39 |
+
|
| 40 |
+
read(item%item, *) rval
|
| 41 |
+
end subroutine get_real
|
| 42 |
+
|
| 43 |
+
subroutine get_int(rval, item)
|
| 44 |
+
integer, intent(out) :: rval
|
| 45 |
+
type(bar), intent(in) :: item
|
| 46 |
+
|
| 47 |
+
read(item%item, *) rval
|
| 48 |
+
end subroutine get_int
|
| 49 |
+
end module foo
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/privatemod.f90
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module foo
|
| 2 |
+
private
|
| 3 |
+
integer :: a
|
| 4 |
+
public :: setA
|
| 5 |
+
integer :: b
|
| 6 |
+
contains
|
| 7 |
+
subroutine setA(v)
|
| 8 |
+
integer, intent(in) :: v
|
| 9 |
+
a = v
|
| 10 |
+
end subroutine setA
|
| 11 |
+
end module foo
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/pubprivmod.f90
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module foo
|
| 2 |
+
public
|
| 3 |
+
integer, private :: a
|
| 4 |
+
integer :: b
|
| 5 |
+
contains
|
| 6 |
+
subroutine setA(v)
|
| 7 |
+
integer, intent(in) :: v
|
| 8 |
+
a = v
|
| 9 |
+
end subroutine setA
|
| 10 |
+
end module foo
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/parameter/constant_both.f90
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
! Check that parameters are correct intercepted.
|
| 2 |
+
! Constants with comma separations are commonly
|
| 3 |
+
! used, for instance Pi = 3._dp
|
| 4 |
+
subroutine foo(x)
|
| 5 |
+
implicit none
|
| 6 |
+
integer, parameter :: sp = selected_real_kind(6)
|
| 7 |
+
integer, parameter :: dp = selected_real_kind(15)
|
| 8 |
+
integer, parameter :: ii = selected_int_kind(9)
|
| 9 |
+
integer, parameter :: il = selected_int_kind(18)
|
| 10 |
+
real(dp), intent(inout) :: x
|
| 11 |
+
dimension x(3)
|
| 12 |
+
real(sp), parameter :: three_s = 3._sp
|
| 13 |
+
real(dp), parameter :: three_d = 3._dp
|
| 14 |
+
integer(ii), parameter :: three_i = 3_ii
|
| 15 |
+
integer(il), parameter :: three_l = 3_il
|
| 16 |
+
x(1) = x(1) + x(2) * three_s * three_i + x(3) * three_d * three_l
|
| 17 |
+
x(2) = x(2) * three_s
|
| 18 |
+
x(3) = x(3) * three_l
|
| 19 |
+
return
|
| 20 |
+
end subroutine
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
subroutine foo_no(x)
|
| 24 |
+
implicit none
|
| 25 |
+
integer, parameter :: sp = selected_real_kind(6)
|
| 26 |
+
integer, parameter :: dp = selected_real_kind(15)
|
| 27 |
+
integer, parameter :: ii = selected_int_kind(9)
|
| 28 |
+
integer, parameter :: il = selected_int_kind(18)
|
| 29 |
+
real(dp), intent(inout) :: x
|
| 30 |
+
dimension x(3)
|
| 31 |
+
real(sp), parameter :: three_s = 3.
|
| 32 |
+
real(dp), parameter :: three_d = 3.
|
| 33 |
+
integer(ii), parameter :: three_i = 3
|
| 34 |
+
integer(il), parameter :: three_l = 3
|
| 35 |
+
x(1) = x(1) + x(2) * three_s * three_i + x(3) * three_d * three_l
|
| 36 |
+
x(2) = x(2) * three_s
|
| 37 |
+
x(3) = x(3) * three_l
|
| 38 |
+
return
|
| 39 |
+
end subroutine
|
| 40 |
+
|
| 41 |
+
subroutine foo_sum(x)
|
| 42 |
+
implicit none
|
| 43 |
+
integer, parameter :: sp = selected_real_kind(6)
|
| 44 |
+
integer, parameter :: dp = selected_real_kind(15)
|
| 45 |
+
integer, parameter :: ii = selected_int_kind(9)
|
| 46 |
+
integer, parameter :: il = selected_int_kind(18)
|
| 47 |
+
real(dp), intent(inout) :: x
|
| 48 |
+
dimension x(3)
|
| 49 |
+
real(sp), parameter :: three_s = 2._sp + 1._sp
|
| 50 |
+
real(dp), parameter :: three_d = 1._dp + 2._dp
|
| 51 |
+
integer(ii), parameter :: three_i = 2_ii + 1_ii
|
| 52 |
+
integer(il), parameter :: three_l = 1_il + 2_il
|
| 53 |
+
x(1) = x(1) + x(2) * three_s * three_i + x(3) * three_d * three_l
|
| 54 |
+
x(2) = x(2) * three_s
|
| 55 |
+
x(3) = x(3) * three_l
|
| 56 |
+
return
|
| 57 |
+
end subroutine
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/parameter/constant_compound.f90
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
! Check that parameters are correct intercepted.
|
| 2 |
+
! Constants with comma separations are commonly
|
| 3 |
+
! used, for instance Pi = 3._dp
|
| 4 |
+
subroutine foo_compound_int(x)
|
| 5 |
+
implicit none
|
| 6 |
+
integer, parameter :: ii = selected_int_kind(9)
|
| 7 |
+
integer(ii), intent(inout) :: x
|
| 8 |
+
dimension x(3)
|
| 9 |
+
integer(ii), parameter :: three = 3_ii
|
| 10 |
+
integer(ii), parameter :: two = 2_ii
|
| 11 |
+
integer(ii), parameter :: six = three * 1_ii * two
|
| 12 |
+
|
| 13 |
+
x(1) = x(1) + x(2) + x(3) * six
|
| 14 |
+
return
|
| 15 |
+
end subroutine
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/parameter/constant_integer.f90
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
! Check that parameters are correct intercepted.
|
| 2 |
+
! Constants with comma separations are commonly
|
| 3 |
+
! used, for instance Pi = 3._dp
|
| 4 |
+
subroutine foo_int(x)
|
| 5 |
+
implicit none
|
| 6 |
+
integer, parameter :: ii = selected_int_kind(9)
|
| 7 |
+
integer(ii), intent(inout) :: x
|
| 8 |
+
dimension x(3)
|
| 9 |
+
integer(ii), parameter :: three = 3_ii
|
| 10 |
+
x(1) = x(1) + x(2) + x(3) * three
|
| 11 |
+
return
|
| 12 |
+
end subroutine
|
| 13 |
+
|
| 14 |
+
subroutine foo_long(x)
|
| 15 |
+
implicit none
|
| 16 |
+
integer, parameter :: ii = selected_int_kind(18)
|
| 17 |
+
integer(ii), intent(inout) :: x
|
| 18 |
+
dimension x(3)
|
| 19 |
+
integer(ii), parameter :: three = 3_ii
|
| 20 |
+
x(1) = x(1) + x(2) + x(3) * three
|
| 21 |
+
return
|
| 22 |
+
end subroutine
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/parameter/constant_non_compound.f90
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
! Check that parameters are correct intercepted.
|
| 2 |
+
! Specifically that types of constants without
|
| 3 |
+
! compound kind specs are correctly inferred
|
| 4 |
+
! adapted Gibbs iteration code from pymc
|
| 5 |
+
! for this test case
|
| 6 |
+
subroutine foo_non_compound_int(x)
|
| 7 |
+
implicit none
|
| 8 |
+
integer, parameter :: ii = selected_int_kind(9)
|
| 9 |
+
|
| 10 |
+
integer(ii) maxiterates
|
| 11 |
+
parameter (maxiterates=2)
|
| 12 |
+
|
| 13 |
+
integer(ii) maxseries
|
| 14 |
+
parameter (maxseries=2)
|
| 15 |
+
|
| 16 |
+
integer(ii) wasize
|
| 17 |
+
parameter (wasize=maxiterates*maxseries)
|
| 18 |
+
integer(ii), intent(inout) :: x
|
| 19 |
+
dimension x(wasize)
|
| 20 |
+
|
| 21 |
+
x(1) = x(1) + x(2) + x(3) + x(4) * wasize
|
| 22 |
+
return
|
| 23 |
+
end subroutine
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/parameter/constant_real.f90
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
! Check that parameters are correct intercepted.
|
| 2 |
+
! Constants with comma separations are commonly
|
| 3 |
+
! used, for instance Pi = 3._dp
|
| 4 |
+
subroutine foo_single(x)
|
| 5 |
+
implicit none
|
| 6 |
+
integer, parameter :: rp = selected_real_kind(6)
|
| 7 |
+
real(rp), intent(inout) :: x
|
| 8 |
+
dimension x(3)
|
| 9 |
+
real(rp), parameter :: three = 3._rp
|
| 10 |
+
x(1) = x(1) + x(2) + x(3) * three
|
| 11 |
+
return
|
| 12 |
+
end subroutine
|
| 13 |
+
|
| 14 |
+
subroutine foo_double(x)
|
| 15 |
+
implicit none
|
| 16 |
+
integer, parameter :: rp = selected_real_kind(15)
|
| 17 |
+
real(rp), intent(inout) :: x
|
| 18 |
+
dimension x(3)
|
| 19 |
+
real(rp), parameter :: three = 3._rp
|
| 20 |
+
x(1) = x(1) + x(2) + x(3) * three
|
| 21 |
+
return
|
| 22 |
+
end subroutine
|
| 23 |
+
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/return_character/foo77.f
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
function t0(value)
|
| 2 |
+
character value
|
| 3 |
+
character t0
|
| 4 |
+
t0 = value
|
| 5 |
+
end
|
| 6 |
+
function t1(value)
|
| 7 |
+
character*1 value
|
| 8 |
+
character*1 t1
|
| 9 |
+
t1 = value
|
| 10 |
+
end
|
| 11 |
+
function t5(value)
|
| 12 |
+
character*5 value
|
| 13 |
+
character*5 t5
|
| 14 |
+
t5 = value
|
| 15 |
+
end
|
| 16 |
+
function ts(value)
|
| 17 |
+
character*(*) value
|
| 18 |
+
character*(*) ts
|
| 19 |
+
ts = value
|
| 20 |
+
end
|
| 21 |
+
|
| 22 |
+
subroutine s0(t0,value)
|
| 23 |
+
character value
|
| 24 |
+
character t0
|
| 25 |
+
cf2py intent(out) t0
|
| 26 |
+
t0 = value
|
| 27 |
+
end
|
| 28 |
+
subroutine s1(t1,value)
|
| 29 |
+
character*1 value
|
| 30 |
+
character*1 t1
|
| 31 |
+
cf2py intent(out) t1
|
| 32 |
+
t1 = value
|
| 33 |
+
end
|
| 34 |
+
subroutine s5(t5,value)
|
| 35 |
+
character*5 value
|
| 36 |
+
character*5 t5
|
| 37 |
+
cf2py intent(out) t5
|
| 38 |
+
t5 = value
|
| 39 |
+
end
|
| 40 |
+
subroutine ss(ts,value)
|
| 41 |
+
character*(*) value
|
| 42 |
+
character*10 ts
|
| 43 |
+
cf2py intent(out) ts
|
| 44 |
+
ts = value
|
| 45 |
+
end
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/src/return_character/foo90.f90
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module f90_return_char
|
| 2 |
+
contains
|
| 3 |
+
function t0(value)
|
| 4 |
+
character :: value
|
| 5 |
+
character :: t0
|
| 6 |
+
t0 = value
|
| 7 |
+
end function t0
|
| 8 |
+
function t1(value)
|
| 9 |
+
character(len=1) :: value
|
| 10 |
+
character(len=1) :: t1
|
| 11 |
+
t1 = value
|
| 12 |
+
end function t1
|
| 13 |
+
function t5(value)
|
| 14 |
+
character(len=5) :: value
|
| 15 |
+
character(len=5) :: t5
|
| 16 |
+
t5 = value
|
| 17 |
+
end function t5
|
| 18 |
+
function ts(value)
|
| 19 |
+
character(len=*) :: value
|
| 20 |
+
character(len=10) :: ts
|
| 21 |
+
ts = value
|
| 22 |
+
end function ts
|
| 23 |
+
|
| 24 |
+
subroutine s0(t0,value)
|
| 25 |
+
character :: value
|
| 26 |
+
character :: t0
|
| 27 |
+
!f2py intent(out) t0
|
| 28 |
+
t0 = value
|
| 29 |
+
end subroutine s0
|
| 30 |
+
subroutine s1(t1,value)
|
| 31 |
+
character(len=1) :: value
|
| 32 |
+
character(len=1) :: t1
|
| 33 |
+
!f2py intent(out) t1
|
| 34 |
+
t1 = value
|
| 35 |
+
end subroutine s1
|
| 36 |
+
subroutine s5(t5,value)
|
| 37 |
+
character(len=5) :: value
|
| 38 |
+
character(len=5) :: t5
|
| 39 |
+
!f2py intent(out) t5
|
| 40 |
+
t5 = value
|
| 41 |
+
end subroutine s5
|
| 42 |
+
subroutine ss(ts,value)
|
| 43 |
+
character(len=*) :: value
|
| 44 |
+
character(len=10) :: ts
|
| 45 |
+
!f2py intent(out) ts
|
| 46 |
+
ts = value
|
| 47 |
+
end subroutine ss
|
| 48 |
+
end module f90_return_char
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_abstract_interface.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import pytest
|
| 3 |
+
import textwrap
|
| 4 |
+
from . import util
|
| 5 |
+
from numpy.f2py import crackfortran
|
| 6 |
+
from numpy.testing import IS_WASM
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@pytest.mark.skipif(IS_WASM, reason="Cannot start subprocess")
|
| 10 |
+
class TestAbstractInterface(util.F2PyTest):
|
| 11 |
+
sources = [util.getpath("tests", "src", "abstract_interface", "foo.f90")]
|
| 12 |
+
|
| 13 |
+
skip = ["add1", "add2"]
|
| 14 |
+
|
| 15 |
+
def test_abstract_interface(self):
|
| 16 |
+
assert self.module.ops_module.foo(3, 5) == (8, 13)
|
| 17 |
+
|
| 18 |
+
def test_parse_abstract_interface(self):
|
| 19 |
+
# Test gh18403
|
| 20 |
+
fpath = util.getpath("tests", "src", "abstract_interface",
|
| 21 |
+
"gh18403_mod.f90")
|
| 22 |
+
mod = crackfortran.crackfortran([str(fpath)])
|
| 23 |
+
assert len(mod) == 1
|
| 24 |
+
assert len(mod[0]["body"]) == 1
|
| 25 |
+
assert mod[0]["body"][0]["block"] == "abstract interface"
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_array_from_pyobj.py
ADDED
|
@@ -0,0 +1,686 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import copy
|
| 4 |
+
import platform
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
from numpy.testing import assert_, assert_equal
|
| 10 |
+
from numpy.core.multiarray import typeinfo as _typeinfo
|
| 11 |
+
from . import util
|
| 12 |
+
|
| 13 |
+
wrap = None
|
| 14 |
+
|
| 15 |
+
# Extend core typeinfo with CHARACTER to test dtype('c')
|
| 16 |
+
_ti = _typeinfo['STRING']
|
| 17 |
+
typeinfo = dict(
|
| 18 |
+
CHARACTER=type(_ti)(('c', _ti.num, 8, _ti.alignment, _ti.type)),
|
| 19 |
+
**_typeinfo)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def setup_module():
|
| 23 |
+
"""
|
| 24 |
+
Build the required testing extension module
|
| 25 |
+
|
| 26 |
+
"""
|
| 27 |
+
global wrap
|
| 28 |
+
|
| 29 |
+
# Check compiler availability first
|
| 30 |
+
if not util.has_c_compiler():
|
| 31 |
+
pytest.skip("No C compiler available")
|
| 32 |
+
|
| 33 |
+
if wrap is None:
|
| 34 |
+
config_code = """
|
| 35 |
+
config.add_extension('test_array_from_pyobj_ext',
|
| 36 |
+
sources=['wrapmodule.c', 'fortranobject.c'],
|
| 37 |
+
define_macros=[])
|
| 38 |
+
"""
|
| 39 |
+
d = os.path.dirname(__file__)
|
| 40 |
+
src = [
|
| 41 |
+
util.getpath("tests", "src", "array_from_pyobj", "wrapmodule.c"),
|
| 42 |
+
util.getpath("src", "fortranobject.c"),
|
| 43 |
+
util.getpath("src", "fortranobject.h"),
|
| 44 |
+
]
|
| 45 |
+
wrap = util.build_module_distutils(src, config_code,
|
| 46 |
+
"test_array_from_pyobj_ext")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def flags_info(arr):
|
| 50 |
+
flags = wrap.array_attrs(arr)[6]
|
| 51 |
+
return flags2names(flags)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def flags2names(flags):
|
| 55 |
+
info = []
|
| 56 |
+
for flagname in [
|
| 57 |
+
"CONTIGUOUS",
|
| 58 |
+
"FORTRAN",
|
| 59 |
+
"OWNDATA",
|
| 60 |
+
"ENSURECOPY",
|
| 61 |
+
"ENSUREARRAY",
|
| 62 |
+
"ALIGNED",
|
| 63 |
+
"NOTSWAPPED",
|
| 64 |
+
"WRITEABLE",
|
| 65 |
+
"WRITEBACKIFCOPY",
|
| 66 |
+
"UPDATEIFCOPY",
|
| 67 |
+
"BEHAVED",
|
| 68 |
+
"BEHAVED_RO",
|
| 69 |
+
"CARRAY",
|
| 70 |
+
"FARRAY",
|
| 71 |
+
]:
|
| 72 |
+
if abs(flags) & getattr(wrap, flagname, 0):
|
| 73 |
+
info.append(flagname)
|
| 74 |
+
return info
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
class Intent:
|
| 78 |
+
def __init__(self, intent_list=[]):
|
| 79 |
+
self.intent_list = intent_list[:]
|
| 80 |
+
flags = 0
|
| 81 |
+
for i in intent_list:
|
| 82 |
+
if i == "optional":
|
| 83 |
+
flags |= wrap.F2PY_OPTIONAL
|
| 84 |
+
else:
|
| 85 |
+
flags |= getattr(wrap, "F2PY_INTENT_" + i.upper())
|
| 86 |
+
self.flags = flags
|
| 87 |
+
|
| 88 |
+
def __getattr__(self, name):
|
| 89 |
+
name = name.lower()
|
| 90 |
+
if name == "in_":
|
| 91 |
+
name = "in"
|
| 92 |
+
return self.__class__(self.intent_list + [name])
|
| 93 |
+
|
| 94 |
+
def __str__(self):
|
| 95 |
+
return "intent(%s)" % (",".join(self.intent_list))
|
| 96 |
+
|
| 97 |
+
def __repr__(self):
|
| 98 |
+
return "Intent(%r)" % (self.intent_list)
|
| 99 |
+
|
| 100 |
+
def is_intent(self, *names):
|
| 101 |
+
for name in names:
|
| 102 |
+
if name not in self.intent_list:
|
| 103 |
+
return False
|
| 104 |
+
return True
|
| 105 |
+
|
| 106 |
+
def is_intent_exact(self, *names):
|
| 107 |
+
return len(self.intent_list) == len(names) and self.is_intent(*names)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
intent = Intent()
|
| 111 |
+
|
| 112 |
+
_type_names = [
|
| 113 |
+
"BOOL",
|
| 114 |
+
"BYTE",
|
| 115 |
+
"UBYTE",
|
| 116 |
+
"SHORT",
|
| 117 |
+
"USHORT",
|
| 118 |
+
"INT",
|
| 119 |
+
"UINT",
|
| 120 |
+
"LONG",
|
| 121 |
+
"ULONG",
|
| 122 |
+
"LONGLONG",
|
| 123 |
+
"ULONGLONG",
|
| 124 |
+
"FLOAT",
|
| 125 |
+
"DOUBLE",
|
| 126 |
+
"CFLOAT",
|
| 127 |
+
"STRING1",
|
| 128 |
+
"STRING5",
|
| 129 |
+
"CHARACTER",
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
_cast_dict = {"BOOL": ["BOOL"]}
|
| 133 |
+
_cast_dict["BYTE"] = _cast_dict["BOOL"] + ["BYTE"]
|
| 134 |
+
_cast_dict["UBYTE"] = _cast_dict["BOOL"] + ["UBYTE"]
|
| 135 |
+
_cast_dict["BYTE"] = ["BYTE"]
|
| 136 |
+
_cast_dict["UBYTE"] = ["UBYTE"]
|
| 137 |
+
_cast_dict["SHORT"] = _cast_dict["BYTE"] + ["UBYTE", "SHORT"]
|
| 138 |
+
_cast_dict["USHORT"] = _cast_dict["UBYTE"] + ["BYTE", "USHORT"]
|
| 139 |
+
_cast_dict["INT"] = _cast_dict["SHORT"] + ["USHORT", "INT"]
|
| 140 |
+
_cast_dict["UINT"] = _cast_dict["USHORT"] + ["SHORT", "UINT"]
|
| 141 |
+
|
| 142 |
+
_cast_dict["LONG"] = _cast_dict["INT"] + ["LONG"]
|
| 143 |
+
_cast_dict["ULONG"] = _cast_dict["UINT"] + ["ULONG"]
|
| 144 |
+
|
| 145 |
+
_cast_dict["LONGLONG"] = _cast_dict["LONG"] + ["LONGLONG"]
|
| 146 |
+
_cast_dict["ULONGLONG"] = _cast_dict["ULONG"] + ["ULONGLONG"]
|
| 147 |
+
|
| 148 |
+
_cast_dict["FLOAT"] = _cast_dict["SHORT"] + ["USHORT", "FLOAT"]
|
| 149 |
+
_cast_dict["DOUBLE"] = _cast_dict["INT"] + ["UINT", "FLOAT", "DOUBLE"]
|
| 150 |
+
|
| 151 |
+
_cast_dict["CFLOAT"] = _cast_dict["FLOAT"] + ["CFLOAT"]
|
| 152 |
+
|
| 153 |
+
_cast_dict['STRING1'] = ['STRING1']
|
| 154 |
+
_cast_dict['STRING5'] = ['STRING5']
|
| 155 |
+
_cast_dict['CHARACTER'] = ['CHARACTER']
|
| 156 |
+
|
| 157 |
+
# 32 bit system malloc typically does not provide the alignment required by
|
| 158 |
+
# 16 byte long double types this means the inout intent cannot be satisfied
|
| 159 |
+
# and several tests fail as the alignment flag can be randomly true or fals
|
| 160 |
+
# when numpy gains an aligned allocator the tests could be enabled again
|
| 161 |
+
#
|
| 162 |
+
# Furthermore, on macOS ARM64, LONGDOUBLE is an alias for DOUBLE.
|
| 163 |
+
if ((np.intp().dtype.itemsize != 4 or np.clongdouble().dtype.alignment <= 8)
|
| 164 |
+
and sys.platform != "win32"
|
| 165 |
+
and (platform.system(), platform.processor()) != ("Darwin", "arm")):
|
| 166 |
+
_type_names.extend(["LONGDOUBLE", "CDOUBLE", "CLONGDOUBLE"])
|
| 167 |
+
_cast_dict["LONGDOUBLE"] = _cast_dict["LONG"] + [
|
| 168 |
+
"ULONG",
|
| 169 |
+
"FLOAT",
|
| 170 |
+
"DOUBLE",
|
| 171 |
+
"LONGDOUBLE",
|
| 172 |
+
]
|
| 173 |
+
_cast_dict["CLONGDOUBLE"] = _cast_dict["LONGDOUBLE"] + [
|
| 174 |
+
"CFLOAT",
|
| 175 |
+
"CDOUBLE",
|
| 176 |
+
"CLONGDOUBLE",
|
| 177 |
+
]
|
| 178 |
+
_cast_dict["CDOUBLE"] = _cast_dict["DOUBLE"] + ["CFLOAT", "CDOUBLE"]
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
class Type:
|
| 182 |
+
_type_cache = {}
|
| 183 |
+
|
| 184 |
+
def __new__(cls, name):
|
| 185 |
+
if isinstance(name, np.dtype):
|
| 186 |
+
dtype0 = name
|
| 187 |
+
name = None
|
| 188 |
+
for n, i in typeinfo.items():
|
| 189 |
+
if not isinstance(i, type) and dtype0.type is i.type:
|
| 190 |
+
name = n
|
| 191 |
+
break
|
| 192 |
+
obj = cls._type_cache.get(name.upper(), None)
|
| 193 |
+
if obj is not None:
|
| 194 |
+
return obj
|
| 195 |
+
obj = object.__new__(cls)
|
| 196 |
+
obj._init(name)
|
| 197 |
+
cls._type_cache[name.upper()] = obj
|
| 198 |
+
return obj
|
| 199 |
+
|
| 200 |
+
def _init(self, name):
|
| 201 |
+
self.NAME = name.upper()
|
| 202 |
+
|
| 203 |
+
if self.NAME == 'CHARACTER':
|
| 204 |
+
info = typeinfo[self.NAME]
|
| 205 |
+
self.type_num = getattr(wrap, 'NPY_STRING')
|
| 206 |
+
self.elsize = 1
|
| 207 |
+
self.dtype = np.dtype('c')
|
| 208 |
+
elif self.NAME.startswith('STRING'):
|
| 209 |
+
info = typeinfo[self.NAME[:6]]
|
| 210 |
+
self.type_num = getattr(wrap, 'NPY_STRING')
|
| 211 |
+
self.elsize = int(self.NAME[6:] or 0)
|
| 212 |
+
self.dtype = np.dtype(f'S{self.elsize}')
|
| 213 |
+
else:
|
| 214 |
+
info = typeinfo[self.NAME]
|
| 215 |
+
self.type_num = getattr(wrap, 'NPY_' + self.NAME)
|
| 216 |
+
self.elsize = info.bits // 8
|
| 217 |
+
self.dtype = np.dtype(info.type)
|
| 218 |
+
|
| 219 |
+
assert self.type_num == info.num
|
| 220 |
+
self.type = info.type
|
| 221 |
+
self.dtypechar = info.char
|
| 222 |
+
|
| 223 |
+
def __repr__(self):
|
| 224 |
+
return (f"Type({self.NAME})|type_num={self.type_num},"
|
| 225 |
+
f" dtype={self.dtype},"
|
| 226 |
+
f" type={self.type}, elsize={self.elsize},"
|
| 227 |
+
f" dtypechar={self.dtypechar}")
|
| 228 |
+
|
| 229 |
+
def cast_types(self):
|
| 230 |
+
return [self.__class__(_m) for _m in _cast_dict[self.NAME]]
|
| 231 |
+
|
| 232 |
+
def all_types(self):
|
| 233 |
+
return [self.__class__(_m) for _m in _type_names]
|
| 234 |
+
|
| 235 |
+
def smaller_types(self):
|
| 236 |
+
bits = typeinfo[self.NAME].alignment
|
| 237 |
+
types = []
|
| 238 |
+
for name in _type_names:
|
| 239 |
+
if typeinfo[name].alignment < bits:
|
| 240 |
+
types.append(Type(name))
|
| 241 |
+
return types
|
| 242 |
+
|
| 243 |
+
def equal_types(self):
|
| 244 |
+
bits = typeinfo[self.NAME].alignment
|
| 245 |
+
types = []
|
| 246 |
+
for name in _type_names:
|
| 247 |
+
if name == self.NAME:
|
| 248 |
+
continue
|
| 249 |
+
if typeinfo[name].alignment == bits:
|
| 250 |
+
types.append(Type(name))
|
| 251 |
+
return types
|
| 252 |
+
|
| 253 |
+
def larger_types(self):
|
| 254 |
+
bits = typeinfo[self.NAME].alignment
|
| 255 |
+
types = []
|
| 256 |
+
for name in _type_names:
|
| 257 |
+
if typeinfo[name].alignment > bits:
|
| 258 |
+
types.append(Type(name))
|
| 259 |
+
return types
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
class Array:
|
| 263 |
+
|
| 264 |
+
def __repr__(self):
|
| 265 |
+
return (f'Array({self.type}, {self.dims}, {self.intent},'
|
| 266 |
+
f' {self.obj})|arr={self.arr}')
|
| 267 |
+
|
| 268 |
+
def __init__(self, typ, dims, intent, obj):
|
| 269 |
+
self.type = typ
|
| 270 |
+
self.dims = dims
|
| 271 |
+
self.intent = intent
|
| 272 |
+
self.obj_copy = copy.deepcopy(obj)
|
| 273 |
+
self.obj = obj
|
| 274 |
+
|
| 275 |
+
# arr.dtypechar may be different from typ.dtypechar
|
| 276 |
+
self.arr = wrap.call(typ.type_num,
|
| 277 |
+
typ.elsize,
|
| 278 |
+
dims, intent.flags, obj)
|
| 279 |
+
|
| 280 |
+
assert isinstance(self.arr, np.ndarray)
|
| 281 |
+
|
| 282 |
+
self.arr_attr = wrap.array_attrs(self.arr)
|
| 283 |
+
|
| 284 |
+
if len(dims) > 1:
|
| 285 |
+
if self.intent.is_intent("c"):
|
| 286 |
+
assert (intent.flags & wrap.F2PY_INTENT_C)
|
| 287 |
+
assert not self.arr.flags["FORTRAN"]
|
| 288 |
+
assert self.arr.flags["CONTIGUOUS"]
|
| 289 |
+
assert (not self.arr_attr[6] & wrap.FORTRAN)
|
| 290 |
+
else:
|
| 291 |
+
assert (not intent.flags & wrap.F2PY_INTENT_C)
|
| 292 |
+
assert self.arr.flags["FORTRAN"]
|
| 293 |
+
assert not self.arr.flags["CONTIGUOUS"]
|
| 294 |
+
assert (self.arr_attr[6] & wrap.FORTRAN)
|
| 295 |
+
|
| 296 |
+
if obj is None:
|
| 297 |
+
self.pyarr = None
|
| 298 |
+
self.pyarr_attr = None
|
| 299 |
+
return
|
| 300 |
+
|
| 301 |
+
if intent.is_intent("cache"):
|
| 302 |
+
assert isinstance(obj, np.ndarray), repr(type(obj))
|
| 303 |
+
self.pyarr = np.array(obj).reshape(*dims).copy()
|
| 304 |
+
else:
|
| 305 |
+
self.pyarr = np.array(
|
| 306 |
+
np.array(obj, dtype=typ.dtypechar).reshape(*dims),
|
| 307 |
+
order=self.intent.is_intent("c") and "C" or "F",
|
| 308 |
+
)
|
| 309 |
+
assert self.pyarr.dtype == typ
|
| 310 |
+
self.pyarr.setflags(write=self.arr.flags["WRITEABLE"])
|
| 311 |
+
assert self.pyarr.flags["OWNDATA"], (obj, intent)
|
| 312 |
+
self.pyarr_attr = wrap.array_attrs(self.pyarr)
|
| 313 |
+
|
| 314 |
+
if len(dims) > 1:
|
| 315 |
+
if self.intent.is_intent("c"):
|
| 316 |
+
assert not self.pyarr.flags["FORTRAN"]
|
| 317 |
+
assert self.pyarr.flags["CONTIGUOUS"]
|
| 318 |
+
assert (not self.pyarr_attr[6] & wrap.FORTRAN)
|
| 319 |
+
else:
|
| 320 |
+
assert self.pyarr.flags["FORTRAN"]
|
| 321 |
+
assert not self.pyarr.flags["CONTIGUOUS"]
|
| 322 |
+
assert (self.pyarr_attr[6] & wrap.FORTRAN)
|
| 323 |
+
|
| 324 |
+
assert self.arr_attr[1] == self.pyarr_attr[1] # nd
|
| 325 |
+
assert self.arr_attr[2] == self.pyarr_attr[2] # dimensions
|
| 326 |
+
if self.arr_attr[1] <= 1:
|
| 327 |
+
assert self.arr_attr[3] == self.pyarr_attr[3], repr((
|
| 328 |
+
self.arr_attr[3],
|
| 329 |
+
self.pyarr_attr[3],
|
| 330 |
+
self.arr.tobytes(),
|
| 331 |
+
self.pyarr.tobytes(),
|
| 332 |
+
)) # strides
|
| 333 |
+
assert self.arr_attr[5][-2:] == self.pyarr_attr[5][-2:], repr((
|
| 334 |
+
self.arr_attr[5], self.pyarr_attr[5]
|
| 335 |
+
)) # descr
|
| 336 |
+
assert self.arr_attr[6] == self.pyarr_attr[6], repr((
|
| 337 |
+
self.arr_attr[6],
|
| 338 |
+
self.pyarr_attr[6],
|
| 339 |
+
flags2names(0 * self.arr_attr[6] - self.pyarr_attr[6]),
|
| 340 |
+
flags2names(self.arr_attr[6]),
|
| 341 |
+
intent,
|
| 342 |
+
)) # flags
|
| 343 |
+
|
| 344 |
+
if intent.is_intent("cache"):
|
| 345 |
+
assert self.arr_attr[5][3] >= self.type.elsize
|
| 346 |
+
else:
|
| 347 |
+
assert self.arr_attr[5][3] == self.type.elsize
|
| 348 |
+
assert (self.arr_equal(self.pyarr, self.arr))
|
| 349 |
+
|
| 350 |
+
if isinstance(self.obj, np.ndarray):
|
| 351 |
+
if typ.elsize == Type(obj.dtype).elsize:
|
| 352 |
+
if not intent.is_intent("copy") and self.arr_attr[1] <= 1:
|
| 353 |
+
assert self.has_shared_memory()
|
| 354 |
+
|
| 355 |
+
def arr_equal(self, arr1, arr2):
|
| 356 |
+
if arr1.shape != arr2.shape:
|
| 357 |
+
return False
|
| 358 |
+
return (arr1 == arr2).all()
|
| 359 |
+
|
| 360 |
+
def __str__(self):
|
| 361 |
+
return str(self.arr)
|
| 362 |
+
|
| 363 |
+
def has_shared_memory(self):
|
| 364 |
+
"""Check that created array shares data with input array."""
|
| 365 |
+
if self.obj is self.arr:
|
| 366 |
+
return True
|
| 367 |
+
if not isinstance(self.obj, np.ndarray):
|
| 368 |
+
return False
|
| 369 |
+
obj_attr = wrap.array_attrs(self.obj)
|
| 370 |
+
return obj_attr[0] == self.arr_attr[0]
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
class TestIntent:
|
| 374 |
+
def test_in_out(self):
|
| 375 |
+
assert str(intent.in_.out) == "intent(in,out)"
|
| 376 |
+
assert intent.in_.c.is_intent("c")
|
| 377 |
+
assert not intent.in_.c.is_intent_exact("c")
|
| 378 |
+
assert intent.in_.c.is_intent_exact("c", "in")
|
| 379 |
+
assert intent.in_.c.is_intent_exact("in", "c")
|
| 380 |
+
assert not intent.in_.is_intent("c")
|
| 381 |
+
|
| 382 |
+
|
| 383 |
+
class TestSharedMemory:
|
| 384 |
+
|
| 385 |
+
@pytest.fixture(autouse=True, scope="class", params=_type_names)
|
| 386 |
+
def setup_type(self, request):
|
| 387 |
+
request.cls.type = Type(request.param)
|
| 388 |
+
request.cls.array = lambda self, dims, intent, obj: Array(
|
| 389 |
+
Type(request.param), dims, intent, obj)
|
| 390 |
+
|
| 391 |
+
@property
|
| 392 |
+
def num2seq(self):
|
| 393 |
+
if self.type.NAME.startswith('STRING'):
|
| 394 |
+
elsize = self.type.elsize
|
| 395 |
+
return ['1' * elsize, '2' * elsize]
|
| 396 |
+
return [1, 2]
|
| 397 |
+
|
| 398 |
+
@property
|
| 399 |
+
def num23seq(self):
|
| 400 |
+
if self.type.NAME.startswith('STRING'):
|
| 401 |
+
elsize = self.type.elsize
|
| 402 |
+
return [['1' * elsize, '2' * elsize, '3' * elsize],
|
| 403 |
+
['4' * elsize, '5' * elsize, '6' * elsize]]
|
| 404 |
+
return [[1, 2, 3], [4, 5, 6]]
|
| 405 |
+
|
| 406 |
+
def test_in_from_2seq(self):
|
| 407 |
+
a = self.array([2], intent.in_, self.num2seq)
|
| 408 |
+
assert not a.has_shared_memory()
|
| 409 |
+
|
| 410 |
+
def test_in_from_2casttype(self):
|
| 411 |
+
for t in self.type.cast_types():
|
| 412 |
+
obj = np.array(self.num2seq, dtype=t.dtype)
|
| 413 |
+
a = self.array([len(self.num2seq)], intent.in_, obj)
|
| 414 |
+
if t.elsize == self.type.elsize:
|
| 415 |
+
assert a.has_shared_memory(), repr((self.type.dtype, t.dtype))
|
| 416 |
+
else:
|
| 417 |
+
assert not a.has_shared_memory()
|
| 418 |
+
|
| 419 |
+
@pytest.mark.parametrize("write", ["w", "ro"])
|
| 420 |
+
@pytest.mark.parametrize("order", ["C", "F"])
|
| 421 |
+
@pytest.mark.parametrize("inp", ["2seq", "23seq"])
|
| 422 |
+
def test_in_nocopy(self, write, order, inp):
|
| 423 |
+
"""Test if intent(in) array can be passed without copies"""
|
| 424 |
+
seq = getattr(self, "num" + inp)
|
| 425 |
+
obj = np.array(seq, dtype=self.type.dtype, order=order)
|
| 426 |
+
obj.setflags(write=(write == 'w'))
|
| 427 |
+
a = self.array(obj.shape,
|
| 428 |
+
((order == 'C' and intent.in_.c) or intent.in_), obj)
|
| 429 |
+
assert a.has_shared_memory()
|
| 430 |
+
|
| 431 |
+
def test_inout_2seq(self):
|
| 432 |
+
obj = np.array(self.num2seq, dtype=self.type.dtype)
|
| 433 |
+
a = self.array([len(self.num2seq)], intent.inout, obj)
|
| 434 |
+
assert a.has_shared_memory()
|
| 435 |
+
|
| 436 |
+
try:
|
| 437 |
+
a = self.array([2], intent.in_.inout, self.num2seq)
|
| 438 |
+
except TypeError as msg:
|
| 439 |
+
if not str(msg).startswith(
|
| 440 |
+
"failed to initialize intent(inout|inplace|cache) array"):
|
| 441 |
+
raise
|
| 442 |
+
else:
|
| 443 |
+
raise SystemError("intent(inout) should have failed on sequence")
|
| 444 |
+
|
| 445 |
+
def test_f_inout_23seq(self):
|
| 446 |
+
obj = np.array(self.num23seq, dtype=self.type.dtype, order="F")
|
| 447 |
+
shape = (len(self.num23seq), len(self.num23seq[0]))
|
| 448 |
+
a = self.array(shape, intent.in_.inout, obj)
|
| 449 |
+
assert a.has_shared_memory()
|
| 450 |
+
|
| 451 |
+
obj = np.array(self.num23seq, dtype=self.type.dtype, order="C")
|
| 452 |
+
shape = (len(self.num23seq), len(self.num23seq[0]))
|
| 453 |
+
try:
|
| 454 |
+
a = self.array(shape, intent.in_.inout, obj)
|
| 455 |
+
except ValueError as msg:
|
| 456 |
+
if not str(msg).startswith(
|
| 457 |
+
"failed to initialize intent(inout) array"):
|
| 458 |
+
raise
|
| 459 |
+
else:
|
| 460 |
+
raise SystemError(
|
| 461 |
+
"intent(inout) should have failed on improper array")
|
| 462 |
+
|
| 463 |
+
def test_c_inout_23seq(self):
|
| 464 |
+
obj = np.array(self.num23seq, dtype=self.type.dtype)
|
| 465 |
+
shape = (len(self.num23seq), len(self.num23seq[0]))
|
| 466 |
+
a = self.array(shape, intent.in_.c.inout, obj)
|
| 467 |
+
assert a.has_shared_memory()
|
| 468 |
+
|
| 469 |
+
def test_in_copy_from_2casttype(self):
|
| 470 |
+
for t in self.type.cast_types():
|
| 471 |
+
obj = np.array(self.num2seq, dtype=t.dtype)
|
| 472 |
+
a = self.array([len(self.num2seq)], intent.in_.copy, obj)
|
| 473 |
+
assert not a.has_shared_memory()
|
| 474 |
+
|
| 475 |
+
def test_c_in_from_23seq(self):
|
| 476 |
+
a = self.array(
|
| 477 |
+
[len(self.num23seq), len(self.num23seq[0])], intent.in_,
|
| 478 |
+
self.num23seq)
|
| 479 |
+
assert not a.has_shared_memory()
|
| 480 |
+
|
| 481 |
+
def test_in_from_23casttype(self):
|
| 482 |
+
for t in self.type.cast_types():
|
| 483 |
+
obj = np.array(self.num23seq, dtype=t.dtype)
|
| 484 |
+
a = self.array(
|
| 485 |
+
[len(self.num23seq), len(self.num23seq[0])], intent.in_, obj)
|
| 486 |
+
assert not a.has_shared_memory()
|
| 487 |
+
|
| 488 |
+
def test_f_in_from_23casttype(self):
|
| 489 |
+
for t in self.type.cast_types():
|
| 490 |
+
obj = np.array(self.num23seq, dtype=t.dtype, order="F")
|
| 491 |
+
a = self.array(
|
| 492 |
+
[len(self.num23seq), len(self.num23seq[0])], intent.in_, obj)
|
| 493 |
+
if t.elsize == self.type.elsize:
|
| 494 |
+
assert a.has_shared_memory()
|
| 495 |
+
else:
|
| 496 |
+
assert not a.has_shared_memory()
|
| 497 |
+
|
| 498 |
+
def test_c_in_from_23casttype(self):
|
| 499 |
+
for t in self.type.cast_types():
|
| 500 |
+
obj = np.array(self.num23seq, dtype=t.dtype)
|
| 501 |
+
a = self.array(
|
| 502 |
+
[len(self.num23seq), len(self.num23seq[0])], intent.in_.c, obj)
|
| 503 |
+
if t.elsize == self.type.elsize:
|
| 504 |
+
assert a.has_shared_memory()
|
| 505 |
+
else:
|
| 506 |
+
assert not a.has_shared_memory()
|
| 507 |
+
|
| 508 |
+
def test_f_copy_in_from_23casttype(self):
|
| 509 |
+
for t in self.type.cast_types():
|
| 510 |
+
obj = np.array(self.num23seq, dtype=t.dtype, order="F")
|
| 511 |
+
a = self.array(
|
| 512 |
+
[len(self.num23seq), len(self.num23seq[0])], intent.in_.copy,
|
| 513 |
+
obj)
|
| 514 |
+
assert not a.has_shared_memory()
|
| 515 |
+
|
| 516 |
+
def test_c_copy_in_from_23casttype(self):
|
| 517 |
+
for t in self.type.cast_types():
|
| 518 |
+
obj = np.array(self.num23seq, dtype=t.dtype)
|
| 519 |
+
a = self.array(
|
| 520 |
+
[len(self.num23seq), len(self.num23seq[0])], intent.in_.c.copy,
|
| 521 |
+
obj)
|
| 522 |
+
assert not a.has_shared_memory()
|
| 523 |
+
|
| 524 |
+
def test_in_cache_from_2casttype(self):
|
| 525 |
+
for t in self.type.all_types():
|
| 526 |
+
if t.elsize != self.type.elsize:
|
| 527 |
+
continue
|
| 528 |
+
obj = np.array(self.num2seq, dtype=t.dtype)
|
| 529 |
+
shape = (len(self.num2seq), )
|
| 530 |
+
a = self.array(shape, intent.in_.c.cache, obj)
|
| 531 |
+
assert a.has_shared_memory()
|
| 532 |
+
|
| 533 |
+
a = self.array(shape, intent.in_.cache, obj)
|
| 534 |
+
assert a.has_shared_memory()
|
| 535 |
+
|
| 536 |
+
obj = np.array(self.num2seq, dtype=t.dtype, order="F")
|
| 537 |
+
a = self.array(shape, intent.in_.c.cache, obj)
|
| 538 |
+
assert a.has_shared_memory()
|
| 539 |
+
|
| 540 |
+
a = self.array(shape, intent.in_.cache, obj)
|
| 541 |
+
assert a.has_shared_memory(), repr(t.dtype)
|
| 542 |
+
|
| 543 |
+
try:
|
| 544 |
+
a = self.array(shape, intent.in_.cache, obj[::-1])
|
| 545 |
+
except ValueError as msg:
|
| 546 |
+
if not str(msg).startswith(
|
| 547 |
+
"failed to initialize intent(cache) array"):
|
| 548 |
+
raise
|
| 549 |
+
else:
|
| 550 |
+
raise SystemError(
|
| 551 |
+
"intent(cache) should have failed on multisegmented array")
|
| 552 |
+
|
| 553 |
+
def test_in_cache_from_2casttype_failure(self):
|
| 554 |
+
for t in self.type.all_types():
|
| 555 |
+
if t.NAME == 'STRING':
|
| 556 |
+
# string elsize is 0, so skipping the test
|
| 557 |
+
continue
|
| 558 |
+
if t.elsize >= self.type.elsize:
|
| 559 |
+
continue
|
| 560 |
+
obj = np.array(self.num2seq, dtype=t.dtype)
|
| 561 |
+
shape = (len(self.num2seq), )
|
| 562 |
+
try:
|
| 563 |
+
self.array(shape, intent.in_.cache, obj) # Should succeed
|
| 564 |
+
except ValueError as msg:
|
| 565 |
+
if not str(msg).startswith(
|
| 566 |
+
"failed to initialize intent(cache) array"):
|
| 567 |
+
raise
|
| 568 |
+
else:
|
| 569 |
+
raise SystemError(
|
| 570 |
+
"intent(cache) should have failed on smaller array")
|
| 571 |
+
|
| 572 |
+
def test_cache_hidden(self):
|
| 573 |
+
shape = (2, )
|
| 574 |
+
a = self.array(shape, intent.cache.hide, None)
|
| 575 |
+
assert a.arr.shape == shape
|
| 576 |
+
|
| 577 |
+
shape = (2, 3)
|
| 578 |
+
a = self.array(shape, intent.cache.hide, None)
|
| 579 |
+
assert a.arr.shape == shape
|
| 580 |
+
|
| 581 |
+
shape = (-1, 3)
|
| 582 |
+
try:
|
| 583 |
+
a = self.array(shape, intent.cache.hide, None)
|
| 584 |
+
except ValueError as msg:
|
| 585 |
+
if not str(msg).startswith(
|
| 586 |
+
"failed to create intent(cache|hide)|optional array"):
|
| 587 |
+
raise
|
| 588 |
+
else:
|
| 589 |
+
raise SystemError(
|
| 590 |
+
"intent(cache) should have failed on undefined dimensions")
|
| 591 |
+
|
| 592 |
+
def test_hidden(self):
|
| 593 |
+
shape = (2, )
|
| 594 |
+
a = self.array(shape, intent.hide, None)
|
| 595 |
+
assert a.arr.shape == shape
|
| 596 |
+
assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
|
| 597 |
+
|
| 598 |
+
shape = (2, 3)
|
| 599 |
+
a = self.array(shape, intent.hide, None)
|
| 600 |
+
assert a.arr.shape == shape
|
| 601 |
+
assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
|
| 602 |
+
assert a.arr.flags["FORTRAN"] and not a.arr.flags["CONTIGUOUS"]
|
| 603 |
+
|
| 604 |
+
shape = (2, 3)
|
| 605 |
+
a = self.array(shape, intent.c.hide, None)
|
| 606 |
+
assert a.arr.shape == shape
|
| 607 |
+
assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
|
| 608 |
+
assert not a.arr.flags["FORTRAN"] and a.arr.flags["CONTIGUOUS"]
|
| 609 |
+
|
| 610 |
+
shape = (-1, 3)
|
| 611 |
+
try:
|
| 612 |
+
a = self.array(shape, intent.hide, None)
|
| 613 |
+
except ValueError as msg:
|
| 614 |
+
if not str(msg).startswith(
|
| 615 |
+
"failed to create intent(cache|hide)|optional array"):
|
| 616 |
+
raise
|
| 617 |
+
else:
|
| 618 |
+
raise SystemError(
|
| 619 |
+
"intent(hide) should have failed on undefined dimensions")
|
| 620 |
+
|
| 621 |
+
def test_optional_none(self):
|
| 622 |
+
shape = (2, )
|
| 623 |
+
a = self.array(shape, intent.optional, None)
|
| 624 |
+
assert a.arr.shape == shape
|
| 625 |
+
assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
|
| 626 |
+
|
| 627 |
+
shape = (2, 3)
|
| 628 |
+
a = self.array(shape, intent.optional, None)
|
| 629 |
+
assert a.arr.shape == shape
|
| 630 |
+
assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
|
| 631 |
+
assert a.arr.flags["FORTRAN"] and not a.arr.flags["CONTIGUOUS"]
|
| 632 |
+
|
| 633 |
+
shape = (2, 3)
|
| 634 |
+
a = self.array(shape, intent.c.optional, None)
|
| 635 |
+
assert a.arr.shape == shape
|
| 636 |
+
assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
|
| 637 |
+
assert not a.arr.flags["FORTRAN"] and a.arr.flags["CONTIGUOUS"]
|
| 638 |
+
|
| 639 |
+
def test_optional_from_2seq(self):
|
| 640 |
+
obj = self.num2seq
|
| 641 |
+
shape = (len(obj), )
|
| 642 |
+
a = self.array(shape, intent.optional, obj)
|
| 643 |
+
assert a.arr.shape == shape
|
| 644 |
+
assert not a.has_shared_memory()
|
| 645 |
+
|
| 646 |
+
def test_optional_from_23seq(self):
|
| 647 |
+
obj = self.num23seq
|
| 648 |
+
shape = (len(obj), len(obj[0]))
|
| 649 |
+
a = self.array(shape, intent.optional, obj)
|
| 650 |
+
assert a.arr.shape == shape
|
| 651 |
+
assert not a.has_shared_memory()
|
| 652 |
+
|
| 653 |
+
a = self.array(shape, intent.optional.c, obj)
|
| 654 |
+
assert a.arr.shape == shape
|
| 655 |
+
assert not a.has_shared_memory()
|
| 656 |
+
|
| 657 |
+
def test_inplace(self):
|
| 658 |
+
obj = np.array(self.num23seq, dtype=self.type.dtype)
|
| 659 |
+
assert not obj.flags["FORTRAN"] and obj.flags["CONTIGUOUS"]
|
| 660 |
+
shape = obj.shape
|
| 661 |
+
a = self.array(shape, intent.inplace, obj)
|
| 662 |
+
assert obj[1][2] == a.arr[1][2], repr((obj, a.arr))
|
| 663 |
+
a.arr[1][2] = 54
|
| 664 |
+
assert obj[1][2] == a.arr[1][2] == np.array(54, dtype=self.type.dtype)
|
| 665 |
+
assert a.arr is obj
|
| 666 |
+
assert obj.flags["FORTRAN"] # obj attributes are changed inplace!
|
| 667 |
+
assert not obj.flags["CONTIGUOUS"]
|
| 668 |
+
|
| 669 |
+
def test_inplace_from_casttype(self):
|
| 670 |
+
for t in self.type.cast_types():
|
| 671 |
+
if t is self.type:
|
| 672 |
+
continue
|
| 673 |
+
obj = np.array(self.num23seq, dtype=t.dtype)
|
| 674 |
+
assert obj.dtype.type == t.type
|
| 675 |
+
assert obj.dtype.type is not self.type.type
|
| 676 |
+
assert not obj.flags["FORTRAN"] and obj.flags["CONTIGUOUS"]
|
| 677 |
+
shape = obj.shape
|
| 678 |
+
a = self.array(shape, intent.inplace, obj)
|
| 679 |
+
assert obj[1][2] == a.arr[1][2], repr((obj, a.arr))
|
| 680 |
+
a.arr[1][2] = 54
|
| 681 |
+
assert obj[1][2] == a.arr[1][2] == np.array(54,
|
| 682 |
+
dtype=self.type.dtype)
|
| 683 |
+
assert a.arr is obj
|
| 684 |
+
assert obj.flags["FORTRAN"] # obj attributes changed inplace!
|
| 685 |
+
assert not obj.flags["CONTIGUOUS"]
|
| 686 |
+
assert obj.dtype.type is self.type.type # obj changed inplace!
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_assumed_shape.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pytest
|
| 3 |
+
import tempfile
|
| 4 |
+
|
| 5 |
+
from . import util
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TestAssumedShapeSumExample(util.F2PyTest):
|
| 9 |
+
sources = [
|
| 10 |
+
util.getpath("tests", "src", "assumed_shape", "foo_free.f90"),
|
| 11 |
+
util.getpath("tests", "src", "assumed_shape", "foo_use.f90"),
|
| 12 |
+
util.getpath("tests", "src", "assumed_shape", "precision.f90"),
|
| 13 |
+
util.getpath("tests", "src", "assumed_shape", "foo_mod.f90"),
|
| 14 |
+
util.getpath("tests", "src", "assumed_shape", ".f2py_f2cmap"),
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
@pytest.mark.slow
|
| 18 |
+
def test_all(self):
|
| 19 |
+
r = self.module.fsum([1, 2])
|
| 20 |
+
assert r == 3
|
| 21 |
+
r = self.module.sum([1, 2])
|
| 22 |
+
assert r == 3
|
| 23 |
+
r = self.module.sum_with_use([1, 2])
|
| 24 |
+
assert r == 3
|
| 25 |
+
|
| 26 |
+
r = self.module.mod.sum([1, 2])
|
| 27 |
+
assert r == 3
|
| 28 |
+
r = self.module.mod.fsum([1, 2])
|
| 29 |
+
assert r == 3
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class TestF2cmapOption(TestAssumedShapeSumExample):
|
| 33 |
+
def setup_method(self):
|
| 34 |
+
# Use a custom file name for .f2py_f2cmap
|
| 35 |
+
self.sources = list(self.sources)
|
| 36 |
+
f2cmap_src = self.sources.pop(-1)
|
| 37 |
+
|
| 38 |
+
self.f2cmap_file = tempfile.NamedTemporaryFile(delete=False)
|
| 39 |
+
with open(f2cmap_src, "rb") as f:
|
| 40 |
+
self.f2cmap_file.write(f.read())
|
| 41 |
+
self.f2cmap_file.close()
|
| 42 |
+
|
| 43 |
+
self.sources.append(self.f2cmap_file.name)
|
| 44 |
+
self.options = ["--f2cmap", self.f2cmap_file.name]
|
| 45 |
+
|
| 46 |
+
super().setup_method()
|
| 47 |
+
|
| 48 |
+
def teardown_method(self):
|
| 49 |
+
os.unlink(self.f2cmap_file.name)
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_block_docstring.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import pytest
|
| 3 |
+
from . import util
|
| 4 |
+
|
| 5 |
+
from numpy.testing import IS_PYPY
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TestBlockDocString(util.F2PyTest):
|
| 9 |
+
sources = [util.getpath("tests", "src", "block_docstring", "foo.f")]
|
| 10 |
+
|
| 11 |
+
@pytest.mark.skipif(sys.platform == "win32",
|
| 12 |
+
reason="Fails with MinGW64 Gfortran (Issue #9673)")
|
| 13 |
+
@pytest.mark.xfail(IS_PYPY,
|
| 14 |
+
reason="PyPy cannot modify tp_doc after PyType_Ready")
|
| 15 |
+
def test_block_docstring(self):
|
| 16 |
+
expected = "bar : 'i'-array(2,3)\n"
|
| 17 |
+
assert self.module.block.__doc__ == expected
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_callback.py
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import textwrap
|
| 3 |
+
import sys
|
| 4 |
+
import pytest
|
| 5 |
+
import threading
|
| 6 |
+
import traceback
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
from numpy.testing import IS_PYPY
|
| 11 |
+
from . import util
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestF77Callback(util.F2PyTest):
|
| 15 |
+
sources = [util.getpath("tests", "src", "callback", "foo.f")]
|
| 16 |
+
|
| 17 |
+
@pytest.mark.parametrize("name", "t,t2".split(","))
|
| 18 |
+
def test_all(self, name):
|
| 19 |
+
self.check_function(name)
|
| 20 |
+
|
| 21 |
+
@pytest.mark.xfail(IS_PYPY,
|
| 22 |
+
reason="PyPy cannot modify tp_doc after PyType_Ready")
|
| 23 |
+
def test_docstring(self):
|
| 24 |
+
expected = textwrap.dedent("""\
|
| 25 |
+
a = t(fun,[fun_extra_args])
|
| 26 |
+
|
| 27 |
+
Wrapper for ``t``.
|
| 28 |
+
|
| 29 |
+
Parameters
|
| 30 |
+
----------
|
| 31 |
+
fun : call-back function
|
| 32 |
+
|
| 33 |
+
Other Parameters
|
| 34 |
+
----------------
|
| 35 |
+
fun_extra_args : input tuple, optional
|
| 36 |
+
Default: ()
|
| 37 |
+
|
| 38 |
+
Returns
|
| 39 |
+
-------
|
| 40 |
+
a : int
|
| 41 |
+
|
| 42 |
+
Notes
|
| 43 |
+
-----
|
| 44 |
+
Call-back functions::
|
| 45 |
+
|
| 46 |
+
def fun(): return a
|
| 47 |
+
Return objects:
|
| 48 |
+
a : int
|
| 49 |
+
""")
|
| 50 |
+
assert self.module.t.__doc__ == expected
|
| 51 |
+
|
| 52 |
+
def check_function(self, name):
|
| 53 |
+
t = getattr(self.module, name)
|
| 54 |
+
r = t(lambda: 4)
|
| 55 |
+
assert r == 4
|
| 56 |
+
r = t(lambda a: 5, fun_extra_args=(6, ))
|
| 57 |
+
assert r == 5
|
| 58 |
+
r = t(lambda a: a, fun_extra_args=(6, ))
|
| 59 |
+
assert r == 6
|
| 60 |
+
r = t(lambda a: 5 + a, fun_extra_args=(7, ))
|
| 61 |
+
assert r == 12
|
| 62 |
+
r = t(lambda a: math.degrees(a), fun_extra_args=(math.pi, ))
|
| 63 |
+
assert r == 180
|
| 64 |
+
r = t(math.degrees, fun_extra_args=(math.pi, ))
|
| 65 |
+
assert r == 180
|
| 66 |
+
|
| 67 |
+
r = t(self.module.func, fun_extra_args=(6, ))
|
| 68 |
+
assert r == 17
|
| 69 |
+
r = t(self.module.func0)
|
| 70 |
+
assert r == 11
|
| 71 |
+
r = t(self.module.func0._cpointer)
|
| 72 |
+
assert r == 11
|
| 73 |
+
|
| 74 |
+
class A:
|
| 75 |
+
def __call__(self):
|
| 76 |
+
return 7
|
| 77 |
+
|
| 78 |
+
def mth(self):
|
| 79 |
+
return 9
|
| 80 |
+
|
| 81 |
+
a = A()
|
| 82 |
+
r = t(a)
|
| 83 |
+
assert r == 7
|
| 84 |
+
r = t(a.mth)
|
| 85 |
+
assert r == 9
|
| 86 |
+
|
| 87 |
+
@pytest.mark.skipif(sys.platform == 'win32',
|
| 88 |
+
reason='Fails with MinGW64 Gfortran (Issue #9673)')
|
| 89 |
+
def test_string_callback(self):
|
| 90 |
+
def callback(code):
|
| 91 |
+
if code == "r":
|
| 92 |
+
return 0
|
| 93 |
+
else:
|
| 94 |
+
return 1
|
| 95 |
+
|
| 96 |
+
f = getattr(self.module, "string_callback")
|
| 97 |
+
r = f(callback)
|
| 98 |
+
assert r == 0
|
| 99 |
+
|
| 100 |
+
@pytest.mark.skipif(sys.platform == 'win32',
|
| 101 |
+
reason='Fails with MinGW64 Gfortran (Issue #9673)')
|
| 102 |
+
def test_string_callback_array(self):
|
| 103 |
+
# See gh-10027
|
| 104 |
+
cu1 = np.zeros((1, ), "S8")
|
| 105 |
+
cu2 = np.zeros((1, 8), "c")
|
| 106 |
+
cu3 = np.array([""], "S8")
|
| 107 |
+
|
| 108 |
+
def callback(cu, lencu):
|
| 109 |
+
if cu.shape != (lencu,):
|
| 110 |
+
return 1
|
| 111 |
+
if cu.dtype != "S8":
|
| 112 |
+
return 2
|
| 113 |
+
if not np.all(cu == b""):
|
| 114 |
+
return 3
|
| 115 |
+
return 0
|
| 116 |
+
|
| 117 |
+
f = getattr(self.module, "string_callback_array")
|
| 118 |
+
for cu in [cu1, cu2, cu3]:
|
| 119 |
+
res = f(callback, cu, cu.size)
|
| 120 |
+
assert res == 0
|
| 121 |
+
|
| 122 |
+
def test_threadsafety(self):
|
| 123 |
+
# Segfaults if the callback handling is not threadsafe
|
| 124 |
+
|
| 125 |
+
errors = []
|
| 126 |
+
|
| 127 |
+
def cb():
|
| 128 |
+
# Sleep here to make it more likely for another thread
|
| 129 |
+
# to call their callback at the same time.
|
| 130 |
+
time.sleep(1e-3)
|
| 131 |
+
|
| 132 |
+
# Check reentrancy
|
| 133 |
+
r = self.module.t(lambda: 123)
|
| 134 |
+
assert r == 123
|
| 135 |
+
|
| 136 |
+
return 42
|
| 137 |
+
|
| 138 |
+
def runner(name):
|
| 139 |
+
try:
|
| 140 |
+
for j in range(50):
|
| 141 |
+
r = self.module.t(cb)
|
| 142 |
+
assert r == 42
|
| 143 |
+
self.check_function(name)
|
| 144 |
+
except Exception:
|
| 145 |
+
errors.append(traceback.format_exc())
|
| 146 |
+
|
| 147 |
+
threads = [
|
| 148 |
+
threading.Thread(target=runner, args=(arg, ))
|
| 149 |
+
for arg in ("t", "t2") for n in range(20)
|
| 150 |
+
]
|
| 151 |
+
|
| 152 |
+
for t in threads:
|
| 153 |
+
t.start()
|
| 154 |
+
|
| 155 |
+
for t in threads:
|
| 156 |
+
t.join()
|
| 157 |
+
|
| 158 |
+
errors = "\n\n".join(errors)
|
| 159 |
+
if errors:
|
| 160 |
+
raise AssertionError(errors)
|
| 161 |
+
|
| 162 |
+
def test_hidden_callback(self):
|
| 163 |
+
try:
|
| 164 |
+
self.module.hidden_callback(2)
|
| 165 |
+
except Exception as msg:
|
| 166 |
+
assert str(msg).startswith("Callback global_f not defined")
|
| 167 |
+
|
| 168 |
+
try:
|
| 169 |
+
self.module.hidden_callback2(2)
|
| 170 |
+
except Exception as msg:
|
| 171 |
+
assert str(msg).startswith("cb: Callback global_f not defined")
|
| 172 |
+
|
| 173 |
+
self.module.global_f = lambda x: x + 1
|
| 174 |
+
r = self.module.hidden_callback(2)
|
| 175 |
+
assert r == 3
|
| 176 |
+
|
| 177 |
+
self.module.global_f = lambda x: x + 2
|
| 178 |
+
r = self.module.hidden_callback(2)
|
| 179 |
+
assert r == 4
|
| 180 |
+
|
| 181 |
+
del self.module.global_f
|
| 182 |
+
try:
|
| 183 |
+
self.module.hidden_callback(2)
|
| 184 |
+
except Exception as msg:
|
| 185 |
+
assert str(msg).startswith("Callback global_f not defined")
|
| 186 |
+
|
| 187 |
+
self.module.global_f = lambda x=0: x + 3
|
| 188 |
+
r = self.module.hidden_callback(2)
|
| 189 |
+
assert r == 5
|
| 190 |
+
|
| 191 |
+
# reproducer of gh18341
|
| 192 |
+
r = self.module.hidden_callback2(2)
|
| 193 |
+
assert r == 3
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
class TestF77CallbackPythonTLS(TestF77Callback):
|
| 197 |
+
"""
|
| 198 |
+
Callback tests using Python thread-local storage instead of
|
| 199 |
+
compiler-provided
|
| 200 |
+
"""
|
| 201 |
+
|
| 202 |
+
options = ["-DF2PY_USE_PYTHON_TLS"]
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
class TestF90Callback(util.F2PyTest):
|
| 206 |
+
sources = [util.getpath("tests", "src", "callback", "gh17797.f90")]
|
| 207 |
+
|
| 208 |
+
def test_gh17797(self):
|
| 209 |
+
def incr(x):
|
| 210 |
+
return x + 123
|
| 211 |
+
|
| 212 |
+
y = np.array([1, 2, 3], dtype=np.int64)
|
| 213 |
+
r = self.module.gh17797(incr, y)
|
| 214 |
+
assert r == 123 + 1 + 2 + 3
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
class TestGH18335(util.F2PyTest):
|
| 218 |
+
"""The reproduction of the reported issue requires specific input that
|
| 219 |
+
extensions may break the issue conditions, so the reproducer is
|
| 220 |
+
implemented as a separate test class. Do not extend this test with
|
| 221 |
+
other tests!
|
| 222 |
+
"""
|
| 223 |
+
sources = [util.getpath("tests", "src", "callback", "gh18335.f90")]
|
| 224 |
+
|
| 225 |
+
def test_gh18335(self):
|
| 226 |
+
def foo(x):
|
| 227 |
+
x[0] += 1
|
| 228 |
+
|
| 229 |
+
r = self.module.gh18335(foo)
|
| 230 |
+
assert r == 123 + 1
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
class TestGH25211(util.F2PyTest):
|
| 234 |
+
sources = [util.getpath("tests", "src", "callback", "gh25211.f"),
|
| 235 |
+
util.getpath("tests", "src", "callback", "gh25211.pyf")]
|
| 236 |
+
module_name = "callback2"
|
| 237 |
+
|
| 238 |
+
def test_gh18335(self):
|
| 239 |
+
def bar(x):
|
| 240 |
+
return x*x
|
| 241 |
+
|
| 242 |
+
res = self.module.foo(bar)
|
| 243 |
+
assert res == 110
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_character.py
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import textwrap
|
| 3 |
+
from numpy.testing import assert_array_equal, assert_equal, assert_raises
|
| 4 |
+
import numpy as np
|
| 5 |
+
from numpy.f2py.tests import util
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TestCharacterString(util.F2PyTest):
|
| 9 |
+
# options = ['--debug-capi', '--build-dir', '/tmp/test-build-f2py']
|
| 10 |
+
suffix = '.f90'
|
| 11 |
+
fprefix = 'test_character_string'
|
| 12 |
+
length_list = ['1', '3', 'star']
|
| 13 |
+
|
| 14 |
+
code = ''
|
| 15 |
+
for length in length_list:
|
| 16 |
+
fsuffix = length
|
| 17 |
+
clength = dict(star='(*)').get(length, length)
|
| 18 |
+
|
| 19 |
+
code += textwrap.dedent(f"""
|
| 20 |
+
|
| 21 |
+
subroutine {fprefix}_input_{fsuffix}(c, o, n)
|
| 22 |
+
character*{clength}, intent(in) :: c
|
| 23 |
+
integer n
|
| 24 |
+
!f2py integer, depend(c), intent(hide) :: n = slen(c)
|
| 25 |
+
integer*1, dimension(n) :: o
|
| 26 |
+
!f2py intent(out) o
|
| 27 |
+
o = transfer(c, o)
|
| 28 |
+
end subroutine {fprefix}_input_{fsuffix}
|
| 29 |
+
|
| 30 |
+
subroutine {fprefix}_output_{fsuffix}(c, o, n)
|
| 31 |
+
character*{clength}, intent(out) :: c
|
| 32 |
+
integer n
|
| 33 |
+
integer*1, dimension(n), intent(in) :: o
|
| 34 |
+
!f2py integer, depend(o), intent(hide) :: n = len(o)
|
| 35 |
+
c = transfer(o, c)
|
| 36 |
+
end subroutine {fprefix}_output_{fsuffix}
|
| 37 |
+
|
| 38 |
+
subroutine {fprefix}_array_input_{fsuffix}(c, o, m, n)
|
| 39 |
+
integer m, i, n
|
| 40 |
+
character*{clength}, intent(in), dimension(m) :: c
|
| 41 |
+
!f2py integer, depend(c), intent(hide) :: m = len(c)
|
| 42 |
+
!f2py integer, depend(c), intent(hide) :: n = f2py_itemsize(c)
|
| 43 |
+
integer*1, dimension(m, n), intent(out) :: o
|
| 44 |
+
do i=1,m
|
| 45 |
+
o(i, :) = transfer(c(i), o(i, :))
|
| 46 |
+
end do
|
| 47 |
+
end subroutine {fprefix}_array_input_{fsuffix}
|
| 48 |
+
|
| 49 |
+
subroutine {fprefix}_array_output_{fsuffix}(c, o, m, n)
|
| 50 |
+
character*{clength}, intent(out), dimension(m) :: c
|
| 51 |
+
integer n
|
| 52 |
+
integer*1, dimension(m, n), intent(in) :: o
|
| 53 |
+
!f2py character(f2py_len=n) :: c
|
| 54 |
+
!f2py integer, depend(o), intent(hide) :: m = len(o)
|
| 55 |
+
!f2py integer, depend(o), intent(hide) :: n = shape(o, 1)
|
| 56 |
+
do i=1,m
|
| 57 |
+
c(i) = transfer(o(i, :), c(i))
|
| 58 |
+
end do
|
| 59 |
+
end subroutine {fprefix}_array_output_{fsuffix}
|
| 60 |
+
|
| 61 |
+
subroutine {fprefix}_2d_array_input_{fsuffix}(c, o, m1, m2, n)
|
| 62 |
+
integer m1, m2, i, j, n
|
| 63 |
+
character*{clength}, intent(in), dimension(m1, m2) :: c
|
| 64 |
+
!f2py integer, depend(c), intent(hide) :: m1 = len(c)
|
| 65 |
+
!f2py integer, depend(c), intent(hide) :: m2 = shape(c, 1)
|
| 66 |
+
!f2py integer, depend(c), intent(hide) :: n = f2py_itemsize(c)
|
| 67 |
+
integer*1, dimension(m1, m2, n), intent(out) :: o
|
| 68 |
+
do i=1,m1
|
| 69 |
+
do j=1,m2
|
| 70 |
+
o(i, j, :) = transfer(c(i, j), o(i, j, :))
|
| 71 |
+
end do
|
| 72 |
+
end do
|
| 73 |
+
end subroutine {fprefix}_2d_array_input_{fsuffix}
|
| 74 |
+
""")
|
| 75 |
+
|
| 76 |
+
@pytest.mark.parametrize("length", length_list)
|
| 77 |
+
def test_input(self, length):
|
| 78 |
+
fsuffix = {'(*)': 'star'}.get(length, length)
|
| 79 |
+
f = getattr(self.module, self.fprefix + '_input_' + fsuffix)
|
| 80 |
+
|
| 81 |
+
a = {'1': 'a', '3': 'abc', 'star': 'abcde' * 3}[length]
|
| 82 |
+
|
| 83 |
+
assert_array_equal(f(a), np.array(list(map(ord, a)), dtype='u1'))
|
| 84 |
+
|
| 85 |
+
@pytest.mark.parametrize("length", length_list[:-1])
|
| 86 |
+
def test_output(self, length):
|
| 87 |
+
fsuffix = length
|
| 88 |
+
f = getattr(self.module, self.fprefix + '_output_' + fsuffix)
|
| 89 |
+
|
| 90 |
+
a = {'1': 'a', '3': 'abc'}[length]
|
| 91 |
+
|
| 92 |
+
assert_array_equal(f(np.array(list(map(ord, a)), dtype='u1')),
|
| 93 |
+
a.encode())
|
| 94 |
+
|
| 95 |
+
@pytest.mark.parametrize("length", length_list)
|
| 96 |
+
def test_array_input(self, length):
|
| 97 |
+
fsuffix = length
|
| 98 |
+
f = getattr(self.module, self.fprefix + '_array_input_' + fsuffix)
|
| 99 |
+
|
| 100 |
+
a = np.array([{'1': 'a', '3': 'abc', 'star': 'abcde' * 3}[length],
|
| 101 |
+
{'1': 'A', '3': 'ABC', 'star': 'ABCDE' * 3}[length],
|
| 102 |
+
], dtype='S')
|
| 103 |
+
|
| 104 |
+
expected = np.array([[c for c in s] for s in a], dtype='u1')
|
| 105 |
+
assert_array_equal(f(a), expected)
|
| 106 |
+
|
| 107 |
+
@pytest.mark.parametrize("length", length_list)
|
| 108 |
+
def test_array_output(self, length):
|
| 109 |
+
fsuffix = length
|
| 110 |
+
f = getattr(self.module, self.fprefix + '_array_output_' + fsuffix)
|
| 111 |
+
|
| 112 |
+
expected = np.array(
|
| 113 |
+
[{'1': 'a', '3': 'abc', 'star': 'abcde' * 3}[length],
|
| 114 |
+
{'1': 'A', '3': 'ABC', 'star': 'ABCDE' * 3}[length]], dtype='S')
|
| 115 |
+
|
| 116 |
+
a = np.array([[c for c in s] for s in expected], dtype='u1')
|
| 117 |
+
assert_array_equal(f(a), expected)
|
| 118 |
+
|
| 119 |
+
@pytest.mark.parametrize("length", length_list)
|
| 120 |
+
def test_2d_array_input(self, length):
|
| 121 |
+
fsuffix = length
|
| 122 |
+
f = getattr(self.module, self.fprefix + '_2d_array_input_' + fsuffix)
|
| 123 |
+
|
| 124 |
+
a = np.array([[{'1': 'a', '3': 'abc', 'star': 'abcde' * 3}[length],
|
| 125 |
+
{'1': 'A', '3': 'ABC', 'star': 'ABCDE' * 3}[length]],
|
| 126 |
+
[{'1': 'f', '3': 'fgh', 'star': 'fghij' * 3}[length],
|
| 127 |
+
{'1': 'F', '3': 'FGH', 'star': 'FGHIJ' * 3}[length]]],
|
| 128 |
+
dtype='S')
|
| 129 |
+
expected = np.array([[[c for c in item] for item in row] for row in a],
|
| 130 |
+
dtype='u1', order='F')
|
| 131 |
+
assert_array_equal(f(a), expected)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
class TestCharacter(util.F2PyTest):
|
| 135 |
+
# options = ['--debug-capi', '--build-dir', '/tmp/test-build-f2py']
|
| 136 |
+
suffix = '.f90'
|
| 137 |
+
fprefix = 'test_character'
|
| 138 |
+
|
| 139 |
+
code = textwrap.dedent(f"""
|
| 140 |
+
subroutine {fprefix}_input(c, o)
|
| 141 |
+
character, intent(in) :: c
|
| 142 |
+
integer*1 o
|
| 143 |
+
!f2py intent(out) o
|
| 144 |
+
o = transfer(c, o)
|
| 145 |
+
end subroutine {fprefix}_input
|
| 146 |
+
|
| 147 |
+
subroutine {fprefix}_output(c, o)
|
| 148 |
+
character :: c
|
| 149 |
+
integer*1, intent(in) :: o
|
| 150 |
+
!f2py intent(out) c
|
| 151 |
+
c = transfer(o, c)
|
| 152 |
+
end subroutine {fprefix}_output
|
| 153 |
+
|
| 154 |
+
subroutine {fprefix}_input_output(c, o)
|
| 155 |
+
character, intent(in) :: c
|
| 156 |
+
character o
|
| 157 |
+
!f2py intent(out) o
|
| 158 |
+
o = c
|
| 159 |
+
end subroutine {fprefix}_input_output
|
| 160 |
+
|
| 161 |
+
subroutine {fprefix}_inout(c, n)
|
| 162 |
+
character :: c, n
|
| 163 |
+
!f2py intent(in) n
|
| 164 |
+
!f2py intent(inout) c
|
| 165 |
+
c = n
|
| 166 |
+
end subroutine {fprefix}_inout
|
| 167 |
+
|
| 168 |
+
function {fprefix}_return(o) result (c)
|
| 169 |
+
character :: c
|
| 170 |
+
character, intent(in) :: o
|
| 171 |
+
c = transfer(o, c)
|
| 172 |
+
end function {fprefix}_return
|
| 173 |
+
|
| 174 |
+
subroutine {fprefix}_array_input(c, o)
|
| 175 |
+
character, intent(in) :: c(3)
|
| 176 |
+
integer*1 o(3)
|
| 177 |
+
!f2py intent(out) o
|
| 178 |
+
integer i
|
| 179 |
+
do i=1,3
|
| 180 |
+
o(i) = transfer(c(i), o(i))
|
| 181 |
+
end do
|
| 182 |
+
end subroutine {fprefix}_array_input
|
| 183 |
+
|
| 184 |
+
subroutine {fprefix}_2d_array_input(c, o)
|
| 185 |
+
character, intent(in) :: c(2, 3)
|
| 186 |
+
integer*1 o(2, 3)
|
| 187 |
+
!f2py intent(out) o
|
| 188 |
+
integer i, j
|
| 189 |
+
do i=1,2
|
| 190 |
+
do j=1,3
|
| 191 |
+
o(i, j) = transfer(c(i, j), o(i, j))
|
| 192 |
+
end do
|
| 193 |
+
end do
|
| 194 |
+
end subroutine {fprefix}_2d_array_input
|
| 195 |
+
|
| 196 |
+
subroutine {fprefix}_array_output(c, o)
|
| 197 |
+
character :: c(3)
|
| 198 |
+
integer*1, intent(in) :: o(3)
|
| 199 |
+
!f2py intent(out) c
|
| 200 |
+
do i=1,3
|
| 201 |
+
c(i) = transfer(o(i), c(i))
|
| 202 |
+
end do
|
| 203 |
+
end subroutine {fprefix}_array_output
|
| 204 |
+
|
| 205 |
+
subroutine {fprefix}_array_inout(c, n)
|
| 206 |
+
character :: c(3), n(3)
|
| 207 |
+
!f2py intent(in) n(3)
|
| 208 |
+
!f2py intent(inout) c(3)
|
| 209 |
+
do i=1,3
|
| 210 |
+
c(i) = n(i)
|
| 211 |
+
end do
|
| 212 |
+
end subroutine {fprefix}_array_inout
|
| 213 |
+
|
| 214 |
+
subroutine {fprefix}_2d_array_inout(c, n)
|
| 215 |
+
character :: c(2, 3), n(2, 3)
|
| 216 |
+
!f2py intent(in) n(2, 3)
|
| 217 |
+
!f2py intent(inout) c(2. 3)
|
| 218 |
+
integer i, j
|
| 219 |
+
do i=1,2
|
| 220 |
+
do j=1,3
|
| 221 |
+
c(i, j) = n(i, j)
|
| 222 |
+
end do
|
| 223 |
+
end do
|
| 224 |
+
end subroutine {fprefix}_2d_array_inout
|
| 225 |
+
|
| 226 |
+
function {fprefix}_array_return(o) result (c)
|
| 227 |
+
character, dimension(3) :: c
|
| 228 |
+
character, intent(in) :: o(3)
|
| 229 |
+
do i=1,3
|
| 230 |
+
c(i) = o(i)
|
| 231 |
+
end do
|
| 232 |
+
end function {fprefix}_array_return
|
| 233 |
+
|
| 234 |
+
function {fprefix}_optional(o) result (c)
|
| 235 |
+
character, intent(in) :: o
|
| 236 |
+
!f2py character o = "a"
|
| 237 |
+
character :: c
|
| 238 |
+
c = o
|
| 239 |
+
end function {fprefix}_optional
|
| 240 |
+
""")
|
| 241 |
+
|
| 242 |
+
@pytest.mark.parametrize("dtype", ['c', 'S1'])
|
| 243 |
+
def test_input(self, dtype):
|
| 244 |
+
f = getattr(self.module, self.fprefix + '_input')
|
| 245 |
+
|
| 246 |
+
assert_equal(f(np.array('a', dtype=dtype)), ord('a'))
|
| 247 |
+
assert_equal(f(np.array(b'a', dtype=dtype)), ord('a'))
|
| 248 |
+
assert_equal(f(np.array(['a'], dtype=dtype)), ord('a'))
|
| 249 |
+
assert_equal(f(np.array('abc', dtype=dtype)), ord('a'))
|
| 250 |
+
assert_equal(f(np.array([['a']], dtype=dtype)), ord('a'))
|
| 251 |
+
|
| 252 |
+
def test_input_varia(self):
|
| 253 |
+
f = getattr(self.module, self.fprefix + '_input')
|
| 254 |
+
|
| 255 |
+
assert_equal(f('a'), ord('a'))
|
| 256 |
+
assert_equal(f(b'a'), ord(b'a'))
|
| 257 |
+
assert_equal(f(''), 0)
|
| 258 |
+
assert_equal(f(b''), 0)
|
| 259 |
+
assert_equal(f(b'\0'), 0)
|
| 260 |
+
assert_equal(f('ab'), ord('a'))
|
| 261 |
+
assert_equal(f(b'ab'), ord('a'))
|
| 262 |
+
assert_equal(f(['a']), ord('a'))
|
| 263 |
+
|
| 264 |
+
assert_equal(f(np.array(b'a')), ord('a'))
|
| 265 |
+
assert_equal(f(np.array([b'a'])), ord('a'))
|
| 266 |
+
a = np.array('a')
|
| 267 |
+
assert_equal(f(a), ord('a'))
|
| 268 |
+
a = np.array(['a'])
|
| 269 |
+
assert_equal(f(a), ord('a'))
|
| 270 |
+
|
| 271 |
+
try:
|
| 272 |
+
f([])
|
| 273 |
+
except IndexError as msg:
|
| 274 |
+
if not str(msg).endswith(' got 0-list'):
|
| 275 |
+
raise
|
| 276 |
+
else:
|
| 277 |
+
raise SystemError(f'{f.__name__} should have failed on empty list')
|
| 278 |
+
|
| 279 |
+
try:
|
| 280 |
+
f(97)
|
| 281 |
+
except TypeError as msg:
|
| 282 |
+
if not str(msg).endswith(' got int instance'):
|
| 283 |
+
raise
|
| 284 |
+
else:
|
| 285 |
+
raise SystemError(f'{f.__name__} should have failed on int value')
|
| 286 |
+
|
| 287 |
+
@pytest.mark.parametrize("dtype", ['c', 'S1', 'U1'])
|
| 288 |
+
def test_array_input(self, dtype):
|
| 289 |
+
f = getattr(self.module, self.fprefix + '_array_input')
|
| 290 |
+
|
| 291 |
+
assert_array_equal(f(np.array(['a', 'b', 'c'], dtype=dtype)),
|
| 292 |
+
np.array(list(map(ord, 'abc')), dtype='i1'))
|
| 293 |
+
assert_array_equal(f(np.array([b'a', b'b', b'c'], dtype=dtype)),
|
| 294 |
+
np.array(list(map(ord, 'abc')), dtype='i1'))
|
| 295 |
+
|
| 296 |
+
def test_array_input_varia(self):
|
| 297 |
+
f = getattr(self.module, self.fprefix + '_array_input')
|
| 298 |
+
assert_array_equal(f(['a', 'b', 'c']),
|
| 299 |
+
np.array(list(map(ord, 'abc')), dtype='i1'))
|
| 300 |
+
assert_array_equal(f([b'a', b'b', b'c']),
|
| 301 |
+
np.array(list(map(ord, 'abc')), dtype='i1'))
|
| 302 |
+
|
| 303 |
+
try:
|
| 304 |
+
f(['a', 'b', 'c', 'd'])
|
| 305 |
+
except ValueError as msg:
|
| 306 |
+
if not str(msg).endswith(
|
| 307 |
+
'th dimension must be fixed to 3 but got 4'):
|
| 308 |
+
raise
|
| 309 |
+
else:
|
| 310 |
+
raise SystemError(
|
| 311 |
+
f'{f.__name__} should have failed on wrong input')
|
| 312 |
+
|
| 313 |
+
@pytest.mark.parametrize("dtype", ['c', 'S1', 'U1'])
|
| 314 |
+
def test_2d_array_input(self, dtype):
|
| 315 |
+
f = getattr(self.module, self.fprefix + '_2d_array_input')
|
| 316 |
+
|
| 317 |
+
a = np.array([['a', 'b', 'c'],
|
| 318 |
+
['d', 'e', 'f']], dtype=dtype, order='F')
|
| 319 |
+
expected = a.view(np.uint32 if dtype == 'U1' else np.uint8)
|
| 320 |
+
assert_array_equal(f(a), expected)
|
| 321 |
+
|
| 322 |
+
def test_output(self):
|
| 323 |
+
f = getattr(self.module, self.fprefix + '_output')
|
| 324 |
+
|
| 325 |
+
assert_equal(f(ord(b'a')), b'a')
|
| 326 |
+
assert_equal(f(0), b'\0')
|
| 327 |
+
|
| 328 |
+
def test_array_output(self):
|
| 329 |
+
f = getattr(self.module, self.fprefix + '_array_output')
|
| 330 |
+
|
| 331 |
+
assert_array_equal(f(list(map(ord, 'abc'))),
|
| 332 |
+
np.array(list('abc'), dtype='S1'))
|
| 333 |
+
|
| 334 |
+
def test_input_output(self):
|
| 335 |
+
f = getattr(self.module, self.fprefix + '_input_output')
|
| 336 |
+
|
| 337 |
+
assert_equal(f(b'a'), b'a')
|
| 338 |
+
assert_equal(f('a'), b'a')
|
| 339 |
+
assert_equal(f(''), b'\0')
|
| 340 |
+
|
| 341 |
+
@pytest.mark.parametrize("dtype", ['c', 'S1'])
|
| 342 |
+
def test_inout(self, dtype):
|
| 343 |
+
f = getattr(self.module, self.fprefix + '_inout')
|
| 344 |
+
|
| 345 |
+
a = np.array(list('abc'), dtype=dtype)
|
| 346 |
+
f(a, 'A')
|
| 347 |
+
assert_array_equal(a, np.array(list('Abc'), dtype=a.dtype))
|
| 348 |
+
f(a[1:], 'B')
|
| 349 |
+
assert_array_equal(a, np.array(list('ABc'), dtype=a.dtype))
|
| 350 |
+
|
| 351 |
+
a = np.array(['abc'], dtype=dtype)
|
| 352 |
+
f(a, 'A')
|
| 353 |
+
assert_array_equal(a, np.array(['Abc'], dtype=a.dtype))
|
| 354 |
+
|
| 355 |
+
def test_inout_varia(self):
|
| 356 |
+
f = getattr(self.module, self.fprefix + '_inout')
|
| 357 |
+
a = np.array('abc', dtype='S3')
|
| 358 |
+
f(a, 'A')
|
| 359 |
+
assert_array_equal(a, np.array('Abc', dtype=a.dtype))
|
| 360 |
+
|
| 361 |
+
a = np.array(['abc'], dtype='S3')
|
| 362 |
+
f(a, 'A')
|
| 363 |
+
assert_array_equal(a, np.array(['Abc'], dtype=a.dtype))
|
| 364 |
+
|
| 365 |
+
try:
|
| 366 |
+
f('abc', 'A')
|
| 367 |
+
except ValueError as msg:
|
| 368 |
+
if not str(msg).endswith(' got 3-str'):
|
| 369 |
+
raise
|
| 370 |
+
else:
|
| 371 |
+
raise SystemError(f'{f.__name__} should have failed on str value')
|
| 372 |
+
|
| 373 |
+
@pytest.mark.parametrize("dtype", ['c', 'S1'])
|
| 374 |
+
def test_array_inout(self, dtype):
|
| 375 |
+
f = getattr(self.module, self.fprefix + '_array_inout')
|
| 376 |
+
n = np.array(['A', 'B', 'C'], dtype=dtype, order='F')
|
| 377 |
+
|
| 378 |
+
a = np.array(['a', 'b', 'c'], dtype=dtype, order='F')
|
| 379 |
+
f(a, n)
|
| 380 |
+
assert_array_equal(a, n)
|
| 381 |
+
|
| 382 |
+
a = np.array(['a', 'b', 'c', 'd'], dtype=dtype)
|
| 383 |
+
f(a[1:], n)
|
| 384 |
+
assert_array_equal(a, np.array(['a', 'A', 'B', 'C'], dtype=dtype))
|
| 385 |
+
|
| 386 |
+
a = np.array([['a', 'b', 'c']], dtype=dtype, order='F')
|
| 387 |
+
f(a, n)
|
| 388 |
+
assert_array_equal(a, np.array([['A', 'B', 'C']], dtype=dtype))
|
| 389 |
+
|
| 390 |
+
a = np.array(['a', 'b', 'c', 'd'], dtype=dtype, order='F')
|
| 391 |
+
try:
|
| 392 |
+
f(a, n)
|
| 393 |
+
except ValueError as msg:
|
| 394 |
+
if not str(msg).endswith(
|
| 395 |
+
'th dimension must be fixed to 3 but got 4'):
|
| 396 |
+
raise
|
| 397 |
+
else:
|
| 398 |
+
raise SystemError(
|
| 399 |
+
f'{f.__name__} should have failed on wrong input')
|
| 400 |
+
|
| 401 |
+
@pytest.mark.parametrize("dtype", ['c', 'S1'])
|
| 402 |
+
def test_2d_array_inout(self, dtype):
|
| 403 |
+
f = getattr(self.module, self.fprefix + '_2d_array_inout')
|
| 404 |
+
n = np.array([['A', 'B', 'C'],
|
| 405 |
+
['D', 'E', 'F']],
|
| 406 |
+
dtype=dtype, order='F')
|
| 407 |
+
a = np.array([['a', 'b', 'c'],
|
| 408 |
+
['d', 'e', 'f']],
|
| 409 |
+
dtype=dtype, order='F')
|
| 410 |
+
f(a, n)
|
| 411 |
+
assert_array_equal(a, n)
|
| 412 |
+
|
| 413 |
+
def test_return(self):
|
| 414 |
+
f = getattr(self.module, self.fprefix + '_return')
|
| 415 |
+
|
| 416 |
+
assert_equal(f('a'), b'a')
|
| 417 |
+
|
| 418 |
+
@pytest.mark.skip('fortran function returning array segfaults')
|
| 419 |
+
def test_array_return(self):
|
| 420 |
+
f = getattr(self.module, self.fprefix + '_array_return')
|
| 421 |
+
|
| 422 |
+
a = np.array(list('abc'), dtype='S1')
|
| 423 |
+
assert_array_equal(f(a), a)
|
| 424 |
+
|
| 425 |
+
def test_optional(self):
|
| 426 |
+
f = getattr(self.module, self.fprefix + '_optional')
|
| 427 |
+
|
| 428 |
+
assert_equal(f(), b"a")
|
| 429 |
+
assert_equal(f(b'B'), b"B")
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
class TestMiscCharacter(util.F2PyTest):
|
| 433 |
+
# options = ['--debug-capi', '--build-dir', '/tmp/test-build-f2py']
|
| 434 |
+
suffix = '.f90'
|
| 435 |
+
fprefix = 'test_misc_character'
|
| 436 |
+
|
| 437 |
+
code = textwrap.dedent(f"""
|
| 438 |
+
subroutine {fprefix}_gh18684(x, y, m)
|
| 439 |
+
character(len=5), dimension(m), intent(in) :: x
|
| 440 |
+
character*5, dimension(m), intent(out) :: y
|
| 441 |
+
integer i, m
|
| 442 |
+
!f2py integer, intent(hide), depend(x) :: m = f2py_len(x)
|
| 443 |
+
do i=1,m
|
| 444 |
+
y(i) = x(i)
|
| 445 |
+
end do
|
| 446 |
+
end subroutine {fprefix}_gh18684
|
| 447 |
+
|
| 448 |
+
subroutine {fprefix}_gh6308(x, i)
|
| 449 |
+
integer i
|
| 450 |
+
!f2py check(i>=0 && i<12) i
|
| 451 |
+
character*5 name, x
|
| 452 |
+
common name(12)
|
| 453 |
+
name(i + 1) = x
|
| 454 |
+
end subroutine {fprefix}_gh6308
|
| 455 |
+
|
| 456 |
+
subroutine {fprefix}_gh4519(x)
|
| 457 |
+
character(len=*), intent(in) :: x(:)
|
| 458 |
+
!f2py intent(out) x
|
| 459 |
+
integer :: i
|
| 460 |
+
! Uncomment for debug printing:
|
| 461 |
+
!do i=1, size(x)
|
| 462 |
+
! print*, "x(",i,")=", x(i)
|
| 463 |
+
!end do
|
| 464 |
+
end subroutine {fprefix}_gh4519
|
| 465 |
+
|
| 466 |
+
pure function {fprefix}_gh3425(x) result (y)
|
| 467 |
+
character(len=*), intent(in) :: x
|
| 468 |
+
character(len=len(x)) :: y
|
| 469 |
+
integer :: i
|
| 470 |
+
do i = 1, len(x)
|
| 471 |
+
j = iachar(x(i:i))
|
| 472 |
+
if (j>=iachar("a") .and. j<=iachar("z") ) then
|
| 473 |
+
y(i:i) = achar(j-32)
|
| 474 |
+
else
|
| 475 |
+
y(i:i) = x(i:i)
|
| 476 |
+
endif
|
| 477 |
+
end do
|
| 478 |
+
end function {fprefix}_gh3425
|
| 479 |
+
|
| 480 |
+
subroutine {fprefix}_character_bc_new(x, y, z)
|
| 481 |
+
character, intent(in) :: x
|
| 482 |
+
character, intent(out) :: y
|
| 483 |
+
!f2py character, depend(x) :: y = x
|
| 484 |
+
!f2py character, dimension((x=='a'?1:2)), depend(x), intent(out) :: z
|
| 485 |
+
character, dimension(*) :: z
|
| 486 |
+
!f2py character, optional, check(x == 'a' || x == 'b') :: x = 'a'
|
| 487 |
+
!f2py callstatement (*f2py_func)(&x, &y, z)
|
| 488 |
+
!f2py callprotoargument character*, character*, character*
|
| 489 |
+
if (y.eq.x) then
|
| 490 |
+
y = x
|
| 491 |
+
else
|
| 492 |
+
y = 'e'
|
| 493 |
+
endif
|
| 494 |
+
z(1) = 'c'
|
| 495 |
+
end subroutine {fprefix}_character_bc_new
|
| 496 |
+
|
| 497 |
+
subroutine {fprefix}_character_bc_old(x, y, z)
|
| 498 |
+
character, intent(in) :: x
|
| 499 |
+
character, intent(out) :: y
|
| 500 |
+
!f2py character, depend(x) :: y = x[0]
|
| 501 |
+
!f2py character, dimension((*x=='a'?1:2)), depend(x), intent(out) :: z
|
| 502 |
+
character, dimension(*) :: z
|
| 503 |
+
!f2py character, optional, check(*x == 'a' || x[0] == 'b') :: x = 'a'
|
| 504 |
+
!f2py callstatement (*f2py_func)(x, y, z)
|
| 505 |
+
!f2py callprotoargument char*, char*, char*
|
| 506 |
+
if (y.eq.x) then
|
| 507 |
+
y = x
|
| 508 |
+
else
|
| 509 |
+
y = 'e'
|
| 510 |
+
endif
|
| 511 |
+
z(1) = 'c'
|
| 512 |
+
end subroutine {fprefix}_character_bc_old
|
| 513 |
+
""")
|
| 514 |
+
|
| 515 |
+
def test_gh18684(self):
|
| 516 |
+
# Test character(len=5) and character*5 usages
|
| 517 |
+
f = getattr(self.module, self.fprefix + '_gh18684')
|
| 518 |
+
x = np.array(["abcde", "fghij"], dtype='S5')
|
| 519 |
+
y = f(x)
|
| 520 |
+
|
| 521 |
+
assert_array_equal(x, y)
|
| 522 |
+
|
| 523 |
+
def test_gh6308(self):
|
| 524 |
+
# Test character string array in a common block
|
| 525 |
+
f = getattr(self.module, self.fprefix + '_gh6308')
|
| 526 |
+
|
| 527 |
+
assert_equal(self.module._BLNK_.name.dtype, np.dtype('S5'))
|
| 528 |
+
assert_equal(len(self.module._BLNK_.name), 12)
|
| 529 |
+
f("abcde", 0)
|
| 530 |
+
assert_equal(self.module._BLNK_.name[0], b"abcde")
|
| 531 |
+
f("12345", 5)
|
| 532 |
+
assert_equal(self.module._BLNK_.name[5], b"12345")
|
| 533 |
+
|
| 534 |
+
def test_gh4519(self):
|
| 535 |
+
# Test array of assumed length strings
|
| 536 |
+
f = getattr(self.module, self.fprefix + '_gh4519')
|
| 537 |
+
|
| 538 |
+
for x, expected in [
|
| 539 |
+
('a', dict(shape=(), dtype=np.dtype('S1'))),
|
| 540 |
+
('text', dict(shape=(), dtype=np.dtype('S4'))),
|
| 541 |
+
(np.array(['1', '2', '3'], dtype='S1'),
|
| 542 |
+
dict(shape=(3,), dtype=np.dtype('S1'))),
|
| 543 |
+
(['1', '2', '34'],
|
| 544 |
+
dict(shape=(3,), dtype=np.dtype('S2'))),
|
| 545 |
+
(['', ''], dict(shape=(2,), dtype=np.dtype('S1')))]:
|
| 546 |
+
r = f(x)
|
| 547 |
+
for k, v in expected.items():
|
| 548 |
+
assert_equal(getattr(r, k), v)
|
| 549 |
+
|
| 550 |
+
def test_gh3425(self):
|
| 551 |
+
# Test returning a copy of assumed length string
|
| 552 |
+
f = getattr(self.module, self.fprefix + '_gh3425')
|
| 553 |
+
# f is equivalent to bytes.upper
|
| 554 |
+
|
| 555 |
+
assert_equal(f('abC'), b'ABC')
|
| 556 |
+
assert_equal(f(''), b'')
|
| 557 |
+
assert_equal(f('abC12d'), b'ABC12D')
|
| 558 |
+
|
| 559 |
+
@pytest.mark.parametrize("state", ['new', 'old'])
|
| 560 |
+
def test_character_bc(self, state):
|
| 561 |
+
f = getattr(self.module, self.fprefix + '_character_bc_' + state)
|
| 562 |
+
|
| 563 |
+
c, a = f()
|
| 564 |
+
assert_equal(c, b'a')
|
| 565 |
+
assert_equal(len(a), 1)
|
| 566 |
+
|
| 567 |
+
c, a = f(b'b')
|
| 568 |
+
assert_equal(c, b'b')
|
| 569 |
+
assert_equal(len(a), 2)
|
| 570 |
+
|
| 571 |
+
assert_raises(Exception, lambda: f(b'c'))
|
| 572 |
+
|
| 573 |
+
|
| 574 |
+
class TestStringScalarArr(util.F2PyTest):
|
| 575 |
+
sources = [util.getpath("tests", "src", "string", "scalar_string.f90")]
|
| 576 |
+
|
| 577 |
+
def test_char(self):
|
| 578 |
+
for out in (self.module.string_test.string,
|
| 579 |
+
self.module.string_test.string77):
|
| 580 |
+
expected = ()
|
| 581 |
+
assert out.shape == expected
|
| 582 |
+
expected = '|S8'
|
| 583 |
+
assert out.dtype == expected
|
| 584 |
+
|
| 585 |
+
def test_char_arr(self):
|
| 586 |
+
for out in (self.module.string_test.strarr,
|
| 587 |
+
self.module.string_test.strarr77):
|
| 588 |
+
expected = (5,7)
|
| 589 |
+
assert out.shape == expected
|
| 590 |
+
expected = '|S12'
|
| 591 |
+
assert out.dtype == expected
|
| 592 |
+
|
| 593 |
+
class TestStringAssumedLength(util.F2PyTest):
|
| 594 |
+
sources = [util.getpath("tests", "src", "string", "gh24008.f")]
|
| 595 |
+
|
| 596 |
+
def test_gh24008(self):
|
| 597 |
+
self.module.greet("joe", "bob")
|
| 598 |
+
|
| 599 |
+
class TestStringOptionalInOut(util.F2PyTest):
|
| 600 |
+
sources = [util.getpath("tests", "src", "string", "gh24662.f90")]
|
| 601 |
+
|
| 602 |
+
def test_gh24662(self):
|
| 603 |
+
self.module.string_inout_optional()
|
| 604 |
+
a = np.array('hi', dtype='S32')
|
| 605 |
+
self.module.string_inout_optional(a)
|
| 606 |
+
assert "output string" in a.tobytes().decode()
|
| 607 |
+
with pytest.raises(Exception):
|
| 608 |
+
aa = "Hi"
|
| 609 |
+
self.module.string_inout_optional(aa)
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
@pytest.mark.slow
|
| 613 |
+
class TestNewCharHandling(util.F2PyTest):
|
| 614 |
+
# from v1.24 onwards, gh-19388
|
| 615 |
+
sources = [
|
| 616 |
+
util.getpath("tests", "src", "string", "gh25286.pyf"),
|
| 617 |
+
util.getpath("tests", "src", "string", "gh25286.f90")
|
| 618 |
+
]
|
| 619 |
+
module_name = "_char_handling_test"
|
| 620 |
+
|
| 621 |
+
def test_gh25286(self):
|
| 622 |
+
info = self.module.charint('T')
|
| 623 |
+
assert info == 2
|
| 624 |
+
|
| 625 |
+
@pytest.mark.slow
|
| 626 |
+
class TestBCCharHandling(util.F2PyTest):
|
| 627 |
+
# SciPy style, "incorrect" bindings with a hook
|
| 628 |
+
sources = [
|
| 629 |
+
util.getpath("tests", "src", "string", "gh25286_bc.pyf"),
|
| 630 |
+
util.getpath("tests", "src", "string", "gh25286.f90")
|
| 631 |
+
]
|
| 632 |
+
module_name = "_char_handling_test"
|
| 633 |
+
|
| 634 |
+
def test_gh25286(self):
|
| 635 |
+
info = self.module.charint('T')
|
| 636 |
+
assert info == 2
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_common.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
from . import util
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestCommonBlock(util.F2PyTest):
|
| 10 |
+
sources = [util.getpath("tests", "src", "common", "block.f")]
|
| 11 |
+
|
| 12 |
+
@pytest.mark.skipif(sys.platform == "win32",
|
| 13 |
+
reason="Fails with MinGW64 Gfortran (Issue #9673)")
|
| 14 |
+
def test_common_block(self):
|
| 15 |
+
self.module.initcb()
|
| 16 |
+
assert self.module.block.long_bn == np.array(1.0, dtype=np.float64)
|
| 17 |
+
assert self.module.block.string_bn == np.array("2", dtype="|S1")
|
| 18 |
+
assert self.module.block.ok == np.array(3, dtype=np.int32)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class TestCommonWithUse(util.F2PyTest):
|
| 22 |
+
sources = [util.getpath("tests", "src", "common", "gh19161.f90")]
|
| 23 |
+
|
| 24 |
+
@pytest.mark.skipif(sys.platform == "win32",
|
| 25 |
+
reason="Fails with MinGW64 Gfortran (Issue #9673)")
|
| 26 |
+
def test_common_gh19161(self):
|
| 27 |
+
assert self.module.data.x == 0
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_crackfortran.py
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
import codecs
|
| 3 |
+
import time
|
| 4 |
+
import unicodedata
|
| 5 |
+
import pytest
|
| 6 |
+
import numpy as np
|
| 7 |
+
from numpy.f2py.crackfortran import markinnerspaces, nameargspattern
|
| 8 |
+
from . import util
|
| 9 |
+
from numpy.f2py import crackfortran
|
| 10 |
+
import textwrap
|
| 11 |
+
import contextlib
|
| 12 |
+
import io
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class TestNoSpace(util.F2PyTest):
|
| 16 |
+
# issue gh-15035: add handling for endsubroutine, endfunction with no space
|
| 17 |
+
# between "end" and the block name
|
| 18 |
+
sources = [util.getpath("tests", "src", "crackfortran", "gh15035.f")]
|
| 19 |
+
|
| 20 |
+
def test_module(self):
|
| 21 |
+
k = np.array([1, 2, 3], dtype=np.float64)
|
| 22 |
+
w = np.array([1, 2, 3], dtype=np.float64)
|
| 23 |
+
self.module.subb(k)
|
| 24 |
+
assert np.allclose(k, w + 1)
|
| 25 |
+
self.module.subc([w, k])
|
| 26 |
+
assert np.allclose(k, w + 1)
|
| 27 |
+
assert self.module.t0("23") == b"2"
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class TestPublicPrivate:
|
| 31 |
+
def test_defaultPrivate(self):
|
| 32 |
+
fpath = util.getpath("tests", "src", "crackfortran", "privatemod.f90")
|
| 33 |
+
mod = crackfortran.crackfortran([str(fpath)])
|
| 34 |
+
assert len(mod) == 1
|
| 35 |
+
mod = mod[0]
|
| 36 |
+
assert "private" in mod["vars"]["a"]["attrspec"]
|
| 37 |
+
assert "public" not in mod["vars"]["a"]["attrspec"]
|
| 38 |
+
assert "private" in mod["vars"]["b"]["attrspec"]
|
| 39 |
+
assert "public" not in mod["vars"]["b"]["attrspec"]
|
| 40 |
+
assert "private" not in mod["vars"]["seta"]["attrspec"]
|
| 41 |
+
assert "public" in mod["vars"]["seta"]["attrspec"]
|
| 42 |
+
|
| 43 |
+
def test_defaultPublic(self, tmp_path):
|
| 44 |
+
fpath = util.getpath("tests", "src", "crackfortran", "publicmod.f90")
|
| 45 |
+
mod = crackfortran.crackfortran([str(fpath)])
|
| 46 |
+
assert len(mod) == 1
|
| 47 |
+
mod = mod[0]
|
| 48 |
+
assert "private" in mod["vars"]["a"]["attrspec"]
|
| 49 |
+
assert "public" not in mod["vars"]["a"]["attrspec"]
|
| 50 |
+
assert "private" not in mod["vars"]["seta"]["attrspec"]
|
| 51 |
+
assert "public" in mod["vars"]["seta"]["attrspec"]
|
| 52 |
+
|
| 53 |
+
def test_access_type(self, tmp_path):
|
| 54 |
+
fpath = util.getpath("tests", "src", "crackfortran", "accesstype.f90")
|
| 55 |
+
mod = crackfortran.crackfortran([str(fpath)])
|
| 56 |
+
assert len(mod) == 1
|
| 57 |
+
tt = mod[0]['vars']
|
| 58 |
+
assert set(tt['a']['attrspec']) == {'private', 'bind(c)'}
|
| 59 |
+
assert set(tt['b_']['attrspec']) == {'public', 'bind(c)'}
|
| 60 |
+
assert set(tt['c']['attrspec']) == {'public'}
|
| 61 |
+
|
| 62 |
+
def test_nowrap_private_proceedures(self, tmp_path):
|
| 63 |
+
fpath = util.getpath("tests", "src", "crackfortran", "gh23879.f90")
|
| 64 |
+
mod = crackfortran.crackfortran([str(fpath)])
|
| 65 |
+
assert len(mod) == 1
|
| 66 |
+
pyf = crackfortran.crack2fortran(mod)
|
| 67 |
+
assert 'bar' not in pyf
|
| 68 |
+
|
| 69 |
+
class TestModuleProcedure():
|
| 70 |
+
def test_moduleOperators(self, tmp_path):
|
| 71 |
+
fpath = util.getpath("tests", "src", "crackfortran", "operators.f90")
|
| 72 |
+
mod = crackfortran.crackfortran([str(fpath)])
|
| 73 |
+
assert len(mod) == 1
|
| 74 |
+
mod = mod[0]
|
| 75 |
+
assert "body" in mod and len(mod["body"]) == 9
|
| 76 |
+
assert mod["body"][1]["name"] == "operator(.item.)"
|
| 77 |
+
assert "implementedby" in mod["body"][1]
|
| 78 |
+
assert mod["body"][1]["implementedby"] == \
|
| 79 |
+
["item_int", "item_real"]
|
| 80 |
+
assert mod["body"][2]["name"] == "operator(==)"
|
| 81 |
+
assert "implementedby" in mod["body"][2]
|
| 82 |
+
assert mod["body"][2]["implementedby"] == ["items_are_equal"]
|
| 83 |
+
assert mod["body"][3]["name"] == "assignment(=)"
|
| 84 |
+
assert "implementedby" in mod["body"][3]
|
| 85 |
+
assert mod["body"][3]["implementedby"] == \
|
| 86 |
+
["get_int", "get_real"]
|
| 87 |
+
|
| 88 |
+
def test_notPublicPrivate(self, tmp_path):
|
| 89 |
+
fpath = util.getpath("tests", "src", "crackfortran", "pubprivmod.f90")
|
| 90 |
+
mod = crackfortran.crackfortran([str(fpath)])
|
| 91 |
+
assert len(mod) == 1
|
| 92 |
+
mod = mod[0]
|
| 93 |
+
assert mod['vars']['a']['attrspec'] == ['private', ]
|
| 94 |
+
assert mod['vars']['b']['attrspec'] == ['public', ]
|
| 95 |
+
assert mod['vars']['seta']['attrspec'] == ['public', ]
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class TestExternal(util.F2PyTest):
|
| 99 |
+
# issue gh-17859: add external attribute support
|
| 100 |
+
sources = [util.getpath("tests", "src", "crackfortran", "gh17859.f")]
|
| 101 |
+
|
| 102 |
+
def test_external_as_statement(self):
|
| 103 |
+
def incr(x):
|
| 104 |
+
return x + 123
|
| 105 |
+
|
| 106 |
+
r = self.module.external_as_statement(incr)
|
| 107 |
+
assert r == 123
|
| 108 |
+
|
| 109 |
+
def test_external_as_attribute(self):
|
| 110 |
+
def incr(x):
|
| 111 |
+
return x + 123
|
| 112 |
+
|
| 113 |
+
r = self.module.external_as_attribute(incr)
|
| 114 |
+
assert r == 123
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class TestCrackFortran(util.F2PyTest):
|
| 118 |
+
# gh-2848: commented lines between parameters in subroutine parameter lists
|
| 119 |
+
sources = [util.getpath("tests", "src", "crackfortran", "gh2848.f90")]
|
| 120 |
+
|
| 121 |
+
def test_gh2848(self):
|
| 122 |
+
r = self.module.gh2848(1, 2)
|
| 123 |
+
assert r == (1, 2)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
class TestMarkinnerspaces:
|
| 127 |
+
# gh-14118: markinnerspaces does not handle multiple quotations
|
| 128 |
+
|
| 129 |
+
def test_do_not_touch_normal_spaces(self):
|
| 130 |
+
test_list = ["a ", " a", "a b c", "'abcdefghij'"]
|
| 131 |
+
for i in test_list:
|
| 132 |
+
assert markinnerspaces(i) == i
|
| 133 |
+
|
| 134 |
+
def test_one_relevant_space(self):
|
| 135 |
+
assert markinnerspaces("a 'b c' \\' \\'") == "a 'b@_@c' \\' \\'"
|
| 136 |
+
assert markinnerspaces(r'a "b c" \" \"') == r'a "b@_@c" \" \"'
|
| 137 |
+
|
| 138 |
+
def test_ignore_inner_quotes(self):
|
| 139 |
+
assert markinnerspaces("a 'b c\" \" d' e") == "a 'b@_@c\"@_@\"@_@d' e"
|
| 140 |
+
assert markinnerspaces("a \"b c' ' d\" e") == "a \"b@_@c'@_@'@_@d\" e"
|
| 141 |
+
|
| 142 |
+
def test_multiple_relevant_spaces(self):
|
| 143 |
+
assert markinnerspaces("a 'b c' 'd e'") == "a 'b@_@c' 'd@_@e'"
|
| 144 |
+
assert markinnerspaces(r'a "b c" "d e"') == r'a "b@_@c" "d@_@e"'
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
class TestDimSpec(util.F2PyTest):
|
| 148 |
+
"""This test suite tests various expressions that are used as dimension
|
| 149 |
+
specifications.
|
| 150 |
+
|
| 151 |
+
There exists two usage cases where analyzing dimensions
|
| 152 |
+
specifications are important.
|
| 153 |
+
|
| 154 |
+
In the first case, the size of output arrays must be defined based
|
| 155 |
+
on the inputs to a Fortran function. Because Fortran supports
|
| 156 |
+
arbitrary bases for indexing, for instance, `arr(lower:upper)`,
|
| 157 |
+
f2py has to evaluate an expression `upper - lower + 1` where
|
| 158 |
+
`lower` and `upper` are arbitrary expressions of input parameters.
|
| 159 |
+
The evaluation is performed in C, so f2py has to translate Fortran
|
| 160 |
+
expressions to valid C expressions (an alternative approach is
|
| 161 |
+
that a developer specifies the corresponding C expressions in a
|
| 162 |
+
.pyf file).
|
| 163 |
+
|
| 164 |
+
In the second case, when user provides an input array with a given
|
| 165 |
+
size but some hidden parameters used in dimensions specifications
|
| 166 |
+
need to be determined based on the input array size. This is a
|
| 167 |
+
harder problem because f2py has to solve the inverse problem: find
|
| 168 |
+
a parameter `p` such that `upper(p) - lower(p) + 1` equals to the
|
| 169 |
+
size of input array. In the case when this equation cannot be
|
| 170 |
+
solved (e.g. because the input array size is wrong), raise an
|
| 171 |
+
error before calling the Fortran function (that otherwise would
|
| 172 |
+
likely crash Python process when the size of input arrays is
|
| 173 |
+
wrong). f2py currently supports this case only when the equation
|
| 174 |
+
is linear with respect to unknown parameter.
|
| 175 |
+
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
suffix = ".f90"
|
| 179 |
+
|
| 180 |
+
code_template = textwrap.dedent("""
|
| 181 |
+
function get_arr_size_{count}(a, n) result (length)
|
| 182 |
+
integer, intent(in) :: n
|
| 183 |
+
integer, dimension({dimspec}), intent(out) :: a
|
| 184 |
+
integer length
|
| 185 |
+
length = size(a)
|
| 186 |
+
end function
|
| 187 |
+
|
| 188 |
+
subroutine get_inv_arr_size_{count}(a, n)
|
| 189 |
+
integer :: n
|
| 190 |
+
! the value of n is computed in f2py wrapper
|
| 191 |
+
!f2py intent(out) n
|
| 192 |
+
integer, dimension({dimspec}), intent(in) :: a
|
| 193 |
+
end subroutine
|
| 194 |
+
""")
|
| 195 |
+
|
| 196 |
+
linear_dimspecs = [
|
| 197 |
+
"n", "2*n", "2:n", "n/2", "5 - n/2", "3*n:20", "n*(n+1):n*(n+5)",
|
| 198 |
+
"2*n, n"
|
| 199 |
+
]
|
| 200 |
+
nonlinear_dimspecs = ["2*n:3*n*n+2*n"]
|
| 201 |
+
all_dimspecs = linear_dimspecs + nonlinear_dimspecs
|
| 202 |
+
|
| 203 |
+
code = ""
|
| 204 |
+
for count, dimspec in enumerate(all_dimspecs):
|
| 205 |
+
lst = [(d.split(":")[0] if ":" in d else "1") for d in dimspec.split(',')]
|
| 206 |
+
code += code_template.format(
|
| 207 |
+
count=count,
|
| 208 |
+
dimspec=dimspec,
|
| 209 |
+
first=", ".join(lst),
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
@pytest.mark.parametrize("dimspec", all_dimspecs)
|
| 213 |
+
def test_array_size(self, dimspec):
|
| 214 |
+
|
| 215 |
+
count = self.all_dimspecs.index(dimspec)
|
| 216 |
+
get_arr_size = getattr(self.module, f"get_arr_size_{count}")
|
| 217 |
+
|
| 218 |
+
for n in [1, 2, 3, 4, 5]:
|
| 219 |
+
sz, a = get_arr_size(n)
|
| 220 |
+
assert a.size == sz
|
| 221 |
+
|
| 222 |
+
@pytest.mark.parametrize("dimspec", all_dimspecs)
|
| 223 |
+
def test_inv_array_size(self, dimspec):
|
| 224 |
+
|
| 225 |
+
count = self.all_dimspecs.index(dimspec)
|
| 226 |
+
get_arr_size = getattr(self.module, f"get_arr_size_{count}")
|
| 227 |
+
get_inv_arr_size = getattr(self.module, f"get_inv_arr_size_{count}")
|
| 228 |
+
|
| 229 |
+
for n in [1, 2, 3, 4, 5]:
|
| 230 |
+
sz, a = get_arr_size(n)
|
| 231 |
+
if dimspec in self.nonlinear_dimspecs:
|
| 232 |
+
# one must specify n as input, the call we'll ensure
|
| 233 |
+
# that a and n are compatible:
|
| 234 |
+
n1 = get_inv_arr_size(a, n)
|
| 235 |
+
else:
|
| 236 |
+
# in case of linear dependence, n can be determined
|
| 237 |
+
# from the shape of a:
|
| 238 |
+
n1 = get_inv_arr_size(a)
|
| 239 |
+
# n1 may be different from n (for instance, when `a` size
|
| 240 |
+
# is a function of some `n` fraction) but it must produce
|
| 241 |
+
# the same sized array
|
| 242 |
+
sz1, _ = get_arr_size(n1)
|
| 243 |
+
assert sz == sz1, (n, n1, sz, sz1)
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
class TestModuleDeclaration:
|
| 247 |
+
def test_dependencies(self, tmp_path):
|
| 248 |
+
fpath = util.getpath("tests", "src", "crackfortran", "foo_deps.f90")
|
| 249 |
+
mod = crackfortran.crackfortran([str(fpath)])
|
| 250 |
+
assert len(mod) == 1
|
| 251 |
+
assert mod[0]["vars"]["abar"]["="] == "bar('abar')"
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
class TestEval(util.F2PyTest):
|
| 255 |
+
def test_eval_scalar(self):
|
| 256 |
+
eval_scalar = crackfortran._eval_scalar
|
| 257 |
+
|
| 258 |
+
assert eval_scalar('123', {}) == '123'
|
| 259 |
+
assert eval_scalar('12 + 3', {}) == '15'
|
| 260 |
+
assert eval_scalar('a + b', dict(a=1, b=2)) == '3'
|
| 261 |
+
assert eval_scalar('"123"', {}) == "'123'"
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
class TestFortranReader(util.F2PyTest):
|
| 265 |
+
@pytest.mark.parametrize("encoding",
|
| 266 |
+
['ascii', 'utf-8', 'utf-16', 'utf-32'])
|
| 267 |
+
def test_input_encoding(self, tmp_path, encoding):
|
| 268 |
+
# gh-635
|
| 269 |
+
f_path = tmp_path / f"input_with_{encoding}_encoding.f90"
|
| 270 |
+
with f_path.open('w', encoding=encoding) as ff:
|
| 271 |
+
ff.write("""
|
| 272 |
+
subroutine foo()
|
| 273 |
+
end subroutine foo
|
| 274 |
+
""")
|
| 275 |
+
mod = crackfortran.crackfortran([str(f_path)])
|
| 276 |
+
assert mod[0]['name'] == 'foo'
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
class TestUnicodeComment(util.F2PyTest):
|
| 280 |
+
sources = [util.getpath("tests", "src", "crackfortran", "unicode_comment.f90")]
|
| 281 |
+
|
| 282 |
+
@pytest.mark.skipif(
|
| 283 |
+
(importlib.util.find_spec("charset_normalizer") is None),
|
| 284 |
+
reason="test requires charset_normalizer which is not installed",
|
| 285 |
+
)
|
| 286 |
+
def test_encoding_comment(self):
|
| 287 |
+
self.module.foo(3)
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
class TestNameArgsPatternBacktracking:
|
| 291 |
+
@pytest.mark.parametrize(
|
| 292 |
+
['adversary'],
|
| 293 |
+
[
|
| 294 |
+
('@)@bind@(@',),
|
| 295 |
+
('@)@bind @(@',),
|
| 296 |
+
('@)@bind foo bar baz@(@',)
|
| 297 |
+
]
|
| 298 |
+
)
|
| 299 |
+
def test_nameargspattern_backtracking(self, adversary):
|
| 300 |
+
'''address ReDOS vulnerability:
|
| 301 |
+
https://github.com/numpy/numpy/issues/23338'''
|
| 302 |
+
trials_per_batch = 12
|
| 303 |
+
batches_per_regex = 4
|
| 304 |
+
start_reps, end_reps = 15, 25
|
| 305 |
+
for ii in range(start_reps, end_reps):
|
| 306 |
+
repeated_adversary = adversary * ii
|
| 307 |
+
# test times in small batches.
|
| 308 |
+
# this gives us more chances to catch a bad regex
|
| 309 |
+
# while still catching it before too long if it is bad
|
| 310 |
+
for _ in range(batches_per_regex):
|
| 311 |
+
times = []
|
| 312 |
+
for _ in range(trials_per_batch):
|
| 313 |
+
t0 = time.perf_counter()
|
| 314 |
+
mtch = nameargspattern.search(repeated_adversary)
|
| 315 |
+
times.append(time.perf_counter() - t0)
|
| 316 |
+
# our pattern should be much faster than 0.2s per search
|
| 317 |
+
# it's unlikely that a bad regex will pass even on fast CPUs
|
| 318 |
+
assert np.median(times) < 0.2
|
| 319 |
+
assert not mtch
|
| 320 |
+
# if the adversary is capped with @)@, it becomes acceptable
|
| 321 |
+
# according to the old version of the regex.
|
| 322 |
+
# that should still be true.
|
| 323 |
+
good_version_of_adversary = repeated_adversary + '@)@'
|
| 324 |
+
assert nameargspattern.search(good_version_of_adversary)
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
class TestFunctionReturn(util.F2PyTest):
|
| 328 |
+
sources = [util.getpath("tests", "src", "crackfortran", "gh23598.f90")]
|
| 329 |
+
|
| 330 |
+
def test_function_rettype(self):
|
| 331 |
+
# gh-23598
|
| 332 |
+
assert self.module.intproduct(3, 4) == 12
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
class TestFortranGroupCounters(util.F2PyTest):
|
| 336 |
+
def test_end_if_comment(self):
|
| 337 |
+
# gh-23533
|
| 338 |
+
fpath = util.getpath("tests", "src", "crackfortran", "gh23533.f")
|
| 339 |
+
try:
|
| 340 |
+
crackfortran.crackfortran([str(fpath)])
|
| 341 |
+
except Exception as exc:
|
| 342 |
+
assert False, f"'crackfortran.crackfortran' raised an exception {exc}"
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
class TestF77CommonBlockReader():
|
| 346 |
+
def test_gh22648(self, tmp_path):
|
| 347 |
+
fpath = util.getpath("tests", "src", "crackfortran", "gh22648.pyf")
|
| 348 |
+
with contextlib.redirect_stdout(io.StringIO()) as stdout_f2py:
|
| 349 |
+
mod = crackfortran.crackfortran([str(fpath)])
|
| 350 |
+
assert "Mismatch" not in stdout_f2py.getvalue()
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_data.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pytest
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from . import util
|
| 6 |
+
from numpy.f2py.crackfortran import crackfortran
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestData(util.F2PyTest):
|
| 10 |
+
sources = [util.getpath("tests", "src", "crackfortran", "data_stmts.f90")]
|
| 11 |
+
|
| 12 |
+
# For gh-23276
|
| 13 |
+
def test_data_stmts(self):
|
| 14 |
+
assert self.module.cmplxdat.i == 2
|
| 15 |
+
assert self.module.cmplxdat.j == 3
|
| 16 |
+
assert self.module.cmplxdat.x == 1.5
|
| 17 |
+
assert self.module.cmplxdat.y == 2.0
|
| 18 |
+
assert self.module.cmplxdat.pi == 3.1415926535897932384626433832795028841971693993751058209749445923078164062
|
| 19 |
+
assert self.module.cmplxdat.medium_ref_index == np.array(1.+0.j)
|
| 20 |
+
assert np.all(self.module.cmplxdat.z == np.array([3.5, 7.0]))
|
| 21 |
+
assert np.all(self.module.cmplxdat.my_array == np.array([ 1.+2.j, -3.+4.j]))
|
| 22 |
+
assert np.all(self.module.cmplxdat.my_real_array == np.array([ 1., 2., 3.]))
|
| 23 |
+
assert np.all(self.module.cmplxdat.ref_index_one == np.array([13.0 + 21.0j]))
|
| 24 |
+
assert np.all(self.module.cmplxdat.ref_index_two == np.array([-30.0 + 43.0j]))
|
| 25 |
+
|
| 26 |
+
def test_crackedlines(self):
|
| 27 |
+
mod = crackfortran(self.sources)
|
| 28 |
+
assert mod[0]['vars']['x']['='] == '1.5'
|
| 29 |
+
assert mod[0]['vars']['y']['='] == '2.0'
|
| 30 |
+
assert mod[0]['vars']['pi']['='] == '3.1415926535897932384626433832795028841971693993751058209749445923078164062d0'
|
| 31 |
+
assert mod[0]['vars']['my_real_array']['='] == '(/1.0d0, 2.0d0, 3.0d0/)'
|
| 32 |
+
assert mod[0]['vars']['ref_index_one']['='] == '(13.0d0, 21.0d0)'
|
| 33 |
+
assert mod[0]['vars']['ref_index_two']['='] == '(-30.0d0, 43.0d0)'
|
| 34 |
+
assert mod[0]['vars']['my_array']['='] == '(/(1.0d0, 2.0d0), (-3.0d0, 4.0d0)/)'
|
| 35 |
+
assert mod[0]['vars']['z']['='] == '(/3.5, 7.0/)'
|
| 36 |
+
|
| 37 |
+
class TestDataF77(util.F2PyTest):
|
| 38 |
+
sources = [util.getpath("tests", "src", "crackfortran", "data_common.f")]
|
| 39 |
+
|
| 40 |
+
# For gh-23276
|
| 41 |
+
def test_data_stmts(self):
|
| 42 |
+
assert self.module.mycom.mydata == 0
|
| 43 |
+
|
| 44 |
+
def test_crackedlines(self):
|
| 45 |
+
mod = crackfortran(str(self.sources[0]))
|
| 46 |
+
print(mod[0]['vars'])
|
| 47 |
+
assert mod[0]['vars']['mydata']['='] == '0'
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class TestDataMultiplierF77(util.F2PyTest):
|
| 51 |
+
sources = [util.getpath("tests", "src", "crackfortran", "data_multiplier.f")]
|
| 52 |
+
|
| 53 |
+
# For gh-23276
|
| 54 |
+
def test_data_stmts(self):
|
| 55 |
+
assert self.module.mycom.ivar1 == 3
|
| 56 |
+
assert self.module.mycom.ivar2 == 3
|
| 57 |
+
assert self.module.mycom.ivar3 == 2
|
| 58 |
+
assert self.module.mycom.ivar4 == 2
|
| 59 |
+
assert self.module.mycom.evar5 == 0
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class TestDataWithCommentsF77(util.F2PyTest):
|
| 63 |
+
sources = [util.getpath("tests", "src", "crackfortran", "data_with_comments.f")]
|
| 64 |
+
|
| 65 |
+
# For gh-23276
|
| 66 |
+
def test_data_stmts(self):
|
| 67 |
+
assert len(self.module.mycom.mytab) == 3
|
| 68 |
+
assert self.module.mycom.mytab[0] == 0
|
| 69 |
+
assert self.module.mycom.mytab[1] == 4
|
| 70 |
+
assert self.module.mycom.mytab[2] == 0
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_docs.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pytest
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy.testing import assert_array_equal, assert_equal
|
| 5 |
+
from . import util
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def get_docdir():
|
| 9 |
+
# assuming that documentation tests are run from a source
|
| 10 |
+
# directory
|
| 11 |
+
return os.path.abspath(os.path.join(
|
| 12 |
+
os.path.dirname(__file__),
|
| 13 |
+
'..', '..', '..',
|
| 14 |
+
'doc', 'source', 'f2py', 'code'))
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
pytestmark = pytest.mark.skipif(
|
| 18 |
+
not os.path.isdir(get_docdir()),
|
| 19 |
+
reason=('Could not find f2py documentation sources'
|
| 20 |
+
f' ({get_docdir()} does not exists)'))
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _path(*a):
|
| 24 |
+
return os.path.join(*((get_docdir(),) + a))
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class TestDocAdvanced(util.F2PyTest):
|
| 28 |
+
# options = ['--debug-capi', '--build-dir', '/tmp/build-f2py']
|
| 29 |
+
sources = [_path('asterisk1.f90'), _path('asterisk2.f90'),
|
| 30 |
+
_path('ftype.f')]
|
| 31 |
+
|
| 32 |
+
def test_asterisk1(self):
|
| 33 |
+
foo = getattr(self.module, 'foo1')
|
| 34 |
+
assert_equal(foo(), b'123456789A12')
|
| 35 |
+
|
| 36 |
+
def test_asterisk2(self):
|
| 37 |
+
foo = getattr(self.module, 'foo2')
|
| 38 |
+
assert_equal(foo(2), b'12')
|
| 39 |
+
assert_equal(foo(12), b'123456789A12')
|
| 40 |
+
assert_equal(foo(24), b'123456789A123456789B')
|
| 41 |
+
|
| 42 |
+
def test_ftype(self):
|
| 43 |
+
ftype = self.module
|
| 44 |
+
ftype.foo()
|
| 45 |
+
assert_equal(ftype.data.a, 0)
|
| 46 |
+
ftype.data.a = 3
|
| 47 |
+
ftype.data.x = [1, 2, 3]
|
| 48 |
+
assert_equal(ftype.data.a, 3)
|
| 49 |
+
assert_array_equal(ftype.data.x,
|
| 50 |
+
np.array([1, 2, 3], dtype=np.float32))
|
| 51 |
+
ftype.data.x[1] = 45
|
| 52 |
+
assert_array_equal(ftype.data.x,
|
| 53 |
+
np.array([1, 45, 3], dtype=np.float32))
|
| 54 |
+
|
| 55 |
+
# TODO: implement test methods for other example Fortran codes
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_f2cmap.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from . import util
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
class TestF2Cmap(util.F2PyTest):
|
| 5 |
+
sources = [
|
| 6 |
+
util.getpath("tests", "src", "f2cmap", "isoFortranEnvMap.f90"),
|
| 7 |
+
util.getpath("tests", "src", "f2cmap", ".f2py_f2cmap")
|
| 8 |
+
]
|
| 9 |
+
|
| 10 |
+
# gh-15095
|
| 11 |
+
def test_long_long_map(self):
|
| 12 |
+
inp = np.ones(3)
|
| 13 |
+
out = self.module.func1(inp)
|
| 14 |
+
exp_out = 3
|
| 15 |
+
assert out == exp_out
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_kind.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pytest
|
| 3 |
+
import platform
|
| 4 |
+
|
| 5 |
+
from numpy.f2py.crackfortran import (
|
| 6 |
+
_selected_int_kind_func as selected_int_kind,
|
| 7 |
+
_selected_real_kind_func as selected_real_kind,
|
| 8 |
+
)
|
| 9 |
+
from . import util
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestKind(util.F2PyTest):
|
| 13 |
+
sources = [util.getpath("tests", "src", "kind", "foo.f90")]
|
| 14 |
+
|
| 15 |
+
def test_int(self):
|
| 16 |
+
"""Test `int` kind_func for integers up to 10**40."""
|
| 17 |
+
selectedintkind = self.module.selectedintkind
|
| 18 |
+
|
| 19 |
+
for i in range(40):
|
| 20 |
+
assert selectedintkind(i) == selected_int_kind(
|
| 21 |
+
i
|
| 22 |
+
), f"selectedintkind({i}): expected {selected_int_kind(i)!r} but got {selectedintkind(i)!r}"
|
| 23 |
+
|
| 24 |
+
def test_real(self):
|
| 25 |
+
"""
|
| 26 |
+
Test (processor-dependent) `real` kind_func for real numbers
|
| 27 |
+
of up to 31 digits precision (extended/quadruple).
|
| 28 |
+
"""
|
| 29 |
+
selectedrealkind = self.module.selectedrealkind
|
| 30 |
+
|
| 31 |
+
for i in range(32):
|
| 32 |
+
assert selectedrealkind(i) == selected_real_kind(
|
| 33 |
+
i
|
| 34 |
+
), f"selectedrealkind({i}): expected {selected_real_kind(i)!r} but got {selectedrealkind(i)!r}"
|
| 35 |
+
|
| 36 |
+
@pytest.mark.xfail(platform.machine().lower().startswith("ppc"),
|
| 37 |
+
reason="Some PowerPC may not support full IEEE 754 precision")
|
| 38 |
+
def test_quad_precision(self):
|
| 39 |
+
"""
|
| 40 |
+
Test kind_func for quadruple precision [`real(16)`] of 32+ digits .
|
| 41 |
+
"""
|
| 42 |
+
selectedrealkind = self.module.selectedrealkind
|
| 43 |
+
|
| 44 |
+
for i in range(32, 40):
|
| 45 |
+
assert selectedrealkind(i) == selected_real_kind(
|
| 46 |
+
i
|
| 47 |
+
), f"selectedrealkind({i}): expected {selected_real_kind(i)!r} but got {selectedrealkind(i)!r}"
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_parameter.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
from . import util
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestParameters(util.F2PyTest):
|
| 10 |
+
# Check that intent(in out) translates as intent(inout)
|
| 11 |
+
sources = [
|
| 12 |
+
util.getpath("tests", "src", "parameter", "constant_real.f90"),
|
| 13 |
+
util.getpath("tests", "src", "parameter", "constant_integer.f90"),
|
| 14 |
+
util.getpath("tests", "src", "parameter", "constant_both.f90"),
|
| 15 |
+
util.getpath("tests", "src", "parameter", "constant_compound.f90"),
|
| 16 |
+
util.getpath("tests", "src", "parameter", "constant_non_compound.f90"),
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
@pytest.mark.slow
|
| 20 |
+
def test_constant_real_single(self):
|
| 21 |
+
# non-contiguous should raise error
|
| 22 |
+
x = np.arange(6, dtype=np.float32)[::2]
|
| 23 |
+
pytest.raises(ValueError, self.module.foo_single, x)
|
| 24 |
+
|
| 25 |
+
# check values with contiguous array
|
| 26 |
+
x = np.arange(3, dtype=np.float32)
|
| 27 |
+
self.module.foo_single(x)
|
| 28 |
+
assert np.allclose(x, [0 + 1 + 2 * 3, 1, 2])
|
| 29 |
+
|
| 30 |
+
@pytest.mark.slow
|
| 31 |
+
def test_constant_real_double(self):
|
| 32 |
+
# non-contiguous should raise error
|
| 33 |
+
x = np.arange(6, dtype=np.float64)[::2]
|
| 34 |
+
pytest.raises(ValueError, self.module.foo_double, x)
|
| 35 |
+
|
| 36 |
+
# check values with contiguous array
|
| 37 |
+
x = np.arange(3, dtype=np.float64)
|
| 38 |
+
self.module.foo_double(x)
|
| 39 |
+
assert np.allclose(x, [0 + 1 + 2 * 3, 1, 2])
|
| 40 |
+
|
| 41 |
+
@pytest.mark.slow
|
| 42 |
+
def test_constant_compound_int(self):
|
| 43 |
+
# non-contiguous should raise error
|
| 44 |
+
x = np.arange(6, dtype=np.int32)[::2]
|
| 45 |
+
pytest.raises(ValueError, self.module.foo_compound_int, x)
|
| 46 |
+
|
| 47 |
+
# check values with contiguous array
|
| 48 |
+
x = np.arange(3, dtype=np.int32)
|
| 49 |
+
self.module.foo_compound_int(x)
|
| 50 |
+
assert np.allclose(x, [0 + 1 + 2 * 6, 1, 2])
|
| 51 |
+
|
| 52 |
+
@pytest.mark.slow
|
| 53 |
+
def test_constant_non_compound_int(self):
|
| 54 |
+
# check values
|
| 55 |
+
x = np.arange(4, dtype=np.int32)
|
| 56 |
+
self.module.foo_non_compound_int(x)
|
| 57 |
+
assert np.allclose(x, [0 + 1 + 2 + 3 * 4, 1, 2, 3])
|
| 58 |
+
|
| 59 |
+
@pytest.mark.slow
|
| 60 |
+
def test_constant_integer_int(self):
|
| 61 |
+
# non-contiguous should raise error
|
| 62 |
+
x = np.arange(6, dtype=np.int32)[::2]
|
| 63 |
+
pytest.raises(ValueError, self.module.foo_int, x)
|
| 64 |
+
|
| 65 |
+
# check values with contiguous array
|
| 66 |
+
x = np.arange(3, dtype=np.int32)
|
| 67 |
+
self.module.foo_int(x)
|
| 68 |
+
assert np.allclose(x, [0 + 1 + 2 * 3, 1, 2])
|
| 69 |
+
|
| 70 |
+
@pytest.mark.slow
|
| 71 |
+
def test_constant_integer_long(self):
|
| 72 |
+
# non-contiguous should raise error
|
| 73 |
+
x = np.arange(6, dtype=np.int64)[::2]
|
| 74 |
+
pytest.raises(ValueError, self.module.foo_long, x)
|
| 75 |
+
|
| 76 |
+
# check values with contiguous array
|
| 77 |
+
x = np.arange(3, dtype=np.int64)
|
| 78 |
+
self.module.foo_long(x)
|
| 79 |
+
assert np.allclose(x, [0 + 1 + 2 * 3, 1, 2])
|
| 80 |
+
|
| 81 |
+
@pytest.mark.slow
|
| 82 |
+
def test_constant_both(self):
|
| 83 |
+
# non-contiguous should raise error
|
| 84 |
+
x = np.arange(6, dtype=np.float64)[::2]
|
| 85 |
+
pytest.raises(ValueError, self.module.foo, x)
|
| 86 |
+
|
| 87 |
+
# check values with contiguous array
|
| 88 |
+
x = np.arange(3, dtype=np.float64)
|
| 89 |
+
self.module.foo(x)
|
| 90 |
+
assert np.allclose(x, [0 + 1 * 3 * 3 + 2 * 3 * 3, 1 * 3, 2 * 3])
|
| 91 |
+
|
| 92 |
+
@pytest.mark.slow
|
| 93 |
+
def test_constant_no(self):
|
| 94 |
+
# non-contiguous should raise error
|
| 95 |
+
x = np.arange(6, dtype=np.float64)[::2]
|
| 96 |
+
pytest.raises(ValueError, self.module.foo_no, x)
|
| 97 |
+
|
| 98 |
+
# check values with contiguous array
|
| 99 |
+
x = np.arange(3, dtype=np.float64)
|
| 100 |
+
self.module.foo_no(x)
|
| 101 |
+
assert np.allclose(x, [0 + 1 * 3 * 3 + 2 * 3 * 3, 1 * 3, 2 * 3])
|
| 102 |
+
|
| 103 |
+
@pytest.mark.slow
|
| 104 |
+
def test_constant_sum(self):
|
| 105 |
+
# non-contiguous should raise error
|
| 106 |
+
x = np.arange(6, dtype=np.float64)[::2]
|
| 107 |
+
pytest.raises(ValueError, self.module.foo_sum, x)
|
| 108 |
+
|
| 109 |
+
# check values with contiguous array
|
| 110 |
+
x = np.arange(3, dtype=np.float64)
|
| 111 |
+
self.module.foo_sum(x)
|
| 112 |
+
assert np.allclose(x, [0 + 1 * 3 * 3 + 2 * 3 * 3, 1 * 3, 2 * 3])
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_quoted_character.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""See https://github.com/numpy/numpy/pull/10676.
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
import sys
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
from . import util
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestQuotedCharacter(util.F2PyTest):
|
| 11 |
+
sources = [util.getpath("tests", "src", "quoted_character", "foo.f")]
|
| 12 |
+
|
| 13 |
+
@pytest.mark.skipif(sys.platform == "win32",
|
| 14 |
+
reason="Fails with MinGW64 Gfortran (Issue #9673)")
|
| 15 |
+
def test_quoted_character(self):
|
| 16 |
+
assert self.module.foo() == (b"'", b'"', b";", b"!", b"(", b")")
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_regression.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
from . import util
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestIntentInOut(util.F2PyTest):
|
| 10 |
+
# Check that intent(in out) translates as intent(inout)
|
| 11 |
+
sources = [util.getpath("tests", "src", "regression", "inout.f90")]
|
| 12 |
+
|
| 13 |
+
@pytest.mark.slow
|
| 14 |
+
def test_inout(self):
|
| 15 |
+
# non-contiguous should raise error
|
| 16 |
+
x = np.arange(6, dtype=np.float32)[::2]
|
| 17 |
+
pytest.raises(ValueError, self.module.foo, x)
|
| 18 |
+
|
| 19 |
+
# check values with contiguous array
|
| 20 |
+
x = np.arange(3, dtype=np.float32)
|
| 21 |
+
self.module.foo(x)
|
| 22 |
+
assert np.allclose(x, [3, 1, 2])
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class TestNegativeBounds(util.F2PyTest):
|
| 26 |
+
# Check that negative bounds work correctly
|
| 27 |
+
sources = [util.getpath("tests", "src", "negative_bounds", "issue_20853.f90")]
|
| 28 |
+
|
| 29 |
+
@pytest.mark.slow
|
| 30 |
+
def test_negbound(self):
|
| 31 |
+
xvec = np.arange(12)
|
| 32 |
+
xlow = -6
|
| 33 |
+
xhigh = 4
|
| 34 |
+
# Calculate the upper bound,
|
| 35 |
+
# Keeping the 1 index in mind
|
| 36 |
+
def ubound(xl, xh):
|
| 37 |
+
return xh - xl + 1
|
| 38 |
+
rval = self.module.foo(is_=xlow, ie_=xhigh,
|
| 39 |
+
arr=xvec[:ubound(xlow, xhigh)])
|
| 40 |
+
expval = np.arange(11, dtype = np.float32)
|
| 41 |
+
assert np.allclose(rval, expval)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class TestNumpyVersionAttribute(util.F2PyTest):
|
| 45 |
+
# Check that th attribute __f2py_numpy_version__ is present
|
| 46 |
+
# in the compiled module and that has the value np.__version__.
|
| 47 |
+
sources = [util.getpath("tests", "src", "regression", "inout.f90")]
|
| 48 |
+
|
| 49 |
+
@pytest.mark.slow
|
| 50 |
+
def test_numpy_version_attribute(self):
|
| 51 |
+
|
| 52 |
+
# Check that self.module has an attribute named "__f2py_numpy_version__"
|
| 53 |
+
assert hasattr(self.module, "__f2py_numpy_version__")
|
| 54 |
+
|
| 55 |
+
# Check that the attribute __f2py_numpy_version__ is a string
|
| 56 |
+
assert isinstance(self.module.__f2py_numpy_version__, str)
|
| 57 |
+
|
| 58 |
+
# Check that __f2py_numpy_version__ has the value numpy.__version__
|
| 59 |
+
assert np.__version__ == self.module.__f2py_numpy_version__
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_include_path():
|
| 63 |
+
incdir = np.f2py.get_include()
|
| 64 |
+
fnames_in_dir = os.listdir(incdir)
|
| 65 |
+
for fname in ("fortranobject.c", "fortranobject.h"):
|
| 66 |
+
assert fname in fnames_in_dir
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class TestModuleAndSubroutine(util.F2PyTest):
|
| 70 |
+
module_name = "example"
|
| 71 |
+
sources = [util.getpath("tests", "src", "regression", "gh25337", "data.f90"),
|
| 72 |
+
util.getpath("tests", "src", "regression", "gh25337", "use_data.f90")]
|
| 73 |
+
|
| 74 |
+
@pytest.mark.slow
|
| 75 |
+
def test_gh25337(self):
|
| 76 |
+
self.module.data.set_shift(3)
|
| 77 |
+
assert "data" in dir(self.module)
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_return_character.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from numpy import array
|
| 4 |
+
from . import util
|
| 5 |
+
import platform
|
| 6 |
+
|
| 7 |
+
IS_S390X = platform.machine() == "s390x"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestReturnCharacter(util.F2PyTest):
|
| 11 |
+
def check_function(self, t, tname):
|
| 12 |
+
if tname in ["t0", "t1", "s0", "s1"]:
|
| 13 |
+
assert t("23") == b"2"
|
| 14 |
+
r = t("ab")
|
| 15 |
+
assert r == b"a"
|
| 16 |
+
r = t(array("ab"))
|
| 17 |
+
assert r == b"a"
|
| 18 |
+
r = t(array(77, "u1"))
|
| 19 |
+
assert r == b"M"
|
| 20 |
+
elif tname in ["ts", "ss"]:
|
| 21 |
+
assert t(23) == b"23"
|
| 22 |
+
assert t("123456789abcdef") == b"123456789a"
|
| 23 |
+
elif tname in ["t5", "s5"]:
|
| 24 |
+
assert t(23) == b"23"
|
| 25 |
+
assert t("ab") == b"ab"
|
| 26 |
+
assert t("123456789abcdef") == b"12345"
|
| 27 |
+
else:
|
| 28 |
+
raise NotImplementedError
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class TestFReturnCharacter(TestReturnCharacter):
|
| 32 |
+
sources = [
|
| 33 |
+
util.getpath("tests", "src", "return_character", "foo77.f"),
|
| 34 |
+
util.getpath("tests", "src", "return_character", "foo90.f90"),
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
@pytest.mark.xfail(IS_S390X, reason="callback returns ' '")
|
| 38 |
+
@pytest.mark.parametrize("name", "t0,t1,t5,s0,s1,s5,ss".split(","))
|
| 39 |
+
def test_all_f77(self, name):
|
| 40 |
+
self.check_function(getattr(self.module, name), name)
|
| 41 |
+
|
| 42 |
+
@pytest.mark.xfail(IS_S390X, reason="callback returns ' '")
|
| 43 |
+
@pytest.mark.parametrize("name", "t0,t1,t5,ts,s0,s1,s5,ss".split(","))
|
| 44 |
+
def test_all_f90(self, name):
|
| 45 |
+
self.check_function(getattr(self.module.f90_return_char, name), name)
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_return_complex.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from numpy import array
|
| 4 |
+
from . import util
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestReturnComplex(util.F2PyTest):
|
| 8 |
+
def check_function(self, t, tname):
|
| 9 |
+
if tname in ["t0", "t8", "s0", "s8"]:
|
| 10 |
+
err = 1e-5
|
| 11 |
+
else:
|
| 12 |
+
err = 0.0
|
| 13 |
+
assert abs(t(234j) - 234.0j) <= err
|
| 14 |
+
assert abs(t(234.6) - 234.6) <= err
|
| 15 |
+
assert abs(t(234) - 234.0) <= err
|
| 16 |
+
assert abs(t(234.6 + 3j) - (234.6 + 3j)) <= err
|
| 17 |
+
# assert abs(t('234')-234.)<=err
|
| 18 |
+
# assert abs(t('234.6')-234.6)<=err
|
| 19 |
+
assert abs(t(-234) + 234.0) <= err
|
| 20 |
+
assert abs(t([234]) - 234.0) <= err
|
| 21 |
+
assert abs(t((234, )) - 234.0) <= err
|
| 22 |
+
assert abs(t(array(234)) - 234.0) <= err
|
| 23 |
+
assert abs(t(array(23 + 4j, "F")) - (23 + 4j)) <= err
|
| 24 |
+
assert abs(t(array([234])) - 234.0) <= err
|
| 25 |
+
assert abs(t(array([[234]])) - 234.0) <= err
|
| 26 |
+
assert abs(t(array([234]).astype("b")) + 22.0) <= err
|
| 27 |
+
assert abs(t(array([234], "h")) - 234.0) <= err
|
| 28 |
+
assert abs(t(array([234], "i")) - 234.0) <= err
|
| 29 |
+
assert abs(t(array([234], "l")) - 234.0) <= err
|
| 30 |
+
assert abs(t(array([234], "q")) - 234.0) <= err
|
| 31 |
+
assert abs(t(array([234], "f")) - 234.0) <= err
|
| 32 |
+
assert abs(t(array([234], "d")) - 234.0) <= err
|
| 33 |
+
assert abs(t(array([234 + 3j], "F")) - (234 + 3j)) <= err
|
| 34 |
+
assert abs(t(array([234], "D")) - 234.0) <= err
|
| 35 |
+
|
| 36 |
+
# pytest.raises(TypeError, t, array([234], 'a1'))
|
| 37 |
+
pytest.raises(TypeError, t, "abc")
|
| 38 |
+
|
| 39 |
+
pytest.raises(IndexError, t, [])
|
| 40 |
+
pytest.raises(IndexError, t, ())
|
| 41 |
+
|
| 42 |
+
pytest.raises(TypeError, t, t)
|
| 43 |
+
pytest.raises(TypeError, t, {})
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
r = t(10**400)
|
| 47 |
+
assert repr(r) in ["(inf+0j)", "(Infinity+0j)"]
|
| 48 |
+
except OverflowError:
|
| 49 |
+
pass
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class TestFReturnComplex(TestReturnComplex):
|
| 53 |
+
sources = [
|
| 54 |
+
util.getpath("tests", "src", "return_complex", "foo77.f"),
|
| 55 |
+
util.getpath("tests", "src", "return_complex", "foo90.f90"),
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
@pytest.mark.parametrize("name", "t0,t8,t16,td,s0,s8,s16,sd".split(","))
|
| 59 |
+
def test_all_f77(self, name):
|
| 60 |
+
self.check_function(getattr(self.module, name), name)
|
| 61 |
+
|
| 62 |
+
@pytest.mark.parametrize("name", "t0,t8,t16,td,s0,s8,s16,sd".split(","))
|
| 63 |
+
def test_all_f90(self, name):
|
| 64 |
+
self.check_function(getattr(self.module.f90_return_complex, name),
|
| 65 |
+
name)
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_return_integer.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from numpy import array
|
| 4 |
+
from . import util
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestReturnInteger(util.F2PyTest):
|
| 8 |
+
def check_function(self, t, tname):
|
| 9 |
+
assert t(123) == 123
|
| 10 |
+
assert t(123.6) == 123
|
| 11 |
+
assert t("123") == 123
|
| 12 |
+
assert t(-123) == -123
|
| 13 |
+
assert t([123]) == 123
|
| 14 |
+
assert t((123, )) == 123
|
| 15 |
+
assert t(array(123)) == 123
|
| 16 |
+
assert t(array(123, "b")) == 123
|
| 17 |
+
assert t(array(123, "h")) == 123
|
| 18 |
+
assert t(array(123, "i")) == 123
|
| 19 |
+
assert t(array(123, "l")) == 123
|
| 20 |
+
assert t(array(123, "B")) == 123
|
| 21 |
+
assert t(array(123, "f")) == 123
|
| 22 |
+
assert t(array(123, "d")) == 123
|
| 23 |
+
|
| 24 |
+
# pytest.raises(ValueError, t, array([123],'S3'))
|
| 25 |
+
pytest.raises(ValueError, t, "abc")
|
| 26 |
+
|
| 27 |
+
pytest.raises(IndexError, t, [])
|
| 28 |
+
pytest.raises(IndexError, t, ())
|
| 29 |
+
|
| 30 |
+
pytest.raises(Exception, t, t)
|
| 31 |
+
pytest.raises(Exception, t, {})
|
| 32 |
+
|
| 33 |
+
if tname in ["t8", "s8"]:
|
| 34 |
+
pytest.raises(OverflowError, t, 100000000000000000000000)
|
| 35 |
+
pytest.raises(OverflowError, t, 10000000011111111111111.23)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class TestFReturnInteger(TestReturnInteger):
|
| 39 |
+
sources = [
|
| 40 |
+
util.getpath("tests", "src", "return_integer", "foo77.f"),
|
| 41 |
+
util.getpath("tests", "src", "return_integer", "foo90.f90"),
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
@pytest.mark.parametrize("name",
|
| 45 |
+
"t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","))
|
| 46 |
+
def test_all_f77(self, name):
|
| 47 |
+
self.check_function(getattr(self.module, name), name)
|
| 48 |
+
|
| 49 |
+
@pytest.mark.parametrize("name",
|
| 50 |
+
"t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","))
|
| 51 |
+
def test_all_f90(self, name):
|
| 52 |
+
self.check_function(getattr(self.module.f90_return_integer, name),
|
| 53 |
+
name)
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_return_logical.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from numpy import array
|
| 4 |
+
from . import util
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestReturnLogical(util.F2PyTest):
|
| 8 |
+
def check_function(self, t):
|
| 9 |
+
assert t(True) == 1
|
| 10 |
+
assert t(False) == 0
|
| 11 |
+
assert t(0) == 0
|
| 12 |
+
assert t(None) == 0
|
| 13 |
+
assert t(0.0) == 0
|
| 14 |
+
assert t(0j) == 0
|
| 15 |
+
assert t(1j) == 1
|
| 16 |
+
assert t(234) == 1
|
| 17 |
+
assert t(234.6) == 1
|
| 18 |
+
assert t(234.6 + 3j) == 1
|
| 19 |
+
assert t("234") == 1
|
| 20 |
+
assert t("aaa") == 1
|
| 21 |
+
assert t("") == 0
|
| 22 |
+
assert t([]) == 0
|
| 23 |
+
assert t(()) == 0
|
| 24 |
+
assert t({}) == 0
|
| 25 |
+
assert t(t) == 1
|
| 26 |
+
assert t(-234) == 1
|
| 27 |
+
assert t(10**100) == 1
|
| 28 |
+
assert t([234]) == 1
|
| 29 |
+
assert t((234, )) == 1
|
| 30 |
+
assert t(array(234)) == 1
|
| 31 |
+
assert t(array([234])) == 1
|
| 32 |
+
assert t(array([[234]])) == 1
|
| 33 |
+
assert t(array([127], "b")) == 1
|
| 34 |
+
assert t(array([234], "h")) == 1
|
| 35 |
+
assert t(array([234], "i")) == 1
|
| 36 |
+
assert t(array([234], "l")) == 1
|
| 37 |
+
assert t(array([234], "f")) == 1
|
| 38 |
+
assert t(array([234], "d")) == 1
|
| 39 |
+
assert t(array([234 + 3j], "F")) == 1
|
| 40 |
+
assert t(array([234], "D")) == 1
|
| 41 |
+
assert t(array(0)) == 0
|
| 42 |
+
assert t(array([0])) == 0
|
| 43 |
+
assert t(array([[0]])) == 0
|
| 44 |
+
assert t(array([0j])) == 0
|
| 45 |
+
assert t(array([1])) == 1
|
| 46 |
+
pytest.raises(ValueError, t, array([0, 0]))
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class TestFReturnLogical(TestReturnLogical):
|
| 50 |
+
sources = [
|
| 51 |
+
util.getpath("tests", "src", "return_logical", "foo77.f"),
|
| 52 |
+
util.getpath("tests", "src", "return_logical", "foo90.f90"),
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
@pytest.mark.slow
|
| 56 |
+
@pytest.mark.parametrize("name", "t0,t1,t2,t4,s0,s1,s2,s4".split(","))
|
| 57 |
+
def test_all_f77(self, name):
|
| 58 |
+
self.check_function(getattr(self.module, name))
|
| 59 |
+
|
| 60 |
+
@pytest.mark.slow
|
| 61 |
+
@pytest.mark.parametrize("name",
|
| 62 |
+
"t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","))
|
| 63 |
+
def test_all_f90(self, name):
|
| 64 |
+
self.check_function(getattr(self.module.f90_return_logical, name))
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_return_real.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import platform
|
| 2 |
+
import pytest
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from numpy import array
|
| 6 |
+
from . import util
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestReturnReal(util.F2PyTest):
|
| 10 |
+
def check_function(self, t, tname):
|
| 11 |
+
if tname in ["t0", "t4", "s0", "s4"]:
|
| 12 |
+
err = 1e-5
|
| 13 |
+
else:
|
| 14 |
+
err = 0.0
|
| 15 |
+
assert abs(t(234) - 234.0) <= err
|
| 16 |
+
assert abs(t(234.6) - 234.6) <= err
|
| 17 |
+
assert abs(t("234") - 234) <= err
|
| 18 |
+
assert abs(t("234.6") - 234.6) <= err
|
| 19 |
+
assert abs(t(-234) + 234) <= err
|
| 20 |
+
assert abs(t([234]) - 234) <= err
|
| 21 |
+
assert abs(t((234, )) - 234.0) <= err
|
| 22 |
+
assert abs(t(array(234)) - 234.0) <= err
|
| 23 |
+
assert abs(t(array(234).astype("b")) + 22) <= err
|
| 24 |
+
assert abs(t(array(234, "h")) - 234.0) <= err
|
| 25 |
+
assert abs(t(array(234, "i")) - 234.0) <= err
|
| 26 |
+
assert abs(t(array(234, "l")) - 234.0) <= err
|
| 27 |
+
assert abs(t(array(234, "B")) - 234.0) <= err
|
| 28 |
+
assert abs(t(array(234, "f")) - 234.0) <= err
|
| 29 |
+
assert abs(t(array(234, "d")) - 234.0) <= err
|
| 30 |
+
if tname in ["t0", "t4", "s0", "s4"]:
|
| 31 |
+
assert t(1e200) == t(1e300) # inf
|
| 32 |
+
|
| 33 |
+
# pytest.raises(ValueError, t, array([234], 'S1'))
|
| 34 |
+
pytest.raises(ValueError, t, "abc")
|
| 35 |
+
|
| 36 |
+
pytest.raises(IndexError, t, [])
|
| 37 |
+
pytest.raises(IndexError, t, ())
|
| 38 |
+
|
| 39 |
+
pytest.raises(Exception, t, t)
|
| 40 |
+
pytest.raises(Exception, t, {})
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
r = t(10**400)
|
| 44 |
+
assert repr(r) in ["inf", "Infinity"]
|
| 45 |
+
except OverflowError:
|
| 46 |
+
pass
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@pytest.mark.skipif(
|
| 50 |
+
platform.system() == "Darwin",
|
| 51 |
+
reason="Prone to error when run with numpy/f2py/tests on mac os, "
|
| 52 |
+
"but not when run in isolation",
|
| 53 |
+
)
|
| 54 |
+
@pytest.mark.skipif(
|
| 55 |
+
np.dtype(np.intp).itemsize < 8,
|
| 56 |
+
reason="32-bit builds are buggy"
|
| 57 |
+
)
|
| 58 |
+
class TestCReturnReal(TestReturnReal):
|
| 59 |
+
suffix = ".pyf"
|
| 60 |
+
module_name = "c_ext_return_real"
|
| 61 |
+
code = """
|
| 62 |
+
python module c_ext_return_real
|
| 63 |
+
usercode \'\'\'
|
| 64 |
+
float t4(float value) { return value; }
|
| 65 |
+
void s4(float *t4, float value) { *t4 = value; }
|
| 66 |
+
double t8(double value) { return value; }
|
| 67 |
+
void s8(double *t8, double value) { *t8 = value; }
|
| 68 |
+
\'\'\'
|
| 69 |
+
interface
|
| 70 |
+
function t4(value)
|
| 71 |
+
real*4 intent(c) :: t4,value
|
| 72 |
+
end
|
| 73 |
+
function t8(value)
|
| 74 |
+
real*8 intent(c) :: t8,value
|
| 75 |
+
end
|
| 76 |
+
subroutine s4(t4,value)
|
| 77 |
+
intent(c) s4
|
| 78 |
+
real*4 intent(out) :: t4
|
| 79 |
+
real*4 intent(c) :: value
|
| 80 |
+
end
|
| 81 |
+
subroutine s8(t8,value)
|
| 82 |
+
intent(c) s8
|
| 83 |
+
real*8 intent(out) :: t8
|
| 84 |
+
real*8 intent(c) :: value
|
| 85 |
+
end
|
| 86 |
+
end interface
|
| 87 |
+
end python module c_ext_return_real
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
@pytest.mark.parametrize("name", "t4,t8,s4,s8".split(","))
|
| 91 |
+
def test_all(self, name):
|
| 92 |
+
self.check_function(getattr(self.module, name), name)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class TestFReturnReal(TestReturnReal):
|
| 96 |
+
sources = [
|
| 97 |
+
util.getpath("tests", "src", "return_real", "foo77.f"),
|
| 98 |
+
util.getpath("tests", "src", "return_real", "foo90.f90"),
|
| 99 |
+
]
|
| 100 |
+
|
| 101 |
+
@pytest.mark.parametrize("name", "t0,t4,t8,td,s0,s4,s8,sd".split(","))
|
| 102 |
+
def test_all_f77(self, name):
|
| 103 |
+
self.check_function(getattr(self.module, name), name)
|
| 104 |
+
|
| 105 |
+
@pytest.mark.parametrize("name", "t0,t4,t8,td,s0,s4,s8,sd".split(","))
|
| 106 |
+
def test_all_f90(self, name):
|
| 107 |
+
self.check_function(getattr(self.module.f90_return_real, name), name)
|
llmeval-env/lib/python3.10/site-packages/numpy/f2py/tests/test_semicolon_split.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import platform
|
| 2 |
+
import pytest
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from . import util
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@pytest.mark.skipif(
|
| 9 |
+
platform.system() == "Darwin",
|
| 10 |
+
reason="Prone to error when run with numpy/f2py/tests on mac os, "
|
| 11 |
+
"but not when run in isolation",
|
| 12 |
+
)
|
| 13 |
+
@pytest.mark.skipif(
|
| 14 |
+
np.dtype(np.intp).itemsize < 8,
|
| 15 |
+
reason="32-bit builds are buggy"
|
| 16 |
+
)
|
| 17 |
+
class TestMultiline(util.F2PyTest):
|
| 18 |
+
suffix = ".pyf"
|
| 19 |
+
module_name = "multiline"
|
| 20 |
+
code = f"""
|
| 21 |
+
python module {module_name}
|
| 22 |
+
usercode '''
|
| 23 |
+
void foo(int* x) {{
|
| 24 |
+
char dummy = ';';
|
| 25 |
+
*x = 42;
|
| 26 |
+
}}
|
| 27 |
+
'''
|
| 28 |
+
interface
|
| 29 |
+
subroutine foo(x)
|
| 30 |
+
intent(c) foo
|
| 31 |
+
integer intent(out) :: x
|
| 32 |
+
end subroutine foo
|
| 33 |
+
end interface
|
| 34 |
+
end python module {module_name}
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
def test_multiline(self):
|
| 38 |
+
assert self.module.foo() == 42
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@pytest.mark.skipif(
|
| 42 |
+
platform.system() == "Darwin",
|
| 43 |
+
reason="Prone to error when run with numpy/f2py/tests on mac os, "
|
| 44 |
+
"but not when run in isolation",
|
| 45 |
+
)
|
| 46 |
+
@pytest.mark.skipif(
|
| 47 |
+
np.dtype(np.intp).itemsize < 8,
|
| 48 |
+
reason="32-bit builds are buggy"
|
| 49 |
+
)
|
| 50 |
+
class TestCallstatement(util.F2PyTest):
|
| 51 |
+
suffix = ".pyf"
|
| 52 |
+
module_name = "callstatement"
|
| 53 |
+
code = f"""
|
| 54 |
+
python module {module_name}
|
| 55 |
+
usercode '''
|
| 56 |
+
void foo(int* x) {{
|
| 57 |
+
}}
|
| 58 |
+
'''
|
| 59 |
+
interface
|
| 60 |
+
subroutine foo(x)
|
| 61 |
+
intent(c) foo
|
| 62 |
+
integer intent(out) :: x
|
| 63 |
+
callprotoargument int*
|
| 64 |
+
callstatement {{ &
|
| 65 |
+
; &
|
| 66 |
+
x = 42; &
|
| 67 |
+
}}
|
| 68 |
+
end subroutine foo
|
| 69 |
+
end interface
|
| 70 |
+
end python module {module_name}
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
def test_callstatement(self):
|
| 74 |
+
assert self.module.foo() == 42
|