Can't pickle a bound method if `getattr(type(im_self), funcname)` fails
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 36k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das MWE mit pickle.dumps und pickle.loads zu reproduzieren, wobei du dich auf die an Foo.f2 gebundene Methode und den gemeldeten getattr(type(im_self), funcname)-Fehler konzentrierst. Verfolge das Round-Trip-Verhalten der Methode und die bestehende pickle-Behandlung; abgeschlossen ist die Aufgabe, wenn das Beispiel erfolgreich deserialisiert wird und [[1.]] ausgibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100