Can't pickle a bound method if `getattr(type(im_self), funcname)` fails
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 36k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug report
MWE:
import pickle
import numpy as np
from scipy.interpolate import RectBivariateSpline
class Foo:
def __init__(self):
x = np.linspace(0,1,10)[:,None]
y = np.linspace(2,3,9)[None,:]
self.f = RectBivariateSpline(x,y,x*y)
def f2(self,x,y):
return self.f(x,y)
self.f2 = f2.__get__(self)
foo = Foo()
pickled = pickle.dumps(foo)
unpickled = pickle.loads(pickled)
print(unpickled.f2(0.5, 0.6))
Expected output is to print [[1.]]. Actually gives an exception
Traceback (most recent call last):
File ".../pickle-test.py", line 18, in <module>
unpickled = pickle.loads(pickled)
AttributeError: 'Foo' object has no attribute 'f2'
This issue follows up from the identification an fixing of the same issue with dill, see
https://github.com/uqfoundation/dill/issues/510
https://github.com/uqfoundation/dill/pull/511
where @anivegesana commented
It seems to be a little bit more subtle than that. Because Python 3 implements its own reduce function for methods, I removed it from dill. It turns out that this is not a safe optimization in the case that the func is an inner function. Will be fixed soon.
Your environment
- CPython versions tested on: 3.9.10
- Operating system and architecture: Linux Mint 20.2 on x86-64
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo il MWE con pickle.dumps e pickle.loads, concentrandoti sul metodo associato assegnato a Foo.f2 e sull’errore segnalato di getattr(type(im_self), funcname). Traccia il comportamento di andata e ritorno del metodo e la gestione esistente di pickle; il lavoro è completato quando l’esempio viene deserializzato correttamente e stampa [[1.]].
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
- Da chiarire
- Idoneità per principianti
- 25/100