diff options
author | Pauli Virtanen <pav@iki.fi> | 2010-03-06 23:59:08 +0000 |
---|---|---|
committer | Pauli Virtanen <pav@iki.fi> | 2010-03-06 23:59:08 +0000 |
commit | 4c1ae336a15e4f701848fac12a521ba67bf784d3 (patch) | |
tree | 287c787e1a6e396f06cc2eb0a35b2472577d7328 /numpy | |
parent | 2fb79c1a20753baa824d2b2e6b0ad3f2b144db9d (diff) | |
download | numpy-4c1ae336a15e4f701848fac12a521ba67bf784d3.tar.gz |
3K: f2py: port much of f2py C code to Py3
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/f2py/capi_maps.py | 6 | ||||
-rw-r--r-- | numpy/f2py/cfuncs.py | 31 | ||||
-rw-r--r-- | numpy/f2py/rules.py | 41 | ||||
-rw-r--r-- | numpy/f2py/src/fortranobject.c | 63 | ||||
-rw-r--r-- | numpy/f2py/src/fortranobject.h | 32 |
5 files changed, 153 insertions, 20 deletions
diff --git a/numpy/f2py/capi_maps.py b/numpy/f2py/capi_maps.py index 8adc4de11..8c864f920 100644 --- a/numpy/f2py/capi_maps.py +++ b/numpy/f2py/capi_maps.py @@ -19,6 +19,7 @@ f2py_version = __version__.version import copy import re import os +import sys from auxfuncs import * from crackfortran import markoutercomma import cb_rules @@ -137,6 +138,11 @@ c2buildvalue_map={'double':'d', 'complex_double':'N', 'complex_long_double':'N', 'string':'z'} + +if sys.version_info[0] >= 3: + # Bytes, not Unicode strings + c2buildvalue_map['string'] = 'y' + if using_newcore: #c2buildvalue_map=??? pass diff --git a/numpy/f2py/cfuncs.py b/numpy/f2py/cfuncs.py index b9964c93b..45f622700 100644 --- a/numpy/f2py/cfuncs.py +++ b/numpy/f2py/cfuncs.py @@ -604,8 +604,26 @@ fprintf(stderr,\"string_from_pyobj(str='%s',len=%d,inistr='%s',obj=%p)\\n\",(cha \t\ttmp = obj; \t\tPy_INCREF(tmp); \t} -\telse +#if PY_VERSION_HEX >= 0x03000000 +\telse if (PyUnicode_Check(obj)) { +\t\ttmp = PyUnicode_AsASCIIString(obj); +\t} +\telse { +\t\tPyObject *tmp2; +\t\ttmp2 = PyObject_Str(obj); +\t\tif (tmp2) { +\t\t\ttmp = PyUnicode_AsASCIIString(tmp2); +\t\t\tPy_DECREF(tmp2); +\t\t} +\t\telse { +\t\t\ttmp = NULL; +\t\t} +\t} +#else +\telse { \t\ttmp = PyObject_Str(obj); +\t} +#endif \tif (tmp == NULL) goto capi_fail; \tif (*len == -1) \t\t*len = PyString_GET_SIZE(tmp); @@ -1011,7 +1029,7 @@ static int create_cb_arglist(PyObject* fun,PyTupleObject* xa,const int maxnofarg \t\t} \t} if (tmp_fun==NULL) { -fprintf(stderr,\"Call-back argument must be function|instance|instance.__call__|f2py-function but got %s.\\n\",(fun==NULL?\"NULL\":fun->ob_type->tp_name)); +fprintf(stderr,\"Call-back argument must be function|instance|instance.__call__|f2py-function but got %s.\\n\",(fun==NULL?\"NULL\":Py_TYPE(fun)->tp_name)); goto capi_fail; } \tif (PyObject_HasAttrString(tmp_fun,\"func_code\")) { @@ -1068,15 +1086,6 @@ def buildcfuncs(): cppmacros[m]='#define %s(v,dims) (PyArray_SimpleNewFromData(1,dims,PyArray_CHAR,(char *)v))'%(m) -############ Automatic Python3 conversions ################### - -if sys.version_info[0] >= 3: - for key, value in cfuncs.items(): - value = value.replace('PyString', 'PyBytes') - value = value.replace('PyInt_AS_LONG', 'PyLong_AsLong') - value = value.replace('PyInt', 'PyLong') - cfuncs[key] = value - ############ Auxiliary functions for sorting needs ################### def append_needs(need,flag=1): diff --git a/numpy/f2py/rules.py b/numpy/f2py/rules.py index e4fb61d81..ae3871df5 100644 --- a/numpy/f2py/rules.py +++ b/numpy/f2py/rules.py @@ -170,18 +170,47 @@ static PyMethodDef f2py_module_methods[] = { \t{NULL,NULL} }; +#if PY_VERSION_HEX >= 0x03000000 +static struct PyModuleDef moduledef = { +\tPyModuleDef_HEAD_INIT, +\t"#modulename#", +\tNULL, +\t-1, +\tf2py_module_methods, +\tNULL, +\tNULL, +\tNULL, +\tNULL +}; +#endif + +#if PY_VERSION_HEX >= 0x03000000 +#define RETVAL m +PyObject *PyInit_#modulename#(void) { +#else +#define RETVAL PyMODINIT_FUNC init#modulename#(void) { +#endif \tint i; \tPyObject *m,*d, *s; +#if PY_VERSION_HEX >= 0x03000000 +\tm = #modulename#_module = PyModule_Create(&moduledef); +#else \tm = #modulename#_module = Py_InitModule(\"#modulename#\", f2py_module_methods); -\tPyFortran_Type.ob_type = &PyType_Type; +#endif +\tPy_TYPE(&PyFortran_Type) = &PyType_Type; \timport_array(); \tif (PyErr_Occurred()) -\t\t{PyErr_SetString(PyExc_ImportError, \"can't initialize module #modulename# (failed to import numpy)\"); return;} +\t\t{PyErr_SetString(PyExc_ImportError, \"can't initialize module #modulename# (failed to import numpy)\"); return RETVAL;} \td = PyModule_GetDict(m); \ts = PyString_FromString(\"$R"""+"""evision: $\"); \tPyDict_SetItemString(d, \"__version__\", s); -\ts = PyString_FromString(\"This module '#modulename#' is auto-generated with f2py (version:#f2py_version#).\\nFunctions:\\n\"\n#docs#\".\"); +#if PY_VERSION_HEX >= 0x03000000 +\ts = PyUnicode_FromString( +#else +\ts = PyString_FromString( +#endif +\t\t\"This module '#modulename#' is auto-generated with f2py (version:#f2py_version#).\\nFunctions:\\n\"\n#docs#\".\"); \tPyDict_SetItemString(d, \"__doc__\", s); \t#modulename#_error = PyErr_NewException (\"#modulename#.error\", NULL, NULL); \tPy_DECREF(s); @@ -197,6 +226,7 @@ PyMODINIT_FUNC init#modulename#(void) { \t\ton_exit(f2py_report_on_exit,(void*)\"#modulename#\"); #endif +\treturn RETVAL; } #ifdef __cplusplus } @@ -393,6 +423,11 @@ rout_rules=[ extern #ctype# #F_FUNC#(#name_lower#,#NAME#)(void); PyObject* o = PyDict_GetItemString(d,"#name#"); PyObject_SetAttrString(o,"_cpointer", F2PyCapsule_FromVoidPtr((void*)#F_FUNC#(#name_lower#,#NAME#),NULL)); +#if PY_VERSION_HEX >= 0x03000000 + PyObject_SetAttrString(o,"__name__", PyUnicode_FromString("#name#")); +#else + PyObject_SetAttrString(o,"__name__", PyString_FromString("#name#")); +#endif } '''}, 'need':{l_not(l_or(ismoduleroutine,isdummyroutine)):['F_WRAPPEDFUNC','F_FUNC']}, diff --git a/numpy/f2py/src/fortranobject.c b/numpy/f2py/src/fortranobject.c index d08b43c6b..1cd3d3f31 100644 --- a/numpy/f2py/src/fortranobject.c +++ b/numpy/f2py/src/fortranobject.c @@ -92,9 +92,12 @@ fortran_dealloc(PyFortranObject *fp) { } +#if PY_VERSION_HEX >= 0x03000000 +#else static PyMethodDef fortran_methods[] = { {NULL, NULL} /* sentinel */ }; +#endif static PyObject * @@ -167,7 +170,11 @@ fortran_doc (FortranDataDef def) { strlen(p),size); goto fail; } +#if PY_VERSION_HEX >= 0x03000000 + s = PyUnicode_FromString(p); +#else s = PyString_FromString(p); +#endif fail: free(p); return s; @@ -221,20 +228,41 @@ fortran_getattr(PyFortranObject *fp, char *name) { return fp->dict; } if (strcmp(name,"__doc__")==0) { +#if PY_VERSION_HEX >= 0x03000000 + PyObject *s = PyUnicode_FromString(""), *s2, *s3; + for (i=0;i<fp->len;i++) { + s2 = fortran_doc(fp->defs[i]); + s3 = PyUnicode_Concat(s, s2); + Py_DECREF(s2); + Py_DECREF(s); + s = s3; + } +#else PyObject *s = PyString_FromString(""); for (i=0;i<fp->len;i++) PyString_ConcatAndDel(&s,fortran_doc(fp->defs[i])); +#endif if (PyDict_SetItemString(fp->dict, name, s)) return NULL; return s; } if ((strcmp(name,"_cpointer")==0) && (fp->len==1)) { - PyObject *cobj = PyCObject_FromVoidPtr((void *)(fp->defs[0].data),NULL); + PyObject *cobj = F2PyCapsule_FromVoidPtr((void *)(fp->defs[0].data),NULL); if (PyDict_SetItemString(fp->dict, name, cobj)) return NULL; return cobj; } +#if PY_VERSION_HEX >= 0x03000000 + if (1) { + PyObject *str, *ret; + str = PyUnicode_FromString(name); + ret = PyObject_GenericGetAttr((PyObject *)fp, str); + Py_DECREF(str); + return ret; + } +#else return Py_FindMethod(fortran_methods, (PyObject *)fp, name); +#endif } static int @@ -322,10 +350,39 @@ fortran_call(PyFortranObject *fp, PyObject *arg, PyObject *kw) { return NULL; } +static PyObject * +fortran_repr(PyFortranObject *fp) +{ + PyObject *name = NULL, *repr = NULL; + name = PyObject_GetAttrString((PyObject *)fp, "__name__"); + PyErr_Clear(); +#if PY_VERSION_HEX >= 0x03000000 + if (name != NULL && PyUnicode_Check(name)) { + repr = PyUnicode_FromFormat("<fortran %U>", name); + } + else { + repr = PyUnicode_FromString("<fortran object>"); + } +#else + if (name != NULL && PyString_Check(name)) { + repr = PyString_FromFormat("<fortran %s>", PyString_AsString(name)); + } + else { + repr = PyString_FromString("<fortran object>"); + } +#endif + Py_XDECREF(name); + return repr; +} + PyTypeObject PyFortran_Type = { +#if PY_VERSION_HEX >= 0x03000000 + PyVarObject_HEAD_INIT(NULL, 0) +#else PyObject_HEAD_INIT(0) 0, /*ob_size*/ +#endif "fortran", /*tp_name*/ sizeof(PyFortranObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -334,8 +391,8 @@ PyTypeObject PyFortran_Type = { 0, /*tp_print*/ (getattrfunc)fortran_getattr, /*tp_getattr*/ (setattrfunc)fortran_setattr, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ + 0, /*tp_compare/tp_reserved*/ + (reprfunc)fortran_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ diff --git a/numpy/f2py/src/fortranobject.h b/numpy/f2py/src/fortranobject.h index 696f79c94..54a386514 100644 --- a/numpy/f2py/src/fortranobject.h +++ b/numpy/f2py/src/fortranobject.h @@ -12,6 +12,31 @@ extern "C" { #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API #include "numpy/arrayobject.h" +/* + * Python 3 support macros + */ +#if PY_VERSION_HEX >= 0x03000000 +#define PyString_Check PyBytes_Check +#define PyString_GET_SIZE PyBytes_GET_SIZE +#define PyString_AS_STRING PyBytes_AS_STRING +#define PyString_FromString PyBytes_FromString +#define PyString_ConcatAndDel PyBytes_ConcatAndDel +#define PyString_AsString PyBytes_AsString + +#define PyInt_Check PyLong_Check +#define PyInt_FromLong PyLong_FromLong +#define PyInt_AS_LONG PyLong_AsLong +#define PyInt_AsLong PyLong_AsLong + +#define PyNumber_Int PyNumber_Long +#endif + +#if (PY_VERSION_HEX < 0x02060000) +#define Py_TYPE(o) (((PyObject*)(o))->ob_type) +#define Py_REFCNT(o) (((PyObject*)(o))->ob_refcnt) +#define Py_SIZE(o) (((PyVarObject*)(o))->ob_size) +#endif + /* #ifdef F2PY_REPORT_ATEXIT_DISABLE #undef F2PY_REPORT_ATEXIT @@ -88,15 +113,15 @@ typedef struct { PyObject *dict; /* Fortran object attribute dictionary */ } PyFortranObject; -#define PyFortran_Check(op) ((op)->ob_type == &PyFortran_Type) -#define PyFortran_Check1(op) (0==strcmp((op)->ob_type->tp_name,"fortran")) +#define PyFortran_Check(op) (Py_TYPE(op) == &PyFortran_Type) +#define PyFortran_Check1(op) (0==strcmp(Py_TYPE(op)->tp_name,"fortran")) extern PyTypeObject PyFortran_Type; extern int F2PyDict_SetItemString(PyObject* dict, char *name, PyObject *obj); extern PyObject * PyFortranObject_New(FortranDataDef* defs, f2py_void_func init); extern PyObject * PyFortranObject_NewAsAttr(FortranDataDef* defs); -#if PY_VERSION_HEX >= 0X03010000 +#if PY_VERSION_HEX >= 0x03010000 PyObject * F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *)); void * F2PyCapsule_AsVoidPtr(PyObject *obj); @@ -146,6 +171,7 @@ int F2PyCapsule_Check(PyObject *ptr); extern void dump_attrs(const PyArrayObject* arr); #endif + #ifdef __cplusplus } #endif |