Skip to content

Commit 8fae935

Browse files
authored
[mypyc] Add missing header includes in lib-rt files (#21046)
These headers use objects from `stdbool.h` and `mypyc_util.h` without including them so they currently rely on the dependencies being included somewhere earlier in the compiled .c file. However compiling just `stringwriter_extra_ops.c` or `byteswriter_extra_ops.c` with `MYPYC_EXPERIMENTAL` defined results in errors. Added a test to compile every .c file in mypyc/lib-rt individually with and without `MYPYC_EXPERIMENTAL` defined to catch these issues in the future. ChatGPT wrote the test and I have verified that removing a necessary header include fails it.
1 parent 5c6cd29 commit 8fae935

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

mypyc/lib-rt/byteswriter_extra_ops.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
#ifdef MYPYC_EXPERIMENTAL
55

6+
#include <stdbool.h>
67
#include <stdint.h>
78
#include <Python.h>
89

10+
#include "mypyc_util.h"
911
#include "strings/librt_strings.h"
1012
#include "strings/librt_strings_common.h"
1113

mypyc/lib-rt/strings/librt_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import_librt_strings(void)
1212

1313
#else // MYPYC_EXPERIMENTAL
1414

15+
#include <stdbool.h>
1516
#include <Python.h>
1617
#include "librt_strings_common.h"
1718

mypyc/lib-rt/stringwriter_extra_ops.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
#ifdef MYPYC_EXPERIMENTAL
55

6+
#include <stdbool.h>
67
#include <stdint.h>
78
#include <Python.h>
89

10+
#include "mypyc_util.h"
911
#include "strings/librt_strings.h"
1012

1113
static inline CPyTagged

mypyc/test/test_external.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,62 @@
77
import sys
88
import tempfile
99
import unittest
10+
from distutils import ccompiler, sysconfig
11+
from typing import Any
12+
13+
from mypyc.build import get_cflags, include_dir
1014

1115
base_dir = os.path.join(os.path.dirname(__file__), "..", "..")
16+
EXCLUDED_LIB_RT_COMPILE_FILES = ["static_data.c"]
1217

1318

1419
class TestExternal(unittest.TestCase):
20+
def test_lib_rt_c_files_compile_individually(self) -> None:
21+
"""Compile each top-level lib-rt C file as its own translation unit."""
22+
lib_rt_dir = include_dir()
23+
source_names = sorted(
24+
name
25+
for name in os.listdir(lib_rt_dir)
26+
if name.endswith(".c")
27+
and os.path.isfile(os.path.join(lib_rt_dir, name))
28+
and name not in EXCLUDED_LIB_RT_COMPILE_FILES
29+
)
30+
compiler: Any = ccompiler.new_compiler()
31+
sysconfig.customize_compiler(compiler)
32+
33+
include_dirs = [lib_rt_dir]
34+
for plat_specific in (False, True):
35+
path = sysconfig.get_python_inc(plat_specific=plat_specific)
36+
if path and path not in include_dirs:
37+
include_dirs.append(path)
38+
39+
with tempfile.TemporaryDirectory() as tmpdir:
40+
for experimental_features in (False, True):
41+
cflags = get_cflags(
42+
compiler_type=compiler.compiler_type,
43+
opt_level="0",
44+
experimental_features=experimental_features,
45+
)
46+
output_dir = os.path.join(
47+
tmpdir, "experimental" if experimental_features else "default"
48+
)
49+
50+
for source_name in source_names:
51+
source_path = os.path.join(lib_rt_dir, source_name)
52+
with self.subTest(source=source_name, experimental=experimental_features):
53+
try:
54+
compiler.compile(
55+
[source_path],
56+
output_dir=output_dir,
57+
include_dirs=include_dirs,
58+
extra_postargs=cflags,
59+
)
60+
except Exception as err:
61+
raise AssertionError(
62+
f"failed to compile {source_name} "
63+
f"(experimental={experimental_features})"
64+
) from err
65+
1566
# TODO: Get this to work on Windows.
1667
# (Or don't. It is probably not a good use of time.)
1768
@unittest.skipIf(sys.platform.startswith("win"), "rt tests don't work on windows")

0 commit comments

Comments
 (0)