summaryrefslogtreecommitdiff
path: root/numpy/f2py/lib/generate_pyobj_tofrom_funcs.py
blob: 7f1c7aa4731f3834de43e8dc231cd815efcf27f8 (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
"""
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 utils import CHAR_BIT

def pyobj_from_npy_int(ctype):
    ctype_bits = int(ctype[7:])
    itemsize = ctype_bits/CHAR_BIT
    dtype = ctype.upper()
    return '''\
static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) {
  return PyArray_Return(PyArray_SimpleNewFromData(0, NULL, %(dtype)s, (char*)value));
}
''' % (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) {
  Py_complex c;
  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 1;
  }
  return 0;
}
#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))
    raise NotImplementedError,`ctype`