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

john <[email protected]>
Newsgroups gmane.comp.gnome.apps.gnucash.devel
Message-ID <[email protected]>
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 <[email protected]> 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
> [email protected]
> https://lists.gnucash.org/mailman/listinfo/gnucash-devel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.