Не удалось сериализовать класс C ++, завернутый в SWIG, используя pickle - PullRequest
3 голосов
/ 16 мая 2019

Для нужд моей повседневной работы мне нужно обернуть SWIG некоторыми классами C ++, расположенными в DSO. Все было хорошо, пока я не решил по соображениям производительности использовать буферный протокол Python, что побудило меня использовать флаг -builtin инструмента SWIG. Мне удалось заставить это работать, но когда я пытаюсь сериализовать / десериализовать с помощью pickle моих упакованных классов, я получаю следующую ошибку:

Traceback (most recent call last):
  File "test.py", line 23, in <module>
    s = pickle.dumps(a) # crash here
_pickle.PicklingError: Can't pickle <class 'mymodule.A'>: attribute lookup A on imp failed

Вот MWE, разоблачающий проблему.

Сначала файл интерфейса SWIG mymodule.i оборачивает встроенный класс C ++ A.

// File mymodule.i
%module mymodule

%feature("python:tp_str")  A "A_str" ;
%feature("python:tp_repr") A "A_str" ;
%{
#include <iostream>
#include <sstream>

// forward declarations
class A ;
static std::ostream & operator << (std::ostream &, const A &) ;
static int  A_getI(const A &) ;
static void A_setI(A &, int) ;

class A
{
  int i_ ;
public:
  A(int i) : i_(i) {}
  friend std::ostream & operator << (std::ostream &, const A &) ;
  friend int  A_getI(const A &) ;
  friend void A_setI(A &, int) ;
} ;

std::ostream & operator << (std::ostream & os, const A & a)
{
  return os << "A(" << a.i_ << ")" ;
}

int A_getI(const A & a)
{
  return a.i_ ;
}

void A_setI(A & a, int i)
{
  a.i_ = i ;
}

static PyObject * A_str(SwigPyObject * obj)
{
  A * p_a = reinterpret_cast<A *>(obj->ptr) ;
  std::ostringstream oss ;
  oss << *p_a ;
  return PyUnicode_FromString(oss.str().c_str()) ;
}

%}

class A
{
public:
  A(int i) ;
} ;

%extend A
{
  PyObject * __getstate__()
  {
    PyObject * tuple = PyTuple_New(1) ;
    PyTuple_SetItem(tuple, 0, PyLong_FromLong(A_getI(*self))) ;
    return tuple ;
  }
  void __setstate__(PyObject * state)
  {
    if (PyTuple_Check(state)) {
      PyObject * val = PyTuple_GetItem(state, 0) ;
      if (PyLong_Check(val)) {
        int i = (int) PyLong_AsLong(val) ;
        A_setI(*self, i) ;
      }
    }
  }
}

Затем test.py Я использую:

#!/usr/bin/env python3
# File: test.py

from mymodule import *

# a first object a
a = A(10)
print("a=",a)

# we manually get the internal state of a
sa = a.__getstate__()
print("sa=",sa)

# a second object b with a different internal state
b = A(-1)
print("b=",b)

# we manually set a's internal state to b
b.__setstate__(sa)
print("b=",b)

# now we try to dump a's state using pickle...
import pickle
s = pickle.dumps(a) # crash here
print(s)

Я компилирую модуль:

swig3.0 -python -py3 -c++ -builtin -o mymodulePYTHON_wrap.cxx mymodule.i
g++ -D_mymodule_EXPORTS -DSWIG_REV="" -fPIC -I/usr/include/python3.4m -std=c++11 -o mymodulePYTHON_wrap.cxx.o -c mymodulePYTHON_wrap.cxx
g++ -fPIC -shared -Wl,-soname,_mymodule.so -o _mymodule.so mymodulePYTHON_wrap.cxx.o

Я запускаю тест:

python3 test.py

И я получаю следующий вывод:

a= A(10)
sa= (10,)
b= A(-1)
b= A(10)
Traceback (most recent call last):
  File "test.py", line 23, in <module>
    s = pickle.dumps(a) # crash here
_pickle.PicklingError: Can't pickle <class 'mymodule.A'>: attribute lookup A on imp failed

В чем причина ошибки?

Как мне решить мою проблему?

Я также пытался:

Версии моих инструментов довольно старые, потому что мне нужно использовать Debian jessie:

  • Python 3.4.2
  • G ++ 4.9.2
  • SWIG 3.0.2

