Unable to access referenced parameter in Python callback

Ben van Basten via Swig-user <[email protected]> Fri, 17 Sep 2021 12:34:06 +0000
Newsgroups gmane.comp.programming.swig
Message-ID <ZR0P278MB0943DFCE5F50311B3C7E80CCE1DD9@ZR0P278MB0943.CHEP278.PROD.OUTLOOK.COM>
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