|
6 | 6 | mFUNC(process_tuple, METH_VARARGS, \ |
7 | 7 | "process the items in the second argument " \ |
8 | 8 | "with the corresponding items in the first argument.") \ |
| 9 | + mFUNC(process_chunk, METH_VARARGS, \ |
| 10 | + "process the items of the chunk given as the second argument " \ |
| 11 | + "with the corresponding items in the first argument.") \ |
9 | 12 | mFUNC(int2_pack, METH_O, "PyInt to serialized, int2") \ |
10 | 13 | mFUNC(int2_unpack, METH_O, "PyInt from serialized, int2") \ |
11 | 14 | mFUNC(int4_pack, METH_O, "PyInt to serialized, int4") \ |
|
24 | 27 | mFUNC(swap_uint4_unpack, METH_O, "PyInt from swapped serialized, int4") \ |
25 | 28 |
|
26 | 29 | /* |
27 | | - * Define the swap functionality. |
| 30 | + * Define the swap functionality for those endians. |
28 | 31 | */ |
29 | 32 | #define swap2(CP) do{register char c; \ |
30 | 33 | c=CP[1];CP[1]=CP[0];CP[0]=c;\ |
@@ -387,14 +390,11 @@ swap_uint4_unpack(PyObject *self, PyObject *arg) |
387 | 390 | * calling the third object in cases of failure to generalize the exception. |
388 | 391 | */ |
389 | 392 | static PyObject * |
390 | | -process_tuple(PyObject *self, PyObject *args) |
| 393 | +_process_tuple(PyObject *procs, PyObject *tup, PyObject *fail) |
391 | 394 | { |
392 | | - PyObject *tup, *procs, *fail, *rob; |
| 395 | + PyObject *rob; |
393 | 396 | Py_ssize_t len, i; |
394 | 397 |
|
395 | | - if (!PyArg_ParseTuple(args, "OOO", &procs, &tup, &fail)) |
396 | | - return(NULL); |
397 | | - |
398 | 398 | if (!PyTuple_CheckExact(procs)) |
399 | 399 | { |
400 | 400 | PyErr_SetString( |
@@ -539,3 +539,100 @@ process_tuple(PyObject *self, PyObject *args) |
539 | 539 |
|
540 | 540 | return(rob); |
541 | 541 | } |
| 542 | + |
| 543 | +/* |
| 544 | + * process the tuple with the associated callables while |
| 545 | + * calling the third object in cases of failure to generalize the exception. |
| 546 | + */ |
| 547 | +static PyObject * |
| 548 | +process_tuple(PyObject *self, PyObject *args) |
| 549 | +{ |
| 550 | + PyObject *tup, *procs, *fail; |
| 551 | + |
| 552 | + if (!PyArg_ParseTuple(args, "OOO", &procs, &tup, &fail)) |
| 553 | + return(NULL); |
| 554 | + |
| 555 | + return(_process_tuple(procs, tup, fail)); |
| 556 | +} |
| 557 | + |
| 558 | +static PyObject * |
| 559 | +_process_chunk_new_list(PyObject *procs, PyObject *tupc, PyObject *fail) |
| 560 | +{ |
| 561 | + PyObject *rob; |
| 562 | + Py_ssize_t i, len; |
| 563 | + |
| 564 | + rob = PyObject_CallFunctionObjArgs((PyObject *) &PyList_Type, tupc, NULL); |
| 565 | + if (rob == NULL) |
| 566 | + return(NULL); |
| 567 | + len = PyList_Size(rob); |
| 568 | + |
| 569 | + for (i = 0; i < len; ++i) |
| 570 | + { |
| 571 | + PyObject *tup, *r; |
| 572 | + /* |
| 573 | + * If it's Py_None, that means it's NULL. No processing necessary. |
| 574 | + */ |
| 575 | + tup = PyList_GetItem(rob, i); |
| 576 | + r = _process_tuple(procs, tup, fail); |
| 577 | + if (r == NULL) |
| 578 | + { |
| 579 | + Py_DECREF(rob); |
| 580 | + return(NULL); |
| 581 | + } |
| 582 | + PyList_SetItem(rob, i, r); |
| 583 | + } |
| 584 | + |
| 585 | + return(rob); |
| 586 | +} |
| 587 | + |
| 588 | +static PyObject * |
| 589 | +_process_chunk_from_list(PyObject *procs, PyObject *tupc, PyObject *fail) |
| 590 | +{ |
| 591 | + PyObject *rob; |
| 592 | + Py_ssize_t i, len; |
| 593 | + |
| 594 | + len = PyList_GET_SIZE(tupc); |
| 595 | + rob = PyList_New(len); |
| 596 | + if (rob == NULL) |
| 597 | + return(NULL); |
| 598 | + |
| 599 | + for (i = 0; i < len; ++i) |
| 600 | + { |
| 601 | + PyObject *tup, *r; |
| 602 | + /* |
| 603 | + * If it's Py_None, that means it's NULL. No processing necessary. |
| 604 | + */ |
| 605 | + tup = PyList_GET_ITEM(tupc, i); |
| 606 | + r = _process_tuple(procs, tup, fail); |
| 607 | + if (r == NULL) |
| 608 | + { |
| 609 | + Py_DECREF(rob); |
| 610 | + return(NULL); |
| 611 | + } |
| 612 | + PyList_SET_ITEM(rob, i, r); |
| 613 | + } |
| 614 | + |
| 615 | + return(rob); |
| 616 | +} |
| 617 | + |
| 618 | +/* |
| 619 | + * process the chunk of tuples with the associated callables while |
| 620 | + * calling the third object in cases of failure to generalize the exception. |
| 621 | + */ |
| 622 | +static PyObject * |
| 623 | +process_chunk(PyObject *self, PyObject *args) |
| 624 | +{ |
| 625 | + PyObject *tupc, *procs, *fail; |
| 626 | + |
| 627 | + if (!PyArg_ParseTuple(args, "OOO", &procs, &tupc, &fail)) |
| 628 | + return(NULL); |
| 629 | + |
| 630 | + if (PyList_Check(tupc)) |
| 631 | + { |
| 632 | + return(_process_chunk_from_list(procs, tupc, fail)); |
| 633 | + } |
| 634 | + else |
| 635 | + { |
| 636 | + return(_process_chunk_new_list(procs, tupc, fail)); |
| 637 | + } |
| 638 | +} |
0 commit comments