`pickle.loads` will crash with self-references inside a custom hash function
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug report
Bug description:
here is a reproduction of the issue:
import pickle
class Foo:
def __init__(self):
self.x: object = {self}
def __hash__(self):
return hash(self.x)
foo = Foo()
print(pickle.loads(pickle.dumps(foo)))
running this will result in the following exception:
Traceback (most recent call last):
File "/home/charles/vyper/foo.py", line 10, in <module>
foo = Foo()
^^^^^
File "/home/charles/vyper/foo.py", line 5, in __init__
self.x: object = {self}
^^^^^^
File "/home/charles/vyper/foo.py", line 8, in __hash__
return hash(self.x)
^^^^^^
AttributeError: 'Foo' object has no attribute 'x'
a workaround to the issue has been described at https://stackoverflow.com/a/44888113. however, i consider this a bug in the cpython implementation, because pickle theoretically handles object cycles (e.g., replacing line 5 with self.x = [self] poses no problem to the unpickler).
i suspect that cpython rehashes all items when reconstructing a dict or set, which makes the issue even more problematic, e.g. if the hash function has any side-effects, they will be executed by the unpickler.
build info:
$ python
Python 3.11.10 (main, Sep 7 2024, 18:35:41) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
CPython versions tested on:
3.11
Operating systems tested on:
Linux
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
Il report fornisce un riproduttore incentrato su pickle.dumps e pickle.loads, ma non indica alcun file sorgente di CPython né alcun test. Inizia eseguendolo nell’ambiente Python 3.11/Linux indicato e tracciando il percorso di deserializzazione; il completamento richiede un comportamento concordato per gli hash personalizzati autoreferenziali, una copertura di regressione e l’assenza di effetti collaterali involontari degli hash.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100