-
-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f7d8e99
Move the C file whitespace check to pre-commit
AA-Turner 25688d9
unused variable
AA-Turner 6e9040c
Exclude vendored code
AA-Turner 0e20e9f
Fix tabs when run locally
AA-Turner b44f4c2
Exit with the right code
AA-Turner d4d31ff
zlib is no more
AA-Turner b70cece
More concise comment
hugovk 986f2a3
Merge branch 'main' into patchcheck/c-file-whitespace
AA-Turner b15ef27
Remove the now-redundant --ci option
AA-Turner 556978b
Remove unused imports, variables, and functions
AA-Turner 6891bdb
Verbose regular expression
AA-Turner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__': | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.)