Passing C++ class instances to embedded Python script

Hello!

I am new with wxPython and have some issues with passing variables of my custom type as argument to Python script.

I successfully built wxPython from sources with my custom type(i.e. I have wxPython + c_customStruct.py/.pyd, embedded sample works)

class wxCustomStruct
{
public:
int intValue;
bool boolValue;
wxString strValue;
wxSize sizeValue;

wxCustomStruct();

void Init();

int GetIntValue() const;
void SetIntValue(int value);

wxSize GetSizeValue() const;
void SetSizeValue(wxSize const &size);

bool GetBoolValue() const;
void SetBoolValue(bool value);

wxString GetStrValue() const;
void SetStrValue(wxString const &value);

wxString ToString() const;

};

But I have two issues:

  1. When I try to run the following python script with interpreter python.exe
    import wx
    import wx.c_customStruct

I get the following error:
Traceback (most recent call last):
File “wxPy.py”, line 3, in
import wx.c_customStruct
SystemError: dynamic module not initialized properly

  1. I don’t understand how can I create an instance of my custom class in C++ and pass it as argument to call of python-function?

Please, give me advice, how to overcome these issues.

Sincerely, Alexei.

Hello!
A little update: the first problem with “SystemError: dynamic module not initialized properly” is solved.
setup.py script before:
if BUILD_CUSTOMSTRUCT:
msg(‘Preparing CustomStruct…’)
location = ‘contrib/CustomStruct’
swig_sources = run_swig([‘c_customStruct.i’], location, GENDIR, PKGDIR,
USE_SWIG, swig_force, swig_args, swig_deps)
ext = Extension(‘c_customStruct’, [ ‘%s/CustomStruct.cpp’ % location ] + swig_sources,
include_dirs = includes + [ location ] + CONTRIBS_INC,
define_macros = defines,
library_dirs = libdirs,
libraries = libs,
extra_compile_args = cflags,
extra_link_args = lflags,
)
wxpExtensions.append(ext)
setup.py script after:
if BUILD_CUSTOMSTRUCT:
msg(‘Preparing CustomStruct…’)
location = ‘contrib/CustomStruct’
swig_sources = run_swig([‘c_customStruct.i’], location, GENDIR, PKGDIR,
USE_SWIG, swig_force, swig_args, swig_deps)
ext = Extension(’_c_customStruct’, [ ‘%s/CustomStruct.cpp’ % location ] + swig_sources,

                include_dirs =  includes + [ location ] + CONTRIBS_INC,
                define_macros = defines,

                library_dirs = libdirs,
                libraries = libs,

                extra_compile_args = cflags,
                extra_link_args = lflags,
                )
wxpExtensions.append(ext)

statujaleha@gmail.com wrote:

2. I don't understand how can I create an instance of my custom class in
C++ and pass it as argument to call of python-function?

Create a wrapped function that either creates and returns a new instance of the C++ class, or one that returns an existing instance if that is more appropriate. The SWIG wrapper for that function will take care of creating a Python proxy object to wrap around the returned C++ instance. You can then pass that object to whatever code you need to in Python, and when they call its methods the swigged code will be called again to reflect the call to the C++ methods.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org