Re: [GNC-dev] Wrapping gncOwnerGetOwnerFromTxn() for Python
John Ralls <[email protected]> Tue, 25 Apr 2023 11:37:56 -0700
| Newsgroups | gmane.comp.gnome.apps.gnucash.devel |
|---|---|
| Message-ID | <[email protected]> |
> On Apr 25, 2023, at 10:35 AM, Steve Brown <[email protected]> wrote: > > The subject function takes a GncOwner as an argument, modifies it and > returns the result through the argument. It's not clear how to create a > Swig INOUT map for a non-primitive type. So, I wrote a wrapper to > return the result in a normal way and indicate an error with a NULL > return. The wrapper allocates the owner with gncOwnerNew() and returns > a Python object of type GncOwner. > > However, I need an object of type Customer, Job,... based on the owner > type. So, on the Python side, I get the type from the instance and > create an object of the correct type such as Vendor via > Vendor(instance=instance). > > This works and I'm able to extract the vendor address and other data I > need. > > It's not clear how to free the owner object allocated by the wrapper. A > call to gncOwnerFree fails in the IN typemap because the Swig pointer > type didn't match one of the 4 alternatives. I added a > SWIG_ConvertPtr() for GncOwner, but got the same "couldn't convert" > message. It's not clear how to inspect the Swig pointer type to see why > it's complaining. > > This all seems overly complicated. Is there a better approach? > > Also, any advice on how to not leak GncOwner. You might combine the two parts into a single wrapper, maybe something like (not tested) GncCustomer* gncOwnerGetCustomerFromTxn(Transaction* txn) { GncOwner owner = { GNC_OWNER_NONE, NULL, NULL }; GncCustomer* customer = NULL; gncOwnerGetOwnerFromTxn(txn, &owner); if (owner->type == GNC_OWNER_CUSTOMER) { customer = gncOwnerGetCustomer(&owner); } return customer; } Regards, John Ralls