Re: Unable to access referenced parameter in Python callback
William S Fulton <[email protected]> Fri, 17 Sep 2021 20:07:33 +0100
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CANGqftA=aeDOB=zypW2CHN4_3B=aWNx7_GeffQsUMtSe5qW=BQ@mail.gmail.com> |
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]> 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]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>
_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user