Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Globals read only inside a class body defined in a by-value function are not pickled (NameError after loading)

オープン
#609 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 9 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
55/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend

調査の方向性

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: Point defined inside the function (measured on 3.13 and 3.14);
  • a class-scoped type Inner = G alias, 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-scoped type alias value (3.12+) use LOAD_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:

Related: #608.

主要言語
Python
スター
1.9k
フォーク
201
平均マージ
1日 10時間
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloudpipe/cloudpickle のほかの issue

cloudpipe/cloudpickle の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。