Globals read only inside a class body defined in a by-value function are not pickled (NameError after loading)
维护者通常 9 天内回复
还没有人认领这个 Issue。
评估
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
🤖 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.
- 主要语言
- Python
- 星标
- 1.9k
- 派生
- 206
- 平均合并
- 1 天 10 小时
- 30 天内合并 PR
- 1
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
cloudpipe/cloudpickle 的其他 Issue
-
Bug: unguarded dict.pop() in _function_setstate causes KeyError crash on crafted pickle (regression from c6f8cd4)可能已有人在做 @soodoku 于 79 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
cloudpipe/cloudpickle#593 · 1 条评论 ·
维护者通常 9 天内回复
-
A class pickled by value before its annotations are read loses them.可能已有人在做 @pralav-25 于 6 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 67/100
cloudpipe/cloudpickle#608 ·
维护者通常 9 天内回复
-
难度 4/5 3-5 天 新手友好度 42/100
cloudpipe/cloudpickle#595 ·
维护者通常 9 天内回复
-
cloudpickle is not compatible with PyPy 7.3.22可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 3/5 1-2 天 新手友好度 65/100
cloudpipe/cloudpickle#592 · 2 条评论 ·
维护者通常 9 天内回复
-
难度 5/5 一周以上 新手友好度 35/100
cloudpipe/cloudpickle#589 ·
维护者通常 9 天内回复
查看 cloudpipe/cloudpickle 的全部 Issue
相似的 Issue
-
Agentic
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
Imbad0202/academic-research-skills#979 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
reflex-dev/reflex#7561 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 62/100
prometheus-community/ansible#906 · 2 条评论 ·