I get an error when I try to save a custom object with
with open(path + '/' + 'my_object.pickle', 'wb') as handle:
pickle.dump(my_class_instance, handle, protocol=pickle.HIGHEST_PROTOCOL)
The error message is:
TypeError: can't pickle SwigPyObject objects
My first question is: What is a SwigPyObject objects?, so I can try to figured out where the errors came from.
What is SWIG?
The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect libraries written in C or C++ with scripting languages such as Python. A well-known alternative to SWIG is Boost. The Boost Library aims to accomplish the same thing.
You use SWIG to create a Python library developed in C/C++ (for interoperability with existing C/C++ library or to gain in performance, or whatever).
Quoting the SWIG documentation:
To build Python extension modules, SWIG uses a layered approach in which parts of the extension module are defined in C and other parts are defined in Python. The C layer contains low-level wrappers whereas Python code is used to define high-level features.
This layered approach recognizes the fact that certain aspects of extension building are better accomplished in each language (instead of trying to do everything in C or C++). Furthermore, by generating code in both languages, you get a lot more flexibility since you can enhance the extension module with support code in either language.
What is a SwigPyObject objects?
The SwigPyObject
object is describe in the topic “Further details on the Python class interface” in the SWIG documentation.
This object is an implementation detail. It is defined by the following C structure:
typedef struct {
PyObject_HEAD
void *ptr;
swig_type_info *ty;
int own;
PyObject *next;
PyObject *dict;
} SwigPyObject;
A SwigPyObject
object stores an instance of a C++ object:
When
-builtin
is used, the pure python layer is stripped off. Each wrapped class is turned into a new python built-in type which inherits fromSwigPyObject
, andSwigPyObject
instances are returned directly from the wrapped methods.
pickle file
The data contained in your pickle file contains a SwigPyObject
object but your interpreter does not know how to de-serialize it. You need to check that your virtualenv contains the Python/C++ library which provides your pickle object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With