#957 %extend overridden method not inherited

open
nobody
python (259)
5
2012-12-21
2008-10-16
No

When using %extend to "reimplement" a virtual class method in class C, this reimplemented method is not available in subclass D of C but instead behaves as the original method.

E.g. (see attached tarball for these files)

=== classes.h ===

class C
{
public:
virtual void return_float(float &f) { f = 123; }
};

class D : public C
{
public:
virtual void return_float(float &f) { f = 456; }
};

=== test.i ===

%module(directors="1") test;

%{
#include "classes.h"
%}

%include "classes.h";

%extend C
{
// Pythonic way of returning a float
PyObject* return_float()
{
float f;
$self->return_float(f);
return Py_BuildValue("f", f);
}
};

When run through swig -c++ -python and GCC 4.1.2 the following then happens:

Python 2.5.2 (r252:60911, Oct 16 2008, 21:18:49)
[GCC 4.1.2 (Gentoo 4.1.2 p1.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> c = test.C()
>>> c.return_float()
123.0
>>> d = test.D()
>>> d.return_float()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 82, in return_float
def return_float(*args): return _test.D_return_float(*args)
TypeError: D_return_float() takes exactly 2 arguments (1 given)
>>>

This used to work (at least in 1.3.29)...

Discussion

  • Paul E.C. Melis

    Paul E.C. Melis - 2008-10-16

    Test case

     
  • Paul E.C. Melis

    Paul E.C. Melis - 2008-10-16

    Forgot to mention that this is with 1.3.36

     

Log in to post a comment.