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