8000 3K: f2py: port much of f2py C code to Py3 · numpy/numpy@4c1ae33 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c1ae33

Browse files
committed
3K: f2py: port much of f2py C code to Py3
1 parent 2fb79c1 commit 4c1ae33

File tree

5 files changed

+153
-20
lines changed

5 files changed

+153
-20
lines changed

numpy/f2py/capi_maps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import copy
2020
import re
2121
import os
22+
import sys
2223
from auxfuncs import *
2324
from crackfortran import markoutercomma
2425
import cb_rules
@@ -137,6 +138,11 @@
137138
'complex_double':'N',
138139
'complex_long_double':'N',
139140
'string':'z'}
141+
142+
if sys.version_info[0] >= 3:
143+
# Bytes, not Unicode strings
144+
c2buildvalue_map['string'] = 'y'
145+
140146
if using_newcore:
141147
#c2buildvalue_map=???
142148
pass

numpy/f2py/cfuncs.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,26 @@
604604
\t\ttmp = obj;
605605
\t\tPy_INCREF(tmp);
606606
\t}
607-
\telse
607+
#if PY_VERSION_HEX >= 0x03000000
608+
\telse if (PyUnicode_Check(obj)) {
609+
\t\ttmp = PyUnicode_AsASCIIString(obj);
610+
\t}
611+
\telse {
612+
\t\tPyObject *tmp2;
613+
\t\ttmp2 = PyObject_Str(obj);
614+
\t\tif (tmp2) {
615+
\t\t\ttmp = PyUnicode_AsASCIIString(tmp2);
616+
\t\t\tPy_DECREF(tmp2);
617+
\t\t}
618+
\t\telse {
619+
\t\t\ttmp = NULL;
620+
\t\t}
621+
\t}
622+
#else
623+
\telse {
608624
\t\ttmp = PyObject_Str(obj);
625+
\t}
626+
#endif
609627
\tif (tmp == NULL) goto capi_fail;
610628
\tif (*len == -1)
611629
\t\t*len = PyString_GET_SIZE(tmp);
@@ -1011,7 +1029,7 @@
10111029
\t\t}
10121030
\t}
10131031
if (tmp_fun==NULL) {
1014-
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));
1032+
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));
10151033
goto capi_fail;
10161034
}
10171035
\tif (PyObject_HasAttrString(tmp_fun,\"func_code\")) {
@@ -1068,15 +1086,6 @@ def buildcfuncs():
10681086
cppmacros[m]='#define %s(v,dims) (PyArray_SimpleNewFromData(1,dims,PyArray_CHAR,(char *)v))'%(m)
10691087

10701088

1071-
############ Automatic Python3 conversions ###################
1072-
1073-
if sys.version_info[0] >= 3:
1074-
for key, value in cfuncs.items():
1075-
value = value.replace('PyString', 'PyBytes')
1076-
value = value.replace('PyInt_AS_LONG', 'PyLong_AsLong')
1077-
value = value.replace('PyInt', 'PyLong')
1078-
cfuncs[key] = value
1079-
10801089
############ Auxiliary functions for sorting needs ###################
10811090

10821091
def append_needs(need,flag=1):

numpy/f2py/rules.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,47 @@
170170
\t{NULL,NULL}
171171
};
172172
173+
#if PY_VERSION_HEX >= 0x03000000
174+
static struct PyModuleDef moduledef = {
175+
\tPyModuleDef_HEAD_INIT,
176+
\t"#modulename#",
177+
\tNULL,
178+
\t-1,
179+
\tf2py_module_methods,
180+
\tNULL,
181+
\tNULL,
182+
\tNULL,
183+
\tNULL
184+
};
185+
#endif
186+
187+
#if PY_VERSION_HEX >= 0x03000000
188+
#define RETVAL m
189+
PyObject *PyInit_#modulename#(void) {
190+
#else
191+
#define RETVAL
173192
PyMODINIT_FUNC init#modulename#(void) {
193+
#endif
174194
\tint i;
175195
\tPyObject *m,*d, *s;
196+
#if PY_VERSION_HEX >= 0x03000000
197+
\tm = #modulename#_module = PyModule_Create(&moduledef);
198+
#else
176199
\tm = #modulename#_module = Py_InitModule(\"#modulename#\", f2py_module_methods);
177-
\tPyFortran_Type.ob_type = &PyType_Type;
200+
#endif
201+
\tPy_TYPE(&PyFortran_Type) = &PyType_Type;
178202
\timport_array();
179203
\tif (PyErr_Occurred())
180-
\t\t{PyErr_SetString(PyExc_ImportError, \"can't initialize module #modulename# (failed to import numpy)\"); return;}
204+
\t\t{PyErr_SetString(PyExc_ImportError, \"can't initialize module #modulename# (failed to import numpy)\"); return RETVAL;}
181205
\td = PyModule_GetDict(m);
182206
\ts = PyString_FromString(\"$R"""+"""evision: $\");
183207
\tPyDict_SetItemString(d, \"__version__\", s);
184-
\ts = PyString_FromString(\"This module '#modulename#' is auto-generated with f2py (version:#f2py_version#).\\nFunctions:\\n\"\n#docs#\".\");
208+
#if PY_VERSION_HEX >= 0x03000000
209+
\ts = PyUnicode_FromString(
210+
#else
211+
\ts = PyString_FromString(
212+
#endif
213+
\t\t\"This module '#modulename#' is auto-generated with f2py (version:#f2py_version#).\\nFunctions:\\n\"\n#docs#\".\");
185214
\tPyDict_SetItemString(d, \"__doc__\", s);
186215
\t#modulename#_error = PyErr_NewException (\"#modulename#.error\", NULL, NULL);
187216
\tPy_DECREF(s);
@@ -197,6 +226,7 @@
197226
\t\ton_exit(f2py_report_on_exit,(void*)\"#modulename#\");
198227
#endif
199228
229+
\treturn RETVAL;
200230
}
201231
#ifdef __cplusplus
202232
}
@@ -393,6 +423,11 @@
393423
extern #ctype# #F_FUNC#(#name_lower#,#NAME#)(void);
394424
PyObject* o = PyDict_GetItemString(d,"#name#");
395425
PyObject_SetAttrString(o,"_cpointer", F2PyCapsule_FromVoidPtr((void*)#F_FUNC#(#name_lower#,#NAME#),NULL));
426+
#if PY_VERSION_HEX >= 0x03000000
427+
PyObject_SetAttrString(o,"__name__", PyUnicode_FromString("#name#"));
428+
#else
429+
PyObject_SetAttrString(o,"__name__", PyString_FromString("#name#"));
430+
#endif
396431
}
397432
'''},
398433
'need':{l_not(l_or(ismoduleroutine,isdummyroutine)):['F_WRAPPEDFUNC','F_FUNC']},

numpy/f2py/src/fortranobject.c

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,12 @@ fortran_dealloc(PyFortranObject *fp) {
9292
}
9393

9494

95+
#if PY_VERSION_HEX >= 0x03000000
96+
#else
9597
static PyMethodDef fortran_methods[] = {
9698
{NULL, NULL} /* sentinel */
9799
};
100+
#endif
98101

99102

100103
static PyObject *
@@ -167,7 +170,11 @@ fortran_doc (FortranDataDef def) {
167170
strlen(p),size);
168171
goto fail;
169172
}
173+
#if PY_VERSION_HEX >= 0x03000000
174+
s = PyUnicode_FromString(p);
175+
#else
170176
s = PyString_FromString(p);
177+
#endif
171178
fail:
172179
free(p);
173180
return s;
@@ -221,20 +228,41 @@ fortran_getattr(PyFortranObject *fp, char *name) {
221228
return fp->dict;
222229
}
223230
if (strcmp(name,"__doc__")==0) {
231+
#if PY_VERSION_HEX >= 0x03000000
232+
PyObject *s = PyUnicode_FromString(""), *s2, *s3;
233+
for (i=0;i<fp->len;i++) {
234+
s2 = fortran_doc(fp->defs[i]);
235+
s3 = PyUnicode_Concat(s, s2);
236+
Py_DECREF(s2);
237+
Py_DECREF(s);
238+
s = s3;
239+
}
240+
#else
224241
PyObject *s = PyString_FromString("");
225242
for (i=0;i<fp->len;i++)
226243
PyString_ConcatAndDel(&s,fortran_doc(fp->defs[i]));
244+
#endif
227245
if (PyDict_SetItemString(fp->dict, name, s))
228246
return NULL;
229247
return s;
230248
}
231249
if ((strcmp(name,"_cpointer")==0) && (fp->len==1)) {
232-
PyObject *cobj = PyCObject_FromVoidPtr((void *)(fp->defs[0].data),NULL);
250+
PyObject *cobj = F2PyCapsule_FromVoidPtr((void *)(fp->defs[0].data),NULL);
233251
if (PyDict_SetItemString(fp->dict, name, cobj))
234252
return NULL;
235253
return cobj;
236254
}
255+
#if PY_VERSION_HEX >= 0x03000000
256+
if (1) {
257+
PyObject *str, *ret;
258+
str = PyUnicode_FromString(name);
259+
ret = PyObject_GenericGetAttr((PyObject *)fp, str);
260+
Py_DECREF(str);
261+
return ret;
262+
}
263+
#else
237264
return Py_FindMethod(fortran_methods, (PyObject *)fp, name);
265+
#endif
238266
}
239267

240268
static int
@@ -322,10 +350,39 @@ fortran_call(PyFortranObject *fp, PyObject *arg, PyObject *kw) {
322350
return NULL;
323351
}
324352

353+
static PyObject *
354+
fortran_repr(PyFortranObject *fp)
355+
{
356+
PyObject *name = NULL, *repr = NULL;
357+
name = PyObject_GetAttrString((PyObject *)fp, "__name__");
358+
PyErr_Clear();
359+
#if PY_VERSION_HEX >= 0x03000000
360+
if (name != NULL && PyUnicode_Check(name)) {
361+
repr = PyUnicode_FromFormat("<fortran %U>", name);
362+
}
363+
else {
364+
repr = PyUnicode_FromString("<fortran object>");
365+
}
366+
#else
367+
if (name != NULL && PyString_Check(name)) {
368+
repr = PyString_FromFormat("<fortran %s>", PyString_AsString(name));
369+
}
370+
else {
371+
repr = PyString_FromString("<fortran object>");
372+
}
373+
#endif
374+
Py_XDECREF(name);
375+
return repr;
376+
}
377+
325378

326379
PyTypeObject PyFortran_Type = {
380+
#if PY_VERSION_HEX >= 0x03000000
381+
PyVarObject_HEAD_INIT(NULL, 0)
382+
#else
327383
PyObject_HEAD_INIT(0)
328384
0, /*ob_size*/
385+
#endif
329386
"fortran", /*tp_name*/
330387
sizeof(PyFortranObject), /*tp_basicsize*/
331388
0, /*tp_itemsize*/
@@ -334,8 +391,8 @@ PyTypeObject PyFortran_Type = {
334391
0, /*tp_print*/
335392
(getattrfunc)fortran_getattr, /*tp_getattr*/
336393
(setattrfunc)fortran_setattr, /*tp_setattr*/
337-
0, /*tp_compare*/
338-
0, /*tp_repr*/
394+
0, /*tp_compare/tp_reserved*/
395+
(reprfunc)fortran_repr, /*tp_repr*/
339396
0, /*tp_as_number*/
340397
0, /*tp_as_sequence*/
341398
0, /*tp_as_mapping*/

numpy/f2py/src/fortranobject.h

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@ extern "C" {
1212
#define PY_ARRAY_UNIQUE_SYMBOL PyArray_API
1313
#include "numpy/arrayobject.h"
1414

15+
/*
16+
* Python 3 support macros
17+
*/
18+
#if PY_VERSION_HEX >= 0x03000000
19+
#define PyString_Check PyBytes_Check
20+
#define PyString_GET_SIZE PyBytes_GET_SIZE
21+
#define PyString_AS_STRING PyBytes_AS_STRING
22+
#define PyString_FromString PyBytes_FromString
23+
#define PyString_ConcatAndDel PyBytes_ConcatAndDel
24+
#define PyString_AsString PyBytes_AsString
25+
26+
#define PyInt_Check PyLong_Check
27+
#define PyInt_FromLong PyLong_FromLong
28+
#define PyInt_AS_LONG PyLong_AsLong
29+
#define PyInt_AsLong PyLong_AsLong
30+
31+
#define PyNumber_Int PyNumber_Long
32+
#endif
33+
34+
#if (PY_VERSION_HEX < 0x02060000)
35+
#define Py_TYPE(o) (((PyObject*)(o))->ob_type)
36+
#define Py_REFCNT(o) (((PyObject*)(o))->ob_refcnt)
37+
#define Py_SIZE(o) (((PyVarObject*)(o))->ob_size)
38+
#endif
39+
1540
/*
1641
#ifdef F2PY_REPORT_ATEXIT_DISABLE
1742
#undef F2PY_REPORT_ATEXIT
@@ -88,15 +113,15 @@ typedef struct {
88113
PyObject *dict; /* Fortran object attribute dictionary */
89114
} PyFortranObject;
90115

91-
#define PyFortran_Check(op) ((op)->ob_type == &PyFortran_Type)
92-
#define PyFortran_Check1(op) (0==strcmp((op)->ob_type->tp_name,"fortran"))
116+
#define PyFortran_Check(op) (Py_TYPE(op) == &PyFortran_Type)
117+
#define PyFortran_Check1(op) (0==strcmp(Py_TYPE(op)->tp_name,"fortran"))
93118

94119
extern PyTypeObject PyFortran_Type;
95120
extern int F2PyDict_SetItemString(PyObject* dict, char *name, PyObject *obj);
96121
extern PyObject * PyFortranObject_New(FortranDataDef* defs, f2py_void_func init);
97122
extern PyObject * PyFortranObject_NewAsAttr(FortranDataDef* defs);
98123

99-
#if PY_VERSION_HEX >= 0X03010000
124+
#if PY_VERSION_HEX >= 0x03010000
100125

101126
PyObject * F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *));
102127
void * F2PyCapsule_AsVoidPtr(PyObject *obj);
@@ -146,6 +171,7 @@ int F2PyCapsule_Check(PyObject *ptr);
146171
extern void dump_attrs(const PyArrayObject* arr);
147172
#endif
148173

174+
149175
#ifdef __cplusplus
150176
}
151177
#endif

0 commit comments

Comments
 (0)
0