-
-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathpymodule.i
More file actions
336 lines (290 loc) · 9.57 KB
/
pymodule.i
File metadata and controls
336 lines (290 loc) · 9.57 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/******************************************************************************
* Project: MapServer
* Purpose: Python-specific enhancements to MapScript
* Author: Sean Gillies, sgillies@frii.com
*
******************************************************************************
*
* Python-specific mapscript code has been moved into this
* SWIG interface file to improve the readability of the main
* interface file. The main mapscript.i file includes this
* file when SWIGPYTHON is defined (via 'swig -python ...').
*
*****************************************************************************/
/******************************************************************************
* Simple Typemaps
*****************************************************************************/
/* Translates Python None to C NULL for strings */
//%typemap(in,parse="z") char * "";
/* To support imageObj::getBytes */
%typemap(out) gdBuffer {
$result = PyBytes_FromStringAndSize((const char*)$1.data, $1.size);
if( $1.owns_data )
msFree($1.data);
}
/*
* Typemap for counted arrays of doubles <- PySequence
*/
%typemap(in,numinputs=1) (int nListSize, double* pListValues)
{
int i;
/* %typemap(in,numinputs=1) (int nListSize, double* pListValues)*/
/* check if is List */
if ( !PySequence_Check($input) ) {
PyErr_SetString(PyExc_TypeError, "not a sequence");
SWIG_fail;
}
$1 = (int) PySequence_Size($input);
$2 = (double*) malloc($1*sizeof(double));
for( i = 0; i<$1; i++ ) {
PyObject *o = PySequence_GetItem($input,i);
if ( !PyArg_Parse(o,"d",&$2[i]) ) {
PyErr_SetString(PyExc_TypeError, "not a number");
Py_DECREF(o);
SWIG_fail;
}
Py_DECREF(o);
}
}
%fragment("CreateTupleFromDoubleArray","header") %{
static PyObject *
CreateTupleFromDoubleArray( double *first, unsigned int size ) {
unsigned int i;
PyObject *out = PyTuple_New( size );
for( i=0; i<size; i++ ) {
PyObject *val = PyFloat_FromDouble( *first );
++first;
PyTuple_SetItem( out, i, val );
}
return out;
}
%}
%typemap(freearg) (int nListSize, double* pListValues)
{
/* %typemap(freearg) (int nListSize, double* pListValues) */
if ($2) {
free((void*) $2);
}
}
%typemap(in,numinputs=0) (double** argout, int* pnListSize) ( double* argout, int nListSize ) {
/* %typemap(python,in,numinputs=0) (double *val, int*hasval) */
$1 = &argout;
$2 = &nListSize;
}
%typemap(argout,fragment="CreateTupleFromDoubleArray") (double** argout, int* pnListSize)
{
/* %typemap(argout) (double* argout, int* pnListSize) */
PyObject *r;
r = CreateTupleFromDoubleArray(*$1, *$2);
free(*$1);
%#if SWIG_VERSION >= 0x040300
$result = SWIG_Python_AppendOutput($result, r, $isvoid);
%#else
$result = SWIG_Python_AppendOutput($result, r);
%#endif
}
/*
* Typemap to turn a Python dict into two sequences and
* an item count. Used for msApplySubstitutions
*/
%typemap(in) (char **names, char **values, int npairs) {
/* Check if is a dict */
if (PyDict_Check($input)) {
int i = 0;
int size = (int) PyDict_Size($input);
PyObject* keys = PyDict_Keys($input);
PyObject* values = PyDict_Values($input);
$3 = size;
$1 = (char **) malloc((size+1)*sizeof(char *));
$2 = (char **) malloc((size+1)*sizeof(char *));
for (i = 0; i < size; i++) {
PyObject* key = PyList_GetItem(keys, i);
PyObject* val = PyList_GetItem(values, i);
%#if PY_MAJOR_VERSION >= 3
// Changed in version 3.7: The return type is now const char * rather than char *
// avoid warning C4090: '=': different 'const' qualifiers
$1[i] = (char *)PyUnicode_AsUTF8(key);
$2[i] = (char *)PyUnicode_AsUTF8(val);
%#else
$1[i] = PyString_AsString(key);
$2[i] = PyString_AsString(val);
%#endif
}
$1[i] = 0;
$2[i] = 0;
} else {
PyErr_SetString(PyExc_TypeError, "Input not a dictionary");
SWIG_fail;
}
}
%typemap(freearg) (char **names, char **values, int npairs) {
free((char *) $1);
free((char *) $2);
}
/**************************************************************************
* MapServer Errors and Python Exceptions
**************************************************************************
*
* Translation of errors reported via the ms_error structure into
* Python exceptions. Original code by Chris Chamberlin <cdc@aracnet.com>
* has been updated by Sean Gillies <sgillies@frii.com> to use
* PyErr_SetString, %exception and $action for SWIG 1.3, do the most
* obvious mapping of MapServer errors to Python exceptions and map all
* others to a new 'MapServerError' exception which can be caught like this:
*
* from mapscript import *
* empty_map = mapObj('')
* try:
* img = empty_map.draw()
* except MapServerError, msg:
* print "Caught MapServerError:", msg
*
*************************************************************************/
%{
PyObject *MSExc_MapServerError;
PyObject *MSExc_MapServerChildError;
%}
/* Module initialization: call msSetup() and register msCleanup() */
%init %{
/* See bug 1203 for discussion of race condition with GD font cache */
if (msSetup() != MS_SUCCESS)
{
msSetError(MS_MISCERR, "Failed to set up threads and font cache",
"msSetup()");
}
Py_AtExit(msCleanup);
/* Define Generic MapServer error */
MSExc_MapServerError=PyErr_NewException("_mapscript.MapServerError",NULL,NULL);
if (MSExc_MapServerError != NULL)
PyDict_SetItemString(d, "MapServerError", MSExc_MapServerError);
/* Define MapServer MS_CHILD error */
MSExc_MapServerChildError = PyErr_NewException("_mapscript.MapServerChildError", NULL, NULL);
if (MSExc_MapServerChildError != NULL)
PyDict_SetItemString(d, "MapServerChildError", MSExc_MapServerChildError);
%}
%{
static void _raise_ms_exception( void );
static void _raise_ms_exception() {
int errcode;
errorObj *ms_error;
char *errmsg;
ms_error = msGetErrorObj();
errcode = ms_error->code;
errmsg = msGetErrorString("\n");
switch (errcode) {
case MS_IOERR:
PyErr_SetString(PyExc_IOError, errmsg);
break;
case MS_MEMERR:
PyErr_SetString(PyExc_MemoryError, errmsg);
break;
case MS_TYPEERR:
PyErr_SetString(PyExc_TypeError, errmsg);
break;
case MS_EOFERR:
PyErr_SetString(PyExc_EOFError, errmsg);
break;
case MS_CHILDERR:
PyErr_SetString(MSExc_MapServerChildError, errmsg);
break;
default:
PyErr_SetString(MSExc_MapServerError, errmsg);
break;
}
free(errmsg);
}
%}
%exception {
$action {
errorObj *ms_error = msGetErrorObj();
switch(ms_error->code) {
case MS_NOERR:
break;
case MS_NOTFOUND:
msResetErrorList();
break;
case -1:
break;
case MS_IOERR:
/*
if (strcmp(ms_error->routine, "msSearchDiskTree()") != 0) {
_raise_ms_exception();
msResetErrorList();
return NULL;
}
*/
default:
_raise_ms_exception();
msResetErrorList();
return NULL;
}
}
}
%pythoncode %{
MapServerError = _mapscript.MapServerError
MapServerChildError = _mapscript.MapServerChildError
%}
%feature("pythonappend") layerObj %{
self.p_map = None
if map:
self.p_map = map%}
%feature("pythonappend") classObj %{
self.p_layer = None
if layer:
self.p_layer = layer%}
%feature("shadow") insertClass %{
def insertClass(*args):
"""
Insert a **copy** of the class into the layer at the requested *index*.
Default index of -1 means insertion at the end of the array of classes. Returns the index at which the class was inserted.
"""
actualIndex=$action(*args)
args[1].p_layer=args[0]
return actualIndex%}
%feature("shadow") getClass %{
def getClass(*args):
"""
Fetch the requested class object at *i*. Returns NULL if the class index is out of the legal range.
The numclasses field contains the number of classes available, and the first class is index 0.
"""
clazz = $action(*args)
if clazz:
if args and len(args)!=0:
clazz.p_layer=args[0]
else:
clazz.p_layer=None
return clazz%}
%feature("shadow") insertLayer %{
def insertLayer(*args):
"""
Insert a copy of *layer* into the map at *index*.
The default value of *index* is -1, which means the last possible index.
Returns the index of the new layer, or -1 in the case of a failure.
"""
actualIndex=$action(*args)
args[1].p_map=args[0]
return actualIndex%}
%feature("shadow") getLayer %{
def getLayer(*args):
"""
Returns a reference to the layer at index *i*.
"""
layer = $action(*args)
if layer:
if args and len(args)!=0:
layer.p_map=args[0]
else:
layer.p_map=None
return layer%}
%feature("shadow") getLayerByName %{
def getLayerByName(*args):
"""
Returns a reference to the named layer.
"""
layer = $action(*args)
if layer:
if args and len(args)!=0:
layer.p_map=args[0]
else:
layer.p_map=None
return layer%}