1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """The Tab Nanny despises ambiguous indentation. She knows no mercy. |
---|
4 | n/a | |
---|
5 | n/a | tabnanny -- Detection of ambiguous indentation |
---|
6 | n/a | |
---|
7 | n/a | For the time being this module is intended to be called as a script. |
---|
8 | n/a | However it is possible to import it into an IDE and use the function |
---|
9 | n/a | check() described below. |
---|
10 | n/a | |
---|
11 | n/a | Warning: The API provided by this module is likely to change in future |
---|
12 | n/a | releases; such changes may not be backward compatible. |
---|
13 | n/a | """ |
---|
14 | n/a | |
---|
15 | n/a | # Released to the public domain, by Tim Peters, 15 April 1998. |
---|
16 | n/a | |
---|
17 | n/a | # XXX Note: this is now a standard library module. |
---|
18 | n/a | # XXX The API needs to undergo changes however; the current code is too |
---|
19 | n/a | # XXX script-like. This will be addressed later. |
---|
20 | n/a | |
---|
21 | n/a | __version__ = "6" |
---|
22 | n/a | |
---|
23 | n/a | import os |
---|
24 | n/a | import sys |
---|
25 | n/a | import getopt |
---|
26 | n/a | import tokenize |
---|
27 | n/a | if not hasattr(tokenize, 'NL'): |
---|
28 | n/a | raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") |
---|
29 | n/a | |
---|
30 | n/a | __all__ = ["check", "NannyNag", "process_tokens"] |
---|
31 | n/a | |
---|
32 | n/a | verbose = 0 |
---|
33 | n/a | filename_only = 0 |
---|
34 | n/a | |
---|
35 | n/a | def errprint(*args): |
---|
36 | n/a | sep = "" |
---|
37 | n/a | for arg in args: |
---|
38 | n/a | sys.stderr.write(sep + str(arg)) |
---|
39 | n/a | sep = " " |
---|
40 | n/a | sys.stderr.write("\n") |
---|
41 | n/a | |
---|
42 | n/a | def main(): |
---|
43 | n/a | global verbose, filename_only |
---|
44 | n/a | try: |
---|
45 | n/a | opts, args = getopt.getopt(sys.argv[1:], "qv") |
---|
46 | n/a | except getopt.error as msg: |
---|
47 | n/a | errprint(msg) |
---|
48 | n/a | return |
---|
49 | n/a | for o, a in opts: |
---|
50 | n/a | if o == '-q': |
---|
51 | n/a | filename_only = filename_only + 1 |
---|
52 | n/a | if o == '-v': |
---|
53 | n/a | verbose = verbose + 1 |
---|
54 | n/a | if not args: |
---|
55 | n/a | errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...") |
---|
56 | n/a | return |
---|
57 | n/a | for arg in args: |
---|
58 | n/a | check(arg) |
---|
59 | n/a | |
---|
60 | n/a | class NannyNag(Exception): |
---|
61 | n/a | """ |
---|
62 | n/a | Raised by tokeneater() if detecting an ambiguous indent. |
---|
63 | n/a | Captured and handled in check(). |
---|
64 | n/a | """ |
---|
65 | n/a | def __init__(self, lineno, msg, line): |
---|
66 | n/a | self.lineno, self.msg, self.line = lineno, msg, line |
---|
67 | n/a | def get_lineno(self): |
---|
68 | n/a | return self.lineno |
---|
69 | n/a | def get_msg(self): |
---|
70 | n/a | return self.msg |
---|
71 | n/a | def get_line(self): |
---|
72 | n/a | return self.line |
---|
73 | n/a | |
---|
74 | n/a | def check(file): |
---|
75 | n/a | """check(file_or_dir) |
---|
76 | n/a | |
---|
77 | n/a | If file_or_dir is a directory and not a symbolic link, then recursively |
---|
78 | n/a | descend the directory tree named by file_or_dir, checking all .py files |
---|
79 | n/a | along the way. If file_or_dir is an ordinary Python source file, it is |
---|
80 | n/a | checked for whitespace related problems. The diagnostic messages are |
---|
81 | n/a | written to standard output using the print statement. |
---|
82 | n/a | """ |
---|
83 | n/a | |
---|
84 | n/a | if os.path.isdir(file) and not os.path.islink(file): |
---|
85 | n/a | if verbose: |
---|
86 | n/a | print("%r: listing directory" % (file,)) |
---|
87 | n/a | names = os.listdir(file) |
---|
88 | n/a | for name in names: |
---|
89 | n/a | fullname = os.path.join(file, name) |
---|
90 | n/a | if (os.path.isdir(fullname) and |
---|
91 | n/a | not os.path.islink(fullname) or |
---|
92 | n/a | os.path.normcase(name[-3:]) == ".py"): |
---|
93 | n/a | check(fullname) |
---|
94 | n/a | return |
---|
95 | n/a | |
---|
96 | n/a | try: |
---|
97 | n/a | f = tokenize.open(file) |
---|
98 | n/a | except OSError as msg: |
---|
99 | n/a | errprint("%r: I/O Error: %s" % (file, msg)) |
---|
100 | n/a | return |
---|
101 | n/a | |
---|
102 | n/a | if verbose > 1: |
---|
103 | n/a | print("checking %r ..." % file) |
---|
104 | n/a | |
---|
105 | n/a | try: |
---|
106 | n/a | process_tokens(tokenize.generate_tokens(f.readline)) |
---|
107 | n/a | |
---|
108 | n/a | except tokenize.TokenError as msg: |
---|
109 | n/a | errprint("%r: Token Error: %s" % (file, msg)) |
---|
110 | n/a | return |
---|
111 | n/a | |
---|
112 | n/a | except IndentationError as msg: |
---|
113 | n/a | errprint("%r: Indentation Error: %s" % (file, msg)) |
---|
114 | n/a | return |
---|
115 | n/a | |
---|
116 | n/a | except NannyNag as nag: |
---|
117 | n/a | badline = nag.get_lineno() |
---|
118 | n/a | line = nag.get_line() |
---|
119 | n/a | if verbose: |
---|
120 | n/a | print("%r: *** Line %d: trouble in tab city! ***" % (file, badline)) |
---|
121 | n/a | print("offending line: %r" % (line,)) |
---|
122 | n/a | print(nag.get_msg()) |
---|
123 | n/a | else: |
---|
124 | n/a | if ' ' in file: file = '"' + file + '"' |
---|
125 | n/a | if filename_only: print(file) |
---|
126 | n/a | else: print(file, badline, repr(line)) |
---|
127 | n/a | return |
---|
128 | n/a | |
---|
129 | n/a | finally: |
---|
130 | n/a | f.close() |
---|
131 | n/a | |
---|
132 | n/a | if verbose: |
---|
133 | n/a | print("%r: Clean bill of health." % (file,)) |
---|
134 | n/a | |
---|
135 | n/a | class Whitespace: |
---|
136 | n/a | # the characters used for space and tab |
---|
137 | n/a | S, T = ' \t' |
---|
138 | n/a | |
---|
139 | n/a | # members: |
---|
140 | n/a | # raw |
---|
141 | n/a | # the original string |
---|
142 | n/a | # n |
---|
143 | n/a | # the number of leading whitespace characters in raw |
---|
144 | n/a | # nt |
---|
145 | n/a | # the number of tabs in raw[:n] |
---|
146 | n/a | # norm |
---|
147 | n/a | # the normal form as a pair (count, trailing), where: |
---|
148 | n/a | # count |
---|
149 | n/a | # a tuple such that raw[:n] contains count[i] |
---|
150 | n/a | # instances of S * i + T |
---|
151 | n/a | # trailing |
---|
152 | n/a | # the number of trailing spaces in raw[:n] |
---|
153 | n/a | # It's A Theorem that m.indent_level(t) == |
---|
154 | n/a | # n.indent_level(t) for all t >= 1 iff m.norm == n.norm. |
---|
155 | n/a | # is_simple |
---|
156 | n/a | # true iff raw[:n] is of the form (T*)(S*) |
---|
157 | n/a | |
---|
158 | n/a | def __init__(self, ws): |
---|
159 | n/a | self.raw = ws |
---|
160 | n/a | S, T = Whitespace.S, Whitespace.T |
---|
161 | n/a | count = [] |
---|
162 | n/a | b = n = nt = 0 |
---|
163 | n/a | for ch in self.raw: |
---|
164 | n/a | if ch == S: |
---|
165 | n/a | n = n + 1 |
---|
166 | n/a | b = b + 1 |
---|
167 | n/a | elif ch == T: |
---|
168 | n/a | n = n + 1 |
---|
169 | n/a | nt = nt + 1 |
---|
170 | n/a | if b >= len(count): |
---|
171 | n/a | count = count + [0] * (b - len(count) + 1) |
---|
172 | n/a | count[b] = count[b] + 1 |
---|
173 | n/a | b = 0 |
---|
174 | n/a | else: |
---|
175 | n/a | break |
---|
176 | n/a | self.n = n |
---|
177 | n/a | self.nt = nt |
---|
178 | n/a | self.norm = tuple(count), b |
---|
179 | n/a | self.is_simple = len(count) <= 1 |
---|
180 | n/a | |
---|
181 | n/a | # return length of longest contiguous run of spaces (whether or not |
---|
182 | n/a | # preceding a tab) |
---|
183 | n/a | def longest_run_of_spaces(self): |
---|
184 | n/a | count, trailing = self.norm |
---|
185 | n/a | return max(len(count)-1, trailing) |
---|
186 | n/a | |
---|
187 | n/a | def indent_level(self, tabsize): |
---|
188 | n/a | # count, il = self.norm |
---|
189 | n/a | # for i in range(len(count)): |
---|
190 | n/a | # if count[i]: |
---|
191 | n/a | # il = il + (i//tabsize + 1)*tabsize * count[i] |
---|
192 | n/a | # return il |
---|
193 | n/a | |
---|
194 | n/a | # quicker: |
---|
195 | n/a | # il = trailing + sum (i//ts + 1)*ts*count[i] = |
---|
196 | n/a | # trailing + ts * sum (i//ts + 1)*count[i] = |
---|
197 | n/a | # trailing + ts * sum i//ts*count[i] + count[i] = |
---|
198 | n/a | # trailing + ts * [(sum i//ts*count[i]) + (sum count[i])] = |
---|
199 | n/a | # trailing + ts * [(sum i//ts*count[i]) + num_tabs] |
---|
200 | n/a | # and note that i//ts*count[i] is 0 when i < ts |
---|
201 | n/a | |
---|
202 | n/a | count, trailing = self.norm |
---|
203 | n/a | il = 0 |
---|
204 | n/a | for i in range(tabsize, len(count)): |
---|
205 | n/a | il = il + i//tabsize * count[i] |
---|
206 | n/a | return trailing + tabsize * (il + self.nt) |
---|
207 | n/a | |
---|
208 | n/a | # return true iff self.indent_level(t) == other.indent_level(t) |
---|
209 | n/a | # for all t >= 1 |
---|
210 | n/a | def equal(self, other): |
---|
211 | n/a | return self.norm == other.norm |
---|
212 | n/a | |
---|
213 | n/a | # return a list of tuples (ts, i1, i2) such that |
---|
214 | n/a | # i1 == self.indent_level(ts) != other.indent_level(ts) == i2. |
---|
215 | n/a | # Intended to be used after not self.equal(other) is known, in which |
---|
216 | n/a | # case it will return at least one witnessing tab size. |
---|
217 | n/a | def not_equal_witness(self, other): |
---|
218 | n/a | n = max(self.longest_run_of_spaces(), |
---|
219 | n/a | other.longest_run_of_spaces()) + 1 |
---|
220 | n/a | a = [] |
---|
221 | n/a | for ts in range(1, n+1): |
---|
222 | n/a | if self.indent_level(ts) != other.indent_level(ts): |
---|
223 | n/a | a.append( (ts, |
---|
224 | n/a | self.indent_level(ts), |
---|
225 | n/a | other.indent_level(ts)) ) |
---|
226 | n/a | return a |
---|
227 | n/a | |
---|
228 | n/a | # Return True iff self.indent_level(t) < other.indent_level(t) |
---|
229 | n/a | # for all t >= 1. |
---|
230 | n/a | # The algorithm is due to Vincent Broman. |
---|
231 | n/a | # Easy to prove it's correct. |
---|
232 | n/a | # XXXpost that. |
---|
233 | n/a | # Trivial to prove n is sharp (consider T vs ST). |
---|
234 | n/a | # Unknown whether there's a faster general way. I suspected so at |
---|
235 | n/a | # first, but no longer. |
---|
236 | n/a | # For the special (but common!) case where M and N are both of the |
---|
237 | n/a | # form (T*)(S*), M.less(N) iff M.len() < N.len() and |
---|
238 | n/a | # M.num_tabs() <= N.num_tabs(). Proof is easy but kinda long-winded. |
---|
239 | n/a | # XXXwrite that up. |
---|
240 | n/a | # Note that M is of the form (T*)(S*) iff len(M.norm[0]) <= 1. |
---|
241 | n/a | def less(self, other): |
---|
242 | n/a | if self.n >= other.n: |
---|
243 | n/a | return False |
---|
244 | n/a | if self.is_simple and other.is_simple: |
---|
245 | n/a | return self.nt <= other.nt |
---|
246 | n/a | n = max(self.longest_run_of_spaces(), |
---|
247 | n/a | other.longest_run_of_spaces()) + 1 |
---|
248 | n/a | # the self.n >= other.n test already did it for ts=1 |
---|
249 | n/a | for ts in range(2, n+1): |
---|
250 | n/a | if self.indent_level(ts) >= other.indent_level(ts): |
---|
251 | n/a | return False |
---|
252 | n/a | return True |
---|
253 | n/a | |
---|
254 | n/a | # return a list of tuples (ts, i1, i2) such that |
---|
255 | n/a | # i1 == self.indent_level(ts) >= other.indent_level(ts) == i2. |
---|
256 | n/a | # Intended to be used after not self.less(other) is known, in which |
---|
257 | n/a | # case it will return at least one witnessing tab size. |
---|
258 | n/a | def not_less_witness(self, other): |
---|
259 | n/a | n = max(self.longest_run_of_spaces(), |
---|
260 | n/a | other.longest_run_of_spaces()) + 1 |
---|
261 | n/a | a = [] |
---|
262 | n/a | for ts in range(1, n+1): |
---|
263 | n/a | if self.indent_level(ts) >= other.indent_level(ts): |
---|
264 | n/a | a.append( (ts, |
---|
265 | n/a | self.indent_level(ts), |
---|
266 | n/a | other.indent_level(ts)) ) |
---|
267 | n/a | return a |
---|
268 | n/a | |
---|
269 | n/a | def format_witnesses(w): |
---|
270 | n/a | firsts = (str(tup[0]) for tup in w) |
---|
271 | n/a | prefix = "at tab size" |
---|
272 | n/a | if len(w) > 1: |
---|
273 | n/a | prefix = prefix + "s" |
---|
274 | n/a | return prefix + " " + ', '.join(firsts) |
---|
275 | n/a | |
---|
276 | n/a | def process_tokens(tokens): |
---|
277 | n/a | INDENT = tokenize.INDENT |
---|
278 | n/a | DEDENT = tokenize.DEDENT |
---|
279 | n/a | NEWLINE = tokenize.NEWLINE |
---|
280 | n/a | JUNK = tokenize.COMMENT, tokenize.NL |
---|
281 | n/a | indents = [Whitespace("")] |
---|
282 | n/a | check_equal = 0 |
---|
283 | n/a | |
---|
284 | n/a | for (type, token, start, end, line) in tokens: |
---|
285 | n/a | if type == NEWLINE: |
---|
286 | n/a | # a program statement, or ENDMARKER, will eventually follow, |
---|
287 | n/a | # after some (possibly empty) run of tokens of the form |
---|
288 | n/a | # (NL | COMMENT)* (INDENT | DEDENT+)? |
---|
289 | n/a | # If an INDENT appears, setting check_equal is wrong, and will |
---|
290 | n/a | # be undone when we see the INDENT. |
---|
291 | n/a | check_equal = 1 |
---|
292 | n/a | |
---|
293 | n/a | elif type == INDENT: |
---|
294 | n/a | check_equal = 0 |
---|
295 | n/a | thisguy = Whitespace(token) |
---|
296 | n/a | if not indents[-1].less(thisguy): |
---|
297 | n/a | witness = indents[-1].not_less_witness(thisguy) |
---|
298 | n/a | msg = "indent not greater e.g. " + format_witnesses(witness) |
---|
299 | n/a | raise NannyNag(start[0], msg, line) |
---|
300 | n/a | indents.append(thisguy) |
---|
301 | n/a | |
---|
302 | n/a | elif type == DEDENT: |
---|
303 | n/a | # there's nothing we need to check here! what's important is |
---|
304 | n/a | # that when the run of DEDENTs ends, the indentation of the |
---|
305 | n/a | # program statement (or ENDMARKER) that triggered the run is |
---|
306 | n/a | # equal to what's left at the top of the indents stack |
---|
307 | n/a | |
---|
308 | n/a | # Ouch! This assert triggers if the last line of the source |
---|
309 | n/a | # is indented *and* lacks a newline -- then DEDENTs pop out |
---|
310 | n/a | # of thin air. |
---|
311 | n/a | # assert check_equal # else no earlier NEWLINE, or an earlier INDENT |
---|
312 | n/a | check_equal = 1 |
---|
313 | n/a | |
---|
314 | n/a | del indents[-1] |
---|
315 | n/a | |
---|
316 | n/a | elif check_equal and type not in JUNK: |
---|
317 | n/a | # this is the first "real token" following a NEWLINE, so it |
---|
318 | n/a | # must be the first token of the next program statement, or an |
---|
319 | n/a | # ENDMARKER; the "line" argument exposes the leading whitespace |
---|
320 | n/a | # for this statement; in the case of ENDMARKER, line is an empty |
---|
321 | n/a | # string, so will properly match the empty string with which the |
---|
322 | n/a | # "indents" stack was seeded |
---|
323 | n/a | check_equal = 0 |
---|
324 | n/a | thisguy = Whitespace(line) |
---|
325 | n/a | if not indents[-1].equal(thisguy): |
---|
326 | n/a | witness = indents[-1].not_equal_witness(thisguy) |
---|
327 | n/a | msg = "indent not equal e.g. " + format_witnesses(witness) |
---|
328 | n/a | raise NannyNag(start[0], msg, line) |
---|
329 | n/a | |
---|
330 | n/a | |
---|
331 | n/a | if __name__ == '__main__': |
---|
332 | n/a | main() |
---|