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
错误的原因是什么?
我怎样才能解决我的问题?在
我也尝试过:
__reduce__
方法返回预期的结果(它起作用了),但是发生了相同的错误我的工具版本很旧,因为我必须使用Debian jessie:
我卡住了。任何帮助都将不胜感激。在
编辑
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.py
SWIG标志的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.
目前没有回答
相关问题 更多 >
编程相关推荐