|
@@ -8,10 +8,20 @@
|
8
|
8
|
/**@brief @ref pymod_pyrpn_RPNExprIter methods definition
|
9
|
9
|
* @ingroup pymod_pyrpn_RPNExprIter */
|
10
|
10
|
static PyMethodDef RPNIterExpr_methods[] = {
|
|
11
|
+ PYRPN_method("params", rpnif_params,
|
|
12
|
+ METH_VARARGS | METH_KEYWORDS | METH_CLASS,
|
|
13
|
+ /**@todo add default values */
|
|
14
|
+ "cls, pos_flag, res_flag, size_lim, const_values, stack_size",
|
|
15
|
+ "Get a name tuple with parameters given a position \
|
|
16
|
+flag, a result flag and size limits"),
|
11
|
17
|
PYRPN_method("get_params", rpnif_get_params,
|
12
|
18
|
METH_NOARGS,
|
13
|
19
|
"self, /",
|
14
|
20
|
"Get a name tuple with parameters"),
|
|
21
|
+ PYRPN_method("set_mmap", rpnif_set_mmap,
|
|
22
|
+ METH_O,
|
|
23
|
+ "self, mmap, /",
|
|
24
|
+ "Set the mmap that stores data"),
|
15
|
25
|
PYRPN_method("shape", rpnif_shape,
|
16
|
26
|
METH_NOARGS,
|
17
|
27
|
"self, /",
|
|
@@ -58,6 +68,8 @@ static PyMethodDef RPNIterExpr_methods[] = {
|
58
|
68
|
static PyMemberDef RPNIterExpr_members[] = {
|
59
|
69
|
{"expressions", T_OBJECT, offsetof(PyRPNIterExpr_t, expr), READONLY,
|
60
|
70
|
"The tuple of expressions"},
|
|
71
|
+ {"mmap", T_OBJECT, offsetof(PyRPNIterExpr_t, mmap), READONLY,
|
|
72
|
+ "The mmap storing data"},
|
61
|
73
|
{NULL}
|
62
|
74
|
};
|
63
|
75
|
|
|
@@ -110,6 +122,44 @@ PyTypeObject RPNIterExprType = {
|
110
|
122
|
.tp_new = rpnif_new,
|
111
|
123
|
};
|
112
|
124
|
|
|
125
|
+PyObject* rpnif_params(PyObject *cls, PyObject *args, PyObject *kwds)
|
|
126
|
+{
|
|
127
|
+ char *names[] = {"pos_flag", "res_flag", "size_lim", "const_values", "stack_size", NULL};
|
|
128
|
+
|
|
129
|
+ unsigned short pos_flag, res_flag, stack_size;
|
|
130
|
+ PyObject *lim_obj, *const_values_obj;
|
|
131
|
+
|
|
132
|
+ stack_size = 16;
|
|
133
|
+ const_values_obj = lim_obj = NULL;
|
|
134
|
+
|
|
135
|
+ if(!PyArg_ParseTupleAndKeywords(args, kwds,
|
|
136
|
+ "HHO|OH:RPNIterExppr.get_params", names,
|
|
137
|
+ &pos_flag, &res_flag, &lim_obj, &const_values_obj,
|
|
138
|
+ &stack_size))
|
|
139
|
+ {
|
|
140
|
+ return NULL;
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ rpn_if_param_t *rif_params = _rpnif_get_params(pos_flag, res_flag,
|
|
144
|
+ lim_obj, const_values_obj, stack_size);
|
|
145
|
+ if(!rif_params)
|
|
146
|
+ {
|
|
147
|
+ return NULL;
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ PyObject *res = _rpnif_params_to_tuple(rif_params);
|
|
151
|
+ if(!res)
|
|
152
|
+ {
|
|
153
|
+ goto err;
|
|
154
|
+ }
|
|
155
|
+ free(rif_params);
|
|
156
|
+ return res;
|
|
157
|
+
|
|
158
|
+err:
|
|
159
|
+ free(rif_params);
|
|
160
|
+ return NULL;
|
|
161
|
+}
|
|
162
|
+
|
113
|
163
|
PyObject* rpnif_new(PyTypeObject *subtype, PyObject *args, PyObject* kwds)
|
114
|
164
|
{
|
115
|
165
|
PyObject *ret, *err;
|
|
@@ -130,175 +180,72 @@ PyObject* rpnif_new(PyTypeObject *subtype, PyObject *args, PyObject* kwds)
|
130
|
180
|
int rpnif_init(PyObject *self, PyObject *args, PyObject *kwds)
|
131
|
181
|
{
|
132
|
182
|
PyRPNIterExpr_t *expr_self;
|
133
|
|
- char *names[] = {"pos_flag", "res_flag", "size_lim", "const_values", "stack_size", NULL};
|
|
183
|
+ char *names[] = {"pos_flag", "res_flag", "size_lim", "const_values", "stack_size", "mmap", NULL};
|
134
|
184
|
unsigned short pos_flag, res_flag, stack_size;
|
135
|
|
- PyObject *lim_obj, *const_values_obj;
|
136
|
|
- int ndim;
|
|
185
|
+ PyObject *lim_obj, *const_values_obj, *mmap_obj;
|
137
|
186
|
|
138
|
|
- char err_str[256];
|
139
|
187
|
|
140
|
188
|
expr_self = (PyRPNIterExpr_t*)self;
|
141
|
189
|
|
142
|
190
|
stack_size = 16;
|
143
|
|
- const_values_obj = lim_obj = NULL;
|
|
191
|
+ const_values_obj = lim_obj = mmap_obj = NULL;
|
144
|
192
|
expr_self->rif = NULL;
|
|
193
|
+ expr_self->mmap = NULL;
|
145
|
194
|
|
146
|
|
- if(!PyArg_ParseTupleAndKeywords(args, kwds, "HHO|OH:RPNIterExpr.__init__", names,
|
147
|
|
- &pos_flag, &res_flag, &lim_obj, &const_values_obj, &stack_size))
|
|
195
|
+ if(!PyArg_ParseTupleAndKeywords(args, kwds, "HHO|OHO:RPNIterExpr.__init__", names,
|
|
196
|
+ &pos_flag, &res_flag, &lim_obj, &const_values_obj, &stack_size,
|
|
197
|
+ &mmap_obj))
|
148
|
198
|
{
|
149
|
199
|
return -1;
|
150
|
200
|
}
|
151
|
201
|
|
152
|
|
- // Args checking
|
153
|
|
- if(stack_size < 4 || stack_size > 255)
|
154
|
|
- {
|
155
|
|
- snprintf(err_str, 128,
|
156
|
|
- "Stack size should be in [0..255] but %u given",
|
157
|
|
- stack_size);
|
158
|
|
- PyErr_SetString(PyExc_ValueError, err_str);
|
159
|
|
- return -1;
|
160
|
|
- }
|
|
202
|
+ rpn_if_param_t *rif_params = _rpnif_get_params(pos_flag, res_flag,
|
|
203
|
+ lim_obj, const_values_obj, stack_size);
|
161
|
204
|
|
162
|
|
- // Checks flags & fetch expected sizes for size_lim & const_values
|
163
|
|
- short expt_sizes[2];
|
164
|
|
- if(rpn_if_sizes_from_flag(pos_flag, res_flag, expt_sizes) < 0)
|
|
205
|
+ if(!rif_params)
|
165
|
206
|
{
|
166
|
|
- if(expt_sizes[0] < 0)
|
167
|
|
- {
|
168
|
|
- PyErr_SetString(PyExc_ValueError,
|
169
|
|
- "Invalid position flag given");
|
170
|
|
- }
|
171
|
|
- else
|
172
|
|
- {
|
173
|
|
- PyErr_SetString(PyExc_ValueError,
|
174
|
|
- "Invalid result flag given");
|
175
|
|
- }
|
176
|
207
|
return -1;
|
177
|
208
|
}
|
178
|
209
|
|
179
|
|
- //Check & convert lim
|
180
|
|
- PyObject *tmp;
|
181
|
|
- tmp = PySequence_Fast(lim_obj, "Sequence expected for size_lim argument");
|
182
|
|
- if(PyErr_Occurred())
|
183
|
|
- {
|
184
|
|
- return -1;
|
185
|
|
- }
|
186
|
|
- Py_ssize_t lim_obj_sz = PySequence_Fast_GET_SIZE(tmp);
|
187
|
|
- ndim = lim_obj_sz;
|
188
|
|
- if(PyErr_Occurred())
|
189
|
|
- {
|
190
|
|
- Py_DECREF(tmp);
|
191
|
|
- return -1;
|
192
|
|
- }
|
193
|
|
- if(lim_obj_sz < 1)
|
194
|
|
- {
|
195
|
|
- Py_DECREF(tmp);
|
196
|
|
- PyErr_SetString(PyExc_ValueError,
|
197
|
|
- "Size limits cannot be empty");
|
198
|
|
- return -1;
|
199
|
|
- }
|
|
210
|
+ expr_self->ndim = ((rpn_if_default_data_t*)(rif_params->data))->ndim;
|
200
|
211
|
|
201
|
|
- if(pos_flag == RPN_IF_POSITION_XDIM)
|
|
212
|
+ const Py_ssize_t expt_sz = rif_params->mem_sz * rif_params->value_sz;
|
|
213
|
+ if(!mmap_obj)
|
202
|
214
|
{
|
203
|
|
- PyObject *item = PySequence_Fast_GET_ITEM(tmp, 0);
|
204
|
|
- Py_ssize_t tmp = PyLong_AsSsize_t(item);
|
205
|
|
- if(PyErr_Occurred())
|
206
|
|
- {
|
207
|
|
- PyErr_SetString(PyExc_ValueError,
|
208
|
|
- "Unable to convert size_lim[0] to int");
|
209
|
|
- Py_DECREF(tmp);
|
210
|
|
- return -1;
|
211
|
|
- }
|
212
|
|
- if(lim_obj_sz != tmp + 1)
|
213
|
|
- {
|
214
|
|
- PyErr_Format(PyExc_ValueError,
|
215
|
|
- "Xdim indicate %d size_lim but len(size_lim)=%d",
|
216
|
|
- tmp+1, lim_obj_sz);
|
217
|
|
- Py_DECREF(tmp);
|
218
|
|
- return -1;
|
219
|
|
- }
|
220
|
|
- expt_sizes[0] = ndim = tmp;
|
|
215
|
+ PyObject *fileno = PyLong_FromLong(-1);
|
|
216
|
+ PyObject *length = PyLong_FromSize_t(expt_sz);
|
|
217
|
+ mmap_obj = PyObject_CallFunctionObjArgs(mmap_cls, fileno, length, NULL);
|
221
|
218
|
}
|
222
|
219
|
else
|
223
|
220
|
{
|
224
|
|
- if(lim_obj_sz != expt_sizes[0])
|
225
|
|
- {
|
226
|
|
- PyErr_Format(PyExc_ValueError,
|
227
|
|
- "Expected %d size_lim but len(size_lim)=%d",
|
228
|
|
- expt_sizes[0], lim_obj_sz);
|
229
|
|
- Py_DECREF(tmp);
|
230
|
|
- return -1;
|
231
|
|
- }
|
232
|
|
- }
|
233
|
|
-
|
234
|
|
- size_t sz_limits[lim_obj_sz];
|
235
|
|
- for(Py_ssize_t i = 0; i<lim_obj_sz; i++)
|
236
|
|
- {
|
237
|
|
- PyObject *item = PySequence_Fast_GET_ITEM(tmp, i);
|
238
|
|
- sz_limits[i] = PyLong_AsSize_t(item);
|
239
|
|
- if(PyErr_Occurred())
|
240
|
|
- {
|
241
|
|
- PyErr_Format(PyExc_ValueError,
|
242
|
|
- "Unable to convert size_lim[%d] to unsigned int",
|
243
|
|
- i);
|
244
|
|
- Py_DECREF(tmp);
|
245
|
|
- return -1;
|
246
|
|
- }
|
247
|
|
- }
|
248
|
|
- Py_DECREF(tmp);
|
249
|
|
- tmp = NULL;
|
250
|
|
-
|
251
|
|
- expr_self->ndim = ndim;
|
252
|
|
-
|
253
|
|
- //Check & convert const values
|
254
|
|
- Py_ssize_t values_obj_sz = 0;
|
255
|
|
- if(expt_sizes[1] > 0)
|
256
|
|
- {
|
257
|
|
- tmp = const_values_obj;
|
258
|
|
- if(!PyTuple_Check(tmp))
|
|
221
|
+ if(!PyObject_TypeCheck(mmap_obj, (PyTypeObject*)mmap_cls))
|
259
|
222
|
{
|
260
|
|
- PyErr_SetString(PyExc_ValueError,
|
261
|
|
- "Invalid type for const_values argument");
|
|
223
|
+ PyErr_Format(PyExc_TypeError,
|
|
224
|
+ "The mmap argument MUST be an instance \
|
|
225
|
+of mmap.mmap");
|
262
|
226
|
return -1;
|
263
|
227
|
}
|
264
|
|
- values_obj_sz = PyTuple_Size(tmp);
|
265
|
|
- if(values_obj_sz != expt_sizes[1])
|
|
228
|
+ /**@todo check if mmap is shared & writable ? */
|
|
229
|
+ if(PyObject_Length(mmap_obj) != (Py_ssize_t) expt_sz)
|
266
|
230
|
{
|
267
|
231
|
PyErr_Format(PyExc_ValueError,
|
268
|
|
- "Expected %d const_values but len(const_values)=%d",
|
269
|
|
- expt_sizes[1], values_obj_sz);
|
|
232
|
+ "Expected mmap length is %ld but mmap with length %ld provided",
|
|
233
|
+ rif_params->mem_sz, PyObject_Length(mmap_obj));
|
270
|
234
|
return -1;
|
271
|
235
|
}
|
272
|
236
|
}
|
273
|
237
|
|
274
|
|
- rpn_value_t const_values[values_obj_sz];
|
275
|
|
- for(Py_ssize_t i = 0; i<values_obj_sz; i++)
|
276
|
|
- {
|
277
|
|
- PyObject *item = PyTuple_GET_ITEM(tmp, i);
|
278
|
|
- const_values[i] = PyLong_AsRpnValue_t(item);
|
279
|
|
- if(PyErr_Occurred())
|
280
|
|
- {
|
281
|
|
- PyErr_Format(PyExc_ValueError,
|
282
|
|
- "Unable to convert size_lim[%d] to unsigned int",
|
283
|
|
- i);
|
284
|
|
- return -1;
|
285
|
|
- }
|
286
|
|
- }
|
287
|
|
-
|
288
|
|
-
|
289
|
|
- // Creating rif params
|
290
|
|
- rpn_if_param_t *rif_params;
|
291
|
238
|
|
292
|
|
- if(!(rif_params = rpn_if_default_params(pos_flag, res_flag,
|
293
|
|
- sz_limits, const_values, stack_size)))
|
|
239
|
+ if(PyObject_GetBuffer(mmap_obj, &expr_self->mm_buff,
|
|
240
|
+ PyBUF_CONTIG) == -1)
|
294
|
241
|
{
|
295
|
|
- PyErr_SetString(PyExc_ValueError, "Unable to create parameters \
|
296
|
|
-with given arguments");
|
297
|
242
|
return -1;
|
298
|
243
|
}
|
299
|
244
|
|
|
245
|
+ expr_self->mmap = mmap_obj;
|
|
246
|
+
|
300
|
247
|
// Creating rif with a new memory map
|
301
|
|
- expr_self->rif = rpn_if_new(rif_params, NULL);
|
|
248
|
+ expr_self->rif = rpn_if_new(rif_params, expr_self->mm_buff.buf);
|
302
|
249
|
if(!expr_self->rif)
|
303
|
250
|
{
|
304
|
251
|
PyErr_Format(PyExc_RuntimeError,
|
|
@@ -326,75 +273,48 @@ with given arguments");
|
326
|
273
|
PyObject *rpnif_get_params(PyObject *self)
|
327
|
274
|
{
|
328
|
275
|
PyRPNIterExpr_t *expr_self = (PyRPNIterExpr_t*)self;
|
329
|
|
- PyObject *res, *val;
|
330
|
|
- rpn_if_default_data_t *params;
|
331
|
|
-
|
332
|
|
- params = (rpn_if_default_data_t*)(expr_self->rif->params->data);
|
|
276
|
+ return _rpnif_params_to_tuple(expr_self->rif->params);
|
|
277
|
+}
|
333
|
278
|
|
334
|
|
- short expt_sizes[2];
|
335
|
|
- if(rpn_if_sizes_from_flag(params->pos_flag, params->res_flag, expt_sizes) < 0)
|
|
279
|
+PyObject *rpnif_set_mmap(PyObject *self, PyObject *mm_obj)
|
|
280
|
+{
|
|
281
|
+ if(!PyObject_TypeCheck(mm_obj, (PyTypeObject*)mmap_cls))
|
336
|
282
|
{
|
337
|
|
- PyErr_SetString(PyExc_RuntimeError, "Invalid internal state");
|
|
283
|
+ PyErr_Format(PyExc_TypeError,
|
|
284
|
+ "Excpected instance of mmap.mmap");
|
338
|
285
|
return NULL;
|
339
|
286
|
}
|
|
287
|
+ PyRPNIterExpr_t *expr_self = (PyRPNIterExpr_t*)self;
|
340
|
288
|
|
341
|
|
- res = PyStructSequence_New(&rpnif_params_SeqDesc);
|
342
|
|
- if(!res)
|
|
289
|
+ Py_buffer buff;
|
|
290
|
+
|
|
291
|
+ /**@todo check mm size &writable ? */
|
|
292
|
+ if(PyObject_GetBuffer(mm_obj, &buff, PyBUF_CONTIG) == -1)
|
343
|
293
|
{
|
344
|
294
|
return NULL;
|
345
|
295
|
}
|
346
|
|
- val = PyLong_FromLong(expr_self->rif->params->rpn_argc);
|
347
|
|
- PyStructSequence_SET_ITEM(res, 0, val);
|
348
|
|
-
|
349
|
|
- val = PyLong_FromLong(params->pos_flag);
|
350
|
|
- PyStructSequence_SET_ITEM(res, 1, val);
|
351
|
296
|
|
352
|
|
- val = PyLong_FromLong(params->res_flag);
|
353
|
|
- PyStructSequence_SET_ITEM(res, 2, val);
|
|
297
|
+ // Release old mmap
|
|
298
|
+ PyBuffer_Release(&expr_self->mm_buff);
|
|
299
|
+ Py_DECREF(expr_self->mmap);
|
354
|
300
|
|
355
|
|
- if(params->pos_flag == RPN_IF_POSITION_XDIM)
|
|
301
|
+ if(expr_self->rif->self_mem)
|
356
|
302
|
{
|
357
|
|
- expt_sizes[0] = params->size_lim[0] + 1;
|
|
303
|
+ const size_t mm_sz = expr_self->rif->params->mem_sz *\
|
|
304
|
+ expr_self->rif->params->value_sz;
|
|
305
|
+ munmap(expr_self->rif->mem, mm_sz);
|
358
|
306
|
}
|
359
|
307
|
|
360
|
|
- PyObject *lim = PyTuple_New(expt_sizes[0]);
|
361
|
|
- if(!lim)
|
362
|
|
- {
|
363
|
|
- Py_DECREF(res);
|
364
|
|
- return NULL;
|
365
|
|
- }
|
366
|
|
- PyStructSequence_SET_ITEM(res, 3, lim);
|
|
308
|
+ // Set new mmap in python struct & rif struct
|
|
309
|
+ expr_self->mm_buff = buff;
|
|
310
|
+ expr_self->mmap = mm_obj;
|
367
|
311
|
|
368
|
|
- for(Py_ssize_t i=0; i<expt_sizes[0]; i++)
|
369
|
|
- {
|
370
|
|
- val = PyLong_FromSize_t(params->size_lim[i]);
|
371
|
|
- PyTuple_SET_ITEM(lim, i, val);
|
372
|
|
- }
|
|
312
|
+ expr_self->rif->mem = buff.buf;
|
|
313
|
+ expr_self->rif->self_mem = 0;
|
373
|
314
|
|
374
|
|
- if(!params->const_val)
|
375
|
|
- {
|
376
|
|
- Py_INCREF(Py_None);
|
377
|
|
- PyTuple_SET_ITEM(res, 4, Py_None);
|
378
|
|
- }
|
379
|
|
- else
|
380
|
|
- {
|
381
|
|
- PyObject *values = PyTuple_New(expt_sizes[1]);
|
382
|
|
- if(!values)
|
383
|
|
- {
|
384
|
|
- Py_DECREF(res);
|
385
|
|
- return NULL;
|
386
|
|
- }
|
387
|
|
- PyStructSequence_SET_ITEM(res, 4, values);
|
388
|
|
- for(Py_ssize_t i=0; i<expt_sizes[1]; i++)
|
389
|
|
- {
|
390
|
|
- val = PyLong_FromRpnValue_t(params->const_val[i]);
|
391
|
|
- PyTuple_SET_ITEM(values, i, val);
|
392
|
|
- }
|
393
|
|
- }
|
394
|
|
- return res;
|
|
315
|
+ Py_RETURN_NONE;
|
395
|
316
|
}
|
396
|
317
|
|
397
|
|
-
|
398
|
318
|
PyObject *rpnif_shape(PyObject *self)
|
399
|
319
|
{
|
400
|
320
|
PyRPNIterExpr_t *expr_self = (PyRPNIterExpr_t*)self;
|
|
@@ -705,6 +625,11 @@ void rpnif_del(PyObject *self)
|
705
|
625
|
rpn_if_free(expr_self->rif);
|
706
|
626
|
expr_self->rif = NULL;
|
707
|
627
|
}
|
|
628
|
+ if(expr_self->mm_buff.buf)
|
|
629
|
+ {
|
|
630
|
+ PyBuffer_Release(&expr_self->mm_buff);
|
|
631
|
+ }
|
|
632
|
+ Py_DECREF(expr_self->mmap);
|
708
|
633
|
}
|
709
|
634
|
|
710
|
635
|
|
|
@@ -912,9 +837,12 @@ int rpnif_getbuffer(PyObject *self, Py_buffer *view, int flags)
|
912
|
837
|
PyRPNIterExpr_t *expr_self;
|
913
|
838
|
expr_self = (PyRPNIterExpr_t*)self;
|
914
|
839
|
|
|
840
|
+ return PyObject_GetBuffer(expr_self->mmap, view, flags);
|
|
841
|
+
|
915
|
842
|
rpn_if_default_data_t *data = (rpn_if_default_data_t*)expr_self->rif->params->data;
|
916
|
843
|
|
917
|
844
|
view->buf = expr_self->rif->mem;
|
|
845
|
+dprintf(2, "buffer addr %p\n", expr_self->mm_buff.buf);
|
918
|
846
|
view->obj = self;
|
919
|
847
|
view->len = expr_self->rif->params->mem_sz * expr_self->rif->params->value_sz;
|
920
|
848
|
view->readonly = 0;
|
|
@@ -1186,7 +1114,7 @@ PyObject* rpnif_getstate(PyObject *self, PyObject *noargs)
|
1186
|
1114
|
PyErr_SetString(PyExc_NotImplementedError,
|
1187
|
1115
|
"getstate Not implemented");
|
1188
|
1116
|
return NULL;
|
1189
|
|
- /**@todo TODO write the function */
|
|
1117
|
+
|
1190
|
1118
|
Py_RETURN_NONE;
|
1191
|
1119
|
}
|
1192
|
1120
|
|
|
@@ -1199,6 +1127,233 @@ PyObject* rpnif_setstate(PyObject *self, PyObject *state_bytes)
|
1199
|
1127
|
Py_RETURN_NONE;
|
1200
|
1128
|
}
|
1201
|
1129
|
|
|
1130
|
+rpn_if_param_t *_rpnif_get_params(unsigned short pos_flag, unsigned short res_flag,
|
|
1131
|
+ PyObject *lim_obj, PyObject *const_values_obj,
|
|
1132
|
+ unsigned short stack_size)
|
|
1133
|
+{
|
|
1134
|
+ char err_str[256];
|
|
1135
|
+ int ndim;
|
|
1136
|
+
|
|
1137
|
+ // Args checking
|
|
1138
|
+ if(stack_size < 4 || stack_size > 255)
|
|
1139
|
+ {
|
|
1140
|
+ snprintf(err_str, 128,
|
|
1141
|
+ "Stack size should be in [0..255] but %u given",
|
|
1142
|
+ stack_size);
|
|
1143
|
+ PyErr_SetString(PyExc_ValueError, err_str);
|
|
1144
|
+ return NULL;
|
|
1145
|
+ }
|
|
1146
|
+
|
|
1147
|
+ // Checks flags & fetch expected sizes for size_lim & const_values
|
|
1148
|
+ short expt_sizes[2];
|
|
1149
|
+ if(rpn_if_sizes_from_flag(pos_flag, res_flag, expt_sizes) < 0)
|
|
1150
|
+ {
|
|
1151
|
+ if(expt_sizes[0] < 0)
|
|
1152
|
+ {
|
|
1153
|
+ PyErr_SetString(PyExc_ValueError,
|
|
1154
|
+ "Invalid position flag given");
|
|
1155
|
+ }
|
|
1156
|
+ else
|
|
1157
|
+ {
|
|
1158
|
+ PyErr_SetString(PyExc_ValueError,
|
|
1159
|
+ "Invalid result flag given");
|
|
1160
|
+ }
|
|
1161
|
+ return NULL;
|
|
1162
|
+ }
|
|
1163
|
+
|
|
1164
|
+ //Check & convert lim
|
|
1165
|
+ PyObject *tmp;
|
|
1166
|
+ tmp = PySequence_Fast(lim_obj, "Sequence expected for size_lim argument");
|
|
1167
|
+ if(PyErr_Occurred())
|
|
1168
|
+ {
|
|
1169
|
+ return NULL;
|
|
1170
|
+ }
|
|
1171
|
+ Py_ssize_t lim_obj_sz = PySequence_Fast_GET_SIZE(tmp);
|
|
1172
|
+ ndim = lim_obj_sz;
|
|
1173
|
+ if(PyErr_Occurred())
|
|
1174
|
+ {
|
|
1175
|
+ Py_DECREF(tmp);
|
|
1176
|
+ return NULL;
|
|
1177
|
+ }
|
|
1178
|
+ if(lim_obj_sz < 1)
|
|
1179
|
+ {
|
|
1180
|
+ Py_DECREF(tmp);
|
|
1181
|
+ PyErr_SetString(PyExc_ValueError,
|
|
1182
|
+ "Size limits cannot be empty");
|
|
1183
|
+ return NULL;
|
|
1184
|
+ }
|
|
1185
|
+
|
|
1186
|
+ if(pos_flag == RPN_IF_POSITION_XDIM)
|
|
1187
|
+ {
|
|
1188
|
+ PyObject *item = PySequence_Fast_GET_ITEM(tmp, 0);
|
|
1189
|
+ Py_ssize_t tmp = PyLong_AsSsize_t(item);
|
|
1190
|
+ if(PyErr_Occurred())
|
|
1191
|
+ {
|
|
1192
|
+ PyErr_SetString(PyExc_ValueError,
|
|
1193
|
+ "Unable to convert size_lim[0] to int");
|
|
1194
|
+ Py_DECREF(tmp);
|
|
1195
|
+ return NULL;
|
|
1196
|
+ }
|
|
1197
|
+ if(lim_obj_sz != tmp + 1)
|
|
1198
|
+ {
|
|
1199
|
+ PyErr_Format(PyExc_ValueError,
|
|
1200
|
+ "Xdim indicate %d size_lim but len(size_lim)=%d",
|
|
1201
|
+ tmp+1, lim_obj_sz);
|
|
1202
|
+ Py_DECREF(tmp);
|
|
1203
|
+ return NULL;
|
|
1204
|
+ }
|
|
1205
|
+ expt_sizes[0] = ndim = tmp;
|
|
1206
|
+ }
|
|
1207
|
+ else
|
|
1208
|
+ {
|
|
1209
|
+ if(lim_obj_sz != expt_sizes[0])
|
|
1210
|
+ {
|
|
1211
|
+ PyErr_Format(PyExc_ValueError,
|
|
1212
|
+ "Expected %d size_lim but len(size_lim)=%d",
|
|
1213
|
+ expt_sizes[0], lim_obj_sz);
|
|
1214
|
+ Py_DECREF(tmp);
|
|
1215
|
+ return NULL;
|
|
1216
|
+ }
|
|
1217
|
+ }
|
|
1218
|
+
|
|
1219
|
+ size_t sz_limits[lim_obj_sz];
|
|
1220
|
+ for(Py_ssize_t i = 0; i<lim_obj_sz; i++)
|
|
1221
|
+ {
|
|
1222
|
+ PyObject *item = PySequence_Fast_GET_ITEM(tmp, i);
|
|
1223
|
+ sz_limits[i] = PyLong_AsSize_t(item);
|
|
1224
|
+ if(PyErr_Occurred())
|
|
1225
|
+ {
|
|
1226
|
+ PyErr_Format(PyExc_ValueError,
|
|
1227
|
+ "Unable to convert size_lim[%d] to unsigned int",
|
|
1228
|
+ i);
|
|
1229
|
+ Py_DECREF(tmp);
|
|
1230
|
+ return NULL;
|
|
1231
|
+ }
|
|
1232
|
+ }
|
|
1233
|
+ Py_DECREF(tmp);
|
|
1234
|
+ tmp = NULL;
|
|
1235
|
+
|
|
1236
|
+ //Check & convert const values
|
|
1237
|
+ Py_ssize_t values_obj_sz = 0;
|
|
1238
|
+ if(expt_sizes[1] > 0)
|
|
1239
|
+ {
|
|
1240
|
+ tmp = const_values_obj;
|
|
1241
|
+ if(!PyTuple_Check(tmp))
|
|
1242
|
+ {
|
|
1243
|
+ PyErr_SetString(PyExc_ValueError,
|
|
1244
|
+ "Invalid type for const_values argument");
|
|
1245
|
+ return NULL;
|
|
1246
|
+ }
|
|
1247
|
+ values_obj_sz = PyTuple_Size(tmp);
|
|
1248
|
+ if(values_obj_sz != expt_sizes[1])
|
|
1249
|
+ {
|
|
1250
|
+ PyErr_Format(PyExc_ValueError,
|
|
1251
|
+ "Expected %d const_values but len(const_values)=%d",
|
|
1252
|
+ expt_sizes[1], values_obj_sz);
|
|
1253
|
+ return NULL;
|
|
1254
|
+ }
|
|
1255
|
+ }
|
|
1256
|
+
|
|
1257
|
+ rpn_value_t const_values[values_obj_sz];
|
|
1258
|
+ for(Py_ssize_t i = 0; i<values_obj_sz; i++)
|
|
1259
|
+ {
|
|
1260
|
+ PyObject *item = PyTuple_GET_ITEM(tmp, i);
|
|
1261
|
+ const_values[i] = PyLong_AsRpnValue_t(item);
|
|
1262
|
+ if(PyErr_Occurred())
|
|
1263
|
+ {
|
|
1264
|
+ PyErr_Format(PyExc_ValueError,
|
|
1265
|
+ "Unable to convert size_lim[%d] to unsigned int",
|
|
1266
|
+ i);
|
|
1267
|
+ return NULL;
|
|
1268
|
+ }
|
|
1269
|
+ }
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+ // Creating rif params
|
|
1273
|
+ rpn_if_param_t *rif_params;
|
|
1274
|
+
|
|
1275
|
+ if(!(rif_params = rpn_if_default_params(pos_flag, res_flag,
|
|
1276
|
+ sz_limits, const_values, stack_size)))
|
|
1277
|
+ {
|
|
1278
|
+ PyErr_SetString(PyExc_ValueError, "Unable to create parameters \
|
|
1279
|
+with given arguments");
|
|
1280
|
+ return NULL;
|
|
1281
|
+ }
|
|
1282
|
+
|
|
1283
|
+ return rif_params;
|
|
1284
|
+}
|
|
1285
|
+
|
|
1286
|
+PyObject *_rpnif_params_to_tuple(const rpn_if_param_t *_params)
|
|
1287
|
+{
|
|
1288
|
+ PyObject *res, *val;
|
|
1289
|
+ short expt_sizes[2];
|
|
1290
|
+ rpn_if_default_data_t *params;
|
|
1291
|
+ params = (rpn_if_default_data_t*)(_params->data);
|
|
1292
|
+
|
|
1293
|
+ if(rpn_if_sizes_from_flag(params->pos_flag, params->res_flag, expt_sizes) < 0)
|
|
1294
|
+ {
|
|
1295
|
+ PyErr_SetString(PyExc_RuntimeError, "Invalid internal state");
|
|
1296
|
+ return NULL;
|
|
1297
|
+ }
|
|
1298
|
+
|
|
1299
|
+ res = PyStructSequence_New(&rpnif_params_SeqDesc);
|
|
1300
|
+ if(!res)
|
|
1301
|
+ {
|
|
1302
|
+ return NULL;
|
|
1303
|
+ }
|
|
1304
|
+ val = PyLong_FromLong(_params->rpn_argc);
|
|
1305
|
+ PyStructSequence_SET_ITEM(res, 0, val);
|
|
1306
|
+
|
|
1307
|
+ val = PyLong_FromLong(params->pos_flag);
|
|
1308
|
+ PyStructSequence_SET_ITEM(res, 1, val);
|
|
1309
|
+
|
|
1310
|
+ val = PyLong_FromLong(params->res_flag);
|
|
1311
|
+ PyStructSequence_SET_ITEM(res, 2, val);
|
|
1312
|
+
|
|
1313
|
+ if(params->pos_flag == RPN_IF_POSITION_XDIM)
|
|
1314
|
+ {
|
|
1315
|
+ expt_sizes[0] = params->size_lim[0] + 1;
|
|
1316
|
+ }
|
|
1317
|
+
|
|
1318
|
+ PyObject *lim = PyTuple_New(expt_sizes[0]);
|
|
1319
|
+ if(!lim)
|
|
1320
|
+ {
|
|
1321
|
+ Py_DECREF(res);
|
|
1322
|
+ return NULL;
|
|
1323
|
+ }
|
|
1324
|
+ PyStructSequence_SET_ITEM(res, 3, lim);
|
|
1325
|
+
|
|
1326
|
+ for(Py_ssize_t i=0; i<expt_sizes[0]; i++)
|
|
1327
|
+ {
|
|
1328
|
+ val = PyLong_FromSize_t(params->size_lim[i]);
|
|
1329
|
+ PyTuple_SET_ITEM(lim, i, val);
|
|
1330
|
+ }
|
|
1331
|
+
|
|
1332
|
+ if(!params->const_val)
|
|
1333
|
+ {
|
|
1334
|
+ Py_INCREF(Py_None);
|
|
1335
|
+ PyTuple_SET_ITEM(res, 4, Py_None);
|
|
1336
|
+ }
|
|
1337
|
+ else
|
|
1338
|
+ {
|
|
1339
|
+ PyObject *values = PyTuple_New(expt_sizes[1]);
|
|
1340
|
+ if(!values)
|
|
1341
|
+ {
|
|
1342
|
+ Py_DECREF(res);
|
|
1343
|
+ return NULL;
|
|
1344
|
+ }
|
|
1345
|
+ PyStructSequence_SET_ITEM(res, 4, values);
|
|
1346
|
+ for(Py_ssize_t i=0; i<expt_sizes[1]; i++)
|
|
1347
|
+ {
|
|
1348
|
+ val = PyLong_FromRpnValue_t(params->const_val[i]);
|
|
1349
|
+ PyTuple_SET_ITEM(values, i, val);
|
|
1350
|
+ }
|
|
1351
|
+ }
|
|
1352
|
+ val = PyLong_FromUnsignedLong(_params->mem_sz * _params->value_sz);
|
|
1353
|
+ PyStructSequence_SET_ITEM(res, 5, val);
|
|
1354
|
+ return res;
|
|
1355
|
+}
|
|
1356
|
+
|
1202
|
1357
|
/**@def _ret_append(key)
|
1203
|
1358
|
* @hiderefs
|
1204
|
1359
|
* local macro */
|