Skip to content

Commit a6163c1

Browse files
committed
TST: Add test for python-including files.
I should add an option to turn off the numpy detection inside numpy.
1 parent 48eec70 commit a6163c1

File tree

6 files changed

+24
-7
lines changed

6 files changed

+24
-7
lines changed

src/check_python_h_first/single_file.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def check_python_h_included_first(name_to_check: str) -> int:
7878
in_comment = False
7979
continue
8080
line = line.split("//", 1)[0].strip()
81+
if len(line) == 0:
82+
continue
8183
# Now that there's no comments, look for headers
8284
match = HEADER_PATTERN.match(line)
8385
if match:
@@ -123,7 +125,8 @@ def check_python_h_included_first(name_to_check: str) -> int:
123125
not included_python
124126
and not warned_python_construct
125127
and ".h" not in basename_to_check
126-
) and ("py::" in line or "PYBIND11_" in line):
128+
) and ("py::" in line or "PYBIND11_" in line
129+
or " npy_" in line or " Py" in line or line.startswith("Py")):
127130
print(
128131
"Python-including header not used before python constructs "
129132
f"in file {name_to_check:s}\nConstruct on line {i:d}",
@@ -132,4 +135,4 @@ def check_python_h_included_first(name_to_check: str) -> int:
132135
warned_python_construct = True
133136
if not includes_headers:
134137
LEAF_HEADERS.append(basename_to_check)
135-
return included_python and len(included_non_python_header)
138+
return (included_python and len(included_non_python_header)) or warned_python_construct

tests/bad_extension.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
const float PI = npy_atanf(1.0) * 4f;
3+
4+
PyMODINIT_Func(&func)
5+
/** I don't actually remember what's supposed to go here
6+
fortunately this doesn't need to compile */

tests/good_extension.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** Test file to do things */
2+
#include "myheader.h"
3+
4+
PyMODINIT_Func(&func)
5+
/** I don't actually remember what's supposed to go here
6+
fortunately this doesn't need to compile */

tests/myheader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
#include <Python.h>
1+
#include <Python.h> // This needs to be first
2+
#include <numpy/npy_common.h>

tests/test_single_file.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
def test_files(file: str):
1818
"""Test whether function on a single file."""
1919
actual = check_python_h_included_first(file)
20-
if os.path.basename(file).startswith("system"):
20+
file_basename = os.path.basename(file)
21+
if file_basename.startswith("system") or file_basename.startswith("bad"):
2122
assert actual > 0
2223
else:
2324
assert actual == 0

tests/test_wrapper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_process_files():
4242
"""
4343
result = process_files(HEADER_LIST + SOURCE_LIST)
4444
assert result == sum(
45-
os.path.basename(name).startswith("system")
45+
os.path.basename(name).startswith("system") or os.path.basename(name).startswith("bad")
4646
for name in HEADER_LIST + SOURCE_LIST
4747
)
4848

@@ -55,5 +55,5 @@ def test_process_files():
5555
)
5656
def test_diff_files():
5757
"""Test whether diff_files picks up the correct files."""
58-
new_files = [os.path.basename(name) for name in diff_files("c8bda44")]
59-
assert new_files == ["system_numpy.c"]
58+
new_files = {os.path.basename(name) for name in diff_files("48eec70")}
59+
assert new_files == {"myheader.h", "bad_extension.c", "good_extension.c"}

0 commit comments

Comments
 (0)