Re: Unable to access referenced parameter in Python callback
Ben van Basten via Swig-user <[email protected]> Tue, 21 Sep 2021 11:24:07 +0000
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <GV0P278MB0952510264A629CF5AF16BDBE1A19@GV0P278MB0952.CHEP278.PROD.OUTLOOK.COM> |
Thanks for the help! Hmm, unfortunately, my callback invocation happens in a Nuget package / DLL, so the provided example is a little bit different.
I did note that the argument in the invoked callback has a different underlying type, than when creating the same object on Python side.
def Start(self, config):
prop_test = test.Property()
type_test = prop_test.GetType() #FINE
type_test2 = config.GetType() # ERROR
When a Property (prop_test) is created on the Python side in Start(), its type is
<mds.Property; proxy of <Swig Object of type 'Property *' at 0x000001CC24DA3D80> >
Whereas the passed property has a type
<Swig Object of type 'Base::Property *' at 0x000001CC24CBE1E0>
I wonder whether this is expected, or this might lead to the underlying issue?
Regards,
Ben
From: William S Fulton <[email protected]>
Sent: Friday, September 17, 2021 9:08 PM
To: Ben van Basten <[email protected]>
Cc: [email protected]
Subject: Re: [Swig-user] Unable to access referenced parameter in Python callback
A few tweaks and below works for me:
%module(directors="1", allprotected="1") example
%feature("director") Base;
%inline %{
enum PropertyType{PT_REAL, PT_STRING};
class Property
{
public:
PropertyType GetType() const;
int GetSize() const;
};
class Base
{
public:
virtual bool Start(const Property& config) = 0; // PASSED AS REFERENCE
};
void callup(Base& b, Property& p) {
b.Start(p);
}
%}
%{
PropertyType Property::GetType() const { return PT_REAL; }
int Property::GetSize() const { return 1; }
%}
from example import *
class PyClient(Base):
def Start(self, config):
config_size = config.GetSize()
config_type = config.GetType()
print("Got {} {}".format(config_size, config_type))
return True
prop = Property()
pc = PyClient()
callup(pc, prop)
William
On Fri, 17 Sept 2021 at 13:49, Ben van Basten via Swig-user <[email protected]<mailto:[email protected]>> wrote:
Hello!
In SWIG, I've got a wrapped C++ base class that is inherited in Python. The C++ side invokes callbacks on the Python derived class, which works fine. Unfortunately, one of the parameters of the callback is passed as reference. Accessing this reference yields:
Fatal unhandled exception: SWIG director method error. Error detected when calling 'Base.Start'
test.i
%module(directors="1", allprotected="1") test
%feature("director") Base;
class Base
{
public:
virtual bool Start(const Property& config) = 0; // PASSED AS REFERENCE
};
enum PropertyType{PT_REAL, PT_STRING};
class Property
{
public:
PropertyType GetType() const;
int GetSize() const;
};
In Python, Start is correctly callback/invoked. Unfortunately, invoking GetType() or GetSize() yields the error above. Invoking functions of parameters passed as pointer is going fine.
import test
class PyClient(test.Base):
def Start(self, config):
config_size = config.GetSize() // YIELDS ERROR
config_type = config.GetType() // YIELDS ERROR
return True
I guess I need to convert the parameter Property from a reference to a pointer, but it is unclear to me how this works in SWIG.
I’m using VS2017. SWIG 4.02, Python 3.7 64bit
Any help would be really appreciated.
Ben
_______________________________________________
Swig-user mailing list
[email protected]<mailto:[email protected]>
https://lists.sourceforge.net/lists/listinfo/swig-user
_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user