[GNC-dev] Python Binding: GncOwner fails to convert from C->Python->C

Derek Atkins derek at ihtfp.com
Sun Jan 29 14:31:07 EST 2023


Are you sure that the GncOwner* isn't a /return value/?
ISTR that many cases of GncOwner* were used so the caller could /set/ the
owner?
But it's been 20 years since I wrote that code.

-derek

On Sun, January 29, 2023 2:06 pm, john wrote:
> Yes, it does look like the in type map ignores the out typemap's tuple and
> tries to convert it into a pointer. That won't work. It should be
> something like
>
> if (! PyTuple_Ceck($input))
>     return NULL;
>
> GncOwnerType type = (GncOwnerType)PyLong_asLong(PyTuple_GetItem($input,
> 0));
> PyObject* py_instance = PyTuple_GetItem($input, 1);
> void* instance = NULL;
> GncOwner* owner = gncOwnerNew()
> switch (type)
> {
>     case GNC_OWNER_CUSTOMER:
>          if (SWIG_ConvertPtr(py_instance, &instance,
>                                          $descriptor(GncCustomer *),
>                                          SWIG_POINTER_EXCEPTION)) == 0)
>           {
>                gncOwnerInitCustomer(owner, (GncCustomer *)instance);
>                $1 = owner;
>           }
>
>     /* ... */
> }
>
> /* Something failed */
> gnc_owner_free(owner);
> PyErr_SetString( PyExc_ValueError,
>                         "Python object passed to function with GncOwner *
> argument "
>                         "couldn't be converted back to pointer of that
> type");
>  $1 = NULL;
>
> That's untested off the top of my head, obvs you need to fill in the other
> types. It should correctly make a GncOwner* to pass back to functions that
> take one.
>
> Now if you actually need the instance pointer you'll want to write a
> typemap(in) for each one that you need. It will probably be cleanest to
> extract the tuple item in python and pass that, letting the new
> %typemap(in) convert it to a C pointer.
>
> Geert, those typemaps are yours, written 11  years ago. Any comments?
>
> Regards,
> John Ralls
>
>> On Jan 29, 2023, at 5:38 AM, Steve Brown <sbrown at opensat.com> wrote:
>>
>> I get a Python GncOwner object returned to Python. It appears well
>> formed.
>>
>> owner:  <class 'gnucash.gnucash_core.GncOwner'>
>> <gnucash.gnucash_core.GncOwner object at 0x7f33d4cc3050>
>> owner_instance:  <class 'tuple'> (4, <Swig Object of type 'GncVendor *'
>> at 0x7f214b5a9500>)
>>
>> It's not clear how to easily inspect the Swig object.
>>
>> However, the object doesn't convert back to C. It's not recognized as
>> GncVendor.
>>
>> I get:
>>
>> ValueError: Python object passed to function with GncOwner * argument
>> couldn't be converted back to pointer of that type
>>
>> The out typemap for GncOwner looks correct. However, I can't see how
>> the in typemap gets the swig wrapper object from the tuple created by
>> the out typemap.
>>
>> I can recreate the problem in simple_business_create.py with the
>> attached patch.
>>
>> If there is nothing obviously wrong with the typemaps, how should I go
>> about debugging this?
>>
>> Thanks,
>> Steve
>> <getaddr.patch>_______________________________________________
>> gnucash-devel mailing list
>> gnucash-devel at gnucash.org
>> https://lists.gnucash.org/mailman/listinfo/gnucash-devel
>
> _______________________________________________
> gnucash-devel mailing list
> gnucash-devel at gnucash.org
> https://lists.gnucash.org/mailman/listinfo/gnucash-devel
>


-- 
       Derek Atkins                 617-623-3745
       derek at ihtfp.com             www.ihtfp.com
       Computer and Internet Security Consultant



More information about the gnucash-devel mailing list