summaryrefslogtreecommitdiff
path: root/numpy/f2py/lib/generate_pyobj_tofrom_funcs.py
blob: f9dedb2767f8efbc7be84257f53d038f75bfd300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
Generate
  int pyobj_to_<ctype>(PyObject* obj, <ctype>* value)
  PyObject* pyobj_from_<stype>(<ctype>* value)
functions.
"""
__all__ = ['pyobj_to_npy_scalar','pyobj_to_f2py_string','pyobj_from_npy_scalar']

from parser.api import CHAR_BIT

def pyobj_from_npy_int(ctype):
    dtype = ctype.upper()
    cls = 'Int'+ctype[7:]
    return '''\
/* depends: SCALARS_IN_BITS.cpp */
static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) {
  PyObject* obj = PyArrayScalar_New(%(cls)s);
  if (obj==NULL) /* TODO: set exception */ return NULL;
  PyArrayScalar_ASSIGN(obj,%(cls)s,*value);
  return obj;
}
''' % (locals())

def pyobj_from_npy_float(ctype):
    dtype = ctype.upper()
    cls = 'Float'+ctype[9:]
    return '''\
/* depends: SCALARS_IN_BITS.cpp */
static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) {
  PyObject* obj = PyArrayScalar_New(%(cls)s);
  if (obj==NULL) /* TODO: set exception */ return NULL;
  PyArrayScalar_ASSIGN(obj,%(cls)s,*value);
  return obj;
}
''' % (locals())

def pyobj_from_npy_complex(ctype):
    dtype = ctype.upper()
    cls = 'Complex'+ctype[11:]
    return '''\
/* depends: SCALARS_IN_BITS.cpp */
static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) {
  PyObject* obj = PyArrayScalar_New(%(cls)s);
  if (obj==NULL) /* TODO: set exception */ return NULL;
  PyArrayScalar_ASSIGN(obj,%(cls)s,*value);
  return obj;
}
''' % (locals())

def pyobj_from_f2py_type(ctype):
    ctype_bits = int(ctype[10:])
    raise NotImplementedError,`ctype`
    return '''\
static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) {
  fprintf(stderr,"In pyobj_from_%(ctype)s (%%p)\\n", value);
}
'''

def pyobj_to_npy_int(ctype):
    ctype_bits = int(ctype[7:])
    return '''
/* depends: pyobj_to_long.c, pyobj_to_npy_longlong.c */
#if NPY_BITSOF_LONG == %(ctype_bits)s
#define pyobj_to_%(ctype)s pyobj_to_long
#else
#if NPY_BITSOF_LONG > %(ctype_bits)s
static int pyobj_to_%(ctype)s(PyObject *obj, %(ctype)s* value) {
  long tmp;
  if (pyobj_to_long(obj,&tmp)) {
    *value = (%(ctype)s)tmp;
    return 1;
  }
  return 0;
}
#else
static int pyobj_to_%(ctype)s(PyObject *obj, %(ctype)s* value) {
  npy_longlong tmp;
  if (pyobj_to_npy_longlong(obj,&tmp)) {
    *value = (%(ctype)s)tmp;
    return 1;
  }
  return 0;
}
#endif
#endif
''' % (locals())

def pyobj_to_npy_float(ctype):
    ctype_bits = int(ctype[9:])
    return '''
/* depends: pyobj_to_double.c */
#if NPY_BITSOF_DOUBLE == %(ctype_bits)s
#define pyobj_to_%(ctype)s pyobj_to_double
#else
#if NPY_BITSOF_DOUBLE > %(ctype_bits)s
static int pyobj_to_%(ctype)s(PyObject *obj, %(ctype)s* value) {
  double tmp;
  if (pyobj_to_double(obj,&tmp)) {
    *value = (%(ctype)s)tmp;
    return 1;
  }
  return 0;
}
#else
#error, "NOTIMPLEMENTED pyobj_to_%(ctype)s"
#endif
#endif
''' % (locals())

def pyobj_to_npy_complex(ctype):
    ctype_bits = int(ctype[11:])
    cfloat_bits = ctype_bits/2
    return '''
/* depends: pyobj_to_Py_complex.c */
#if NPY_BITSOF_DOUBLE >= %(cfloat_bits)s
static int pyobj_to_%(ctype)s(PyObject *obj, %(ctype)s* value) {
  int return_value = 0;
  Py_complex c;
#if defined(F2PY_DEBUG_PYOBJ_TOFROM)
  fprintf(stderr,"pyobj_to_%(ctype)s(type=%%s)\\n",PyString_AS_STRING(PyObject_Repr(PyObject_Type(obj))));
#endif
  if (pyobj_to_Py_complex(obj,&c)) {
    (*value).real = (npy_float%(cfloat_bits)s)c.real;
    (*value).imag = (npy_float%(cfloat_bits)s)c.imag;
    return_value = 1;
  }
#if defined(F2PY_DEBUG_PYOBJ_TOFROM)
  fprintf(stderr,"pyobj_to_%(ctype)s: return_value=%%d, PyErr_Occurred()=%%p\\n", return_value, PyErr_Occurred());
#endif
  return return_value;
}
#else
#error, "NOTIMPLEMENTED pyobj_to_%(ctype)s"
#endif
''' % (locals())

def pyobj_to_npy_scalar(ctype):
    if ctype.startswith('npy_int'):
        return dict(c_code=pyobj_to_npy_int(ctype))
    elif ctype.startswith('npy_float'):
        return dict(c_code=pyobj_to_npy_float(ctype))
    elif ctype.startswith('npy_complex'):
        return dict(c_code=pyobj_to_npy_complex(ctype))
    raise NotImplementedError,`ctype`

def pyobj_to_f2py_string(ctype):
    ctype_bits = int(ctype[11:])
    ctype_bytes = ctype_bits / CHAR_BIT
    return dict(
        c_code = '''
/* depends: pyobj_to_string_len.c */
static int pyobj_to_%(ctype)s(PyObject *obj, %(ctype)s* value) {
  return pyobj_to_string_len(obj, (f2py_string*)value, %(ctype_bytes)s);
}
''' % (locals()),
        typedef = ['typedef char * f2py_string;',
                         'typedef struct { char data[%(ctype_bytes)s]; } %(ctype); ' % (locals())],
        header = ['#include <string.h>'],
        )

def pyobj_from_npy_scalar(ctype):
    if ctype.startswith('npy_int'):
        return dict(c_code=pyobj_from_npy_int(ctype))
    elif ctype.startswith('npy_float'):
        return dict(c_code=pyobj_from_npy_float(ctype))
    elif ctype.startswith('npy_complex'):
        return dict(c_code=pyobj_from_npy_complex(ctype))
    raise NotImplementedError,`ctype`