Сгенерированные SWIG Python аннотации функций не работают - PullRequest
0 голосов
/ 08 января 2020

Я недавно начал использовать swig, чтобы обернуть одну из моих библиотек C ++. Я хочу использовать этот код в Python, и пока он работает хорошо. Проблема в том, что Python не знает, какие объекты возвращают упакованные функции. Конечно, я все еще могу вызывать методы для возвращенного объекта, потому что я знаю его тип, но я не могу использовать intellisense таким образом. Этот код python предназначен для использования другими людьми, и эта проблема делает кодирование немного сложнее, если не знать типы возвращаемых данных.

Это один из этих методов:

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

Возвращает объект SC_Message. Класс SC_Message также определен в python коде, созданном SWIG следующим образом:

    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)

Так что я действительно должен видеть метод GetContent () при использовании intellisense. Когда я меняю аннотацию функции в сгенерированном Python коде на «SC_Message», все работает как положено. Является ли это ожидаемым поведением SWIG или SWIG также может создавать более полезные аннотации? Без опции -py3 не было бы аннотаций.

Моя команда SWIG выглядит следующим образом:

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

И это мой файл SWIG:

 %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"

Заранее спасибо.

1 Ответ

0 голосов
/ 13 января 2020

Кажется, что SWIG обычно генерирует аннотации с типами c и c ++. Это будет решено в версии 2.0, насколько я могу судить. Для получения дополнительной информации смотрите здесь https://github.com/SimpleITK/SimpleITK/issues/809

...