When SWIG wraps the equality operator, it only allows objects of the same type to compared; otherwise you get an error like this:


>>> Color(1,2,3) == None

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/sw/coreex/008-geoffp-01/cppCircle.py", line 1488, in __eq__

    return _cppCircle.Color___eq__(*args)

ValueError: invalid null reference in method 'Color___eq__', argument 2 of type 'Color const &'


A fix posted on the Internet is to use this code in the interface file:


%feature("pythonprepend") operator== %{
        if not isinstance(args[1], type(args[0])):
            return False


However, when I do this I get a syntax error from the SWIG pre-processor:


test.i:299: Error: Syntax error in input(1).

test.i:301: Error: Misplaced %}.


I tried using __eq__ in place of operator==, to no avail (it was ignored).


What is the correct way to prepend code to the == operator?


I’m using SWIG 1.3.31