Re: [GNC-dev] Python-Bindings: issue when C expects GList argument

Matteo Lasagni <[email protected]>
Newsgroups gmane.comp.gnome.apps.gnucash.devel
Message-ID <CABa9PpTh590B7XGm5GSNZ=-2eu+rbZB7-OXNzjqnESpMiHEmaA@mail.gmail.com>
Thank you, John!

I fixed it by adding the following into base-typemaps.i:

%typemap(in) GList * {
    $1 = NULL;
    /* Check if is a list */
    if (PyList_Check($input)) {
        int i;
        int size = PyList_Size($input);
        for (i = size-1; i >= 0; i--) {
            PyObject *o = PyList_GetItem($input, i);
            $1 = g_list_prepend($1, o);
        }
    } else {
        PyErr_SetString(PyExc_TypeError, "not a list");
        return NULL;
    }
}

I had overlooked the swig definition in engine.i, where there is a typemap
(in) for GList, but then it is cleared right afterwards...
I am not (yet) very familiar with swig :-)

I have also noticed that CreatePaymentLotSecs does not work out of the box,
as it expects **Transaction while only *Transaction is type mapped.

I will test these changes then I will figure out how to share them with the
developers.


Matteo


On Tue, 10 May 2022 at 02:32, John Ralls <[email protected]> wrote:

>
>
> > On May 9, 2022, at 2:17 PM, Matteo Lasagni <[email protected]> wrote:
> >
> > Hi,
> > I am writing a python script to automatically associate a set of
> > invoices to a single transaction.
> > To this end, I am trying to use the function AutoApplyPaymentsWithLots
> > with no success:
> >
> >
> > lots = [lot1, lot2, lotn, ..]
> > owner.AutoApplyPaymentsWithLots(lots)
> >
> > This causes the following error:
> >
> > TypeError: in method 'gncOwnerAutoApplyPaymentsWithLots', argument 2 of
> > type 'GList *'
> >
> > I have tried different solutions from passing different types of python
> > objects, to updating the swig typemap and also to modifying the
> > auto-generated gnucash_core.c...nothing worked out so far.
> > I could write an additional c-wrapper, but I am sure there is a more
> > general solution.
>
> The fundamental problem is that there's only a Python out typemap for
> GList, that is one that converts from a GList* to a python list. Lacking an
> in typemap the gncOwner.AutoApplyPaymentsWithLots python wrapper expects an
> already constructed and Swig-wrapped GList*.
>
> Regards,
> John Ralls
>
>

-- 

-------------
Matteo Lasagni


*"You can't connect the dots looking forward,you can only connect them
looking backwards"* [Steve Jobs]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.