cloudpipe / cloudpipe/cloudpickle
Pickling functions breaks if Python module defined from types.ModuleType is used
- Lingua principale
- Python
- Stelle
- 1.9k
- Fork
- 195
- Merge medio
- 1g 10h
- PR unite (30g)
- 1
Descrizione
Hi there!
Our project ([the Python bindings of ROOT](https://root.cern/)) uses inside the Python module a "facade" object to do some magic with the lookups in the module and this causes issues with cloudpickle, which shows up when using dask. However, this facade object, derived from a `types.ModuleType`, has never caused issues, also when using the standard pickle in the Python standard library.
Below you can find a standalone reproducer, which shows that the standard pickle is fine with such a construct but cloudpickle fails with the error message `TypeError: cannot pickle 'Facade' object`.
Edit: We have found out that we can move the `import my_module` line inside the function `foo` as a workaround. I hope this gives an hint to the underlying issue!
```python
# Define the module facade and inject it as module
import sys
from types import ModuleType
class Facade(ModuleType):
def __init__(self, name):
super().__init__(name)
self.foo = 42
sys.modules['my_module'] = Facade('my_module')
# Pickle a function which uses the module with the facade
import my_module
import cloudpickle as pickle # breaks with cloudpickle
#import pickle # works fine with the standard pickle
def foo():
my_module.foo
return 42
with open('f.pkl', 'wb') as f:
pickle.dump(foo, f) # Breaks with "TypeError: cannot pickle 'Facade' object"
```
Python: 3.8.6
cloudpickle: 1.6.0
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Inizia eseguendo il riproduttore Python standalone presente nell’issue con Python 3.8.6 e cloudpickle 1.6.0, quindi esamina la gestione da parte di cloudpickle dei globali delle funzioni e degli oggetti basati su ModuleType. Il lavoro è completato quando la funzione che usa il modulo supportato da Facade può essere sottoposta con successo a dump, preservando il comportamento standard di pickle descritto nel report.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100