[issue43093] Make modules picklable

Arusekk report at bugs.python.org
Mon Feb 1 14:44:18 EST 2021


Arusekk <arek_koz at o2.pl> added the comment:

Sorry, I forgot to state what is my actual goal: for the module objects to be pickleable in native CPython (possibly from C layer) without the need to add this to every code that wants to pickle module objects.

The point is that they can already be unpickled from the representation generated by the code, so it should only be necessary to implement __reduce__ in moduleobject.c, and probably to change the object's qualname / module attributes. Or to introduce a helper function, like PyPy did, for the fake module case; I just found supporting existing unpickling paths more elegant (and working across all Python versions since 2.0.0), as much as unelegant is the monkey-patching done in my example. It is possible that (for the actual module case) _compat_pickle.REVERSE_IMPORT_MAPPING should be considered as well for the old protocols (which would probably imply using __reduce_ex__ instead), but I did not explore that.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43093>
_______________________________________


More information about the Python-bugs-list mailing list