Skip to content

Pickle globals read from nested class bodies - #614

Open
Eryx-Z wants to merge 1 commit into
cloudpipe:masterfrom
Eryx-Z:fix/class-body-globals
Open

Eryx-Z wants to merge 1 commit into
cloudpipe:masterfrom
Eryx-Z:fix/class-body-globals

Conversation

@Eryx-Z

@Eryx-Z Eryx-Z commented Oct 17, 2026

Copy link
Copy Markdown

Summary

Fixes #609.

Globals referenced only inside a nested class body were omitted from by-value function pickles because _walk_global_ops only considered LOAD_GLOBAL / STORE_GLOBAL. Class bodies use LOAD_NAME (and on 3.12+, sometimes LOAD_FROM_DICT_OR_GLOBALS for annotations / type aliases).

This change treats those loads as global references for non-CO_OPTIMIZED code objects. To avoid the compatibility trap noted on the issue — a class-local name that shadows an unpicklable global — LOAD_NAME is counted only when it appears before any STORE_NAME of the same name in that class body.

Test plan

  • test_class_body_reads_globals_by_value (issue repro)
  • test_class_body_local_shadow_skips_unpicklable_global (shadowed threading.Lock)
  • Existing nested-globals / recursion tests still pass

Class bodies compile free-variable reads as LOAD_NAME (and sometimes
LOAD_FROM_DICT_OR_GLOBALS), not LOAD_GLOBAL. Include those loads when
extracting globals for by-value functions, but ignore LOAD_NAME names
that were already STOREd in the same class body so a local assignment
that shadows an unpicklable global does not break pickling.

Fixes cloudpipe#609.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant