5
5
:maxdepth: 2
6
6
7
7
====================================
8
- Pickling and C Extension Types
8
+ Pickling 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.
12
12
13
- This example shows you how to provided pickle support for for the ``custom2 `` type described in the C extension tutorial in the
13
+ This example shows you how to provided pickle support for for the ``custom2.Custom `` type described in the C extension tutorial in the
14
14
`Python documentation <https://docs.python.org/3/extending/newtypes_tutorial.html#adding-data-and-methods-to-the-basic-example >`_.
15
15
16
16
Pickle Version Control
@@ -73,6 +73,9 @@ We are being passed an arbitrary Python object and need to check:
73
73
Note that our ``__new__ `` method (``Custom_new() ``) has already been called on ``self ``.
74
74
Before setting any member value we need to de-allocate the existing value set by ``Custom_new() `` otherwise we will have a memory leak.
75
75
76
+ Error Checking
77
+ ^^^^^^^^^^^^^^^^^^^^^^^^
78
+
76
79
.. code-block :: c
77
80
78
81
/* Un-pickle the object */
@@ -101,7 +104,8 @@ Before setting any member value we need to de-allocate the existing value set by
101
104
return NULL;
102
105
}
103
106
104
- Set ``first ``:
107
+ Set the ``first `` Member
108
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
105
109
106
110
.. code-block :: c
107
111
@@ -117,7 +121,8 @@ Set ``first``:
117
121
/* Increment the borrowed reference for our instance of it. */
118
122
Py_INCREF(self->first);
119
123
120
- Set ``last ``:
124
+ Set the ``last `` Member
125
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121
126
122
127
.. code-block :: c
123
128
@@ -131,7 +136,10 @@ Set ``last``:
131
136
}
132
137
Py_INCREF(self->last);
133
138
134
- Set ``number ``, this is a C fundamental type:
139
+ Set the ``number `` Member
140
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141
+
142
+ This is a C fundamental type so the code is slightly different:
135
143
136
144
.. code-block :: c
137
145
@@ -151,7 +159,8 @@ And we are done.
151
159
Py_RETURN_NONE;
152
160
}
153
161
154
- The complete code is:
162
+ ``__setstate__ `` in Full
163
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
155
164
156
165
.. code-block :: c
157
166
@@ -215,8 +224,6 @@ The complete code is:
215
224
Py_RETURN_NONE;
216
225
}
217
226
218
-
219
-
220
227
Add the Special Methods
221
228
---------------------------------
222
229
@@ -225,22 +232,22 @@ Now we need to add these two special methods to the methods table which now look
225
232
.. code-block :: c
226
233
227
234
static PyMethodDef Custom_methods[] = {
228
- {"name", (PyCFunction) Custom_name, METH_NOARGS,
229
- "Return the name, combining the first and last name"
230
- },
231
- {"__getstate__", (PyCFunction) Custom___getstate__, METH_NOARGS,
232
- "Pickle the Custom object"
233
- },
234
- {"__setstate__", (PyCFunction) Custom___setstate__, METH_O,
235
- "Un-pickle the Custom object"
236
- },
237
- {NULL} /* Sentinel */
235
+ {"name", (PyCFunction) Custom_name, METH_NOARGS,
236
+ "Return the name, combining the first and last name"
237
+ },
238
+ {"__getstate__", (PyCFunction) Custom___getstate__, METH_NOARGS,
239
+ "Pickle the Custom object"
240
+ },
241
+ {"__setstate__", (PyCFunction) Custom___setstate__, METH_O,
242
+ "Un-pickle the Custom object"
243
+ },
244
+ {NULL} /* Sentinel */
238
245
};
239
246
240
- Example of Using ``custom2.Custom ``
247
+ Pickling a ``custom2.Custom `` Object
241
248
-------------------------------------
242
249
243
- We can test this with code like this that pickles one object then creates another object from that pickle.
250
+ We can test this with code like this that pickles one `` custom2.Custom `` object then creates another `` custom2.Custom `` object from that pickle.
244
251
Here is some Python code that exercises our module:
245
252
246
253
.. code-block :: python
0 commit comments