[GNC-dev] Python bindings: How to expose additional engine functions
john
jralls at ceridwen.us
Wed Jan 25 23:58:23 EST 2023
You might need to precede your gncOwnerGetOwnerFromTxn declaration with
%ignore gncOwnerGetOwnerFromTxn
outside of any %{…%} block. If you don't SWIG will create a default decl from
the #include and ignore yours.
If gnc_get_current_session is returning nullptr in what you think is a running
instance of GnuCash I'd guess that it has to do with connecting the python
interpreter. You might try it in the python console; that did work the last time
I tried it.
Regards,
John Ralls
> On Jan 25, 2023, at 12:13 PM, Steve Brown <sbrown at opensat.com> wrote:
>
> Hi John,
>
> Thanks for the swig link. That's exactly what I need.
>
> I added the typemaps.i include and the following to gnucash_core.i
>
> gboolean gncOwnerGetOwnerFromTxn(Transaction *INPUT, GncOwner *OUTPUT);
>
> The *OUTPUT was ignored and arg2 is processed according the
> GncOwner(in) typemap.
>
> OTOH:
>
> gboolean gncOwnerGetOwnerFromTxn(Transaction *INPUT, int *OUTPUT);
>
> The value of arg2 after the call is appended to the return as a tuple.
> That seems correct.
>
> Any idea?
>
> ===
>
> The issue with gnc_get_current_session() is very minor.
>
> I do use a context manager like the example. However, if I call
> gnc_get_current_session(), it doesn't know that a session already
> exists and creates a new one. That wasn't the behavior I expected.
>
> On the surface, it seems unnecessary to call that function at all.
> However, my environment is a plugin with an embedded Python
> interpreter. Calls to gnc_get_current_session() are useful and work
> just fine. The Python scripts are easier to debug standalone. As this
> is unlikely to be an issue for anybody, but me, I can work around it.
>
> Thanks,
> Steve
>
> On Tue, 2023-01-24 at 14:37 -0800, John Ralls wrote:
>>
>>
>>> 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