|
@@ -22,6 +22,11 @@ PyMethodDef RPNExpr_methods[] = {
|
22
|
22
|
{"eval", (PyCFunction)rpnexpr_eval, METH_FASTCALL, "Evaluate an expression"},
|
23
|
23
|
{"reset_stack", (PyCFunction)rpnexpr_reset_stack, METH_NOARGS,
|
24
|
24
|
"Reset stack memory storage (set all items to 0)"},
|
|
25
|
+ {"__getstate__", (PyCFunction)rpnexpr_getstate, METH_NOARGS,
|
|
26
|
+ "Pickling method. Return a bytes repr of tokenized expression \
|
|
27
|
+and the stack state."},
|
|
28
|
+ {"__setstate__", (PyCFunction)rpnexpr_setstate, METH_O,
|
|
29
|
+ "Unpickling method"},
|
25
|
30
|
{NULL} //Sentinel
|
26
|
31
|
};
|
27
|
32
|
|
|
@@ -31,7 +36,7 @@ PyMemberDef RPNExpr_members[] = {
|
31
|
36
|
|
32
|
37
|
PyTypeObject RPNExprType = {
|
33
|
38
|
PyVarObject_HEAD_INIT(NULL, 0)
|
34
|
|
- "rpn.RPNExpr", /* tp_name */
|
|
39
|
+ "pyrpn.RPNExpr", /* tp_name */
|
35
|
40
|
sizeof(PyRPNExpr_t), /* tp_basicsize */
|
36
|
41
|
0, /* tp_itemsize */
|
37
|
42
|
(destructor)rpnexpr_del, /* tp_dealloc */
|
|
@@ -149,13 +154,7 @@ int rpnexpr_init(PyObject *self, PyObject *args, PyObject *kwds)
|
149
|
154
|
return -1;
|
150
|
155
|
}
|
151
|
156
|
|
152
|
|
- if(!stack_size)
|
153
|
|
- {
|
154
|
|
- expr_self->args = NULL;
|
155
|
|
- return 0;
|
156
|
|
- }
|
157
|
|
-
|
158
|
|
- expr_self->args = malloc(sizeof(unsigned long) * args_count);
|
|
157
|
+ expr_self->args = malloc(sizeof(rpn_value_t) * args_count);
|
159
|
158
|
if(!expr_self->args)
|
160
|
159
|
{
|
161
|
160
|
snprintf(err_str, 256,
|
|
@@ -191,6 +190,226 @@ void rpnexpr_del(PyObject *self)
|
191
|
190
|
}
|
192
|
191
|
}
|
193
|
192
|
|
|
193
|
+
|
|
194
|
+PyObject* rpnexpr_getstate(PyObject *self, PyObject *noargs)
|
|
195
|
+{
|
|
196
|
+ PyObject *res, *part;
|
|
197
|
+ PyRPNExpr_state_t resbuf;
|
|
198
|
+ PyRPNExpr_t *expr_self;
|
|
199
|
+ size_t total_sz;
|
|
200
|
+ char err_str[128];
|
|
201
|
+
|
|
202
|
+ expr_self = (PyRPNExpr_t*)self;
|
|
203
|
+
|
|
204
|
+ if(expr_self->rpn->state != RPN_READY)
|
|
205
|
+ {
|
|
206
|
+ snprintf(err_str, 128,
|
|
207
|
+ "RPNExpr.__getstate__() instance in bad state : %s",
|
|
208
|
+ expr_self->rpn->state==RPN_ERROR?"error":"init");
|
|
209
|
+ PyErr_SetString(PyExc_ValueError, err_str);
|
|
210
|
+ return NULL;
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ total_sz = sizeof(PyRPNExpr_state_t);
|
|
214
|
+ total_sz += expr_self->rpn->toks.tokens_sz * sizeof(rpn_token_t);
|
|
215
|
+ total_sz += expr_self->rpn->stack_sz * sizeof(rpn_value_t);
|
|
216
|
+
|
|
217
|
+ resbuf.total_sz = total_sz;
|
|
218
|
+ resbuf.argc = expr_self->rpn->args_count;
|
|
219
|
+ resbuf.stack_sz = expr_self->rpn->stack_sz;
|
|
220
|
+ resbuf.token_sz = expr_self->rpn->toks.tokens_sz;
|
|
221
|
+
|
|
222
|
+ if(!(res = PyBytes_FromStringAndSize((char*)&resbuf, sizeof(resbuf))))
|
|
223
|
+ {
|
|
224
|
+ return NULL;
|
|
225
|
+ }
|
|
226
|
+ if(resbuf.stack_sz)
|
|
227
|
+ {
|
|
228
|
+ if(!(part=PyBytes_FromStringAndSize(
|
|
229
|
+ (char*)expr_self->rpn->stack,
|
|
230
|
+ sizeof(rpn_value_t) * resbuf.stack_sz)))
|
|
231
|
+ {
|
|
232
|
+ return NULL;
|
|
233
|
+ }
|
|
234
|
+ PyBytes_ConcatAndDel(&res, part);
|
|
235
|
+ if(!res)
|
|
236
|
+ {
|
|
237
|
+ return NULL;
|
|
238
|
+ }
|
|
239
|
+ }
|
|
240
|
+ if(resbuf.token_sz)
|
|
241
|
+ {
|
|
242
|
+ if(!(part=PyBytes_FromStringAndSize(
|
|
243
|
+ (char*)expr_self->rpn->toks.tokens,
|
|
244
|
+ sizeof(rpn_token_t) * resbuf.token_sz)))
|
|
245
|
+ {
|
|
246
|
+ return NULL;
|
|
247
|
+ }
|
|
248
|
+ PyBytes_ConcatAndDel(&res, part);
|
|
249
|
+ if(!res)
|
|
250
|
+ {
|
|
251
|
+ return NULL;
|
|
252
|
+ }
|
|
253
|
+ }
|
|
254
|
+ return res;
|
|
255
|
+}
|
|
256
|
+
|
|
257
|
+PyObject* rpnexpr_setstate(PyObject *self, PyObject *state_bytes)
|
|
258
|
+{
|
|
259
|
+ PyObject *tmp, *tmp2;
|
|
260
|
+ PyRPNExpr_state_t *state;
|
|
261
|
+ PyRPNExpr_t *expr_self;
|
|
262
|
+ rpn_value_t *stack;
|
|
263
|
+ rpn_tokenized_t toks;
|
|
264
|
+ const char *data;
|
|
265
|
+ size_t bsize, csize;
|
|
266
|
+ int err;
|
|
267
|
+ char err_str[256];
|
|
268
|
+ size_t i;
|
|
269
|
+
|
|
270
|
+ expr_self = (PyRPNExpr_t*)self;
|
|
271
|
+
|
|
272
|
+ if(!PyBytes_Check(state_bytes)) /* Arg check */
|
|
273
|
+ {
|
|
274
|
+ tmp2 = NULL;
|
|
275
|
+ tmp = PyObject_Type(state_bytes);
|
|
276
|
+ if(tmp)
|
|
277
|
+ {
|
|
278
|
+ tmp2 = PyObject_Str(tmp);
|
|
279
|
+ }
|
|
280
|
+ if(tmp2)
|
|
281
|
+ {
|
|
282
|
+ snprintf(err_str, 128,
|
|
283
|
+ "RPNExpr.__setstate__() expected a bytes as \
|
|
284
|
+argument but %s found",
|
|
285
|
+ PyUnicode_AsUTF8(tmp2));
|
|
286
|
+ PyErr_SetString(PyExc_ValueError, err_str);
|
|
287
|
+ }
|
|
288
|
+ else
|
|
289
|
+ {
|
|
290
|
+ PyErr_SetString(PyExc_RuntimeError,
|
|
291
|
+ "Failing to fetch arguments type will \
|
|
292
|
+generating exception message !");
|
|
293
|
+ }
|
|
294
|
+ return NULL;
|
|
295
|
+ }
|
|
296
|
+ if(expr_self->rpn || expr_self->args) /* checking instance state */
|
|
297
|
+ {
|
|
298
|
+ PyErr_SetString(PyExc_ValueError,
|
|
299
|
+ "RPNExpr.__getstate__() instance in bad state : \
|
|
300
|
+should not be initialized");
|
|
301
|
+ return NULL;
|
|
302
|
+ }
|
|
303
|
+
|
|
304
|
+ /* Checking data size */
|
|
305
|
+ bsize = PyBytes_GET_SIZE(state_bytes);
|
|
306
|
+ data = PyBytes_AS_STRING(state_bytes);
|
|
307
|
+ state = (PyRPNExpr_state_t*)data;
|
|
308
|
+
|
|
309
|
+ if(bsize < sizeof(size_t))
|
|
310
|
+ {
|
|
311
|
+ PyErr_SetString(PyExc_ValueError, "Invalid argument");
|
|
312
|
+ }
|
|
313
|
+ if(bsize != state->total_sz)
|
|
314
|
+ {
|
|
315
|
+ snprintf(err_str, 128,
|
|
316
|
+ "RPNExpr.__setstate__() error : expected state to \
|
|
317
|
+contains %ld bytes but %ld found",
|
|
318
|
+ state->total_sz, bsize);
|
|
319
|
+ PyErr_SetString(PyExc_ValueError, err_str);
|
|
320
|
+ }
|
|
321
|
+
|
|
322
|
+ /* Checking data "integrity" */
|
|
323
|
+ csize = sizeof(PyRPNExpr_state_t) +
|
|
324
|
+ state->token_sz * sizeof(rpn_token_t) +
|
|
325
|
+ state->stack_sz * sizeof(rpn_value_t);
|
|
326
|
+ if(state->total_sz != csize)
|
|
327
|
+ {
|
|
328
|
+ snprintf(err_str, 128,
|
|
329
|
+ "RPNExpr.__setstate__() error : should have %ld as \
|
|
330
|
+total size but %ld found",
|
|
331
|
+ csize, state->total_sz);
|
|
332
|
+ PyErr_SetString(PyExc_ValueError, err_str);
|
|
333
|
+ return NULL;
|
|
334
|
+ }
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+ /* Alloc and init rpn expression & args buffer*/
|
|
338
|
+ if(!(expr_self->rpn = malloc(sizeof(rpn_expr_t))))
|
|
339
|
+ {
|
|
340
|
+ err = errno;
|
|
341
|
+ snprintf(err_str, 128,
|
|
342
|
+ "RPNExpr.__setstate__() failed to allocate memory for \
|
|
343
|
+rpn_expr_t : %s",
|
|
344
|
+ strerror(err));
|
|
345
|
+ PyErr_SetString(PyExc_MemoryError, err_str);
|
|
346
|
+ return NULL;
|
|
347
|
+ }
|
|
348
|
+ bzero(expr_self->rpn, sizeof(rpn_expr_t));
|
|
349
|
+ if(!(expr_self->args = malloc(sizeof(rpn_value_t)*state->argc)))
|
|
350
|
+ {
|
|
351
|
+ err = errno;
|
|
352
|
+ snprintf(err_str, 128,
|
|
353
|
+ "RPNExpr.__setstate__() failed to allocate memory for \
|
|
354
|
+args buffer : %s",
|
|
355
|
+ strerror(err));
|
|
356
|
+ PyErr_SetString(PyExc_MemoryError, err_str);
|
|
357
|
+ return NULL;
|
|
358
|
+ }
|
|
359
|
+
|
|
360
|
+ if(rpn_expr_init(expr_self->rpn, state->stack_sz, state->argc) < 0)
|
|
361
|
+ {
|
|
362
|
+ snprintf(err_str, 256,
|
|
363
|
+ "RPNExpr.__setstate__() failed to init RPN expression \
|
|
364
|
+: %s",
|
|
365
|
+ expr_self->rpn->err_reason);
|
|
366
|
+ PyErr_SetString(PyExc_RuntimeError, err_str);
|
|
367
|
+ goto free_err;
|
|
368
|
+ }
|
|
369
|
+
|
|
370
|
+ /* restore stack & untokenize expression */
|
|
371
|
+ stack = (rpn_value_t*)(state+1);
|
|
372
|
+ memcpy(expr_self->rpn->stack, stack,
|
|
373
|
+ sizeof(rpn_value_t)*state->stack_sz);
|
|
374
|
+
|
|
375
|
+ toks.argc = state->argc;
|
|
376
|
+ toks.tokens_sz = state->token_sz;
|
|
377
|
+ toks.tokens = malloc(sizeof(rpn_token_t)*state->token_sz);
|
|
378
|
+ if(!toks.tokens)
|
|
379
|
+ {
|
|
380
|
+ snprintf(err_str, 128,
|
|
381
|
+ "RPNExpr.__setstate__() failed to allocate tokens \
|
|
382
|
+: %s",
|
|
383
|
+ strerror(errno));
|
|
384
|
+ PyErr_SetString(PyExc_RuntimeError, err_str);
|
|
385
|
+ goto close_err;
|
|
386
|
+ }
|
|
387
|
+ memcpy(toks.tokens, (rpn_token_t*)(stack+state->stack_sz),
|
|
388
|
+ sizeof(rpn_token_t)*state->token_sz);
|
|
389
|
+
|
|
390
|
+ for(i=0; i<toks.tokens_sz;i++)
|
|
391
|
+ {
|
|
392
|
+ // restore op pointers
|
|
393
|
+ toks.tokens[i].op = &(rpn_ops[toks.tokens[i].op_n]);
|
|
394
|
+ }
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+ if(rpn_expr_untokenize(expr_self->rpn, &toks, 0) < 0)
|
|
398
|
+ {
|
|
399
|
+ snprintf(err_str, 256,
|
|
400
|
+ "RPNExpr.__setstate__() unable to untokenize : %s",
|
|
401
|
+ expr_self->rpn->err_reason);
|
|
402
|
+ PyErr_SetString(PyExc_RuntimeError, err_str);
|
|
403
|
+ goto close_err;
|
|
404
|
+ }
|
|
405
|
+
|
|
406
|
+ Py_RETURN_NONE;
|
|
407
|
+ close_err:
|
|
408
|
+ rpn_expr_close(expr_self->rpn);
|
|
409
|
+ free_err:
|
|
410
|
+ return NULL;
|
|
411
|
+}
|
|
412
|
+
|
194
|
413
|
PyObject* rpnexpr_eval(PyObject* self, PyObject** argv, Py_ssize_t argc)
|
195
|
414
|
{
|
196
|
415
|
PyRPNExpr_t *expr_self;
|