Mailing List Archive

Pickle for C extension?
TL;DR Is it possible to use C code to implement the (un)pickling of an type
written in a C extension, as it was written in _pickle.c?

Long explaining: I'm trying to create a C extension for frozendict. For
simplicity, first I wrote it in CPython, then I started to move it in a C
extension. It seems to work, but I have to move the code I wrote in
_pickle.c and pickle.py in the C extension. Is it possible, or I have to
create a slower `__reduce_ex__` method that simply converts it to dict?

This is, for example, the C code for pickling frozendict in _pickle.c:
https://github.com/Marco-Sulla/cpython/blob/41a640a947c36007e56bbc28f362c261110d2001/Modules/_pickle.c#L3370
Re: Pickle for C extension? [ In reply to ]
19.10.20 20:39, Marco Sulla ????:
> TL;DR Is it possible to use C code to implement the (un)pickling of an
> type written in a C extension, as it was written in _pickle.c?
>
> Long explaining: I'm trying to create a C extension for frozendict. For
> simplicity, first I wrote it in CPython, then I started to move it in a
> C extension. It seems to work, but I have to move the code I wrote in
> _pickle.c and pickle.py in the C extension. Is it possible, or I have to
> create a slower `__reduce_ex__` method that simply converts it to dict?
>
> This is, for example, the C code for pickling frozendict in _pickle.c:
> https://github.com/Marco-Sulla/cpython/blob/41a640a947c36007e56bbc28f362c261110d2001/Modules/_pickle.c#L3370

For adding new opcode to the pickle format you need a new pickle version
and a PEP.

Implement __reduce_ex__, it is the way to support pickling of new types
(as it was for sets, bytearrays, and many extension types).
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/WLBLDASK25BWANWS347U4HIOBA763IEW/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: Pickle for C extension? [ In reply to ]
Ah, okay. This is a problem for me, since I can't pass the 5th argument to
__reduce__, since it's an immutable type.