1

I recently started using swig to wrap one of my C++ libraries. I want to use this code in Python and it is working good so far. The Problem is that Python does not know what objects the wrapped functions return. Of course I can still call methods on the returned object because I know its type but I can't use intellisense in that way. This python code is meant to be used by other people and this problem makes coding just a little bit harder if one does not know the return types.

This is one of those methods:

def CreateJoinRequestMessage(self) -> "Hive::SC_Message":
    return _Hive.SC_MessageHandler_CreateJoinRequestMessage(self)

It returns an SC_Message object. The SC_Message class is also defined in the python code produced by SWIG like this:

    class SC_Message(object):
    thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag")
    __repr__ = _swig_repr

    def __init__(self, messageString: "std::string const &", messageType: "Hive::SC_MessageType const &"):
        _Hive.SC_Message_swiginit(self, _Hive.new_SC_Message(messageString, messageType))

    def GetContent(self) -> "std::string":
        return _Hive.SC_Message_GetContent(self)

    def GetMessageType(self) -> "Hive::SC_MessageType":
        return _Hive.SC_Message_GetMessageType(self)
    __swig_destroy__ = _Hive.delete_SC_Message
    _Hive.SC_Message_swigregister(SC_Message)

So I should really be able to see the GetContent() method when using intellisense. When I change the function annotation in the generated Python code to "SC_Message" everything works as expected. Is this expected behavior by SWIG or can SWIG also produce more helpful annotations? Without the -py3 option there would be no annotations.

My SWIG command is the following:

swig -c++ -python -py3 hive.i

And this is my SWIG file:

 %module Hive
    %{
    #define SWIG_FILE_WITH_INIT
    #include "include/hive/PieceType.hpp"
    #include "include/hive/MoveType.hpp"
    #include "include/hive/AxialPosition.hpp"
    #include "include/hive/Color.hpp"
    #include "include/hive/neighbourMap.hpp"
    #include "include/hive/Piece.hpp"
    #include "include/hive/Player.hpp"
    #include "include/hive/PieceStack.hpp"
    #include "include/hive/globals.hpp"
    #include "include/hive/Move.hpp"
    #include "include/hive/board.hpp"
    #include "include/hive/gameState.hpp"
    #include "include/communication/SC_MessageType.hpp"
    #include "include/communication/SC_Message.hpp"
    #include "include/communication/SC_MessageHandler.hpp"
    #include "include/hive/benchmark/benchmark.hpp"
    %}
%include "std_string.i"
%include "std_array.i"
%include "std_pair.i"
%include "std_vector.i"
%include "include/hive/PieceType.hpp"
%include "include/hive/MoveType.hpp"
%include "include/hive/AxialPosition.hpp"
%include "include/hive/Color.hpp"
%include "include/hive/neighbourMap.hpp"
%include "include/hive/Piece.hpp"
%include "include/hive/Player.hpp"
%include "include/hive/PieceStack.hpp"
%include "include/hive/globals.hpp"
%include "include/hive/Move.hpp"
%include "include/hive/board.hpp"
%include "include/hive/gameState.hpp"
%include "include/communication/SC_MessageType.hpp"
%include "include/communication/SC_Message.hpp"
%include "include/communication/SC_MessageHandler.hpp"
%include "include/hive/benchmark/benchmark.hpp"

Thanks in advance.

sunfl0w
  • 11
  • 3

1 Answers1

0

It seems like SWIG usually generates annotations with c and c++ types. This will be addressed in version 2.0 as far as I can tell. For further information look here https://github.com/SimpleITK/SimpleITK/issues/809

sunfl0w
  • 11
  • 3