如何使SWIG扩展模块与Pickle一起使用? [英] How to make my SWIG extension module work with Pickle?

查看:84
本文介绍了如何使SWIG扩展模块与Pickle一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用SWIG作为包装器的Python扩展模块,我尝试用Pickle对其进行序列化,但失败=)

I have an extension module for Python that uses SWIG as a wrapper and I try to serialize it with Pickle and I fail =)

  1. 如果任何人都有可以腌制的SWIG扩展程序源,很乐意看到它!
  2. 似乎我应该实现 __reduce_ex__ 方法在我的C ++代码中.有人有__reduce_ex__的例子吗? 有类似的Stackoverflow问题,但省略了manager_constructor规范和实现.
  1. If anyone has a source of SWIG extension that can be pickled, would love to see it!
  2. It seems like I should implement __reduce_ex__ method in my C++ code. Does anyone have example of __reduce_ex__? There is similar Stackoverflow question but it omits manager_constructor specification and implementation.

推荐答案

似乎我找到了适用于我的简单解决方案:

Seems like I found simlple solution that works for me:

因此,假设我们有用SWIG生成的类C,然后将其包装

So let's say we have class C that was generated with SWIG, then we wrap it with

class PickalableC(C, PickalableSWIG):

    def __init__(self, *args):
        self.args = args
        C.__init__(self)

其中PickalableSWIG

class PickalableSWIG:

    def __setstate__(self, state):
        self.__init__(*state['args'])

    def __getstate__(self):
        return {'args': self.args}

然后

pickle.loads(pickle.dumps(C()))

失败,但是

pickle.loads(pickle.dumps(PickalableC()))

成功=)

这篇关于如何使SWIG扩展模块与Pickle一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