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