Skip to content

Added SWIG type translator for C functions that take GList* arguments #2066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions bindings/python/gnucash_core.i
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,40 @@
}
}

/* SWIG type converter for functions that take lists as arguments so that a Python list can be passed and automatically converted into a GList*. */
%typemap(in) GList * {
$1 = NULL;
/* Check if is a list */
if (PyList_Check($input)) {
int i;
int size = PyList_Size($input);
for (i = size - 1; i >= 0; i--) {
// Get the high-level Python object from bindings/python/gnucash_core.py.
PyObject *python_object_wrapper = PyList_GetItem($input, i);
// Get the .instance attribute of the Python object, which is the raw SWIG handle.
PyObject *python_object = PyObject_GetAttrString(python_object_wrapper, "instance");
// Get the pointer to the actual C object.
SwigPyObject *swig_object = SWIG_Python_GetSwigThis(python_object);
void *c_object;
if (SWIG_ConvertPtr(python_object, &c_object, swig_object->ty, SWIG_POINTER_EXCEPTION) == 0) { // Convert to whatever type Python says it is. Unfortunately do not have a way to figure out what type we should be converting to, since many Gnucash C functions take a generic GList.
$1 = g_list_prepend($1, c_object);
}
else {
PyErr_SetString(PyExc_TypeError, "list must contain object of known type with .instance attribute, see gnucash_core.i in SWIG Python bindings.");
g_list_free($1);
return NULL;
}

// Reverse list to preserve original order.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed. The prepends start from the end of the list. Plus, the g_list_reverse would typically be outside rather than inside the for loop.

$1 = g_list_reverse($1);
}
}
else {
PyErr_SetString(PyExc_TypeError, "not a Python list, cannot convert to GList");
return NULL;
}
}

%typemap(freearg) GncOwner * {
gncOwnerFree($1);
}
Expand Down
11 changes: 11 additions & 0 deletions bindings/python/tests/test_business.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,16 @@ def test_invoice_transaction(self):
invoice_from_transaction = posted_transaction.GetInvoiceFromTxn()
self.assertTrue( invoice_from_transaction != None and invoice_from_transaction.GetID() == self.invoice.GetID() )

def test_invoice_payment(self):
"""
Test that you can pay an invoice into a transfer account (this also tests conversion of Python lists to GList).
"""
self.receivable.RecomputeBalance()
self.assertTrue( int(self.receivable.GetBalance().to_double()) == 100 )
lots = [self.invoice.GetPostedLot()]
self.customer.ApplyPayment(None, lots, self.receivable, self.bank, GncNumeric(100), GncNumeric(1), self.invoice.GetDatePosted(), "Paid into bank account.", "1234", False)
self.receivable.RecomputeBalance()
self.assertTrue( self.receivable.GetBalance().to_fraction().numerator == 0 )

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions gnucash/gnome/dialog-sx-since-last-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <config.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <stdint.h>

#include "dialog-utils.h"
#include "gnc-sx-instance-model.h"
Expand Down
Loading