Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`tokenize` --- Tokenizer for Python source |
| 2 | =============================================== |
| 3 | |
| 4 | .. module:: tokenize |
| 5 | :synopsis: Lexical scanner for Python source code. |
| 6 | .. moduleauthor:: Ka Ping Yee |
| 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 8 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 9 | **Source code:** :source:`Lib/tokenize.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 11 | -------------- |
| 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | The :mod:`tokenize` module provides a lexical scanner for Python source code, |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 14 | implemented in Python. The scanner in this module returns comments as tokens |
| 15 | as well, making it useful for implementing "pretty-printers," including |
| 16 | colorizers for on-screen displays. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
Meador Inge | 972cfb9 | 2012-01-19 00:22:22 -0600 | [diff] [blame] | 18 | To simplify token stream handling, all :ref:`operators` and :ref:`delimiters` |
| 19 | tokens are returned using the generic :data:`token.OP` token type. The exact |
| 20 | type can be determined by checking the token ``string`` field on the |
| 21 | :term:`named tuple` returned from :func:`tokenize.tokenize` for the character |
| 22 | sequence that identifies a specific operator token. |
| 23 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 24 | The primary entry point is a :term:`generator`: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 26 | .. function:: tokenize(readline) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 28 | The :func:`tokenize` generator requires one argument, *readline*, which |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | must be a callable object which provides the same interface as the |
Antoine Pitrou | 4adb288 | 2010-01-04 18:50:53 +0000 | [diff] [blame] | 30 | :meth:`io.IOBase.readline` method of file objects. Each call to the |
| 31 | function should return one line of input as bytes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 33 | The generator produces 5-tuples with these members: the token type; the |
| 34 | token string; a 2-tuple ``(srow, scol)`` of ints specifying the row and |
| 35 | column where the token begins in the source; a 2-tuple ``(erow, ecol)`` of |
| 36 | ints specifying the row and column where the token ends in the source; and |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 37 | the line on which the token was found. The line passed (the last tuple item) |
Raymond Hettinger | a48db39 | 2009-04-29 00:34:27 +0000 | [diff] [blame] | 38 | is the *logical* line; continuation lines are included. The 5 tuple is |
| 39 | returned as a :term:`named tuple` with the field names: |
| 40 | ``type string start end line``. |
| 41 | |
| 42 | .. versionchanged:: 3.1 |
| 43 | Added support for named tuples. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 44 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 45 | :func:`tokenize` determines the source encoding of the file by looking for a |
| 46 | UTF-8 BOM or encoding cookie, according to :pep:`263`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 48 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | All constants from the :mod:`token` module are also exported from |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 50 | :mod:`tokenize`, as are three additional token type values: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | .. data:: COMMENT |
| 53 | |
| 54 | Token value used to indicate a comment. |
| 55 | |
| 56 | |
| 57 | .. data:: NL |
| 58 | |
| 59 | Token value used to indicate a non-terminating newline. The NEWLINE token |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 60 | indicates the end of a logical line of Python code; NL tokens are generated |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 61 | when a logical line of code is continued over multiple physical lines. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 63 | |
| 64 | .. data:: ENCODING |
| 65 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 66 | Token value that indicates the encoding used to decode the source bytes |
| 67 | into text. The first token returned by :func:`tokenize` will always be an |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 68 | ENCODING token. |
| 69 | |
| 70 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 71 | Another function is provided to reverse the tokenization process. This is |
| 72 | useful for creating tools that tokenize a script, modify the token stream, and |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 73 | write back the modified script. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | .. function:: untokenize(iterable) |
| 77 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 78 | Converts tokens back into Python source code. The *iterable* must return |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 79 | sequences with at least two elements, the token type and the token string. |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 80 | Any additional sequence elements are ignored. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 81 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 82 | The reconstructed script is returned as a single string. The result is |
| 83 | guaranteed to tokenize back to match the input so that the conversion is |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 84 | lossless and round-trips are assured. The guarantee applies only to the |
| 85 | token type and token string as the spacing between tokens (column |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 86 | positions) may change. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 87 | |
| 88 | It returns bytes, encoded using the ENCODING token, which is the first |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 89 | token sequence output by :func:`tokenize`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 92 | :func:`tokenize` needs to detect the encoding of source files it tokenizes. The |
| 93 | function it uses to do this is available: |
| 94 | |
| 95 | .. function:: detect_encoding(readline) |
| 96 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 97 | The :func:`detect_encoding` function is used to detect the encoding that |
Georg Brandl | ae2dbe2 | 2009-03-13 19:04:40 +0000 | [diff] [blame] | 98 | should be used to decode a Python source file. It requires one argument, |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 99 | readline, in the same way as the :func:`tokenize` generator. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 100 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 101 | It will call readline a maximum of twice, and return the encoding used |
| 102 | (as a string) and a list of any lines (not decoded from bytes) it has read |
| 103 | in. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 104 | |
Ezio Melotti | a8f6f1e | 2009-12-20 12:24:57 +0000 | [diff] [blame] | 105 | It detects the encoding from the presence of a UTF-8 BOM or an encoding |
| 106 | cookie as specified in :pep:`263`. If both a BOM and a cookie are present, |
Benjamin Peterson | 689a558 | 2010-03-18 22:29:52 +0000 | [diff] [blame] | 107 | but disagree, a SyntaxError will be raised. Note that if the BOM is found, |
| 108 | ``'utf-8-sig'`` will be returned as an encoding. |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 109 | |
Benjamin Peterson | b3a4829 | 2010-03-18 22:43:41 +0000 | [diff] [blame] | 110 | If no encoding is specified, then the default of ``'utf-8'`` will be |
| 111 | returned. |
| 112 | |
Victor Stinner | 58c0752 | 2010-11-09 01:08:59 +0000 | [diff] [blame] | 113 | Use :func:`open` to open Python source files: it uses |
| 114 | :func:`detect_encoding` to detect the file encoding. |
Benjamin Peterson | b3a4829 | 2010-03-18 22:43:41 +0000 | [diff] [blame] | 115 | |
Victor Stinner | 58c0752 | 2010-11-09 01:08:59 +0000 | [diff] [blame] | 116 | |
| 117 | .. function:: open(filename) |
| 118 | |
| 119 | Open a file in read only mode using the encoding detected by |
| 120 | :func:`detect_encoding`. |
| 121 | |
| 122 | .. versionadded:: 3.2 |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 123 | |
| 124 | |
Raymond Hettinger | 6c60d09 | 2010-09-09 04:32:39 +0000 | [diff] [blame] | 125 | Example of a script rewriter that transforms float literals into Decimal |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 126 | objects:: |
| 127 | |
Ezio Melotti | a8f6f1e | 2009-12-20 12:24:57 +0000 | [diff] [blame] | 128 | from tokenize import tokenize, untokenize, NUMBER, STRING, NAME, OP |
| 129 | from io import BytesIO |
| 130 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 131 | def decistmt(s): |
| 132 | """Substitute Decimals for floats in a string of statements. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 133 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 134 | >>> from decimal import Decimal |
| 135 | >>> s = 'print(+21.3e-5*-.1234/81.7)' |
| 136 | >>> decistmt(s) |
| 137 | "print (+Decimal ('21.3e-5')*-Decimal ('.1234')/Decimal ('81.7'))" |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 138 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 139 | The format of the exponent is inherited from the platform C library. |
| 140 | Known cases are "e-007" (Windows) and "e-07" (not Windows). Since |
| 141 | we're only showing 12 digits, and the 13th isn't close to 5, the |
| 142 | rest of the output should be platform-independent. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 143 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 144 | >>> exec(s) #doctest: +ELLIPSIS |
| 145 | -3.21716034272e-0...7 |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 146 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 147 | Output from calculations with Decimal should be identical across all |
| 148 | platforms. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 149 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 150 | >>> exec(decistmt(s)) |
| 151 | -3.217160342717258261933904529E-7 |
| 152 | """ |
| 153 | result = [] |
| 154 | g = tokenize(BytesIO(s.encode('utf-8')).readline) # tokenize the string |
| 155 | for toknum, tokval, _, _, _ in g: |
| 156 | if toknum == NUMBER and '.' in tokval: # replace NUMBER tokens |
| 157 | result.extend([ |
| 158 | (NAME, 'Decimal'), |
| 159 | (OP, '('), |
| 160 | (STRING, repr(tokval)), |
| 161 | (OP, ')') |
| 162 | ]) |
| 163 | else: |
| 164 | result.append((toknum, tokval)) |
| 165 | return untokenize(result).decode('utf-8') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 166 | |