| 2745 | } |
| 2746 | |
| 2747 | static int |
| 2748 | einsum_sub_op_from_str(PyObject *args, PyObject **str_obj, char **subscripts, |
| 2749 | PyArrayObject **op) |
| 2750 | { |
| 2751 | int i, nop; |
| 2752 | PyObject *subscripts_str; |
| 2753 | |
| 2754 | nop = PyTuple_GET_SIZE(args) - 1; |
| 2755 | if (nop <= 0) { |
| 2756 | PyErr_SetString(PyExc_ValueError, |
| 2757 | "must specify the einstein sum subscripts string " |
| 2758 | "and at least one operand"); |
| 2759 | return -1; |
| 2760 | } |
| 2761 | else if (nop >= NPY_MAXARGS) { |
| 2762 | PyErr_SetString(PyExc_ValueError, "too many operands"); |
| 2763 | return -1; |
| 2764 | } |
| 2765 | |
| 2766 | /* Get the subscripts string */ |
| 2767 | subscripts_str = PyTuple_GET_ITEM(args, 0); |
| 2768 | if (PyUnicode_Check(subscripts_str)) { |
| 2769 | *str_obj = PyUnicode_AsASCIIString(subscripts_str); |
| 2770 | if (*str_obj == NULL) { |
| 2771 | return -1; |
| 2772 | } |
| 2773 | subscripts_str = *str_obj; |
| 2774 | } |
| 2775 | |
| 2776 | *subscripts = PyBytes_AsString(subscripts_str); |
| 2777 | if (*subscripts == NULL) { |
| 2778 | Py_XDECREF(*str_obj); |
| 2779 | *str_obj = NULL; |
| 2780 | return -1; |
| 2781 | } |
| 2782 | |
| 2783 | /* Set the operands to NULL */ |
| 2784 | for (i = 0; i < nop; ++i) { |
| 2785 | op[i] = NULL; |
| 2786 | } |
| 2787 | |
| 2788 | /* Get the operands */ |
| 2789 | for (i = 0; i < nop; ++i) { |
| 2790 | PyObject *obj = PyTuple_GET_ITEM(args, i+1); |
| 2791 | |
| 2792 | op[i] = (PyArrayObject *)PyArray_FROM_OF(obj, NPY_ARRAY_ENSUREARRAY); |
| 2793 | if (op[i] == NULL) { |
| 2794 | goto fail; |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | return nop; |
| 2799 | |
| 2800 | fail: |
| 2801 | for (i = 0; i < nop; ++i) { |
| 2802 | Py_XDECREF(op[i]); |
| 2803 | op[i] = NULL; |
| 2804 | } |