@@ -17,8 +17,8 @@ some special functions.
17
17
This example shows you how to provided pickle support for for the ``custom2.Custom `` type described in the C extension
18
18
tutorial in the
19
19
`Python documentation <https://docs.python.org/3/extending/newtypes_tutorial.html#adding-data-and-methods-to-the-basic-example >`_.
20
- This defines an ``CustomObject `` object that haas three fields; a first name, a last name and a number.
21
- The ``CustomObject `` definition that needs to be pickled and un-pickled looks like this in C.
20
+ This defines an ``CustomObject `` object that has three fields; a first name, a last name and a number.
21
+ The ``CustomObject `` definition that needs to be pickled and un-pickled looks like this in C:
22
22
23
23
.. code-block :: c
24
24
@@ -29,6 +29,9 @@ The ``CustomObject`` definition that needs to be pickled and un-pickled looks li
29
29
int number;
30
30
} CustomObject;
31
31
32
+ - The example C code is in ``src/cpy/Pickle/cCustomPickle.c ``.
33
+ - The test code is in ``tests/unit/test_c_custom_pickle.py ``.
34
+
32
35
.. index ::
33
36
single: Pickling; Version Control
34
37
@@ -185,9 +188,10 @@ Set the ``first`` Member
185
188
Set the ``last `` Member
186
189
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
187
190
191
+ This code is very similar to the code for the first member above.
192
+
188
193
.. code-block :: c
189
194
190
- /* Similar to self->first above. */
191
195
temp = PyDict_GetItemString(state, "last"); /* Borrowed reference. */
192
196
if (temp == NULL) {
193
197
/* PyDict_GetItemString does not set any error state so we have to. */
@@ -268,8 +272,11 @@ And we are done.
268
272
}
269
273
int pickle_version = (int) PyLong_AsLong(temp);
270
274
if (pickle_version != PICKLE_VERSION) {
271
- PyErr_Format(PyExc_ValueError, "Pickle version mismatch. Got version %d but expected version %d.",
272
- pickle_version, PICKLE_VERSION);
275
+ PyErr_Format(
276
+ PyExc_ValueError,
277
+ "Pickle version mismatch. Got version %d but expected version %d.",
278
+ pickle_version, PICKLE_VERSION
279
+ );
273
280
return NULL;
274
281
}
275
282
@@ -369,16 +376,18 @@ Here is some Python code that exercises our module (tests are in ``tests/unit/te
369
376
370
377
def test_module_dir ():
371
378
assert dir (cPickle) == [
372
- ' Custom' , ' __doc__' , ' __file__' , ' __loader__' , ' __name__' , ' __package__' , ' __spec__'
379
+ ' Custom' , ' __doc__' , ' __file__' , ' __loader__' ,
380
+ ' __name__' , ' __package__' , ' __spec__' ,
373
381
]
374
382
375
383
376
384
ARGS_FOR_CUSTOM_CLASS = (' FIRST' , ' LAST' , 11 )
377
- PICKLE_BYTES_FOR_CUSTOM_CLASS = (b ' \x80\x04\x95 f\x00\x00\x00\x00\x00\x00\x00\x8c\x12 cPyExtPatt.cPickle\x94 '
378
- b ' \x8c\x06 Custom\x94\x93\x94 )\x81\x94 }\x94 (\x8c\x05 first\x94\x8c\x05 FIRST'
379
- b ' \x94\x8c\x04 last\x94\x8c\x04 LAST\x94\x8c\x06 number\x94 K\x0b\x8c\x0f _pickle_'
380
- b ' version\x94 K\x01 ub.' )
381
-
385
+ PICKLE_BYTES_FOR_CUSTOM_CLASS = (
386
+ b ' \x80\x04\x95 f\x00\x00\x00\x00\x00\x00\x00\x8c\x12 cPyExtPatt.cPickle\x94 '
387
+ b ' \x8c\x06 Custom\x94\x93\x94 )\x81\x94 }\x94 (\x8c\x05 first\x94\x8c\x05 FIRST'
388
+ b ' \x94\x8c\x04 last\x94\x8c\x04 LAST\x94\x8c\x06 number\x94 K\x0b\x8c\x0f _pickle_'
389
+ b ' version\x94 K\x01 ub.'
390
+ )
382
391
383
392
def test_pickle_getstate ():
384
393
custom = cPickle.Custom(* ARGS_FOR_CUSTOM_CLASS )
@@ -435,6 +444,10 @@ Here is a test for that:
435
444
436
445
The expected output will be something like this:
437
446
447
+ .. raw :: latex
448
+
449
+ \begin {landscape }
450
+
438
451
.. code-block :: text
439
452
440
453
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.'
@@ -471,6 +484,10 @@ The expected output will be something like this:
471
484
101: . STOP Stop the unpickling machine.
472
485
highest protocol among opcodes = 4
473
486
487
+ .. raw :: latex
488
+
489
+ \end {landscape}
490
+
474
491
.. index ::
475
492
single: Pickling; External State
476
493
0 commit comments