-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
GH-109408: Move the C file whitespace check from patchcheck to pre-commit #109890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
f7d8e99
25688d9
6e9040c
0e20e9f
b44f4c2
d4d31ff
b70cece
986f2a3
b15ef27
556978b
6891bdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,16 +8,8 @@ | |
import sysconfig | ||
|
||
import reindent | ||
import untabify | ||
|
||
|
||
# Excluded directories which are copies of external libraries: | ||
# don't check their coding style | ||
EXCLUDE_DIRS = [ | ||
os.path.join('Modules', '_decimal', 'libmpdec'), | ||
os.path.join('Modules', 'expat'), | ||
os.path.join('Modules', 'zlib'), | ||
] | ||
SRCDIR = sysconfig.get_config_var('srcdir') | ||
|
||
|
||
|
@@ -147,15 +139,8 @@ def changed_files(base_branch=None): | |
else: | ||
sys.exit('need a git checkout to get modified files') | ||
|
||
filenames2 = [] | ||
for filename in filenames: | ||
# Normalize the path to be able to match using .startswith() | ||
filename = os.path.normpath(filename) | ||
if any(filename.startswith(path) for path in EXCLUDE_DIRS): | ||
# Exclude the file | ||
continue | ||
filenames2.append(filename) | ||
|
||
# Normalize the path to be able to match using .startswith() | ||
filenames2 = list(map(os.path.normpath, filenames)) | ||
return filenames2 | ||
|
||
|
||
|
@@ -191,20 +176,6 @@ def normalize_whitespace(file_paths): | |
return fixed | ||
|
||
|
||
@status("Fixing C file whitespace", info=report_modified_files) | ||
def normalize_c_whitespace(file_paths): | ||
"""Report if any C files """ | ||
fixed = [] | ||
for path in file_paths: | ||
abspath = os.path.join(SRCDIR, path) | ||
with open(abspath, 'r') as f: | ||
if '\t' not in f.read(): | ||
continue | ||
untabify.process(abspath, 8, verbose=False) | ||
fixed.append(path) | ||
return fixed | ||
|
||
|
||
ws_re = re.compile(br'\s+(\r?\n)$') | ||
|
||
@status("Fixing docs whitespace", info=report_modified_files) | ||
|
@@ -267,12 +238,10 @@ def ci(pull_request): | |
base_branch = get_base_branch() | ||
file_paths = changed_files(base_branch) | ||
python_files = [fn for fn in file_paths if fn.endswith('.py')] | ||
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] | ||
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and | ||
fn.endswith(('.rst', '.inc'))] | ||
fixed = [] | ||
fixed.extend(normalize_whitespace(python_files)) | ||
fixed.extend(normalize_c_whitespace(c_files)) | ||
fixed.extend(normalize_docs_whitespace(doc_files)) | ||
if not fixed: | ||
print('No whitespace issues found') | ||
|
@@ -285,14 +254,11 @@ def main(): | |
base_branch = get_base_branch() | ||
file_paths = changed_files(base_branch) | ||
python_files = [fn for fn in file_paths if fn.endswith('.py')] | ||
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] | ||
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and | ||
fn.endswith(('.rst', '.inc'))] | ||
misc_files = {p for p in file_paths if p.startswith('Misc')} | ||
# PEP 8 whitespace rules enforcement. | ||
normalize_whitespace(python_files) | ||
# C rules enforcement. | ||
normalize_c_whitespace(c_files) | ||
# Doc whitespace enforcement. | ||
normalize_docs_whitespace(doc_files) | ||
# Docs updated. | ||
|
@@ -307,10 +273,12 @@ def main(): | |
regenerated_pyconfig_h_in(file_paths) | ||
|
||
# Test suite run and passed. | ||
if python_files or c_files: | ||
end = " and check for refleaks?" if c_files else "?" | ||
print() | ||
print("Did you run the test suite" + end) | ||
has_c_files = any(fn for fn in file_paths if fn.endswith(('.c', '.h'))) | ||
print() | ||
if has_c_files: | ||
print("Did you run the test suite and check for refleaks?") | ||
elif python_files: | ||
print("Did you run the test suite?") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's so much clearer like this! 👍 |
||
|
||
|
||
if __name__ == '__main__': | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,10 +32,10 @@ def process(filename, tabsize, verbose=True): | |
encoding = f.encoding | ||
except IOError as msg: | ||
print("%r: I/O error: %s" % (filename, msg)) | ||
return | ||
return 2 | ||
newtext = text.expandtabs(tabsize) | ||
if newtext == text: | ||
return | ||
return 0 | ||
backup = filename + "~" | ||
try: | ||
os.unlink(backup) | ||
|
@@ -49,7 +49,8 @@ def process(filename, tabsize, verbose=True): | |
f.write(newtext) | ||
if verbose: | ||
print(filename) | ||
return 1 | ||
|
||
|
||
if __name__ == '__main__': | ||
main() | ||
raise SystemExit(main()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I didn't realise! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We were running on
.c
and.h
files before:Both of those are matched by the
c
type, so this would be closer to parity:We do have half a dozen
.cpp
files in the codebase, do we want to expand to include them? Should we also addc++
fortrailing-whitespace
above?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test passed with 'c++' included, I imagined that it was a previous oversight that they weren't included. I'll check if the trailing whitespace check also passes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tools/msi/bundle/bootstrap/PythonBootstrapperApplication.cpp
fails with 16 lines changed. Other than that all good (@zooba would you be alright with us enabling the trailing whitespace check here? No real views either way, if you'd prefer to keep the whitespace then that's the status quo anyway!)A
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(We can always remove
c++
later if needed.)