If a Python 3 class is pickled using protocol 2, it is supposed to work in Python 2, but unfortunately, this fails because the names of some classes have changed.
Assume we have code called as follows.
Sender
pickle.dumps(obj,2)
Receiver
pickle.loads(atom)
To give a specific case, if obj={}
, then the error given is:
ImportError: No module named builtins
This is because Python 2 uses __builtin__
instead.
The question is the best way to fix this problem.
This problem is Python issue 3675. This bug is actually fixed in Python 3.11.
If we import:
MAPPING maps Python 2 names to Python 3 names. We want this in reverse.
We can override the Unpickler and loads
We then call this loads instead of pickle.loads.
This should solve the problem.