[GNC-dev] Python bindings: How to expose additional engine functions

John Ralls jralls at ceridwen.us
Tue Jan 24 17:37:37 EST 2023


> On Jan 24, 2023, at 2:03 PM, Steve Brown <sbrown at opensat.com> wrote:
> 
> My use case is accessing owner name and address from a transaction.
> The transaction is found by guid.
> 
> Exposing guid_from_string() was pretty straightforward as was GetLot()
> and GetFirstAPARAcctSplit().
> 
> I also need GetOwnerFromTxn or GetOwnerFromLot. However, this and many
> similar functions return the result through an argument and indicate
> success or failure by returning a boolean. It's not clear whether there
> is an general way with swig to deal with this other than adding a
> wrapper to return the result and indicate failure by returning NULL.

Yes, but SWIG makes it pretty simple, see https://www.swig.org/Doc3.0/Python.html#Python_nn46.
Note that you can still have the bool rv to test success.

> I also noticed that there is a problem with gnc-
> session.c:gnc_get_current_session() using the bindings. In the
> executable, gnc_set_current_session() is called in gnc_file.c after
> each qof_session_new(). But with the Python bindings, the swig-
> generated code calls qof_session_new(). So, current_session isn't
> initialized and the first call subtly creates a new session. I added a
> gnc_set_current_session() call to qof_session_new() and that fixed
> things for me . I don't think that's the best solution.  

Neither set_ nor get_current_session are wrapped. You should be creating a Python Session object, bindings/python/examples/simple_book.py.

Regards,
John Ralls



More information about the gnucash-devel mailing list