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

john jralls at ceridwen.us
Fri Jan 27 12:15:47 EST 2023


You forgot to copy the list.

If you can't get the direct type maps to work then writing a wrapper seems a reasonable thing to do.

Regards,
John Ralls


> On Jan 27, 2023, at 2:46 AM, Steve Brown <sbrown at opensat.com> wrote:
> 
> Hi John,
> 
> On Wed, 2023-01-25 at 20:58 -0800, john wrote:
>> 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.
> 
> That's what I thought too. So, I commented out the declaration in
> gncOwner.h. Didn't make any difference. I tested with this.
> 
> swig -python -I.. -I../../libgnucash/engine -I../../common -I/usr/include/glib-2.0 -v gnucash_core.i
> 
> 1. After reading chapter 10.3, only one typemap gets chosen for an
> argument or return. I don't totally understand the precedence rules,
> but you only get one and *OUTPUT has a typemap. So, does GncOwner.
> 
> 2. It also appears you have to write their own OUTPUT typemap for any
> user-defined type, even a type with no conflicting typemap. The
> INOUT/OUTPUT/INOUT typemaps are in inoutlist.swg. I don't see where
> that restriction is implemented, but empirically, it's there. 
> 
> I'm inclined o just write some swig-friendly C functions that call the
> current ones that return results through an argument.
> 
>> If qofsession.cp 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.
> 
> I think my explanation was lacking. The get_current_session works as
> expected in the embedded Python. I need it to get the current session.
> It's in my standalone test environment where I'm testing a script that
> will ultimately run under the embedded interpreter that the problem
> appeared. It's unlikely to trouble anybody but me.
> 
> In case anybody else runs into this, attached is a patch to simple_book
> that illustrates the problem. Also, attached is the patch to
> qofsession.cpp that fixed it for me. 
> 
> Thanks again for your help,
> 
> Steve
> 
>> 
>> 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
>>>> 
>>> 
>> 
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Update-current-session-so-gnc_get_current_session-wo.patch
Type: text/x-patch
Size: 967 bytes
Desc: not available
URL: <http://lists.gnucash.org/pipermail/gnucash-devel/attachments/20230127/9e36d65a/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: get_session_test.patch
Type: text/x-patch
Size: 1366 bytes
Desc: not available
URL: <http://lists.gnucash.org/pipermail/gnucash-devel/attachments/20230127/9e36d65a/attachment-0001.bin>


More information about the gnucash-devel mailing list