Globals read only inside a class body defined in a by-value function are not pickled (NameError after loading)
メンテナーはふだん 9 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
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
- フォーク
- 201
- 平均マージ
- 1日 10時間
- マージ済み PR(30日)
- 1
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
cloudpipe/cloudpickle のほかの issue
-
Bug: unguarded dict.pop() in _function_setstate causes KeyError crash on crafted pickle (regression from c6f8cd4)対応中かも @soodoku が 78 日前に担当しました。 オープン
難易度 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 が 4 日前に担当しました。 オープン
難易度 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対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 65/100
cloudpipe/cloudpickle#592 · コメント 2 件 ·
メンテナーはふだん 9 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
cloudpipe/cloudpickle#589 ·
メンテナーはふだん 9 日以内に返信
cloudpipe/cloudpickle の issue をすべて見る
似ている issue
-
area: agents area: docs good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
openapi/gknowledge#23 ·
-
get() for StrEnum対応中かも @FeJo5952 が今日担当しました。 オープンgood first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
collective/icalendar#1921 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Improvement Python Spans
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
getsentry/sentry-python#7994 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
alphagov/notifications-api#5004 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
equinor/radix-flux#3922 ·
メンテナーはふだん 1 日以内に返信