#868 Python directors & dirvtable bug

open
nobody
python (259)
5
2010-05-26
2007-10-28
nitro
No

The following text is almost identical to this one:

http://thread.gmane.org/gmane.comp.programming.swig/11560

-------------------------------------

Hello guys,

there seems to be a bug in swig regarding python directors. Attached is a
minimal interface file which reproduces the problem.

Swig generates code like this:

#if defined(SWIG_PYTHON_DIRECTOR_VTABLE)
const size_t swig_method_index = 0;
const char * const swig_method_name = "my_method";
PyObject* method = swig_get_method(swig_method_index, swig_method_name);
swig::PyObject_var result = PyObject_CallFunctionObjArgs(method
,(PyObject *)obj0, NULL);
#else
swig::PyObject_var swig_method_name = PyString_FromString((char
*)"Render");
swig::PyObject_var result = PyObject_CallMethodObjArgs(swig_get_self(),
(PyObject *) swig_method_name ,(PyObject *)obj0, NULL);
#endif

In my case SWIG_PYTHON_DIRECTOR_VTABLE is defined, so only the first
branch is executed. This includes the call to swig_get_method. This
function is generated for each python director class. It tries to retrieve
the python method called "my_method" from the current object. If this
fails, because the method wasn't defined in python it triggers this line
within swig_get_method:

Swig::DirectorMethodException::raise(msg.c_str());

Unfortunately I don't think there is any way to capture this exception or
to override it somehow. My program just crashes :(. I am already using
%feature("director:except") and %exception. However, these are useless as
they don't enclose the position where the DirectorMethodException is
raised.
Notice that if SWIG_PYTHON_DIRECTOR_VTABLE is not defined (alas, running
swig without -dirvtable and/or without -O) the swig code will rise a
DirectorMethodException a bit later. By using %feature("director:except")
I can override this behaviour though. Overriding just doesn't work in the
case where SWIG_PYTHON_DIRECTOR_VTABLE is defined.
One solution I can see is to make swig_get_method return 0 instead of
throwing an exception. The calling code would look like this:

#if defined(SWIG_PYTHON_DIRECTOR_VTABLE)
const size_t swig_method_index = 0;
const char * const swig_method_name = "my_method";
PyObject* method = swig_get_method(swig_method_index, swig_method_name);
swig::PyObject_var result = method ? PyObject_CallFunctionObjArgs(method
,(PyObject *)obj0, NULL) : 0;
#else
swig::PyObject_var swig_method_name = PyString_FromString((char
*)"Render");
swig::PyObject_var result = PyObject_CallMethodObjArgs(swig_get_self(),
(PyObject *) swig_method_name ,(PyObject *)obj0, NULL);
#endif

Discussion

  • nitro

    nitro - 2007-10-28

    Interface file which reproduces the problem

     
  • Olly Betts

    Olly Betts - 2010-05-26
    • labels: --> python
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks