Skip to content

Conversation

@AA-Turner
Copy link
Member

@AA-TurnerAA-Turner commented Sep 26, 2023

@AA-TurnerAA-Turner changed the title gh-109408: Move the C file whitespace check from patchcheck to pre-commitGH-109408: Move the C file whitespace check from patchcheck to pre-commitSep 26, 2023
Copy link
Member

@AlexWaygoodAlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the one hand, the patchcheck script will currently autofix this issue for you if run it locally, and it seems a shame to delete that functionality without adding it to pre-commit. On the other hand, I like having this check as part of pre-commit, and I also somewhat doubt that many people are trying to commit C source code that uses tabs in it into CPython -- so maybe the ability to autofix tabs isn't actually that useful after all.

@AA-Turner
Copy link
MemberAuthor

We could change it to run python -c "python -c "import sys; n=(t:=(f:=open(sys.argv[1],'r+b')).read()).replace(b'\t',b' '*8); f.seek(0); f.write(n); sys.exit(t!=n)" as a 'system' script (or add a script in Tools/scripts to achieve the same effect) -- I believe that this would alter the source files if run locally.

A

@AlexWaygood
Copy link
Member

We could change it to run python -c "python -c "import sys; n=(t:=(f:=open(sys.argv[1],'r+b')).read()).replace(b'\t',b' '*8); f.seek(0); f.write(n); sys.exit(t!=n)" as a 'system' script (or add a script in Tools/scripts to achieve the same effect) -- I believe that this would alter the source files if run locally.

A

I vote for the latter -- code golf is fun but pretty unmaintainable ;)

@AA-Turner
Copy link
MemberAuthor

I vote for the latter -- code golf is fun but pretty unmaintainable ;)

Done, I've just reused untabify, which is what we currently use -- wins all around!

A


if__name__=='__main__':
main()
raiseSystemExit(main())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process() now has return values, which go to main(), but main() always returns None so we never get any errors here.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't realise! pre-commit was failing in my local testing when I introduced tabs, perhaps it just fails when a file is changed after the hook has run.

name: "Check C file whitespace"
entry: "python Tools/patchcheck/untabify.py"
language: "system"
types_or: ['c', 'c++']
Copy link
Member

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:

c_files= [fnforfninfile_pathsiffn.endswith(('.c', '.h'))]

Both of those are matched by the c type, so this would be closer to parity:

Suggested change
types_or: ['c', 'c++']
types_or: [c]

We do have half a dozen .cpp files in the codebase, do we want to expand to include them? Should we also add c++ for trailing-whitespace above?

Copy link
MemberAuthor

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.

Copy link
MemberAuthor

@AA-TurnerAA-TurnerSep 26, 2023

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

Copy link
Member

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.)

Comment on lines 278 to 281
ifhas_c_files:
print("Did you run the test suite and check for refleaks?")
elifpython_files:
print("Did you run the test suite?")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's so much clearer like this! 👍

@hugovk
Copy link
Member

@AA-Turner Looks good, please could you resolve the conflict?

Copy link
Member

@hugovkhugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@hugovkhugovk merged commit f5edb56 into python:mainOct 10, 2023
@miss-islington
Copy link
Contributor

Thanks @AA-Turner for the PR, and @hugovk for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Thanks @AA-Turner for the PR, and @hugovk for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @AA-Turner and @hugovk, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker f5edb56328b46f262b74a53343b8098a3934f761 3.12 

@miss-islington
Copy link
Contributor

Sorry, @AA-Turner and @hugovk, I could not cleanly backport this to 3.11 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker f5edb56328b46f262b74a53343b8098a3934f761 3.11 

AA-Turner added a commit to AA-Turner/cpython that referenced this pull request Oct 10, 2023
…eck to pre-commit (pythonGH-109890) Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com> (cherry picked from commit f5edb56) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
@bedevere-app
Copy link

GH-110636 is a backport of this pull request to the 3.12 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.12 only security fixes label Oct 10, 2023
AA-Turner added a commit to AA-Turner/cpython that referenced this pull request Oct 10, 2023
…eck to pre-commit (pythonGH-109890) Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>. (cherry picked from commit f5edb56) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
@bedevere-app
Copy link

GH-110640 is a backport of this pull request to the 3.11 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.11 only security fixes label Oct 10, 2023
AlexWaygood pushed a commit that referenced this pull request Oct 10, 2023
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…pre-commit (python#109890) Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@AA-Turner@AlexWaygood@hugovk@miss-islington