@@ -13,3 +13,77 @@ def f2():
1313 """
1414
1515assert f2 .__doc__ == '\n x\n y\n '
16+
17+
18+ # Regression test: a module docstring must be stored through module name
19+ # resolution, so a nested `global __doc__` declaration makes the module body
20+ # emit STORE_GLOBAL (writing module globals) instead of STORE_NAME (writing
21+ # the provided locals mapping). Mirrors CPython codegen_nameop behavior.
22+
23+ import dis
24+
25+
26+ def opnames (code ):
27+ return [instr .opname for instr in dis .get_instructions (code )]
28+
29+
30+ def run_split (src , optimize = 0 ):
31+ code = compile (src , "<test>" , "exec" , optimize = optimize )
32+ g , loc = {}, {}
33+ exec (code , g , loc )
34+ return code , g , loc
35+
36+
37+ # A nested function declaring `global __doc__` makes the module docstring
38+ # store target module globals.
39+ src_global = '"module doc"\n def f():\n global __doc__\n '
40+ code , g , loc = run_split (src_global )
41+ ops = opnames (code )
42+ assert "STORE_GLOBAL" in ops , ops
43+ assert "STORE_NAME" not in ops or "__doc__" not in [
44+ instr .argval for instr in dis .get_instructions (code ) if instr .opname == "STORE_NAME"
45+ ], ops
46+ assert g .get ("__doc__" ) == "module doc" , g
47+ assert "__doc__" not in loc , loc
48+
49+ # The declaration may be nested more than one scope deep.
50+ src_deep = '"module doc"\n def f():\n def g():\n global __doc__\n '
51+ code , g , loc = run_split (src_deep )
52+ assert "STORE_GLOBAL" in opnames (code ), opnames (code )
53+ assert g .get ("__doc__" ) == "module doc" , g
54+ assert "__doc__" not in loc , loc
55+
56+ # Control: without a nested global declaration the docstring stays a
57+ # STORE_NAME and lands in the provided locals mapping.
58+ src_plain = '"module doc"\n x = 1\n '
59+ code , g , loc = run_split (src_plain )
60+ doc_stores = [
61+ instr .opname
62+ for instr in dis .get_instructions (code )
63+ if instr .argval == "__doc__" and instr .opname .startswith ("STORE_" )
64+ ]
65+ assert doc_stores == ["STORE_NAME" ], doc_stores
66+ assert loc .get ("__doc__" ) == "module doc" , loc
67+ assert "__doc__" not in g , g
68+
69+ # optimize=1 keeps the docstring and the same global resolution.
70+ code , g , loc = run_split (src_global , optimize = 1 )
71+ assert "STORE_GLOBAL" in opnames (code ), opnames (code )
72+ assert g .get ("__doc__" ) == "module doc" , g
73+
74+ # optimize=2 strips the docstring entirely: no __doc__ store in either dict.
75+ code , g , loc = run_split (src_global , optimize = 2 )
76+ assert not any (
77+ instr .argval == "__doc__" and instr .opname .startswith ("STORE_" )
78+ for instr in dis .get_instructions (code )
79+ ), opnames (code )
80+ assert "__doc__" not in g and "__doc__" not in loc , (g , loc )
81+
82+ # A class docstring still belongs to the class namespace even when a nested
83+ # scope declares `global __doc__`.
84+ src_class = '"module doc"\n def f():\n global __doc__\n class C:\n "class doc"\n '
85+ g , loc = {}, {}
86+ exec (compile (src_class , "<test>" , "exec" ), g , loc )
87+ assert g .get ("__doc__" ) == "module doc" , g
88+ assert loc ["C" ].__doc__ == "class doc" , loc ["C" ].__doc__
89+ assert "__doc__" not in loc , loc
0 commit comments