无法将用pi包装的C++类序列化使用SWIG。

2024-04-26 18:19:46 发布

您现在位置:Python中文网/ 问答频道 /正文

对于我日常工作的需要,我必须用SWIG包一些位于DSO中的C++类。一切都很好,直到出于性能原因决定使用Python缓冲区协议,这导致我使用SWIG工具的-builtin标志。我已经成功地让它正常工作,但是当我尝试用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接口文件^ {CD1>}封装了一个内联C++类A/< ^{pr2}$

然后,我使用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版
  • 3.0.2瑞士法郎

我卡住了。任何帮助都将不胜感激。在

编辑

SWIG生成的mymodule.py文件(带有-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.pySWIG标志的mymodule.py文件:

# 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.

Tags: nameimportselfreturnifvaluedeftype