MCPcopy Index your code
hub / github.com/numpy/numpy / check_python_h_included_first

Function check_python_h_included_first

tools/check_python_h_first.py:55–143  ·  view source on GitHub ↗

Check that the passed file includes Python.h first if it does at all. Perhaps overzealous, but that should work around concerns with recursion. Parameters ---------- name_to_check : str The name of the file to check. Returns ------- int The number o

(name_to_check: str)

Source from the content-addressed store, hash-verified

53
54
55def check_python_h_included_first(name_to_check: str) -> int:
56 """Check that the passed file includes Python.h first if it does at all.
57
58 Perhaps overzealous, but that should work around concerns with
59 recursion.
60
61 Parameters
62 ----------
63 name_to_check : str
64 The name of the file to check.
65
66 Returns
67 -------
68 int
69 The number of headers before Python.h
70 """
71 included_python = False
72 included_non_python_header = []
73 warned_python_construct = False
74 basename_to_check = os.path.basename(name_to_check)
75 in_comment = False
76 includes_headers = False
77 with open(name_to_check) as in_file:
78 for i, line in enumerate(in_file, 1):
79 # Very basic comment parsing
80 # Assumes /*...*/ comments are on their own lines
81 if "/*" in line:
82 if "*/" not in line:
83 in_comment = True
84 # else-branch could use regex to remove comment and continue
85 continue
86 if in_comment:
87 if "*/" in line:
88 in_comment = False
89 continue
90 line = line.split("//", 1)[0].strip()
91 match = HEADER_PATTERN.match(line)
92 if match:
93 includes_headers = True
94 this_header = match.group(1)
95 if this_header in PYTHON_INCLUDING_HEADERS:
96 if included_non_python_header and not included_python:
97 # Headers before python-including header
98 print(
99 f"Header before Python.h in file {name_to_check:s}\n"
100 f"Python.h on line {i:d}, other header(s) on line(s)"
101 f" {included_non_python_header}",
102 file=sys.stderr,
103 )
104 # else: # no headers before python-including header
105 included_python = True
106 PYTHON_INCLUDING_HEADERS.append(basename_to_check)
107 if os.path.dirname(name_to_check).endswith("include/numpy"):
108 PYTHON_INCLUDING_HEADERS.append(f"numpy/{basename_to_check:s}")
109 # We just found out where Python.h comes in this file
110 break
111 elif this_header in LEAF_HEADERS:
112 # This header is just defines, so it won't include

Callers 1

process_filesFunction · 0.85

Calls 5

openFunction · 0.85
stripMethod · 0.80
splitMethod · 0.80
endswithMethod · 0.80
lowerMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…