blob: 46f8163e5f7e013151b095824ed049fe6e0c2058 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Guido van Rossum9ab75cb1998-03-31 14:31:39 +00002
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +00003"""The Tab Nanny despises ambiguous indentation. She knows no mercy.
4
Tim Peters8ac14952002-05-23 15:15:30 +00005tabnanny -- Detection of ambiguous indentation
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +00006
7For the time being this module is intended to be called as a script.
8However it is possible to import it into an IDE and use the function
Tim Peters8ac14952002-05-23 15:15:30 +00009check() described below.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000010
11Warning: The API provided by this module is likely to change in future
Tim Peters8ac14952002-05-23 15:15:30 +000012releases; such changes may not be backward compatible.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000013"""
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000014
Guido van Rossumaa2a7a41998-06-09 19:02:21 +000015# Released to the public domain, by Tim Peters, 15 April 1998.
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000016
Guido van Rossumdc688332000-02-23 15:32:19 +000017# XXX Note: this is now a standard library module.
18# XXX The API needs to undergo changes however; the current code is too
19# XXX script-like. This will be addressed later.
20
Guido van Rossuma74c5561999-07-30 17:48:20 +000021__version__ = "6"
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000022
23import os
24import sys
25import getopt
26import tokenize
Tim Peters4efb6e92001-06-29 23:51:08 +000027if not hasattr(tokenize, 'NL'):
28 raise ValueError("tokenize.NL doesn't exist -- tokenize module too old")
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000029
Guido van Rossumc5943b12001-08-07 17:19:25 +000030__all__ = ["check", "NannyNag", "process_tokens"]
Skip Montanaro40fc1602001-03-01 04:27:19 +000031
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000032verbose = 0
Andrew M. Kuchlingdc86a4e1998-12-18 13:56:58 +000033filename_only = 0
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000034
Guido van Rossumf9a6d7d1998-09-14 16:22:21 +000035def errprint(*args):
36 sep = ""
37 for arg in args:
38 sys.stderr.write(sep + str(arg))
39 sep = " "
40 sys.stderr.write("\n")
41
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000042def main():
Andrew M. Kuchlingdc86a4e1998-12-18 13:56:58 +000043 global verbose, filename_only
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000044 try:
Andrew M. Kuchlingdc86a4e1998-12-18 13:56:58 +000045 opts, args = getopt.getopt(sys.argv[1:], "qv")
Guido van Rossumb940e112007-01-10 16:19:56 +000046 except getopt.error as msg:
Guido van Rossumf9a6d7d1998-09-14 16:22:21 +000047 errprint(msg)
Guido van Rossum8053d891998-04-06 14:45:26 +000048 return
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000049 for o, a in opts:
Andrew M. Kuchlingdc86a4e1998-12-18 13:56:58 +000050 if o == '-q':
51 filename_only = filename_only + 1
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000052 if o == '-v':
53 verbose = verbose + 1
Guido van Rossum8053d891998-04-06 14:45:26 +000054 if not args:
Guido van Rossumf9a6d7d1998-09-14 16:22:21 +000055 errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...")
Guido van Rossum8053d891998-04-06 14:45:26 +000056 return
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000057 for arg in args:
58 check(arg)
59
Neal Norwitzf74e46c2002-03-31 13:59:18 +000060class NannyNag(Exception):
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000061 """
62 Raised by tokeneater() if detecting an ambiguous indent.
Tim Peters8ac14952002-05-23 15:15:30 +000063 Captured and handled in check().
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000064 """
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000065 def __init__(self, lineno, msg, line):
66 self.lineno, self.msg, self.line = lineno, msg, line
67 def get_lineno(self):
68 return self.lineno
69 def get_msg(self):
70 return self.msg
71 def get_line(self):
72 return self.line
73
74def check(file):
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000075 """check(file_or_dir)
Tim Peters8ac14952002-05-23 15:15:30 +000076
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000077 If file_or_dir is a directory and not a symbolic link, then recursively
78 descend the directory tree named by file_or_dir, checking all .py files
79 along the way. If file_or_dir is an ordinary Python source file, it is
80 checked for whitespace related problems. The diagnostic messages are
Tim Peters8ac14952002-05-23 15:15:30 +000081 written to standard output using the print statement.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000082 """
Tim Peters8ac14952002-05-23 15:15:30 +000083
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000084 if os.path.isdir(file) and not os.path.islink(file):
85 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000086 print("%r: listing directory" % (file,))
Guido van Rossum9ab75cb1998-03-31 14:31:39 +000087 names = os.listdir(file)
88 for name in names:
89 fullname = os.path.join(file, name)
90 if (os.path.isdir(fullname) and
91 not os.path.islink(fullname) or
92 os.path.normcase(name[-3:]) == ".py"):
93 check(fullname)
94 return
95
96 try:
Victor Stinner58c07522010-11-09 01:08:59 +000097 f = tokenize.open(file)
Guido van Rossumb940e112007-01-10 16:19:56 +000098 except IOError as msg:
Walter Dörwald70a6b492004-02-12 17:35:32 +000099 errprint("%r: I/O Error: %s" % (file, msg))
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000100 return
101
102 if verbose > 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000103 print("checking %r ..." % file)
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000104
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000105 try:
Tim Peters5ca576e2001-06-18 22:08:13 +0000106 process_tokens(tokenize.generate_tokens(f.readline))
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000107
Guido van Rossumb940e112007-01-10 16:19:56 +0000108 except tokenize.TokenError as msg:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000109 errprint("%r: Token Error: %s" % (file, msg))
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000110 return
111
Guido van Rossumb940e112007-01-10 16:19:56 +0000112 except IndentationError as msg:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000113 errprint("%r: Indentation Error: %s" % (file, msg))
114 return
115
Guido van Rossumb940e112007-01-10 16:19:56 +0000116 except NannyNag as nag:
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000117 badline = nag.get_lineno()
118 line = nag.get_line()
119 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000120 print("%r: *** Line %d: trouble in tab city! ***" % (file, badline))
121 print("offending line: %r" % (line,))
122 print(nag.get_msg())
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000123 else:
Guido van Rossuma74c5561999-07-30 17:48:20 +0000124 if ' ' in file: file = '"' + file + '"'
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000125 if filename_only: print(file)
126 else: print(file, badline, repr(line))
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000127 return
128
129 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000130 print("%r: Clean bill of health." % (file,))
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000131
132class Whitespace:
133 # the characters used for space and tab
134 S, T = ' \t'
135
136 # members:
137 # raw
138 # the original string
139 # n
140 # the number of leading whitespace characters in raw
141 # nt
142 # the number of tabs in raw[:n]
143 # norm
144 # the normal form as a pair (count, trailing), where:
145 # count
146 # a tuple such that raw[:n] contains count[i]
147 # instances of S * i + T
148 # trailing
149 # the number of trailing spaces in raw[:n]
150 # It's A Theorem that m.indent_level(t) ==
151 # n.indent_level(t) for all t >= 1 iff m.norm == n.norm.
152 # is_simple
153 # true iff raw[:n] is of the form (T*)(S*)
154
155 def __init__(self, ws):
156 self.raw = ws
157 S, T = Whitespace.S, Whitespace.T
158 count = []
159 b = n = nt = 0
160 for ch in self.raw:
161 if ch == S:
162 n = n + 1
163 b = b + 1
164 elif ch == T:
165 n = n + 1
166 nt = nt + 1
167 if b >= len(count):
168 count = count + [0] * (b - len(count) + 1)
169 count[b] = count[b] + 1
170 b = 0
171 else:
172 break
173 self.n = n
174 self.nt = nt
175 self.norm = tuple(count), b
176 self.is_simple = len(count) <= 1
177
178 # return length of longest contiguous run of spaces (whether or not
179 # preceding a tab)
180 def longest_run_of_spaces(self):
181 count, trailing = self.norm
182 return max(len(count)-1, trailing)
183
184 def indent_level(self, tabsize):
185 # count, il = self.norm
186 # for i in range(len(count)):
187 # if count[i]:
188 # il = il + (i/tabsize + 1)*tabsize * count[i]
189 # return il
190
191 # quicker:
192 # il = trailing + sum (i/ts + 1)*ts*count[i] =
193 # trailing + ts * sum (i/ts + 1)*count[i] =
194 # trailing + ts * sum i/ts*count[i] + count[i] =
195 # trailing + ts * [(sum i/ts*count[i]) + (sum count[i])] =
196 # trailing + ts * [(sum i/ts*count[i]) + num_tabs]
197 # and note that i/ts*count[i] is 0 when i < ts
198
199 count, trailing = self.norm
200 il = 0
201 for i in range(tabsize, len(count)):
202 il = il + i/tabsize * count[i]
203 return trailing + tabsize * (il + self.nt)
204
205 # return true iff self.indent_level(t) == other.indent_level(t)
206 # for all t >= 1
207 def equal(self, other):
208 return self.norm == other.norm
209
210 # return a list of tuples (ts, i1, i2) such that
211 # i1 == self.indent_level(ts) != other.indent_level(ts) == i2.
212 # Intended to be used after not self.equal(other) is known, in which
213 # case it will return at least one witnessing tab size.
214 def not_equal_witness(self, other):
215 n = max(self.longest_run_of_spaces(),
216 other.longest_run_of_spaces()) + 1
217 a = []
218 for ts in range(1, n+1):
219 if self.indent_level(ts) != other.indent_level(ts):
220 a.append( (ts,
221 self.indent_level(ts),
222 other.indent_level(ts)) )
223 return a
224
Tim Petersbc0e9102002-04-04 22:55:58 +0000225 # Return True iff self.indent_level(t) < other.indent_level(t)
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000226 # for all t >= 1.
227 # The algorithm is due to Vincent Broman.
228 # Easy to prove it's correct.
229 # XXXpost that.
230 # Trivial to prove n is sharp (consider T vs ST).
231 # Unknown whether there's a faster general way. I suspected so at
232 # first, but no longer.
233 # For the special (but common!) case where M and N are both of the
234 # form (T*)(S*), M.less(N) iff M.len() < N.len() and
235 # M.num_tabs() <= N.num_tabs(). Proof is easy but kinda long-winded.
236 # XXXwrite that up.
237 # Note that M is of the form (T*)(S*) iff len(M.norm[0]) <= 1.
238 def less(self, other):
239 if self.n >= other.n:
Tim Petersbc0e9102002-04-04 22:55:58 +0000240 return False
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000241 if self.is_simple and other.is_simple:
242 return self.nt <= other.nt
243 n = max(self.longest_run_of_spaces(),
244 other.longest_run_of_spaces()) + 1
245 # the self.n >= other.n test already did it for ts=1
246 for ts in range(2, n+1):
247 if self.indent_level(ts) >= other.indent_level(ts):
Tim Petersbc0e9102002-04-04 22:55:58 +0000248 return False
249 return True
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000250
251 # return a list of tuples (ts, i1, i2) such that
252 # i1 == self.indent_level(ts) >= other.indent_level(ts) == i2.
253 # Intended to be used after not self.less(other) is known, in which
254 # case it will return at least one witnessing tab size.
255 def not_less_witness(self, other):
256 n = max(self.longest_run_of_spaces(),
257 other.longest_run_of_spaces()) + 1
258 a = []
259 for ts in range(1, n+1):
260 if self.indent_level(ts) >= other.indent_level(ts):
261 a.append( (ts,
262 self.indent_level(ts),
263 other.indent_level(ts)) )
264 return a
265
266def format_witnesses(w):
Georg Brandlcbd2ab12010-12-04 10:39:14 +0000267 firsts = (str(tup[0]) for tup in w)
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000268 prefix = "at tab size"
269 if len(w) > 1:
270 prefix = prefix + "s"
Neal Norwitz7ce734c2002-05-31 14:13:04 +0000271 return prefix + " " + ', '.join(firsts)
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000272
Tim Peters4efb6e92001-06-29 23:51:08 +0000273def process_tokens(tokens):
274 INDENT = tokenize.INDENT
275 DEDENT = tokenize.DEDENT
276 NEWLINE = tokenize.NEWLINE
277 JUNK = tokenize.COMMENT, tokenize.NL
Tim Peters5ca576e2001-06-18 22:08:13 +0000278 indents = [Whitespace("")]
279 check_equal = 0
280
281 for (type, token, start, end, line) in tokens:
Tim Petersb90f89a2001-01-15 03:26:36 +0000282 if type == NEWLINE:
283 # a program statement, or ENDMARKER, will eventually follow,
284 # after some (possibly empty) run of tokens of the form
285 # (NL | COMMENT)* (INDENT | DEDENT+)?
286 # If an INDENT appears, setting check_equal is wrong, and will
287 # be undone when we see the INDENT.
288 check_equal = 1
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000289
Tim Petersb90f89a2001-01-15 03:26:36 +0000290 elif type == INDENT:
291 check_equal = 0
292 thisguy = Whitespace(token)
293 if not indents[-1].less(thisguy):
294 witness = indents[-1].not_less_witness(thisguy)
295 msg = "indent not greater e.g. " + format_witnesses(witness)
296 raise NannyNag(start[0], msg, line)
297 indents.append(thisguy)
Guido van Rossumf4b44fa1998-04-06 14:41:20 +0000298
Tim Petersb90f89a2001-01-15 03:26:36 +0000299 elif type == DEDENT:
300 # there's nothing we need to check here! what's important is
301 # that when the run of DEDENTs ends, the indentation of the
302 # program statement (or ENDMARKER) that triggered the run is
303 # equal to what's left at the top of the indents stack
Guido van Rossumaa2a7a41998-06-09 19:02:21 +0000304
Tim Petersb90f89a2001-01-15 03:26:36 +0000305 # Ouch! This assert triggers if the last line of the source
306 # is indented *and* lacks a newline -- then DEDENTs pop out
307 # of thin air.
308 # assert check_equal # else no earlier NEWLINE, or an earlier INDENT
309 check_equal = 1
Guido van Rossumaa2a7a41998-06-09 19:02:21 +0000310
Tim Petersb90f89a2001-01-15 03:26:36 +0000311 del indents[-1]
Guido van Rossumf4b44fa1998-04-06 14:41:20 +0000312
Tim Petersb90f89a2001-01-15 03:26:36 +0000313 elif check_equal and type not in JUNK:
314 # this is the first "real token" following a NEWLINE, so it
315 # must be the first token of the next program statement, or an
316 # ENDMARKER; the "line" argument exposes the leading whitespace
317 # for this statement; in the case of ENDMARKER, line is an empty
318 # string, so will properly match the empty string with which the
319 # "indents" stack was seeded
320 check_equal = 0
321 thisguy = Whitespace(line)
322 if not indents[-1].equal(thisguy):
323 witness = indents[-1].not_equal_witness(thisguy)
324 msg = "indent not equal e.g. " + format_witnesses(witness)
325 raise NannyNag(start[0], msg, line)
Guido van Rossumf4b44fa1998-04-06 14:41:20 +0000326
Guido van Rossum9ab75cb1998-03-31 14:31:39 +0000327
328if __name__ == '__main__':
329 main()