[New-bugs-announce] [issue37292] _xxsubinterpreters: Can't unpickle objects defined in __main__

Crusader Ky report at bugs.python.org
Sat Jun 15 09:06:31 EDT 2019


New submission from Crusader Ky <crusaderky at gmail.com>:

As of CPython 3.8.0b1:

If one pickles an object that is defined in the __main__ module, sends it to a subinterpreter as bytes, and then tries unpickling it there, it fails saying that __main__ doesn't define it.


import _xxsubinterpreters as interpreters
import pickle


class C:
    pass


c = C()

interp_id = interpreters.create()
c_bytes = pickle.dumps(c)
interpreters.run_string(
    interp_id,
    "import pickle; pickle.loads(c_bytes)",
    shared={"c_bytes": c_bytes},
)


If the above is executed directly with the python command-line, it fails. If it's imported from another module, it works.
One would expected behaviour compatible with sub-processes spawned with the spawn method, where the__main__ of the parent process is visible to the subprocess too.

Workarounds:
1 - define everything that must be pickled in an imported module
2 - use CloudPickle

----------
messages: 345680
nosy: Crusader Ky, eric.snow
priority: normal
severity: normal
status: open
title: _xxsubinterpreters: Can't unpickle objects defined in __main__
type: behavior
versions: Python 3.9

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


More information about the New-bugs-announce mailing list