Re: [GNC-dev] Python bindings: How to expose additional engine functions
john <[email protected]>
| Newsgroups | gmane.comp.gnome.apps.gnucash.devel |
|---|---|
| Message-ID | <[email protected]> |
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 <[email protected]> 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 <[email protected]> >>> 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 <[email protected]> >>>>> 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 >>>> >>> >> > _______________________________________________ gnucash-devel mailing list [email protected] https://lists.gnucash.org/mailman/listinfo/gnucash-devel
0001-Update-current-session-so-gnc_get_current_session-wo.patch
(text/x-patch, 967 B)
From 1f7c56bcd0f7369331d79ae9ae8c5d4cc7729c4b Mon Sep 17 00:00:00 2001 From: Steve Brown <[email protected]> Date: Tue, 24 Jan 2023 07:51:36 -0500 Subject: [PATCH] Update current session so gnc_get_current_session works --- libgnucash/engine/qofsession.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libgnucash/engine/qofsession.cpp b/libgnucash/engine/qofsession.cpp index 226471241..cc9e10833 100644 --- a/libgnucash/engine/qofsession.cpp +++ b/libgnucash/engine/qofsession.cpp @@ -51,6 +51,7 @@ extern "C" #include "qof.h" #include "qofobject-p.h" +#include "gnc-session.h" static QofLogModule log_module = QOF_MOD_SESSION; } //extern 'C' @@ -149,7 +150,9 @@ qof_session_destroy (QofSession * session) QofSession * qof_session_new (QofBook* book) { - return new QofSessionImpl(book); + QofSession * session = new QofSessionImpl(book); + gnc_set_current_session(session); + return session; } void -- 2.34.1
get_session_test.patch
(text/x-patch, 1.3 KB)
diff --git a/bindings/python/example_scripts/simple_book.py b/bindings/python/example_scripts/simple_book.py
index 0059ff64e..a91023951 100644
--- a/bindings/python/example_scripts/simple_book.py
+++ b/bindings/python/example_scripts/simple_book.py
@@ -6,16 +6,27 @@
import sys
from gnucash import Session, SessionOpenMode
+from gnucash import app_utils
# We need to tell GnuCash the data format to create the new file as (xml://)
uri = "xml:///tmp/simple_book.gnucash"
print("uri:", uri)
-with Session(uri, SessionOpenMode.SESSION_NEW_STORE) as ses:
+with Session(uri, SessionOpenMode.SESSION_NEW_OVERWRITE) as ses:
book = ses.get_book()
- #Call some methods that produce output to show that Book works
book.get_root_account().SetDescription("hello, book")
+ print(ses)
+ print('book: ', book.get_root_account().GetDescription())
+ ses = app_utils.gnc_get_current_session()
+ print(ses)
+ book = ses.get_book()
+ print('book: ', book.get_root_account().GetDescription())
+ ses = app_utils.gnc_get_current_session()
+ print(ses)
+ book = ses.get_book()
+ print('book: ', book.get_root_account().GetDescription())
+ #Call some methods that produce output to show that Book works
print("Book is saved:", not book.session_not_saved())
#As long as there's no exceptions, book is automatically saved