| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import tempfile |
| 5 |
import unittest |
| 6 |
|
| 7 |
from mypyc.build import get_header_deps, resolve_cfile_deps |
| 8 |
from mypyc.ir.ops import BasicBlock |
| 9 |
from mypyc.ir.pprint import format_blocks, generate_names_for_ir |
| 10 |
from mypyc.irbuild.ll_builder import LowLevelIRBuilder |
| 11 |
from mypyc.options import CompilerOptions |
| 12 |
|
| 13 |
|
| 14 |
class TestMisc(unittest.TestCase): |
| 15 |
def test_debug_op(self) -> None: |
| 16 |
block = BasicBlock() |
| 17 |
builder = LowLevelIRBuilder( |
| 18 |
errors=None, options=CompilerOptions(strict_traceback_checks=True) |
| 19 |
) |
| 20 |
builder.activate_block(block) |
| 21 |
builder.debug_print("foo") |
| 22 |
|
| 23 |
names = generate_names_for_ir([], [block]) |
| 24 |
code = format_blocks([block], names, {}) |
| 25 |
assert code[:-1] == ["L0:", " r0 = 'foo'", " CPyDebug_PrintObject(r0)"] |
| 26 |
|
| 27 |
|
| 28 |
class TestHeaderDeps(unittest.TestCase): |
| 29 |
""" |
| 30 |
Tests for the header-dependency tracking used to build `Extension.depends`, which drives |
| 31 |
setuptools' `newer_group` decision about whether to recompile a .o file on incremental builds. |
| 32 |
""" |
| 33 |
|
| 34 |
def test_get_header_deps_quoted_includes(self) -> None: |
| 35 |
# Quoted includes: the historical form. Used by the .c file to reach its own __native_<mod>.h / |
| 36 |
# __native_internal_<mod>.h. The `False` in each tuple marks the include as non-angled, which |
| 37 |
# `resolve_cfile_deps` uses to search the includer's directory. |
| 38 |
cfile = '#include "__native_caller.h"\n#include "__native_internal_caller.h"\n' |
| 39 |
assert get_header_deps([("caller.c", cfile)]) == [ |
| 40 |
(False, "__native_caller.h"), |
| 41 |
(False, "__native_internal_caller.h"), |
| 42 |
] |
| 43 |
|
| 44 |
def test_get_header_deps_angle_bracket_includes(self) -> None: |
| 45 |
# Angle-bracket includes are also matched, and reported with is_angled=True so that the resolver |
| 46 |
# skips the includer's dir for them (matching the C preprocessor). The cross-group export header |
| 47 |
# is reached via `#include <other_group/__native_other.h>` in __native_internal_<mod>.h. Before |
| 48 |
# this was matched the dep was missed entirely and the consumer's .o was never invalidated when |
| 49 |
# the other group's struct layout shifted. |
| 50 |
cfile = "#include <Python.h>\n#include <lib/__native_functions.h>\n" |
| 51 |
assert get_header_deps([("caller.c", cfile)]) == [ |
| 52 |
(True, "Python.h"), |
| 53 |
(True, "lib/__native_functions.h"), |
| 54 |
] |
| 55 |
|
| 56 |
def test_get_header_deps_mixed_and_whitespace(self) -> None: |
| 57 |
# The preprocessor tolerates whitespace and the leading-hash form. `get_header_deps` returns sorted |
| 58 |
# tuples — non-angled (False) sorts before angled (True), then alphabetical within each kind. |
| 59 |
cfile = '# include "a.h"\n# include <b.h>\n#include\t"c.h"\n' |
| 60 |
assert get_header_deps([("x.c", cfile)]) == [(False, "a.h"), (False, "c.h"), (True, "b.h")] |
| 61 |
|
| 62 |
def test_resolve_walks_transitively_through_headers(self) -> None: |
| 63 |
# Reproduces the bug scenario: caller's .c only directly includes caller's own headers, but |
| 64 |
# caller's __native_internal_caller.h includes the cross-group export header. The resolver |
| 65 |
# must follow that chain so setuptools sees the cross-group header as a dep. |
| 66 |
with tempfile.TemporaryDirectory() as tmp: |
| 67 |
build_dir = tmp |
| 68 |
os.makedirs(os.path.join(build_dir, "lib")) |
| 69 |
os.makedirs(os.path.join(build_dir, "other_group")) |
| 70 |
|
| 71 |
# caller.c's directly-included headers, both live alongside |
| 72 |
# caller.c under build/ (resolved via target_dir). |
| 73 |
internal_h = os.path.join(build_dir, "__native_internal_caller.h") |
| 74 |
caller_h = os.path.join(build_dir, "__native_caller.h") |
| 75 |
cross_group_h = os.path.join(build_dir, "lib", "__native_functions.h") |
| 76 |
unrelated_h = os.path.join(build_dir, "other_group", "__native_other.h") |
| 77 |
|
| 78 |
with open(caller_h, "w") as f: |
| 79 |
# Headers outside build/ (CPython's <Python.h>, lib-rt's <CPy.h>) don't resolve under |
| 80 |
# target_dir, so they get dropped during resolution and aren't recursed into. |
| 81 |
f.write("#include <Python.h>\n#include <CPy.h>\n") |
| 82 |
with open(internal_h, "w") as f: |
| 83 |
# This header includes a header in another group via angle brackets. Pre-fix, this dep |
| 84 |
# was invisible to setuptools. |
| 85 |
f.write( |
| 86 |
"#include <Python.h>\n" |
| 87 |
'#include "__native_caller.h"\n' |
| 88 |
"#include <lib/__native_functions.h>\n" |
| 89 |
) |
| 90 |
with open(cross_group_h, "w") as f: |
| 91 |
f.write("struct export_table_lib___functions { int x; };\n") |
| 92 |
with open(unrelated_h, "w") as f: |
| 93 |
# Sibling group not reached from caller's chain => must NOT appear in the resolved set. |
| 94 |
f.write("struct unrelated { int x; };\n") |
| 95 |
|
| 96 |
# caller.c is in build_dir, so its includer-dir is build_dir. Both directly-included headers |
| 97 |
# are quoted (`False`); the cross-group header that __native_internal_caller.h reaches via |
| 98 |
# `<lib/__native_functions.h>` is found by the recursive walk re-reading the on-disk header. |
| 99 |
deps = resolve_cfile_deps( |
| 100 |
cfile_dir=build_dir, |
| 101 |
direct_includes=[ |
| 102 |
(False, "__native_caller.h"), |
| 103 |
(False, "__native_internal_caller.h"), |
| 104 |
], |
| 105 |
target_dir=build_dir, |
| 106 |
) |
| 107 |
|
| 108 |
assert deps == {caller_h, internal_h, cross_group_h}, ( |
| 109 |
f"expected the cross-group header to be reached transitively; " |
| 110 |
f"got {sorted(deps)!r}" |
| 111 |
) |
| 112 |
|
| 113 |
def test_resolve_drops_unresolvable_includes(self) -> None: |
| 114 |
# `<Python.h>`, `<CPy.h>`, etc. don't live under target_dir, so they're dropped from depends. They |
| 115 |
# never change between builds, so this is the right behavior. Crucially, it stops setuptools' |
| 116 |
# `missing="newer"` from treating them as always-newer and force-rebuilding every translation unit. |
| 117 |
with tempfile.TemporaryDirectory() as tmp: |
| 118 |
cfile_dir = tmp |
| 119 |
deps = resolve_cfile_deps( |
| 120 |
cfile_dir=cfile_dir, |
| 121 |
direct_includes=[(True, "Python.h"), (True, "CPy.h"), (False, "init.c")], |
| 122 |
target_dir=cfile_dir, |
| 123 |
) |
| 124 |
assert deps == set() |
| 125 |
|
| 126 |
def test_resolve_search_order_matches_preprocessor(self) -> None: |
| 127 |
# When the same header name exists both next to the includer and under target_dir, the C preprocessor |
| 128 |
# picks the includer-dir copy for `#include "shared.h"` and the target_dir copy for `#include <shared.h>`. |
| 129 |
# The resolver must record the same path the compiler will actually consume, otherwise mtimes of the |
| 130 |
# wrong file drive incremental rebuild decisions. |
| 131 |
with tempfile.TemporaryDirectory() as tmp: |
| 132 |
includer = os.path.join(tmp, "groupA") |
| 133 |
target = os.path.join(tmp, "build") |
| 134 |
os.makedirs(includer) |
| 135 |
os.makedirs(target) |
| 136 |
|
| 137 |
local_h = os.path.join(includer, "shared.h") |
| 138 |
global_h = os.path.join(target, "shared.h") |
| 139 |
with open(local_h, "w") as f: |
| 140 |
f.write("/* local */\n") |
| 141 |
with open(global_h, "w") as f: |
| 142 |
f.write("/* global */\n") |
| 143 |
|
| 144 |
# Quoted form: resolves to the includer-dir copy. |
| 145 |
assert resolve_cfile_deps( |
| 146 |
cfile_dir=includer, direct_includes=[(False, "shared.h")], target_dir=target |
| 147 |
) == {local_h} |
| 148 |
|
| 149 |
# Angled form: skips the includer-dir copy, resolves under -I. |
| 150 |
assert resolve_cfile_deps( |
| 151 |
cfile_dir=includer, direct_includes=[(True, "shared.h")], target_dir=target |
| 152 |
) == {global_h} |