forked from dblalock/swig-numpy-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyfragments.swg
119 lines (111 loc) · 3.12 KB
/
pyfragments.swg
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
/*-*- C -*-*/
/**********************************************************************/
/* For numpy versions prior to 1.0, the names of certain data types
* are different than in later versions. This fragment provides macro
* substitutions that allow us to support old and new versions of
* numpy.
*/
/**********************************************************************/
/* Override the SWIG_AsVal_frag(long) fragment so that it also checks
* for numpy scalar array types. The code through the %#endif is
* essentially cut-and-paste from pyprimtype.swg
*/
%fragment(SWIG_AsVal_frag(long), "header",
fragment="SWIG_CanCastAsInteger",
fragment="NumPy_Backward_Compatibility")
{
SWIGINTERN int
SWIG_AsVal_dec(long)(PyObject * obj, long * val)
{
PyArray_Descr * longDescr = PyArray_DescrNewFromType(NPY_LONG);
if (PyInt_Check(obj)) {
if (val) *val = PyInt_AsLong(obj);
return SWIG_OK;
} else if (PyLong_Check(obj)) {
long v = PyLong_AsLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_OK;
} else {
PyErr_Clear();
}
}
%#ifdef SWIG_PYTHON_CAST_MODE
{
int dispatch = 0;
long v = PyInt_AsLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_AddCast(SWIG_OK);
} else {
PyErr_Clear();
}
if (!dispatch) {
double d;
int res = SWIG_AddCast(SWIG_AsVal(double)(obj,&d));
if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, LONG_MIN, LONG_MAX)) {
if (val) *val = (long)(d);
return res;
}
}
}
%#endif
if (!PyArray_IsScalar(obj,Integer)) return SWIG_TypeError;
PyArray_CastScalarToCtype(obj, (void*)val, longDescr);
return SWIG_OK;
}
}
/* Override the SWIG_AsVal_frag(unsigned long) fragment so that it
* also checks for numpy scalar array types. The code through the
* %#endif is essentially cut-and-paste from pyprimtype.swg
*/
%fragment(SWIG_AsVal_frag(unsigned long),"header",
fragment="SWIG_CanCastAsInteger",
fragment="NumPy_Backward_Compatibility")
{
SWIGINTERN int
SWIG_AsVal_dec(unsigned long)(PyObject *obj, unsigned long *val)
{
PyArray_Descr * ulongDescr = PyArray_DescrNewFromType(NPY_ULONG);
if (PyInt_Check(obj)) {
long v = PyInt_AsLong(obj);
if (v >= 0) {
if (val) *val = v;
return SWIG_OK;
} else {
return SWIG_OverflowError;
}
} else if (PyLong_Check(obj)) {
unsigned long v = PyLong_AsUnsignedLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_OK;
} else {
PyErr_Clear();
}
}
%#ifdef SWIG_PYTHON_CAST_MODE
{
int dispatch = 0;
unsigned long v = PyLong_AsUnsignedLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_AddCast(SWIG_OK);
} else {
PyErr_Clear();
}
if (!dispatch) {
double d;
int res = SWIG_AddCast(SWIG_AsVal(double)(obj,&d));
if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, 0, ULONG_MAX)) {
if (val) *val = (unsigned long)(d);
return res;
}
}
}
%#endif
if (!PyArray_IsScalar(obj,Integer)) return SWIG_TypeError;
PyArray_CastScalarToCtype(obj, (void*)val, ulongDescr);
return SWIG_OK;
}
}