Re: [GNC-dev] Python Binding: GncOwner fails to convert from C->Python->C
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, , > $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 wrote: >> >> I get a Python GncOwner object returned to Python. It appears well >> formed. >> >> owner: >> >> owner_instance: (4, > 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 >> ___ >> gnucash-devel mailing list >> gnucash-devel@gnucash.org >> https://lists.gnucash.org/mailman/listinfo/gnucash-devel > > ___ > gnucash-devel mailing list > gnucash-devel@gnucash.org > https://lists.gnucash.org/mailman/listinfo/gnucash-devel > -- Derek Atkins 617-623-3745 de...@ihtfp.com www.ihtfp.com Computer and Internet Security Consultant ___ gnucash-devel mailing list gnucash-devel@gnucash.org https://lists.gnucash.org/mailman/listinfo/gnucash-devel
Re: [GNC-dev] Python Binding: GncOwner fails to convert from C->Python->C
Partially answering my own question. It doesn't look like the GncOwner out typemap is quite right. The return should be an object of type Customer, Job, Employee or Vendor as suggested by the decorate at gnucash_business.py:176. I wrote a function in my script based on that decorate that converted the return from GetOwner to one of the above. That solved my problem. Now, GetAddr works and I can access the addresses. I'm not sure how to change the typemap or what else would have to be changed as a consequence. But, I hope the above is helpful to that end. Steve On Sun, 2023-01-29 at 08:38 -0500, Steve Brown wrote: > I get a Python GncOwner object returned to Python. It appears well > formed. > > owner: > > owner_instance: (4, *' 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 > ___ > gnucash-devel mailing list > gnucash-devel@gnucash.org > https://lists.gnucash.org/mailman/listinfo/gnucash-devel ___ gnucash-devel mailing list gnucash-devel@gnucash.org https://lists.gnucash.org/mailman/listinfo/gnucash-devel
Re: [GNC-dev] Python Binding: GncOwner fails to convert from C->Python->C
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, , $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 wrote: > > I get a Python GncOwner object returned to Python. It appears well > formed. > > owner: > > owner_instance: (4, 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 > ___ > gnucash-devel mailing list > gnucash-devel@gnucash.org > https://lists.gnucash.org/mailman/listinfo/gnucash-devel ___ gnucash-devel mailing list gnucash-devel@gnucash.org https://lists.gnucash.org/mailman/listinfo/gnucash-devel