|
| 1 | +"""Check that Python.h is included before any stdlib headers. |
| 2 | +
|
| 3 | +May be a bit overzealous, but it should get the job done. |
| 4 | +""" |
| 5 | + |
| 6 | +import os.path |
| 7 | +import re |
| 8 | +import sys |
| 9 | + |
| 10 | +HEADER_PATTERN = re.compile( |
| 11 | + r'^\s*#\s*include\s*[<"]((?:\w+/)*\w+(?:\.h[hp+]{0,2})?)[>"]\s*$' |
| 12 | +) |
| 13 | + |
| 14 | +PYTHON_INCLUDING_HEADERS = [ |
| 15 | + "Python.h", |
| 16 | + # This isn't all of Python.h, but it is the visibility macros |
| 17 | + "pyconfig.h", |
| 18 | + # NumPy |
| 19 | + "numpy/npy_common.h", |
| 20 | + "numpy/npy_math.h", |
| 21 | + "numpy/arrayobject.h", |
| 22 | + "numpy/ndarrayobject.h", |
| 23 | + "numpy/ndarraytypes.h", |
| 24 | + "numpy/random/distributions.h", |
| 25 | + # Pybind |
| 26 | + "pybind11/pybind11.h", |
| 27 | + # Boost::Python |
| 28 | + "boost/python.hpp", |
| 29 | + # Pythran |
| 30 | + "pythonic/core.hpp", |
| 31 | + # xsf::numpy |
| 32 | + "xsf/numpy.h", |
| 33 | +] |
| 34 | +LEAF_HEADERS = [ |
| 35 | + "numpy/numpyconfig.h", |
| 36 | + "numpy/npy_os.h", |
| 37 | + "numpy/npy_cpu.h", |
| 38 | + "numpy/utils.h", |
| 39 | +] |
| 40 | + |
| 41 | + |
| 42 | +def check_python_h_included_first(name_to_check: str) -> int: |
| 43 | + """Check that the passed file includes Python.h first if it does at all. |
| 44 | +
|
| 45 | + Perhaps overzealous, but that should work around concerns with |
| 46 | + recursion. |
| 47 | +
|
| 48 | + Parameters |
| 49 | + ---------- |
| 50 | + name_to_check : str |
| 51 | + The name of the file to check. |
| 52 | +
|
| 53 | + Returns |
| 54 | + ------- |
| 55 | + int |
| 56 | + The number of headers before Python.h |
| 57 | + """ |
| 58 | + included_python = False |
| 59 | + included_non_python_header = [] |
| 60 | + warned_python_construct = False |
| 61 | + basename_to_check = os.path.basename(name_to_check) |
| 62 | + in_comment = False |
| 63 | + includes_headers = False |
| 64 | + with open(name_to_check) as in_file: |
| 65 | + for i, line in enumerate(in_file, 1): |
| 66 | + # Very basic comment parsing |
| 67 | + # Assumes /*...*/ comments are on their own lines |
| 68 | + if "/*" in line: |
| 69 | + if "*/" not in line: |
| 70 | + in_comment = True |
| 71 | + # else-branch could use regex to remove comment and continue |
| 72 | + continue |
| 73 | + if in_comment: |
| 74 | + if "*/" in line: |
| 75 | + in_comment = False |
| 76 | + continue |
| 77 | + line = line.split("//", 1)[0].strip() |
| 78 | + # Now that there's no comments, look for headers |
| 79 | + match = HEADER_PATTERN.match(line) |
| 80 | + if match: |
| 81 | + includes_headers = True |
| 82 | + this_header = match.group(1) |
| 83 | + if this_header in PYTHON_INCLUDING_HEADERS: |
| 84 | + if included_non_python_header and not included_python: |
| 85 | + # Headers before python-including header |
| 86 | + print( |
| 87 | + f"Header before Python.h in file {name_to_check:s}\n" |
| 88 | + f"Python.h on line {i:d}, other header(s) on line(s)" |
| 89 | + f" {included_non_python_header}", |
| 90 | + file=sys.stderr, |
| 91 | + ) |
| 92 | + # else: # no headers before python-including header |
| 93 | + included_python = True |
| 94 | + PYTHON_INCLUDING_HEADERS.append(basename_to_check) |
| 95 | + if os.path.dirname(name_to_check).endswith("include/numpy"): |
| 96 | + PYTHON_INCLUDING_HEADERS.append(f"numpy/{basename_to_check:s}") |
| 97 | + # We just found out where Python.h comes in this file |
| 98 | + break |
| 99 | + elif this_header in LEAF_HEADERS: |
| 100 | + # This header is just defines, so it won't include |
| 101 | + # the system headers that cause problems |
| 102 | + continue |
| 103 | + elif not included_python and ( |
| 104 | + "numpy/" in this_header |
| 105 | + and this_header not in LEAF_HEADERS |
| 106 | + or "python" in this_header.lower() |
| 107 | + or "pybind" in this_header |
| 108 | + ): |
| 109 | + print( |
| 110 | + f"Python.h not included before python-including header " |
| 111 | + f"in file {name_to_check:s}\n" |
| 112 | + f"{this_header:s} on line {i:d}", |
| 113 | + file=sys.stderr, |
| 114 | + ) |
| 115 | + included_python = True |
| 116 | + PYTHON_INCLUDING_HEADERS.append(basename_to_check) |
| 117 | + elif not included_python and this_header not in LEAF_HEADERS: |
| 118 | + included_non_python_header.append(i) |
| 119 | + elif ( |
| 120 | + not included_python |
| 121 | + and not warned_python_construct |
| 122 | + and ".h" not in basename_to_check |
| 123 | + ) and ("py::" in line or "PYBIND11_" in line): |
| 124 | + print( |
| 125 | + "Python-including header not used before python constructs " |
| 126 | + f"in file {name_to_check:s}\nConstruct on line {i:d}", |
| 127 | + file=sys.stderr, |
| 128 | + ) |
| 129 | + warned_python_construct = True |
| 130 | + if not includes_headers: |
| 131 | + LEAF_HEADERS.append(basename_to_check) |
| 132 | + return included_python and len(included_non_python_header) |
0 commit comments