Skip to content

Commit e31f138

Browse files
committed
Made GList converter get type from SWIG itself to avoid long string of type-checks.
1 parent 857ce39 commit e31f138

File tree

1 file changed

+11
-48
lines changed

1 file changed

+11
-48
lines changed

bindings/python/gnucash_core.i

+11-48
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
}
187187

188188
/* SWIG type converter for functions that take lists as arguments so that a Python list can be passed and automatically converted into a GList*. */
189-
%typemap(in) GList *, CommodityList *, SplitList *, AccountList *, LotList *, MonetaryList *, PriceList *, EntryList * {
189+
%typemap(in) GList * {
190190
$1 = NULL;
191191
/* Check if is a list */
192192
if (PyList_Check($input)) {
@@ -197,60 +197,23 @@
197197
PyObject *python_object_wrapper = PyList_GetItem($input, i);
198198
// Get the .instance attribute of the Python object, which is the raw SWIG handle.
199199
PyObject *python_object = PyObject_GetAttrString(python_object_wrapper, "instance");
200+
// Get the pointer to the actual C object.
201+
SwigPyObject *swig_object = SWIG_Python_GetSwigThis(python_object);
200202
void *c_object;
201-
// Attempt to convert the SWIG handle into a Gnucash C/C++ object and add it to the GList.
202-
if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(GncCustomer *), SWIG_POINTER_EXCEPTION) == 0) {
203-
$1 = g_list_prepend($1, (GncCustomer *) c_object);
204-
}
205-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(Account *), SWIG_POINTER_EXCEPTION) == 0) {
206-
$1 = g_list_prepend($1, (Account *) c_object);
207-
}
208-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(GNCLot *), SWIG_POINTER_EXCEPTION) == 0) {
209-
$1 = g_list_prepend($1, (GNCLot *) c_object);
210-
}
211-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(Split *), SWIG_POINTER_EXCEPTION) == 0) {
212-
$1 = g_list_prepend($1, (Split *) c_object);
213-
}
214-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(Transaction *), SWIG_POINTER_EXCEPTION) == 0) {
215-
$1 = g_list_prepend($1, (Transaction *) c_object);
216-
}
217-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(gnc_commodity *), SWIG_POINTER_EXCEPTION) == 0) {
218-
$1 = g_list_prepend($1, (gnc_commodity *) c_object);
219-
}
220-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(gnc_monetary *), SWIG_POINTER_EXCEPTION) == 0) {
221-
$1 = g_list_prepend($1, (gnc_monetary *) c_object);
222-
}
223-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(gnc_commodity_namespace *), SWIG_POINTER_EXCEPTION) == 0) {
224-
$1 = g_list_prepend($1, (gnc_commodity_namespace *) c_object);
225-
}
226-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(GNCPrice *), SWIG_POINTER_EXCEPTION) == 0) {
227-
$1 = g_list_prepend($1, (GNCPrice *) c_object);
228-
}
229-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(GncInvoice *), SWIG_POINTER_EXCEPTION) == 0) {
230-
$1 = g_list_prepend($1, (GncInvoice *) c_object);
231-
}
232-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(GncEntry *), SWIG_POINTER_EXCEPTION) == 0) {
233-
$1 = g_list_prepend($1, (GncEntry *) c_object);
234-
}
235-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(GncVendor *), SWIG_POINTER_EXCEPTION) == 0) {
236-
$1 = g_list_prepend($1, (GncVendor *) c_object);
237-
}
238-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(GncEmployee *), SWIG_POINTER_EXCEPTION) == 0) {
239-
$1 = g_list_prepend($1, (GncEmployee *) c_object);
240-
}
241-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(GncJob *), SWIG_POINTER_EXCEPTION) == 0) {
242-
$1 = g_list_prepend($1, (GncJob *) c_object);
243-
}
244-
else if (SWIG_ConvertPtr(python_object, &c_object, $descriptor(GncTaxTable *), SWIG_POINTER_EXCEPTION) == 0) {
245-
$1 = g_list_prepend($1, (GncTaxTable *) c_object);
203+
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.
204+
$1 = g_list_prepend($1, c_object);
246205
}
247206
else {
248-
PyErr_SetString(PyExc_TypeError, "list must contain object of known type with .instance attribute, see base-typemaps.i in SWIG bindings.");
207+
PyErr_SetString(PyExc_TypeError, "list must contain object of known type with .instance attribute, see gnucash_core.i in SWIG Python bindings.");
249208
g_list_free($1);
250209
return NULL;
251210
}
211+
212+
// Reverse list to preserve original order.
213+
$1 = g_list_reverse($1);
252214
}
253-
} else {
215+
}
216+
else {
254217
PyErr_SetString(PyExc_TypeError, "not a Python list, cannot convert to GList");
255218
return NULL;
256219
}

0 commit comments

Comments
 (0)