-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
make patchcheck
should check the whitespace of .c/.h files
#53158
Comments
This would be especially useful now that only spaces are used in .c/.h files and not tabs. |
Here’s a quick crack at it. Conservative approach: Don’t reindent, just complain. |
Updating patch to check header files too. Untested. |
Thanks for the patch, Éric. I will get to it when I can. |
I may add that although there is no formal test of any kind, I added tabs in two places in one .c file and in one place in another place, and it correctly reported two files. It’s a small function mostly copied from the one above; the only part where I used my brain is the counter (Python did the rest), so it should be ok. |
Éric, It will be helpful if your code could also check for lines longer than 79 characters. |
.. and trailing white space. |
Replacing VCS hooks, are we? :) Updated and attached. Ideas for other feature requests:
|
Nitpick: shouldn't that
be
? Either that, or strip the line ending from the line before computing its length. |
Similarly, isn't line[-1].isspace() always going to be true (well, except possibly at the end of the file), thanks to the line-ending character? |
Forgot readling kept the line end, thanks for catching. Updated (yay for Mercurial Queues!) |
s/readling/readline/ |
Sorry to be nitpicky, but: (a) the line[-2] produces "IndexError: string index out of range" on empty (b) this won't detect trailing whitespace on the last line of a file, if there's no newline at the end of the file. Actually, it might also be worth checking that there *is* a newline at the end of the file; this is actually required by the C standards. (E.g., C99 5.1.1.2p2: "A source file that is not empty shall end in a new-line character".) |
s/actually//g |
Thanks for the helpful reviews. I have fixed the trailing whitespace check with “line[-2:-1].isspace()”, but I have a bug with my file counting. Before I go further, I’d like feedback from people using patchcheck:
|
Another one: I’d like to move the file name filtering from the worker functions into the main function. With that change, looping over all file names to get only the C files would be written and run only once, even if there are four functions that operate on these files. There is not backward compatibility guarantee for Tools/*, right? |
Answering as a rather infrequent user of 'make patchcheck', but someone who vows to use it more often in future... :) (1) Well, it would be awkward to use grep or wc on Windows, so it's convenient not to need external tools. (2) +1 to a more detailed report, at least giving the file name and what was wrong with it ("trailing whitespace in yourfile.c"); I could imagine that people might care about finding trailing whitespace or tabs, but not about line length, for example. (Many of the existing C files already have lines >= 80 characters.) (3) Don't really care: emacs already does both these things for me nicely. I'd rather just have patchcheck do the reporting. And automatic replacement of tabs with spaces in C files might not guess correctly what the author intended. |
and your proposed refactoring sounds fine to me. I can't really see any backwards compatibility concerns. |
|
How about a --report-only (insert better name here) command line option to patchcheck, then? My editor doesn't delete trailing whitespace *automatically*, but it's trivial to remove trailing whitespace from a file if you know it's there (M-x delete-trailing-whitespace, I believe); some people (like me) might prefer to make all changes within the editor rather than having an external tool do it for them. This is especially true if you've got the editor open for the relevant file at the time, since you can end up with two different conflicting versions of the file---one in the editor and one on disk; saving in the editor can undo the changes that patchcheck made on disk. |
Agreed. reindent.py has a dry-run mode too, so adding such a global flag and making it the default is okay with me. |
Committed in r82567. I also cleaned up the output slightly so that "N file(s)" pluralizes "file" properly based on N. Also now list what files have their whitespace fixed. |
Hmm, it looks like patchcheck does not fix trailing whitespace in c files: $ grep ' $' Modules/datetimemodule.c| cat -ve
$
GET_TD_DAYS(offset1) * 86400 - $
Py_DECREF(offset); $
if ((offset1 != offset2) && $
$
$ make patchcheck Modules/Setup.dist is newer than Modules/Setup; Did you run the test suite? |
Sorry if I was unclear. Out of the four points I listed, this patch only did the refactoring (4). I have another patch to add and use a check_pep7 function, I’ll refresh it and post it. |
On Sun, Jul 4, 2010 at 22:40, Éric Araujo <report@bugs.python.org> wrote:
You were clear, I just prematurely closed it by accident. |
I wonder how to have sane code reuse between patchcheck and Mercurial hooks. Mark seems to have started seconding Dirkjan with hooks, see e.g. |
Note directly related to this issue, but untabify.py fails on files that contain non-ascii characters. For example: $ ./python.exe Tools/scripts/untabify.py Modules/_heapqmodule.c
Traceback (most recent call last):
...
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe7 in position 173: invalid continuation byte I am not sure what relevant C standard has to say about using non-ascii characters in comments, but the checking tool should not fail with a traceback in such situation. |
I opened bpo-9598 for the untabify bug. For the purposes of source checking, I believe non-ascii characters should be disallowed in python C source code. According to my understanding of C89 standard, interpretation of characters outside of the basic character set is implementation and locale dependent. |
The following commit from today is related to this issue: 815b88454e3e "Remove trailing whitespace in order to silence warnings on HP-UX." |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: