You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/sphinx/source/pickle.rst
+95-14
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@
5
5
:maxdepth:2
6
6
7
7
====================================
8
-
Pickling and C Extensions
8
+
Pickling and C Extension Types
9
9
====================================
10
10
11
11
If you need to provide support for pickling your specialised types from your C extension then you need to implement some special functions.
@@ -44,7 +44,7 @@ Implementing ``__getstate__``
44
44
Note that a ``Custom`` object has two Python objects (``first`` and ``last``) and a C integer (``number``) that need to be converted to a Python object.
45
45
We also need to add the version information.
46
46
47
-
Her is the C implementation:
47
+
Here is the C implementation:
48
48
49
49
.. code-block:: c
50
50
@@ -100,6 +100,11 @@ Before setting any member value we need to de-allocate the existing value set by
100
100
pickle_version, PICKLE_VERSION);
101
101
return NULL;
102
102
}
103
+
104
+
Set ``first``:
105
+
106
+
.. code-block:: c
107
+
103
108
/* NOTE: Custom_new() will have been invoked so self->first and self->last
104
109
* will have been allocated so we have to de-allocate them. */
105
110
Py_DECREF(self->first);
@@ -112,6 +117,10 @@ Before setting any member value we need to de-allocate the existing value set by
112
117
/* Increment the borrowed reference for our instance of it. */
113
118
Py_INCREF(self->first);
114
119
120
+
Set ``last``:
121
+
122
+
.. code-block:: c
123
+
115
124
/* Similar to self->first above. */
116
125
Py_DECREF(self->last);
117
126
self->last = PyDict_GetItemString(state, "last"); /* Borrowed reference. */
@@ -122,6 +131,10 @@ Before setting any member value we need to de-allocate the existing value set by
122
131
}
123
132
Py_INCREF(self->last);
124
133
134
+
Set ``number``, this is a C fundamental type:
135
+
136
+
.. code-block:: c
137
+
125
138
/* Borrowed reference but no need to incref as we create a C long from it. */
original is <custom2.Custom object at 0x102b00810> @ 0x102b00810 first: FIRST last: LAST number: 11 name: FIRST LAST
264
+
original is <custom2.Custom object at 0x1049e6810> @ 0x1049e6810
265
+
original first: FIRST last: LAST number: 11 name: FIRST LAST
187
266
Pickled original is b'\x80\x04\x95[\x00\x00\x00\x00\x00\x00\x00\x8c\x07custom2\x94\x8c\x06Custom\x94\x93\x94)\x81\x94}\x94(\x8c\x05first\x94\x8c\x05FIRST\x94\x8c\x04last\x94\x8c\x04LAST\x94\x8c\x06number\x94K\x0b\x8c\x0f_pickle_version\x94K\x01ub.'
188
-
result is <custom2.Custom object at 0x102a3f510> @ 0x102a3f510 first: FIRST last: LAST number: 11 name: FIRST LAST
267
+
result is <custom2.Custom object at 0x1049252d0> @ 0x1049252d0
268
+
result first: FIRST last: LAST number: 11 name: FIRST LAST
189
269
190
-
So we have pickled one object and recreated a different, but equivalent, instance from that object.
270
+
So we have pickled one object and recreated a different, but equivalent, instance from the pickle of the original object which is what we set out to do.
191
271
192
272
Pickling Objects with External State
193
273
-----------------------------------------
194
274
195
275
This is just a simple example, if your object relies on external state such as open files, databases and the like you need to be careful, and knowledgeable about your state management.
276
+
There is some useful information here: `Handling Stateful Objects <https://docs.python.org/3/library/pickle.html#pickle-state>`_
0 commit comments