From fd8ddd449d415cecba27d27fb2875db3e4395511 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:40:08 +0100 Subject: [PATCH 1/3] [3.11] GH-109408: Move the C file whitespace check from patchcheck to pre-commit (GH-109890) Co-authored-by: Hugo van Kemenade . (cherry picked from commit f5edb56328b46f262b74a53343b8098a3934f761) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- .pre-commit-config.yaml | 26 +++++++++ Tools/scripts/patchcheck.py | 108 ++++-------------------------------- Tools/scripts/untabify.py | 10 ++-- 3 files changed, 41 insertions(+), 103 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1ae3dd71354e63..53e692187806ce 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,6 +16,32 @@ repos: - id: trailing-whitespace types_or: [c, inc, python, rst] + - repo: local + hooks: + - id: python-file-whitespace + name: "Check Python file whitespace" + entry: 'python Tools/patchcheck/reindent.py --nobackup --newline LF' + language: 'system' + types: [python] + exclude: '^(Lib/test/tokenizedata/|Tools/c-analyzer/cpython/_parser).*$' + + - repo: local + hooks: + - id: c-file-whitespace + name: "Check C file whitespace" + entry: "python Tools/patchcheck/untabify.py" + language: "system" + types_or: ['c', 'c++'] + # Don't check the style of vendored libraries + exclude: | + (?x)^( + Modules/_ctypes/libffi_osx/.* + | Modules/_ctypes/libffi_msvc/.* + | Modules/_decimal/.* + | Modules/libmpdec/.* + | Modules/expat/.* + )$ + - repo: https://github.com/sphinx-contrib/sphinx-lint rev: v0.6.8 hooks: diff --git a/Tools/scripts/patchcheck.py b/Tools/scripts/patchcheck.py index c53dd45d8601d3..95b5bc0cf39c97 100755 --- a/Tools/scripts/patchcheck.py +++ b/Tools/scripts/patchcheck.py @@ -1,30 +1,15 @@ #!/usr/bin/env python3 """Check proposed changes for common issues.""" -import re import sys -import shutil import os.path import subprocess import sysconfig -import reindent -import untabify - - def get_python_source_dir(): src_dir = sysconfig.get_config_var('abs_srcdir') if not src_dir: src_dir = sysconfig.get_config_var('srcdir') return os.path.abspath(src_dir) - - -# Excluded directories which are copies of external libraries: -# don't check their coding style -EXCLUDE_DIRS = [os.path.join('Modules', '_ctypes', 'libffi_osx'), - os.path.join('Modules', '_ctypes', 'libffi_msvc'), - os.path.join('Modules', '_decimal', 'libmpdec'), - os.path.join('Modules', 'expat'), - os.path.join('Modules', 'zlib')] SRCDIR = get_python_source_dir() @@ -154,50 +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) - - return filenames2 - - -def report_modified_files(file_paths): - count = len(file_paths) - if count == 0: - return n_files_str(count) - else: - lines = [f"{n_files_str(count)}:"] - for path in file_paths: - lines.append(f" {path}") - return "\n".join(lines) - - -@status("Fixing Python file whitespace", info=report_modified_files) -def normalize_whitespace(file_paths): - """Make sure that the whitespace for .py files have been normalized.""" - reindent.makebackup = False # No need to create backups. - fixed = [path for path in file_paths if path.endswith('.py') and - reindent.check(os.path.join(SRCDIR, path))] - 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 + # Normalize the path to be able to match using str.startswith() + return list(map(os.path.normpath, filenames)) @status("Docs modified", modal=True) @@ -237,38 +180,12 @@ def regenerated_pyconfig_h_in(file_paths): return "not needed" -def ci(pull_request): - if pull_request == 'false': - print('Not a pull request; skipping') - return - 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'))] - fixed = [] - fixed.extend(normalize_whitespace(python_files)) - fixed.extend(normalize_c_whitespace(c_files)) - if not fixed: - print('No whitespace issues found') - else: - count = len(fixed) - print(f'Please fix the {n_files_str(count)} with whitespace issues') - print('(on Unix you can run `make patchcheck` to make the fixes)') - sys.exit(1) - - 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) # Docs updated. docs_modified(doc_files) # Misc/ACKS changed. @@ -281,19 +198,14 @@ 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'))) + has_python_files = any(fn for fn in file_paths if fn.endswith('.py')) + print() + if has_c_files: + print("Did you run the test suite and check for refleaks?") + elif has_python_files: + print("Did you run the test suite?") if __name__ == '__main__': - import argparse - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument('--ci', - help='Perform pass/fail checks') - args = parser.parse_args() - if args.ci: - ci(args.ci) - else: - main() + main() diff --git a/Tools/scripts/untabify.py b/Tools/scripts/untabify.py index 861c83ced90f24..5c9d1208540830 100755 --- a/Tools/scripts/untabify.py +++ b/Tools/scripts/untabify.py @@ -21,8 +21,7 @@ def main(): if optname == '-t': tabsize = int(optvalue) - for filename in args: - process(filename, tabsize) + return max(process(filename, tabsize) for filename in args) def process(filename, tabsize, verbose=True): @@ -32,10 +31,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 +48,8 @@ def process(filename, tabsize, verbose=True): f.write(newtext) if verbose: print(filename) + return 1 if __name__ == '__main__': - main() + raise SystemExit(main()) From afd9d760d45e3814eca27dc9dc43127fd9d048db Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:50:01 +0100 Subject: [PATCH 2/3] fix the directory --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 53e692187806ce..196b08a78aed49 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,7 +20,7 @@ repos: hooks: - id: python-file-whitespace name: "Check Python file whitespace" - entry: 'python Tools/patchcheck/reindent.py --nobackup --newline LF' + entry: 'python Tools/scripts/reindent.py --nobackup --newline LF' language: 'system' types: [python] exclude: '^(Lib/test/tokenizedata/|Tools/c-analyzer/cpython/_parser).*$' @@ -29,7 +29,7 @@ repos: hooks: - id: c-file-whitespace name: "Check C file whitespace" - entry: "python Tools/patchcheck/untabify.py" + entry: "python Tools/scripts/untabify.py" language: "system" types_or: ['c', 'c++'] # Don't check the style of vendored libraries From 707ffaa5b16a8b2e8c5b37bcccb114a6522817fb Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:37:34 +0100 Subject: [PATCH 3/3] Fix excludes --- .pre-commit-config.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 196b08a78aed49..4bc0747a129064 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,7 +23,7 @@ repos: entry: 'python Tools/scripts/reindent.py --nobackup --newline LF' language: 'system' types: [python] - exclude: '^(Lib/test/tokenizedata/|Tools/c-analyzer/cpython/_parser).*$' + exclude: '^(Lib/test/tokenizedata/|Tools/c-analyzer/cpython/(_parser|_capi)).*$' - repo: local hooks: @@ -35,7 +35,8 @@ repos: # Don't check the style of vendored libraries exclude: | (?x)^( - Modules/_ctypes/libffi_osx/.* + Modules/_ctypes/darwin/dlfcn.h + | Modules/_ctypes/libffi_osx/.* | Modules/_ctypes/libffi_msvc/.* | Modules/_decimal/.* | Modules/libmpdec/.* pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy