Globals read only inside a class body defined in a by-value function are not pickled (NameError after loading)
I maintainer di solito rispondono entro 9 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 55/100
Direzione di ricerca
Start by reading _extract_code_globals around GLOBAL_OPS in cloudpickle/cloudpickle.py, then inspect _function_getstate and the base_globals path in _function_getnewargs identified in the issue. Reproduce the provided example and check the related class-scope opcode cases across the listed Python versions. Done means the globals used by those class scopes are available after loading, with regression tests covering the reported cases.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
🤖 AI text below 🤖
A global that a by-value function reads only inside the body of a class it defines is not pickled, so the loaded function raises NameError. This happens on Python 3.11 through 3.14, with cloudpickle 3.1.2 and with master (4af5936).
import subprocess, sys
import cloudpickle
ns = {"__name__": "not_importable", "CONST": 42} # not importable, so f is pickled by value
exec("def f():\n class Rec:\n q = CONST\n return Rec.q\n", ns)
print("local:", ns["f"]())
load = "import pickle, sys; print(pickle.loads(sys.stdin.buffer.read())())"
r = subprocess.run([sys.executable, "-c", load], input=cloudpickle.dumps(ns["f"]), capture_output=True)
print("loaded:", (r.stdout or r.stderr).decode().strip().splitlines()[-1])
Output on 3.11.15, 3.12.10, 3.13.3 and 3.14.6:
local: 42
loaded: NameError: name 'CONST' is not defined
Other class-scope reads fail the same way:
- a field annotation naming a global, for example a dataclass
p: Pointdefined inside the function (measured on 3.13 and 3.14); - a class-scoped
type Inner = Galias, once its value is evaluated (measured on 3.12, 3.13 and 3.14).
Cause: _extract_code_globals only collects names from GLOBAL_OPS, which is STORE_GLOBAL, DELETE_GLOBAL and LOAD_GLOBAL (L388-L397). Inside a class scope, the compiler reads names with other opcodes (measured with dis):
- class-body statements, and annotations before 3.14, use
LOAD_NAME(3.11-3.14); - a class's
__annotate__function (3.14) and a class-scopedtypealias value (3.12+) useLOAD_FROM_DICT_OR_GLOBALS.
Suggested fix: add LOAD_NAME to GLOBAL_OPS, and also LOAD_FROM_DICT_OR_GLOBALS on 3.12+. Both consumers already keep only names present in func.__globals__, so class-local names collected this way are dropped:
_function_getstate(L734-L735);- the
base_globalspath in_function_getnewargs(L1307-L1314).
Related: #608.
- Lingua principale
- Python
- Stelle
- 1.9k
- Fork
- 206
- Merge medio
- 1g 10h
- PR unite (30g)
- 1
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
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.
Altre issue di cloudpipe/cloudpickle
-
Bug: unguarded dict.pop() in _function_setstate causes KeyError crash on crafted pickle (regression from c6f8cd4)Forse già presa @soodoku l’ha presa 79 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
cloudpipe/cloudpickle#593 · 1 commento ·
I maintainer di solito rispondono entro 9 giorni
-
A class pickled by value before its annotations are read loses them.Forse già presa @pralav-25 l’ha presa 6 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 67/100
cloudpipe/cloudpickle#608 ·
I maintainer di solito rispondono entro 9 giorni
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
cloudpipe/cloudpickle#595 ·
I maintainer di solito rispondono entro 9 giorni
-
cloudpickle is not compatible with PyPy 7.3.22Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 65/100
cloudpipe/cloudpickle#592 · 2 commenti ·
I maintainer di solito rispondono entro 9 giorni
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
cloudpipe/cloudpickle#589 ·
I maintainer di solito rispondono entro 9 giorni
Tutte le issue di cloudpipe/cloudpickle
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
-
[Bug]: Importing weave changes the process-wide localeForse già presa @himanshu748 l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
-
UX
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
mediajunkie/piper-morgan-product#2008 · 2 commenti ·