Я застрял. Любая помощь будет оценена.

EDIT

Файл mymodule.py, созданный SWIG (флаг WITH -builtin):

# This file was automatically generated by SWIG (http://www.swig.org).
# Version 3.0.2
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.





from sys import version_info
if version_info >= (2,6,0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:
            fp, pathname, description = imp.find_module('_mymodule', [dirname(__file__)])
        except ImportError:
            import _mymodule
            return _mymodule
        if fp is not None:
            try:
                _mod = imp.load_module('_mymodule', fp, pathname, description)
            finally:
                fp.close()
            return _mod
    _mymodule = swig_import_helper()
    del swig_import_helper
else:
    import _mymodule
del version_info
from _mymodule import *
try:
    _swig_property = property
except NameError:
    pass # Python < 2.2 doesn't have 'property'.
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
    if (name == "thisown"): return self.this.own(value)
    if (name == "this"):
        if type(value).__name__ == 'SwigPyObject':
            self.__dict__[name] = value
            return
    method = class_type.__swig_setmethods__.get(name,None)
    if method: return method(self,value)
    if (not static):
        self.__dict__[name] = value
    else:
        raise AttributeError("You cannot add attributes to %s" % self)

def _swig_setattr(self,class_type,name,value):
    return _swig_setattr_nondynamic(self,class_type,name,value,0)

def _swig_getattr(self,class_type,name):
    if (name == "thisown"): return self.this.own()
    method = class_type.__swig_getmethods__.get(name,None)
    if method: return method(self)
    raise AttributeError(name)

def _swig_repr(self):
    try: strthis = "proxy of " + self.this.__repr__()
    except: strthis = ""
    return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)

try:
    _object = object
    _newclass = 1
except AttributeError:
    class _object : pass
    _newclass = 0



# This file is compatible with both classic and new-style classes.

Файл mymodule.py БЕЗ -builtin Флаг SWIG:

# This file was automatically generated by SWIG (http://www.swig.org).
# Version 3.0.2
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.





from sys import version_info
if version_info >= (2,6,0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:
            fp, pathname, description = imp.find_module('_mymodule', [dirname(__file__)])
        except ImportError:
            import _mymodule
            return _mymodule
        if fp is not None:
            try:
                _mod = imp.load_module('_mymodule', fp, pathname, description)
            finally:
                fp.close()
            return _mod
    _mymodule = swig_import_helper()
    del swig_import_helper
else:
    import _mymodule
del version_info
try:
    _swig_property = property
except NameError:
    pass # Python < 2.2 doesn't have 'property'.
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
    if (name == "thisown"): return self.this.own(value)
    if (name == "this"):
        if type(value).__name__ == 'SwigPyObject':
            self.__dict__[name] = value
            return
    method = class_type.__swig_setmethods__.get(name,None)
    if method: return method(self,value)
    if (not static):
        self.__dict__[name] = value
    else:
        raise AttributeError("You cannot add attributes to %s" % self)

def _swig_setattr(self,class_type,name,value):
    return _swig_setattr_nondynamic(self,class_type,name,value,0)

def _swig_getattr(self,class_type,name):
    if (name == "thisown"): return self.this.own()
    method = class_type.__swig_getmethods__.get(name,None)
    if method: return method(self)
    raise AttributeError(name)

def _swig_repr(self):
    try: strthis = "proxy of " + self.this.__repr__()
    except: strthis = ""
    return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)

try:
    _object = object
    _newclass = 1
except AttributeError:
    class _object : pass
    _newclass = 0


class A(_object):
    __swig_setmethods__ = {}
    __setattr__ = lambda self, name, value: _swig_setattr(self, A, name, value)
    __swig_getmethods__ = {}
    __getattr__ = lambda self, name: _swig_getattr(self, A, name)
    __repr__ = _swig_repr
    def __init__(self, *args): 
        this = _mymodule.new_A(*args)
        try: self.this.append(this)
        except: self.this = this
    def __getstate__(self) -> "PyObject *" : return _mymodule.A___getstate__(self)
    def __setstate__(self, *args) -> "void" : return _mymodule.A___setstate__(self, *args)
    __swig_destroy__ = _mymodule.delete_A
    __del__ = lambda self : None;
A_swigregister = _mymodule.A_swigregister
A_swigregister(A)

# This file is compatible with both classic and new-style classes.
...