blob: 825aa90646057e67f67939d46a0633e1c750269f [file] [log] [blame]
Guido van Rossumb51eaa11997-03-07 00:21:55 +00001"""Tokenization help for Python programs.
Guido van Rossum4d8e8591992-01-01 19:34:47 +00002
Florent Xicluna43e4ea12010-09-03 19:54:02 +00003tokenize(readline) is a generator that breaks a stream of bytes into
4Python tokens. It decodes the bytes according to PEP-0263 for
5determining source file encoding.
Trent Nelson428de652008-03-18 22:41:35 +00006
Florent Xicluna43e4ea12010-09-03 19:54:02 +00007It accepts a readline-like method which is called repeatedly to get the
8next line of input (or b"" for EOF). It generates 5-tuples with these
9members:
Tim Peters4efb6e92001-06-29 23:51:08 +000010
11 the token type (see token.py)
12 the token (a string)
13 the starting (row, column) indices of the token (a 2-tuple of ints)
14 the ending (row, column) indices of the token (a 2-tuple of ints)
15 the original line (string)
16
17It is designed to match the working of the Python tokenizer exactly, except
18that it produces COMMENT tokens for comments and gives type OP for all
Florent Xicluna43e4ea12010-09-03 19:54:02 +000019operators. Additionally, all token lists start with an ENCODING token
20which tells you which encoding was used to decode the bytes stream.
21"""
Guido van Rossumb51eaa11997-03-07 00:21:55 +000022
Ka-Ping Yee244c5932001-03-01 13:56:40 +000023__author__ = 'Ka-Ping Yee <ping@lfw.org>'
Trent Nelson428de652008-03-18 22:41:35 +000024__credits__ = ('GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, '
25 'Skip Montanaro, Raymond Hettinger, Trent Nelson, '
26 'Michael Foord')
Serhiy Storchakacf4a2f22015-03-11 17:18:03 +020027from builtins import open as _builtin_open
Benjamin Peterson433f32c2008-12-12 01:25:05 +000028from codecs import lookup, BOM_UTF8
Raymond Hettinger3fb79c72010-09-09 07:15:18 +000029import collections
Victor Stinner58c07522010-11-09 01:08:59 +000030from io import TextIOWrapper
Terry Jan Reedy5b8d2c32014-02-17 23:12:16 -050031from itertools import chain
Eric V. Smith1c8222c2015-10-26 04:37:55 -040032import itertools as _itertools
Terry Jan Reedy5b8d2c32014-02-17 23:12:16 -050033import re
34import sys
35from token import *
36
Serhiy Storchakae431d3c2016-03-20 23:36:29 +020037cookie_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
Serhiy Storchaka768c16c2014-01-09 18:36:09 +020038blank_re = re.compile(br'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII)
Guido van Rossum4d8e8591992-01-01 19:34:47 +000039
Skip Montanaro40fc1602001-03-01 04:27:19 +000040import token
Alexander Belopolskyb9d10d02010-11-11 14:07:41 +000041__all__ = token.__all__ + ["COMMENT", "tokenize", "detect_encoding",
42 "NL", "untokenize", "ENCODING", "TokenInfo"]
Skip Montanaro40fc1602001-03-01 04:27:19 +000043del token
44
Guido van Rossum1aec3231997-04-08 14:24:39 +000045COMMENT = N_TOKENS
46tok_name[COMMENT] = 'COMMENT'
Guido van Rossuma90c78b1998-04-03 16:05:38 +000047NL = N_TOKENS + 1
48tok_name[NL] = 'NL'
Trent Nelson428de652008-03-18 22:41:35 +000049ENCODING = N_TOKENS + 2
50tok_name[ENCODING] = 'ENCODING'
51N_TOKENS += 3
Meador Inge00c7f852012-01-19 00:44:45 -060052EXACT_TOKEN_TYPES = {
53 '(': LPAR,
54 ')': RPAR,
55 '[': LSQB,
56 ']': RSQB,
57 ':': COLON,
58 ',': COMMA,
59 ';': SEMI,
60 '+': PLUS,
61 '-': MINUS,
62 '*': STAR,
63 '/': SLASH,
64 '|': VBAR,
65 '&': AMPER,
66 '<': LESS,
67 '>': GREATER,
68 '=': EQUAL,
69 '.': DOT,
70 '%': PERCENT,
71 '{': LBRACE,
72 '}': RBRACE,
73 '==': EQEQUAL,
74 '!=': NOTEQUAL,
75 '<=': LESSEQUAL,
76 '>=': GREATEREQUAL,
77 '~': TILDE,
78 '^': CIRCUMFLEX,
79 '<<': LEFTSHIFT,
80 '>>': RIGHTSHIFT,
81 '**': DOUBLESTAR,
82 '+=': PLUSEQUAL,
83 '-=': MINEQUAL,
84 '*=': STAREQUAL,
85 '/=': SLASHEQUAL,
86 '%=': PERCENTEQUAL,
87 '&=': AMPEREQUAL,
88 '|=': VBAREQUAL,
89 '^=': CIRCUMFLEXEQUAL,
90 '<<=': LEFTSHIFTEQUAL,
91 '>>=': RIGHTSHIFTEQUAL,
92 '**=': DOUBLESTAREQUAL,
93 '//': DOUBLESLASH,
94 '//=': DOUBLESLASHEQUAL,
Benjamin Petersond51374e2014-04-09 23:55:56 -040095 '@': AT,
96 '@=': ATEQUAL,
Meador Inge00c7f852012-01-19 00:44:45 -060097}
Guido van Rossum1aec3231997-04-08 14:24:39 +000098
Raymond Hettinger3fb79c72010-09-09 07:15:18 +000099class TokenInfo(collections.namedtuple('TokenInfo', 'type string start end line')):
Raymond Hettingeraa17a7f2009-04-29 14:21:25 +0000100 def __repr__(self):
Raymond Hettingera0e79402010-09-09 08:29:05 +0000101 annotated_type = '%d (%s)' % (self.type, tok_name[self.type])
102 return ('TokenInfo(type=%s, string=%r, start=%r, end=%r, line=%r)' %
103 self._replace(type=annotated_type))
Raymond Hettingeraa17a7f2009-04-29 14:21:25 +0000104
Meador Inge00c7f852012-01-19 00:44:45 -0600105 @property
106 def exact_type(self):
107 if self.type == OP and self.string in EXACT_TOKEN_TYPES:
108 return EXACT_TOKEN_TYPES[self.string]
109 else:
110 return self.type
111
Eric S. Raymondb08b2d32001-02-09 11:10:16 +0000112def group(*choices): return '(' + '|'.join(choices) + ')'
Guido van Rossum68468eb2003-02-27 20:14:51 +0000113def any(*choices): return group(*choices) + '*'
114def maybe(*choices): return group(*choices) + '?'
Guido van Rossum4d8e8591992-01-01 19:34:47 +0000115
Antoine Pitroufd036452008-08-19 17:56:33 +0000116# Note: we use unicode matching for names ("\w") but ascii matching for
117# number literals.
Guido van Rossum3b631771997-10-27 20:44:15 +0000118Whitespace = r'[ \f\t]*'
119Comment = r'#[^\r\n]*'
120Ignore = Whitespace + any(r'\\\r?\n' + Whitespace) + maybe(Comment)
Benjamin Peterson33856de2010-08-30 14:41:20 +0000121Name = r'\w+'
Guido van Rossum4d8e8591992-01-01 19:34:47 +0000122
Brett Cannona721aba2016-09-09 14:57:09 -0700123Hexnumber = r'0[xX](?:_?[0-9a-fA-F])+'
124Binnumber = r'0[bB](?:_?[01])+'
125Octnumber = r'0[oO](?:_?[0-7])+'
126Decnumber = r'(?:0(?:_?0)*|[1-9](?:_?[0-9])*)'
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000127Intnumber = group(Hexnumber, Binnumber, Octnumber, Decnumber)
Brett Cannona721aba2016-09-09 14:57:09 -0700128Exponent = r'[eE][-+]?[0-9](?:_?[0-9])*'
129Pointfloat = group(r'[0-9](?:_?[0-9])*\.(?:[0-9](?:_?[0-9])*)?',
130 r'\.[0-9](?:_?[0-9])*') + maybe(Exponent)
131Expfloat = r'[0-9](?:_?[0-9])*' + Exponent
Guido van Rossum1aec3231997-04-08 14:24:39 +0000132Floatnumber = group(Pointfloat, Expfloat)
Brett Cannona721aba2016-09-09 14:57:09 -0700133Imagnumber = group(r'[0-9](?:_?[0-9])*[jJ]', Floatnumber + r'[jJ]')
Guido van Rossum1aec3231997-04-08 14:24:39 +0000134Number = group(Imagnumber, Floatnumber, Intnumber)
Guido van Rossum4d8e8591992-01-01 19:34:47 +0000135
Eric V. Smith1c8222c2015-10-26 04:37:55 -0400136# Return the empty string, plus all of the valid string prefixes.
137def _all_string_prefixes():
138 # The valid string prefixes. Only contain the lower case versions,
139 # and don't contain any permuations (include 'fr', but not
140 # 'rf'). The various permutations will be generated.
141 _valid_string_prefixes = ['b', 'r', 'u', 'f', 'br', 'fr']
142 # if we add binary f-strings, add: ['fb', 'fbr']
143 result = set([''])
144 for prefix in _valid_string_prefixes:
145 for t in _itertools.permutations(prefix):
146 # create a list with upper and lower versions of each
147 # character
148 for u in _itertools.product(*[(c, c.upper()) for c in t]):
149 result.add(''.join(u))
150 return result
151
152def _compile(expr):
153 return re.compile(expr, re.UNICODE)
154
155# Note that since _all_string_prefixes includes the empty string,
156# StringPrefix can be the empty string (making it optional).
157StringPrefix = group(*_all_string_prefixes())
Armin Ronacherc0eaeca2012-03-04 13:07:57 +0000158
Tim Petersde495832000-10-07 05:09:39 +0000159# Tail end of ' string.
160Single = r"[^'\\]*(?:\\.[^'\\]*)*'"
161# Tail end of " string.
162Double = r'[^"\\]*(?:\\.[^"\\]*)*"'
163# Tail end of ''' string.
164Single3 = r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''"
165# Tail end of """ string.
166Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
Armin Ronacherc0eaeca2012-03-04 13:07:57 +0000167Triple = group(StringPrefix + "'''", StringPrefix + '"""')
Tim Petersde495832000-10-07 05:09:39 +0000168# Single-line ' or " string.
Armin Ronacherc0eaeca2012-03-04 13:07:57 +0000169String = group(StringPrefix + r"'[^\n'\\]*(?:\\.[^\n'\\]*)*'",
170 StringPrefix + r'"[^\n"\\]*(?:\\.[^\n"\\]*)*"')
Guido van Rossum4d8e8591992-01-01 19:34:47 +0000171
Tim Petersde495832000-10-07 05:09:39 +0000172# Because of leftmost-then-longest match semantics, be sure to put the
173# longest operators first (e.g., if = came before ==, == would get
174# recognized as two instances of =).
Guido van Rossumb053cd82006-08-24 03:53:23 +0000175Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"!=",
Neal Norwitzc1505362006-12-28 06:47:50 +0000176 r"//=?", r"->",
Benjamin Petersond51374e2014-04-09 23:55:56 -0400177 r"[+\-*/%&@|^=<>]=?",
Tim Petersde495832000-10-07 05:09:39 +0000178 r"~")
Thomas Wouterse1519a12000-08-24 21:44:52 +0000179
Guido van Rossum4d8e8591992-01-01 19:34:47 +0000180Bracket = '[][(){}]'
Georg Brandldde00282007-03-18 19:01:53 +0000181Special = group(r'\r?\n', r'\.\.\.', r'[:;.,@]')
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000182Funny = group(Operator, Bracket, Special)
Guido van Rossum4d8e8591992-01-01 19:34:47 +0000183
Guido van Rossum3b631771997-10-27 20:44:15 +0000184PlainToken = group(Number, Funny, String, Name)
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000185Token = Ignore + PlainToken
Guido van Rossum4d8e8591992-01-01 19:34:47 +0000186
Tim Petersde495832000-10-07 05:09:39 +0000187# First (or only) line of ' or " string.
Armin Ronacherc0eaeca2012-03-04 13:07:57 +0000188ContStr = group(StringPrefix + r"'[^\n'\\]*(?:\\.[^\n'\\]*)*" +
Ka-Ping Yee1ff08b12001-01-15 22:04:30 +0000189 group("'", r'\\\r?\n'),
Armin Ronacherc0eaeca2012-03-04 13:07:57 +0000190 StringPrefix + r'"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
Ka-Ping Yee1ff08b12001-01-15 22:04:30 +0000191 group('"', r'\\\r?\n'))
Ezio Melotti2cc3b4b2012-11-03 17:38:43 +0200192PseudoExtras = group(r'\\\r?\n|\Z', Comment, Triple)
Guido van Rossum3b631771997-10-27 20:44:15 +0000193PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)
Guido van Rossum1aec3231997-04-08 14:24:39 +0000194
Eric V. Smith1c8222c2015-10-26 04:37:55 -0400195# For a given string prefix plus quotes, endpats maps it to a regex
196# to match the remainder of that string. _prefix can be empty, for
197# a normal single or triple quoted string (with no prefix).
198endpats = {}
199for _prefix in _all_string_prefixes():
200 endpats[_prefix + "'"] = Single
201 endpats[_prefix + '"'] = Double
202 endpats[_prefix + "'''"] = Single3
203 endpats[_prefix + '"""'] = Double3
Benjamin Peterson33856de2010-08-30 14:41:20 +0000204
Eric V. Smith1c8222c2015-10-26 04:37:55 -0400205# A set of all of the single and triple quoted string prefixes,
206# including the opening quotes.
207single_quoted = set()
208triple_quoted = set()
209for t in _all_string_prefixes():
210 for u in (t + '"', t + "'"):
211 single_quoted.add(u)
212 for u in (t + '"""', t + "'''"):
213 triple_quoted.add(u)
Guido van Rossum9d6897a2002-08-24 06:54:19 +0000214
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000215tabsize = 8
Fred Drake9b8d8012000-08-17 04:45:13 +0000216
Ka-Ping Yee28c62bb2001-03-23 05:22:49 +0000217class TokenError(Exception): pass
218
219class StopTokenizing(Exception): pass
Fred Drake9b8d8012000-08-17 04:45:13 +0000220
Tim Peters5ca576e2001-06-18 22:08:13 +0000221
Thomas Wouters89f507f2006-12-13 04:49:30 +0000222class Untokenizer:
223
224 def __init__(self):
225 self.tokens = []
226 self.prev_row = 1
227 self.prev_col = 0
Trent Nelson428de652008-03-18 22:41:35 +0000228 self.encoding = None
Thomas Wouters89f507f2006-12-13 04:49:30 +0000229
230 def add_whitespace(self, start):
231 row, col = start
Terry Jan Reedy5e6db312014-02-17 16:45:48 -0500232 if row < self.prev_row or row == self.prev_row and col < self.prev_col:
233 raise ValueError("start ({},{}) precedes previous end ({},{})"
234 .format(row, col, self.prev_row, self.prev_col))
Terry Jan Reedy9dc3a362014-02-23 23:33:08 -0500235 row_offset = row - self.prev_row
Terry Jan Reedyf106f8f2014-02-23 23:39:57 -0500236 if row_offset:
Terry Jan Reedy9dc3a362014-02-23 23:33:08 -0500237 self.tokens.append("\\\n" * row_offset)
238 self.prev_col = 0
Thomas Wouters89f507f2006-12-13 04:49:30 +0000239 col_offset = col - self.prev_col
240 if col_offset:
241 self.tokens.append(" " * col_offset)
242
243 def untokenize(self, iterable):
Terry Jan Reedy5b8d2c32014-02-17 23:12:16 -0500244 it = iter(iterable)
Dingyuan Wange411b662015-06-22 10:01:12 +0800245 indents = []
246 startline = False
Terry Jan Reedy5b8d2c32014-02-17 23:12:16 -0500247 for t in it:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000248 if len(t) == 2:
Terry Jan Reedy5b8d2c32014-02-17 23:12:16 -0500249 self.compat(t, it)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000250 break
251 tok_type, token, start, end, line = t
Trent Nelson428de652008-03-18 22:41:35 +0000252 if tok_type == ENCODING:
253 self.encoding = token
254 continue
Terry Jan Reedy9dc3a362014-02-23 23:33:08 -0500255 if tok_type == ENDMARKER:
256 break
Dingyuan Wange411b662015-06-22 10:01:12 +0800257 if tok_type == INDENT:
258 indents.append(token)
259 continue
260 elif tok_type == DEDENT:
261 indents.pop()
262 self.prev_row, self.prev_col = end
263 continue
264 elif tok_type in (NEWLINE, NL):
265 startline = True
266 elif startline and indents:
267 indent = indents[-1]
268 if start[1] >= len(indent):
269 self.tokens.append(indent)
270 self.prev_col = len(indent)
271 startline = False
Thomas Wouters89f507f2006-12-13 04:49:30 +0000272 self.add_whitespace(start)
273 self.tokens.append(token)
274 self.prev_row, self.prev_col = end
275 if tok_type in (NEWLINE, NL):
276 self.prev_row += 1
277 self.prev_col = 0
278 return "".join(self.tokens)
279
280 def compat(self, token, iterable):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000281 indents = []
282 toks_append = self.tokens.append
Terry Jan Reedy5b8d2c32014-02-17 23:12:16 -0500283 startline = token[0] in (NEWLINE, NL)
Christian Heimesba4af492008-03-28 00:55:15 +0000284 prevstring = False
Terry Jan Reedy5b8d2c32014-02-17 23:12:16 -0500285
286 for tok in chain([token], iterable):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000287 toknum, tokval = tok[:2]
Trent Nelson428de652008-03-18 22:41:35 +0000288 if toknum == ENCODING:
289 self.encoding = tokval
290 continue
Thomas Wouters89f507f2006-12-13 04:49:30 +0000291
Yury Selivanov75445082015-05-11 22:57:16 -0400292 if toknum in (NAME, NUMBER, ASYNC, AWAIT):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000293 tokval += ' '
294
Christian Heimesba4af492008-03-28 00:55:15 +0000295 # Insert a space between two consecutive strings
296 if toknum == STRING:
297 if prevstring:
298 tokval = ' ' + tokval
299 prevstring = True
300 else:
301 prevstring = False
302
Thomas Wouters89f507f2006-12-13 04:49:30 +0000303 if toknum == INDENT:
304 indents.append(tokval)
305 continue
306 elif toknum == DEDENT:
307 indents.pop()
308 continue
309 elif toknum in (NEWLINE, NL):
310 startline = True
311 elif startline and indents:
312 toks_append(indents[-1])
313 startline = False
314 toks_append(tokval)
Raymond Hettinger68c04532005-06-10 11:05:19 +0000315
Trent Nelson428de652008-03-18 22:41:35 +0000316
Raymond Hettinger68c04532005-06-10 11:05:19 +0000317def untokenize(iterable):
318 """Transform tokens back into Python source code.
Trent Nelson428de652008-03-18 22:41:35 +0000319 It returns a bytes object, encoded using the ENCODING
320 token, which is the first token sequence output by tokenize.
Raymond Hettinger68c04532005-06-10 11:05:19 +0000321
322 Each element returned by the iterable must be a token sequence
Thomas Wouters89f507f2006-12-13 04:49:30 +0000323 with at least two elements, a token number and token value. If
324 only two tokens are passed, the resulting output is poor.
Raymond Hettinger68c04532005-06-10 11:05:19 +0000325
Thomas Wouters89f507f2006-12-13 04:49:30 +0000326 Round-trip invariant for full input:
327 Untokenized source will match input source exactly
328
Berker Peksagff8d0872015-12-30 01:41:58 +0200329 Round-trip invariant for limited input:
330 # Output bytes will tokenize back to the input
Trent Nelson428de652008-03-18 22:41:35 +0000331 t1 = [tok[:2] for tok in tokenize(f.readline)]
Raymond Hettinger68c04532005-06-10 11:05:19 +0000332 newcode = untokenize(t1)
Trent Nelson428de652008-03-18 22:41:35 +0000333 readline = BytesIO(newcode).readline
334 t2 = [tok[:2] for tok in tokenize(readline)]
Raymond Hettinger68c04532005-06-10 11:05:19 +0000335 assert t1 == t2
336 """
Thomas Wouters89f507f2006-12-13 04:49:30 +0000337 ut = Untokenizer()
Trent Nelson428de652008-03-18 22:41:35 +0000338 out = ut.untokenize(iterable)
339 if ut.encoding is not None:
340 out = out.encode(ut.encoding)
341 return out
Raymond Hettinger68c04532005-06-10 11:05:19 +0000342
Trent Nelson428de652008-03-18 22:41:35 +0000343
Benjamin Petersond3afada2009-10-09 21:43:09 +0000344def _get_normal_name(orig_enc):
345 """Imitates get_normal_name in tokenizer.c."""
346 # Only care about the first 12 characters.
347 enc = orig_enc[:12].lower().replace("_", "-")
348 if enc == "utf-8" or enc.startswith("utf-8-"):
349 return "utf-8"
350 if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or \
351 enc.startswith(("latin-1-", "iso-8859-1-", "iso-latin-1-")):
352 return "iso-8859-1"
353 return orig_enc
354
Trent Nelson428de652008-03-18 22:41:35 +0000355def detect_encoding(readline):
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000356 """
Trent Nelson428de652008-03-18 22:41:35 +0000357 The detect_encoding() function is used to detect the encoding that should
Ezio Melotti4bcc7962013-11-25 05:14:51 +0200358 be used to decode a Python source file. It requires one argument, readline,
Trent Nelson428de652008-03-18 22:41:35 +0000359 in the same way as the tokenize() generator.
360
361 It will call readline a maximum of twice, and return the encoding used
Florent Xicluna43e4ea12010-09-03 19:54:02 +0000362 (as a string) and a list of any lines (left as bytes) it has read in.
Trent Nelson428de652008-03-18 22:41:35 +0000363
364 It detects the encoding from the presence of a utf-8 bom or an encoding
Florent Xicluna43e4ea12010-09-03 19:54:02 +0000365 cookie as specified in pep-0263. If both a bom and a cookie are present,
366 but disagree, a SyntaxError will be raised. If the encoding cookie is an
367 invalid charset, raise a SyntaxError. Note that if a utf-8 bom is found,
Benjamin Peterson689a5582010-03-18 22:29:52 +0000368 'utf-8-sig' is returned.
Trent Nelson428de652008-03-18 22:41:35 +0000369
370 If no encoding is specified, then the default of 'utf-8' will be returned.
371 """
Brett Cannonc33f3f22012-04-20 13:23:54 -0400372 try:
373 filename = readline.__self__.name
374 except AttributeError:
375 filename = None
Trent Nelson428de652008-03-18 22:41:35 +0000376 bom_found = False
377 encoding = None
Benjamin Peterson689a5582010-03-18 22:29:52 +0000378 default = 'utf-8'
Trent Nelson428de652008-03-18 22:41:35 +0000379 def read_or_stop():
380 try:
381 return readline()
382 except StopIteration:
383 return b''
384
385 def find_cookie(line):
386 try:
Martin v. Löwis63674f42012-04-20 14:36:47 +0200387 # Decode as UTF-8. Either the line is an encoding declaration,
388 # in which case it should be pure ASCII, or it must be UTF-8
389 # per default encoding.
390 line_string = line.decode('utf-8')
Trent Nelson428de652008-03-18 22:41:35 +0000391 except UnicodeDecodeError:
Brett Cannonc33f3f22012-04-20 13:23:54 -0400392 msg = "invalid or missing encoding declaration"
393 if filename is not None:
394 msg = '{} for {!r}'.format(msg, filename)
395 raise SyntaxError(msg)
Benjamin Peterson433f32c2008-12-12 01:25:05 +0000396
Serhiy Storchakadafea852013-09-16 23:51:56 +0300397 match = cookie_re.match(line_string)
398 if not match:
Benjamin Peterson433f32c2008-12-12 01:25:05 +0000399 return None
Serhiy Storchakadafea852013-09-16 23:51:56 +0300400 encoding = _get_normal_name(match.group(1))
Benjamin Peterson433f32c2008-12-12 01:25:05 +0000401 try:
402 codec = lookup(encoding)
403 except LookupError:
404 # This behaviour mimics the Python interpreter
Brett Cannonc33f3f22012-04-20 13:23:54 -0400405 if filename is None:
406 msg = "unknown encoding: " + encoding
407 else:
408 msg = "unknown encoding for {!r}: {}".format(filename,
409 encoding)
410 raise SyntaxError(msg)
Benjamin Peterson433f32c2008-12-12 01:25:05 +0000411
Benjamin Peterson1613ed82010-03-18 22:34:15 +0000412 if bom_found:
Florent Xicluna11f0b412012-07-07 12:13:35 +0200413 if encoding != 'utf-8':
Benjamin Peterson1613ed82010-03-18 22:34:15 +0000414 # This behaviour mimics the Python interpreter
Brett Cannonc33f3f22012-04-20 13:23:54 -0400415 if filename is None:
416 msg = 'encoding problem: utf-8'
417 else:
418 msg = 'encoding problem for {!r}: utf-8'.format(filename)
419 raise SyntaxError(msg)
Benjamin Peterson1613ed82010-03-18 22:34:15 +0000420 encoding += '-sig'
Benjamin Peterson433f32c2008-12-12 01:25:05 +0000421 return encoding
Trent Nelson428de652008-03-18 22:41:35 +0000422
423 first = read_or_stop()
Benjamin Peterson433f32c2008-12-12 01:25:05 +0000424 if first.startswith(BOM_UTF8):
Trent Nelson428de652008-03-18 22:41:35 +0000425 bom_found = True
426 first = first[3:]
Benjamin Peterson689a5582010-03-18 22:29:52 +0000427 default = 'utf-8-sig'
Trent Nelson428de652008-03-18 22:41:35 +0000428 if not first:
Benjamin Peterson689a5582010-03-18 22:29:52 +0000429 return default, []
Trent Nelson428de652008-03-18 22:41:35 +0000430
431 encoding = find_cookie(first)
432 if encoding:
433 return encoding, [first]
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200434 if not blank_re.match(first):
435 return default, [first]
Trent Nelson428de652008-03-18 22:41:35 +0000436
437 second = read_or_stop()
438 if not second:
Benjamin Peterson689a5582010-03-18 22:29:52 +0000439 return default, [first]
Trent Nelson428de652008-03-18 22:41:35 +0000440
441 encoding = find_cookie(second)
442 if encoding:
443 return encoding, [first, second]
444
Benjamin Peterson689a5582010-03-18 22:29:52 +0000445 return default, [first, second]
Trent Nelson428de652008-03-18 22:41:35 +0000446
447
Victor Stinner58c07522010-11-09 01:08:59 +0000448def open(filename):
449 """Open a file in read only mode using the encoding detected by
450 detect_encoding().
451 """
Victor Stinner96917502014-12-05 10:17:10 +0100452 buffer = _builtin_open(filename, 'rb')
Victor Stinner387729e2015-05-26 00:43:58 +0200453 try:
454 encoding, lines = detect_encoding(buffer.readline)
455 buffer.seek(0)
456 text = TextIOWrapper(buffer, encoding, line_buffering=True)
457 text.mode = 'r'
458 return text
459 except:
460 buffer.close()
461 raise
Victor Stinner58c07522010-11-09 01:08:59 +0000462
463
Trent Nelson428de652008-03-18 22:41:35 +0000464def tokenize(readline):
465 """
Berker Peksagff8d0872015-12-30 01:41:58 +0200466 The tokenize() generator requires one argument, readline, which
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000467 must be a callable object which provides the same interface as the
Florent Xicluna43e4ea12010-09-03 19:54:02 +0000468 readline() method of built-in file objects. Each call to the function
Berker Peksagff8d0872015-12-30 01:41:58 +0200469 should return one line of input as bytes. Alternatively, readline
Raymond Hettinger68c04532005-06-10 11:05:19 +0000470 can be a callable function terminating with StopIteration:
Trent Nelson428de652008-03-18 22:41:35 +0000471 readline = open(myfile, 'rb').__next__ # Example of alternate readline
Tim Peters8ac14952002-05-23 15:15:30 +0000472
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000473 The generator produces 5-tuples with these members: the token type; the
474 token string; a 2-tuple (srow, scol) of ints specifying the row and
475 column where the token begins in the source; a 2-tuple (erow, ecol) of
476 ints specifying the row and column where the token ends in the source;
Florent Xicluna43e4ea12010-09-03 19:54:02 +0000477 and the line on which the token was found. The line passed is the
Tim Peters8ac14952002-05-23 15:15:30 +0000478 logical line; continuation lines are included.
Trent Nelson428de652008-03-18 22:41:35 +0000479
480 The first token sequence will always be an ENCODING token
481 which tells you which encoding was used to decode the bytes stream.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000482 """
Benjamin Peterson21db77e2009-11-14 16:27:26 +0000483 # This import is here to avoid problems when the itertools module is not
484 # built yet and tokenize is imported.
Benjamin Peterson81dd8b92009-11-14 18:09:17 +0000485 from itertools import chain, repeat
Trent Nelson428de652008-03-18 22:41:35 +0000486 encoding, consumed = detect_encoding(readline)
Benjamin Peterson81dd8b92009-11-14 18:09:17 +0000487 rl_gen = iter(readline, b"")
488 empty = repeat(b"")
489 return _tokenize(chain(consumed, rl_gen, empty).__next__, encoding)
Trent Nelson428de652008-03-18 22:41:35 +0000490
491
492def _tokenize(readline, encoding):
Guido van Rossum1aec3231997-04-08 14:24:39 +0000493 lnum = parenlev = continued = 0
Benjamin Peterson33856de2010-08-30 14:41:20 +0000494 numchars = '0123456789'
Guido van Rossumde655271997-04-09 17:15:54 +0000495 contstr, needcont = '', 0
Guido van Rossuma90c78b1998-04-03 16:05:38 +0000496 contline = None
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000497 indents = [0]
Guido van Rossum1aec3231997-04-08 14:24:39 +0000498
Yury Selivanov96ec9342015-07-23 15:01:58 +0300499 # 'stashed' and 'async_*' are used for async/await parsing
Yury Selivanov75445082015-05-11 22:57:16 -0400500 stashed = None
Yury Selivanov96ec9342015-07-23 15:01:58 +0300501 async_def = False
502 async_def_indent = 0
503 async_def_nl = False
Yury Selivanov75445082015-05-11 22:57:16 -0400504
Trent Nelson428de652008-03-18 22:41:35 +0000505 if encoding is not None:
Benjamin Peterson689a5582010-03-18 22:29:52 +0000506 if encoding == "utf-8-sig":
507 # BOM will already have been stripped.
508 encoding = "utf-8"
Raymond Hettingera48db392009-04-29 00:34:27 +0000509 yield TokenInfo(ENCODING, encoding, (0, 0), (0, 0), '')
Benjamin Peterson0fe14382008-06-05 23:07:42 +0000510 while True: # loop over lines in stream
Raymond Hettinger68c04532005-06-10 11:05:19 +0000511 try:
512 line = readline()
513 except StopIteration:
Trent Nelson428de652008-03-18 22:41:35 +0000514 line = b''
515
516 if encoding is not None:
517 line = line.decode(encoding)
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000518 lnum += 1
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000519 pos, max = 0, len(line)
520
521 if contstr: # continued string
Guido van Rossumde655271997-04-09 17:15:54 +0000522 if not line:
Collin Winterce36ad82007-08-30 01:19:48 +0000523 raise TokenError("EOF in multi-line string", strstart)
Guido van Rossum3b631771997-10-27 20:44:15 +0000524 endmatch = endprog.match(line)
525 if endmatch:
526 pos = end = endmatch.end(0)
Raymond Hettingera48db392009-04-29 00:34:27 +0000527 yield TokenInfo(STRING, contstr + line[:end],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000528 strstart, (lnum, end), contline + line)
Guido van Rossumde655271997-04-09 17:15:54 +0000529 contstr, needcont = '', 0
Guido van Rossuma90c78b1998-04-03 16:05:38 +0000530 contline = None
Guido van Rossumde655271997-04-09 17:15:54 +0000531 elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n':
Raymond Hettingera48db392009-04-29 00:34:27 +0000532 yield TokenInfo(ERRORTOKEN, contstr + line,
Guido van Rossuma90c78b1998-04-03 16:05:38 +0000533 strstart, (lnum, len(line)), contline)
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000534 contstr = ''
Guido van Rossuma90c78b1998-04-03 16:05:38 +0000535 contline = None
Guido van Rossumde655271997-04-09 17:15:54 +0000536 continue
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000537 else:
538 contstr = contstr + line
Guido van Rossuma90c78b1998-04-03 16:05:38 +0000539 contline = contline + line
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000540 continue
541
Guido van Rossum1aec3231997-04-08 14:24:39 +0000542 elif parenlev == 0 and not continued: # new statement
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000543 if not line: break
544 column = 0
Guido van Rossum1aec3231997-04-08 14:24:39 +0000545 while pos < max: # measure leading whitespace
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000546 if line[pos] == ' ':
547 column += 1
548 elif line[pos] == '\t':
549 column = (column//tabsize + 1)*tabsize
550 elif line[pos] == '\f':
551 column = 0
552 else:
553 break
554 pos += 1
555 if pos == max:
556 break
Guido van Rossum1aec3231997-04-08 14:24:39 +0000557
558 if line[pos] in '#\r\n': # skip comments or blank lines
Thomas Wouters89f507f2006-12-13 04:49:30 +0000559 if line[pos] == '#':
560 comment_token = line[pos:].rstrip('\r\n')
561 nl_pos = pos + len(comment_token)
Raymond Hettingera48db392009-04-29 00:34:27 +0000562 yield TokenInfo(COMMENT, comment_token,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000563 (lnum, pos), (lnum, pos + len(comment_token)), line)
Raymond Hettingera48db392009-04-29 00:34:27 +0000564 yield TokenInfo(NL, line[nl_pos:],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000565 (lnum, nl_pos), (lnum, len(line)), line)
566 else:
Raymond Hettingera48db392009-04-29 00:34:27 +0000567 yield TokenInfo((NL, COMMENT)[line[pos] == '#'], line[pos:],
Guido van Rossum1aec3231997-04-08 14:24:39 +0000568 (lnum, pos), (lnum, len(line)), line)
569 continue
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000570
571 if column > indents[-1]: # count indents or dedents
572 indents.append(column)
Raymond Hettingera48db392009-04-29 00:34:27 +0000573 yield TokenInfo(INDENT, line[:pos], (lnum, 0), (lnum, pos), line)
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000574 while column < indents[-1]:
Raymond Hettingerda99d1c2005-06-21 07:43:58 +0000575 if column not in indents:
576 raise IndentationError(
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000577 "unindent does not match any outer indentation level",
578 ("<tokenize>", lnum, pos, line))
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000579 indents = indents[:-1]
Yury Selivanov75445082015-05-11 22:57:16 -0400580
Yury Selivanov96ec9342015-07-23 15:01:58 +0300581 if async_def and async_def_indent >= indents[-1]:
582 async_def = False
583 async_def_nl = False
584 async_def_indent = 0
Yury Selivanov75445082015-05-11 22:57:16 -0400585
Raymond Hettingera48db392009-04-29 00:34:27 +0000586 yield TokenInfo(DEDENT, '', (lnum, pos), (lnum, pos), line)
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000587
Yury Selivanov96ec9342015-07-23 15:01:58 +0300588 if async_def and async_def_nl and async_def_indent >= indents[-1]:
589 async_def = False
590 async_def_nl = False
591 async_def_indent = 0
592
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000593 else: # continued statement
Guido van Rossumde655271997-04-09 17:15:54 +0000594 if not line:
Collin Winterce36ad82007-08-30 01:19:48 +0000595 raise TokenError("EOF in multi-line statement", (lnum, 0))
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000596 continued = 0
597
598 while pos < max:
Antoine Pitrou10a99b02011-10-11 15:45:56 +0200599 pseudomatch = _compile(PseudoToken).match(line, pos)
Guido van Rossum3b631771997-10-27 20:44:15 +0000600 if pseudomatch: # scan for tokens
601 start, end = pseudomatch.span(1)
Guido van Rossumde655271997-04-09 17:15:54 +0000602 spos, epos, pos = (lnum, start), (lnum, end), end
Ezio Melotti2cc3b4b2012-11-03 17:38:43 +0200603 if start == end:
604 continue
Guido van Rossum1aec3231997-04-08 14:24:39 +0000605 token, initial = line[start:end], line[start]
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000606
Georg Brandldde00282007-03-18 19:01:53 +0000607 if (initial in numchars or # ordinary number
608 (initial == '.' and token != '.' and token != '...')):
Raymond Hettingera48db392009-04-29 00:34:27 +0000609 yield TokenInfo(NUMBER, token, spos, epos, line)
Guido van Rossum1aec3231997-04-08 14:24:39 +0000610 elif initial in '\r\n':
Yury Selivanov75445082015-05-11 22:57:16 -0400611 if stashed:
612 yield stashed
613 stashed = None
Yury Selivanov96ec9342015-07-23 15:01:58 +0300614 if parenlev > 0:
615 yield TokenInfo(NL, token, spos, epos, line)
616 else:
617 yield TokenInfo(NEWLINE, token, spos, epos, line)
618 if async_def:
619 async_def_nl = True
620
Guido van Rossum1aec3231997-04-08 14:24:39 +0000621 elif initial == '#':
Thomas Wouters89f507f2006-12-13 04:49:30 +0000622 assert not token.endswith("\n")
Yury Selivanov75445082015-05-11 22:57:16 -0400623 if stashed:
624 yield stashed
625 stashed = None
Raymond Hettingera48db392009-04-29 00:34:27 +0000626 yield TokenInfo(COMMENT, token, spos, epos, line)
Eric V. Smith1c8222c2015-10-26 04:37:55 -0400627
Guido van Rossum9d6897a2002-08-24 06:54:19 +0000628 elif token in triple_quoted:
Antoine Pitrou10a99b02011-10-11 15:45:56 +0200629 endprog = _compile(endpats[token])
Guido van Rossum3b631771997-10-27 20:44:15 +0000630 endmatch = endprog.match(line, pos)
631 if endmatch: # all on one line
632 pos = endmatch.end(0)
Guido van Rossum1aec3231997-04-08 14:24:39 +0000633 token = line[start:pos]
Raymond Hettingera48db392009-04-29 00:34:27 +0000634 yield TokenInfo(STRING, token, spos, (lnum, pos), line)
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000635 else:
Guido van Rossum1aec3231997-04-08 14:24:39 +0000636 strstart = (lnum, start) # multiple lines
637 contstr = line[start:]
Guido van Rossuma90c78b1998-04-03 16:05:38 +0000638 contline = line
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000639 break
Eric V. Smith1c8222c2015-10-26 04:37:55 -0400640
641 # Check up to the first 3 chars of the token to see if
642 # they're in the single_quoted set. If so, they start
643 # a string.
644 # We're using the first 3, because we're looking for
645 # "rb'" (for example) at the start of the token. If
646 # we switch to longer prefixes, this needs to be
647 # adjusted.
648 # Note that initial == token[:1].
Berker Peksaga7161e72015-12-30 01:42:43 +0200649 # Also note that single quote checking must come after
Eric V. Smith1c8222c2015-10-26 04:37:55 -0400650 # triple quote checking (above).
651 elif (initial in single_quoted or
652 token[:2] in single_quoted or
653 token[:3] in single_quoted):
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000654 if token[-1] == '\n': # continued string
Guido van Rossum1aec3231997-04-08 14:24:39 +0000655 strstart = (lnum, start)
Eric V. Smith1c8222c2015-10-26 04:37:55 -0400656 # Again, using the first 3 chars of the
657 # token. This is looking for the matching end
658 # regex for the correct type of quote
659 # character. So it's really looking for
660 # endpats["'"] or endpats['"'], by trying to
661 # skip string prefix characters, if any.
662 endprog = _compile(endpats.get(initial) or
663 endpats.get(token[1]) or
664 endpats.get(token[2]))
Guido van Rossumde655271997-04-09 17:15:54 +0000665 contstr, needcont = line[start:], 1
Guido van Rossuma90c78b1998-04-03 16:05:38 +0000666 contline = line
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000667 break
668 else: # ordinary string
Raymond Hettingera48db392009-04-29 00:34:27 +0000669 yield TokenInfo(STRING, token, spos, epos, line)
Eric V. Smith1c8222c2015-10-26 04:37:55 -0400670
Benjamin Peterson33856de2010-08-30 14:41:20 +0000671 elif initial.isidentifier(): # ordinary name
Yury Selivanov75445082015-05-11 22:57:16 -0400672 if token in ('async', 'await'):
Yury Selivanov96ec9342015-07-23 15:01:58 +0300673 if async_def:
Yury Selivanov75445082015-05-11 22:57:16 -0400674 yield TokenInfo(
675 ASYNC if token == 'async' else AWAIT,
676 token, spos, epos, line)
677 continue
678
679 tok = TokenInfo(NAME, token, spos, epos, line)
680 if token == 'async' and not stashed:
681 stashed = tok
682 continue
683
684 if token == 'def':
685 if (stashed
686 and stashed.type == NAME
687 and stashed.string == 'async'):
688
Yury Selivanov96ec9342015-07-23 15:01:58 +0300689 async_def = True
690 async_def_indent = indents[-1]
Yury Selivanov75445082015-05-11 22:57:16 -0400691
692 yield TokenInfo(ASYNC, stashed.string,
693 stashed.start, stashed.end,
694 stashed.line)
695 stashed = None
Yury Selivanov75445082015-05-11 22:57:16 -0400696
697 if stashed:
698 yield stashed
699 stashed = None
700
701 yield tok
Guido van Rossum3b631771997-10-27 20:44:15 +0000702 elif initial == '\\': # continued stmt
703 continued = 1
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000704 else:
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000705 if initial in '([{':
706 parenlev += 1
707 elif initial in ')]}':
708 parenlev -= 1
Yury Selivanov75445082015-05-11 22:57:16 -0400709 if stashed:
710 yield stashed
711 stashed = None
Raymond Hettingera48db392009-04-29 00:34:27 +0000712 yield TokenInfo(OP, token, spos, epos, line)
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000713 else:
Raymond Hettingera48db392009-04-29 00:34:27 +0000714 yield TokenInfo(ERRORTOKEN, line[pos],
Guido van Rossumde655271997-04-09 17:15:54 +0000715 (lnum, pos), (lnum, pos+1), line)
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000716 pos += 1
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000717
Yury Selivanov75445082015-05-11 22:57:16 -0400718 if stashed:
719 yield stashed
720 stashed = None
721
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000722 for indent in indents[1:]: # pop remaining indent levels
Raymond Hettingera48db392009-04-29 00:34:27 +0000723 yield TokenInfo(DEDENT, '', (lnum, 0), (lnum, 0), '')
724 yield TokenInfo(ENDMARKER, '', (lnum, 0), (lnum, 0), '')
Guido van Rossumfc6f5331997-03-07 00:21:12 +0000725
Trent Nelson428de652008-03-18 22:41:35 +0000726
727# An undocumented, backwards compatible, API for all the places in the standard
728# library that expect to be able to use tokenize with strings
729def generate_tokens(readline):
730 return _tokenize(readline, None)
Raymond Hettinger6c60d092010-09-09 04:32:39 +0000731
Meador Inge14c0f032011-10-07 08:53:38 -0500732def main():
733 import argparse
734
735 # Helper error handling routines
736 def perror(message):
737 print(message, file=sys.stderr)
738
739 def error(message, filename=None, location=None):
740 if location:
741 args = (filename,) + location + (message,)
742 perror("%s:%d:%d: error: %s" % args)
743 elif filename:
744 perror("%s: error: %s" % (filename, message))
745 else:
746 perror("error: %s" % message)
747 sys.exit(1)
748
749 # Parse the arguments and options
750 parser = argparse.ArgumentParser(prog='python -m tokenize')
751 parser.add_argument(dest='filename', nargs='?',
752 metavar='filename.py',
753 help='the file to tokenize; defaults to stdin')
Meador Inge00c7f852012-01-19 00:44:45 -0600754 parser.add_argument('-e', '--exact', dest='exact', action='store_true',
755 help='display token names using the exact type')
Meador Inge14c0f032011-10-07 08:53:38 -0500756 args = parser.parse_args()
757
758 try:
759 # Tokenize the input
760 if args.filename:
761 filename = args.filename
Victor Stinner96917502014-12-05 10:17:10 +0100762 with _builtin_open(filename, 'rb') as f:
Meador Inge14c0f032011-10-07 08:53:38 -0500763 tokens = list(tokenize(f.readline))
764 else:
765 filename = "<stdin>"
766 tokens = _tokenize(sys.stdin.readline, None)
767
768 # Output the tokenization
769 for token in tokens:
Meador Inge00c7f852012-01-19 00:44:45 -0600770 token_type = token.type
771 if args.exact:
772 token_type = token.exact_type
Meador Inge14c0f032011-10-07 08:53:38 -0500773 token_range = "%d,%d-%d,%d:" % (token.start + token.end)
774 print("%-20s%-15s%-15r" %
Meador Inge00c7f852012-01-19 00:44:45 -0600775 (token_range, tok_name[token_type], token.string))
Meador Inge14c0f032011-10-07 08:53:38 -0500776 except IndentationError as err:
777 line, column = err.args[1][1:3]
778 error(err.args[0], filename, (line, column))
779 except TokenError as err:
780 line, column = err.args[1]
781 error(err.args[0], filename, (line, column))
782 except SyntaxError as err:
783 error(err, filename)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200784 except OSError as err:
Meador Inge14c0f032011-10-07 08:53:38 -0500785 error(err)
786 except KeyboardInterrupt:
787 print("interrupted\n")
788 except Exception as err:
789 perror("unexpected error: %s" % err)
790 raise
791
Raymond Hettinger6c60d092010-09-09 04:32:39 +0000792if __name__ == "__main__":
Meador Inge14c0f032011-10-07 08:53:38 -0500793 main()