blob: bbe73d00e9a5582997161072e5397182a92fcb24 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`tokenize` --- Tokenizer for Python source
3===============================================
4
5.. module:: tokenize
6 :synopsis: Lexical scanner for Python source code.
7.. moduleauthor:: Ka Ping Yee
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
10
11The :mod:`tokenize` module provides a lexical scanner for Python source code,
Trent Nelson428de652008-03-18 22:41:35 +000012implemented in Python. The scanner in this module returns comments as tokens
13as well, making it useful for implementing "pretty-printers," including
14colorizers for on-screen displays.
Georg Brandl116aa622007-08-15 14:28:22 +000015
Georg Brandl9afde1c2007-11-01 20:32:30 +000016The primary entry point is a :term:`generator`:
Georg Brandl116aa622007-08-15 14:28:22 +000017
18
Trent Nelson428de652008-03-18 22:41:35 +000019.. function:: tokenize(readline)
Georg Brandl116aa622007-08-15 14:28:22 +000020
Trent Nelson428de652008-03-18 22:41:35 +000021 The :func:`tokenize` generator requires one argument, *readline*, which
Georg Brandl116aa622007-08-15 14:28:22 +000022 must be a callable object which provides the same interface as the
23 :meth:`readline` method of built-in file objects (see section
Trent Nelson428de652008-03-18 22:41:35 +000024 :ref:`bltin-file-objects`). Each call to the function should return one
25 line of input as bytes.
Georg Brandl116aa622007-08-15 14:28:22 +000026
Trent Nelson428de652008-03-18 22:41:35 +000027 The generator produces 5-tuples with these members: the token type; the
28 token string; a 2-tuple ``(srow, scol)`` of ints specifying the row and
29 column where the token begins in the source; a 2-tuple ``(erow, ecol)`` of
30 ints specifying the row and column where the token ends in the source; and
31 the line on which the token was found. The line passed is the *logical*
32 line; continuation lines are included.
33
34 tokenize determines the source encoding of the file by looking for a utf-8
35 bom or encoding cookie, according to :pep:`263`.
Georg Brandl116aa622007-08-15 14:28:22 +000036
Georg Brandl55ac8f02007-09-01 13:51:09 +000037
Georg Brandl116aa622007-08-15 14:28:22 +000038All constants from the :mod:`token` module are also exported from
Trent Nelson428de652008-03-18 22:41:35 +000039:mod:`tokenize`, as are three additional token type values:
Georg Brandl116aa622007-08-15 14:28:22 +000040
Georg Brandl116aa622007-08-15 14:28:22 +000041.. data:: COMMENT
42
43 Token value used to indicate a comment.
44
45
46.. data:: NL
47
48 Token value used to indicate a non-terminating newline. The NEWLINE token
Trent Nelson428de652008-03-18 22:41:35 +000049 indicates the end of a logical line of Python code; NL tokens are generated
50 when a logical line of code is continued over multiple physical lines.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Trent Nelson428de652008-03-18 22:41:35 +000052
53.. data:: ENCODING
54
55 Token value that indicates the encoding used to decode the source bytes
56 into text. The first token returned by :func:`tokenize` will always be an
57 ENCODING token.
58
59
60Another function is provided to reverse the tokenization process. This is
61useful for creating tools that tokenize a script, modify the token stream, and
62write back the modified script.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64
65.. function:: untokenize(iterable)
66
Trent Nelson428de652008-03-18 22:41:35 +000067 Converts tokens back into Python source code. The *iterable* must return
68 sequences with at least two elements, the token type and the token string.
69 Any additional sequence elements are ignored.
70
71 The reconstructed script is returned as a single string. The result is
72 guaranteed to tokenize back to match the input so that the conversion is
73 lossless and round-trips are assured. The guarantee applies only to the
74 token type and token string as the spacing between tokens (column
75 positions) may change.
76
77 It returns bytes, encoded using the ENCODING token, which is the first
78 token sequence output by :func:`tokenize`.
Georg Brandl116aa622007-08-15 14:28:22 +000079
Georg Brandl116aa622007-08-15 14:28:22 +000080
Trent Nelson428de652008-03-18 22:41:35 +000081:func:`tokenize` needs to detect the encoding of source files it tokenizes. The
82function it uses to do this is available:
83
84.. function:: detect_encoding(readline)
85
86 The :func:`detect_encoding` function is used to detect the encoding that
87 should be used to decode a Python source file. It requires one argment,
88 readline, in the same way as the :func:`tokenize` generator.
89
90 It will call readline a maximum of twice, and return the encoding used
91 (as a string) and a list of any lines (not decoded from bytes) it has read
92 in.
93
94 It detects the encoding from the presence of a utf-8 bom or an encoding
95 cookie as specified in pep-0263. If both a bom and a cookie are present,
96 but disagree, a SyntaxError will be raised.
97
98 If no encoding is specified, then the default of 'utf-8' will be returned.
99
100
Georg Brandl116aa622007-08-15 14:28:22 +0000101Example of a script re-writer that transforms float literals into Decimal
102objects::
103
Trent Nelson428de652008-03-18 22:41:35 +0000104 def decistmt(s):
105 """Substitute Decimals for floats in a string of statements.
106
107 >>> from decimal import Decimal
108 >>> s = 'print(+21.3e-5*-.1234/81.7)'
109 >>> decistmt(s)
110 "print (+Decimal ('21.3e-5')*-Decimal ('.1234')/Decimal ('81.7'))"
111
112 The format of the exponent is inherited from the platform C library.
113 Known cases are "e-007" (Windows) and "e-07" (not Windows). Since
114 we're only showing 12 digits, and the 13th isn't close to 5, the
115 rest of the output should be platform-independent.
116
117 >>> exec(s) #doctest: +ELLIPSIS
118 -3.21716034272e-0...7
119
120 Output from calculations with Decimal should be identical across all
121 platforms.
122
123 >>> exec(decistmt(s))
124 -3.217160342717258261933904529E-7
125 """
126 result = []
127 g = tokenize(BytesIO(s.encode('utf-8')).readline) # tokenize the string
128 for toknum, tokval, _, _, _ in g:
129 if toknum == NUMBER and '.' in tokval: # replace NUMBER tokens
130 result.extend([
131 (NAME, 'Decimal'),
132 (OP, '('),
133 (STRING, repr(tokval)),
134 (OP, ')')
135 ])
136 else:
137 result.append((toknum, tokval))
138 return untokenize(result).decode('utf-8')
Georg Brandl116aa622007-08-15 14:28:22 +0000139
Georg Brandl116aa622007-08-15 14:28:22 +0000140