annotate Lib/test/test_tokenize.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 31ba3fa1585b
children 45b62725377a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
1 doctests = """
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
2 Tests for the tokenize module.
39638
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
3
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
4 >>> import glob, random, sys
39638
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
5
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
6 The tests can be really simple. Given a small fragment of source
45801
b5cb7a678f38 Added PEP 3127 support to tokenize (with tests); added PEP 3127 to NEWS.
Eric Smith <eric@trueblade.com>
parents: 45750
diff changeset
7 code, print out a table with tokens. The ENDMARK is omitted for
39638
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
8 brevity.
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
9
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
10 >>> dump_tokens("1 + 1")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
11 NUMBER '1' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
12 OP '+' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
13 NUMBER '1' (1, 4) (1, 5)
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
14
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
15 >>> dump_tokens("if False:\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
16 ... " # NL\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
17 ... " True = False # NEWLINE\\n")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
18 NAME 'if' (1, 0) (1, 2)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
19 NAME 'False' (1, 3) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
20 OP ':' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
21 NEWLINE '\\n' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
22 COMMENT '# NL' (2, 4) (2, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
23 NL '\\n' (2, 8) (2, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
24 INDENT ' ' (3, 0) (3, 4)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
25 NAME 'True' (3, 4) (3, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
26 OP '=' (3, 9) (3, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
27 NAME 'False' (3, 11) (3, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
28 COMMENT '# NEWLINE' (3, 17) (3, 26)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
29 NEWLINE '\\n' (3, 26) (3, 27)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
30 DEDENT '' (4, 0) (4, 0)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
31
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
32 >>> indent_error_file = \"""
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
33 ... def k(x):
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
34 ... x += 2
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
35 ... x += 5
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
36 ... \"""
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
37
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
38 >>> for tok in generate_tokens(StringIO(indent_error_file).readline): pass
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
39 Traceback (most recent call last):
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
40 ...
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
41 IndentationError: unindent does not match any outer indentation level
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
42
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
43 Test roundtrip for `untokenize`. `f` is an open file or a string. The source
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
44 code in f is tokenized, converted back to source code via tokenize.untokenize(),
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
45 and tokenized again from the latter. The test fails if the second tokenization
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
46 doesn't match the first.
39683
904958216813 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39640
diff changeset
47
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
48 >>> def roundtrip(f):
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
49 ... if isinstance(f, str): f = StringIO(f)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
50 ... token_list = list(generate_tokens(f.readline))
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
51 ... f.close()
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
52 ... tokens1 = [tok[:2] for tok in token_list]
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
53 ... new_text = untokenize(tokens1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
54 ... readline = iter(new_text.splitlines(1)).next
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
55 ... tokens2 = [tok[:2] for tok in generate_tokens(readline)]
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
56 ... return tokens1 == tokens2
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
57 ...
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
58
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
59 There are some standard formattig practises that are easy to get right.
39638
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
60
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
61 >>> roundtrip("if x == 1:\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
62 ... " print x\\n")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
63 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
64
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
65 >>> roundtrip("# This is a comment\\n# This also")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
66 True
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
67
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
68 Some people use different formatting conventions, which makes
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
69 untokenize a little trickier. Note that this test involves trailing
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
70 whitespace after the colon. Note that we use hex escapes to make the
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
71 two trailing blanks apperant in the expected output.
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
72
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
73 >>> roundtrip("if x == 1 : \\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
74 ... " print x\\n")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
75 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
76
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
77 >>> f = test_support.findfile("tokenize_tests" + os.extsep + "txt")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
78 >>> roundtrip(open(f))
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
79 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
80
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
81 >>> roundtrip("if x == 1:\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
82 ... " # A comment by itself.\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
83 ... " print x # Comment here, too.\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
84 ... " # Another comment.\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
85 ... "after_if = True\\n")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
86 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
87
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
88 >>> roundtrip("if (x # The comments need to go in the right place\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
89 ... " == 1):\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
90 ... " print 'x==1'\\n")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
91 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
92
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
93 >>> roundtrip("class Test: # A comment here\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
94 ... " # A comment with weird indent\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
95 ... " after_com = 5\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
96 ... " def x(m): return m*5 # a one liner\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
97 ... " def y(m): # A whitespace after the colon\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
98 ... " return y*4 # 3-space indent\\n")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
99 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
100
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
101 Some error-handling code
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
102
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
103 >>> roundtrip("try: import somemodule\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
104 ... "except ImportError: # comment\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
105 ... " print 'Can not import' # comment2\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
106 ... "else: print 'Loaded'\\n")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
107 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
108
45801
b5cb7a678f38 Added PEP 3127 support to tokenize (with tests); added PEP 3127 to NEWS.
Eric Smith <eric@trueblade.com>
parents: 45750
diff changeset
109 Balancing continuation
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
110
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
111 >>> roundtrip("a = (3,4, \\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
112 ... "5,6)\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
113 ... "y = [3, 4,\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
114 ... "5]\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
115 ... "z = {'a': 5,\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
116 ... "'b':15, 'c':True}\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
117 ... "x = len(y) + 5 - a[\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
118 ... "3] - a[2]\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
119 ... "+ len(z) - z[\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
120 ... "'b']\\n")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
121 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
122
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
123 Ordinary integers and binary operators
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
124
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
125 >>> dump_tokens("0xff <= 255")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
126 NUMBER '0xff' (1, 0) (1, 4)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
127 OP '<=' (1, 5) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
128 NUMBER '255' (1, 8) (1, 11)
45801
b5cb7a678f38 Added PEP 3127 support to tokenize (with tests); added PEP 3127 to NEWS.
Eric Smith <eric@trueblade.com>
parents: 45750
diff changeset
129 >>> dump_tokens("0b10 <= 255")
b5cb7a678f38 Added PEP 3127 support to tokenize (with tests); added PEP 3127 to NEWS.
Eric Smith <eric@trueblade.com>
parents: 45750
diff changeset
130 NUMBER '0b10' (1, 0) (1, 4)
b5cb7a678f38 Added PEP 3127 support to tokenize (with tests); added PEP 3127 to NEWS.
Eric Smith <eric@trueblade.com>
parents: 45750
diff changeset
131 OP '<=' (1, 5) (1, 7)
b5cb7a678f38 Added PEP 3127 support to tokenize (with tests); added PEP 3127 to NEWS.
Eric Smith <eric@trueblade.com>
parents: 45750
diff changeset
132 NUMBER '255' (1, 8) (1, 11)
b5cb7a678f38 Added PEP 3127 support to tokenize (with tests); added PEP 3127 to NEWS.
Eric Smith <eric@trueblade.com>
parents: 45750
diff changeset
133 >>> dump_tokens("0o123 <= 0123")
b5cb7a678f38 Added PEP 3127 support to tokenize (with tests); added PEP 3127 to NEWS.
Eric Smith <eric@trueblade.com>
parents: 45750
diff changeset
134 NUMBER '0o123' (1, 0) (1, 5)
b5cb7a678f38 Added PEP 3127 support to tokenize (with tests); added PEP 3127 to NEWS.
Eric Smith <eric@trueblade.com>
parents: 45750
diff changeset
135 OP '<=' (1, 6) (1, 8)
b5cb7a678f38 Added PEP 3127 support to tokenize (with tests); added PEP 3127 to NEWS.
Eric Smith <eric@trueblade.com>
parents: 45750
diff changeset
136 NUMBER '0123' (1, 9) (1, 13)
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
137 >>> dump_tokens("01234567 > ~0x15")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
138 NUMBER '01234567' (1, 0) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
139 OP '>' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
140 OP '~' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
141 NUMBER '0x15' (1, 12) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
142 >>> dump_tokens("2134568 != 01231515")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
143 NUMBER '2134568' (1, 0) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
144 OP '!=' (1, 8) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
145 NUMBER '01231515' (1, 11) (1, 19)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
146 >>> dump_tokens("(-124561-1) & 0200000000")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
147 OP '(' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
148 OP '-' (1, 1) (1, 2)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
149 NUMBER '124561' (1, 2) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
150 OP '-' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
151 NUMBER '1' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
152 OP ')' (1, 10) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
153 OP '&' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
154 NUMBER '0200000000' (1, 14) (1, 24)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
155 >>> dump_tokens("0xdeadbeef != -1")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
156 NUMBER '0xdeadbeef' (1, 0) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
157 OP '!=' (1, 11) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
158 OP '-' (1, 14) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
159 NUMBER '1' (1, 15) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
160 >>> dump_tokens("0xdeadc0de & 012345")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
161 NUMBER '0xdeadc0de' (1, 0) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
162 OP '&' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
163 NUMBER '012345' (1, 13) (1, 19)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
164 >>> dump_tokens("0xFF & 0x15 | 1234")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
165 NUMBER '0xFF' (1, 0) (1, 4)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
166 OP '&' (1, 5) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
167 NUMBER '0x15' (1, 7) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
168 OP '|' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
169 NUMBER '1234' (1, 14) (1, 18)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
170
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
171 Long integers
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
172
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
173 >>> dump_tokens("x = 0L")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
174 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
175 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
176 NUMBER '0L' (1, 4) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
177 >>> dump_tokens("x = 0xfffffffffff")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
178 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
179 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
180 NUMBER '0xffffffffff (1, 4) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
181 >>> dump_tokens("x = 123141242151251616110l")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
182 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
183 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
184 NUMBER '123141242151 (1, 4) (1, 26)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
185 >>> dump_tokens("x = -15921590215012591L")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
186 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
187 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
188 OP '-' (1, 4) (1, 5)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
189 NUMBER '159215902150 (1, 5) (1, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
190
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
191 Floating point numbers
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
192
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
193 >>> dump_tokens("x = 3.14159")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
194 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
195 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
196 NUMBER '3.14159' (1, 4) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
197 >>> dump_tokens("x = 314159.")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
198 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
199 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
200 NUMBER '314159.' (1, 4) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
201 >>> dump_tokens("x = .314159")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
202 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
203 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
204 NUMBER '.314159' (1, 4) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
205 >>> dump_tokens("x = 3e14159")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
206 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
207 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
208 NUMBER '3e14159' (1, 4) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
209 >>> dump_tokens("x = 3E123")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
210 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
211 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
212 NUMBER '3E123' (1, 4) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
213 >>> dump_tokens("x+y = 3e-1230")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
214 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
215 OP '+' (1, 1) (1, 2)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
216 NAME 'y' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
217 OP '=' (1, 4) (1, 5)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
218 NUMBER '3e-1230' (1, 6) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
219 >>> dump_tokens("x = 3.14e159")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
220 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
221 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
222 NUMBER '3.14e159' (1, 4) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
223
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
224 String literals
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
225
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
226 >>> dump_tokens("x = ''; y = \\\"\\\"")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
227 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
228 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
229 STRING "''" (1, 4) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
230 OP ';' (1, 6) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
231 NAME 'y' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
232 OP '=' (1, 10) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
233 STRING '""' (1, 12) (1, 14)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
234 >>> dump_tokens("x = '\\\"'; y = \\\"'\\\"")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
235 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
236 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
237 STRING '\\'"\\'' (1, 4) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
238 OP ';' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
239 NAME 'y' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
240 OP '=' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
241 STRING '"\\'"' (1, 13) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
242 >>> dump_tokens("x = \\\"doesn't \\\"shrink\\\", does it\\\"")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
243 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
244 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
245 STRING '"doesn\\'t "' (1, 4) (1, 14)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
246 NAME 'shrink' (1, 14) (1, 20)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
247 STRING '", does it"' (1, 20) (1, 31)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
248 >>> dump_tokens("x = u'abc' + U'ABC'")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
249 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
250 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
251 STRING "u'abc'" (1, 4) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
252 OP '+' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
253 STRING "U'ABC'" (1, 13) (1, 19)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
254 >>> dump_tokens('y = u"ABC" + U"ABC"')
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
255 NAME 'y' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
256 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
257 STRING 'u"ABC"' (1, 4) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
258 OP '+' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
259 STRING 'U"ABC"' (1, 13) (1, 19)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
260 >>> dump_tokens("x = ur'abc' + Ur'ABC' + uR'ABC' + UR'ABC'")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
261 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
262 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
263 STRING "ur'abc'" (1, 4) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
264 OP '+' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
265 STRING "Ur'ABC'" (1, 14) (1, 21)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
266 OP '+' (1, 22) (1, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
267 STRING "uR'ABC'" (1, 24) (1, 31)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
268 OP '+' (1, 32) (1, 33)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
269 STRING "UR'ABC'" (1, 34) (1, 41)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
270 >>> dump_tokens('y = ur"abc" + Ur"ABC" + uR"ABC" + UR"ABC"')
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
271 NAME 'y' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
272 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
273 STRING 'ur"abc"' (1, 4) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
274 OP '+' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
275 STRING 'Ur"ABC"' (1, 14) (1, 21)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
276 OP '+' (1, 22) (1, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
277 STRING 'uR"ABC"' (1, 24) (1, 31)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
278 OP '+' (1, 32) (1, 33)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
279 STRING 'UR"ABC"' (1, 34) (1, 41)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
280
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
281 Operators
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
282
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
283 >>> dump_tokens("def d22(a, b, c=2, d=2, *k): pass")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
284 NAME 'def' (1, 0) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
285 NAME 'd22' (1, 4) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
286 OP '(' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
287 NAME 'a' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
288 OP ',' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
289 NAME 'b' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
290 OP ',' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
291 NAME 'c' (1, 14) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
292 OP '=' (1, 15) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
293 NUMBER '2' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
294 OP ',' (1, 17) (1, 18)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
295 NAME 'd' (1, 19) (1, 20)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
296 OP '=' (1, 20) (1, 21)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
297 NUMBER '2' (1, 21) (1, 22)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
298 OP ',' (1, 22) (1, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
299 OP '*' (1, 24) (1, 25)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
300 NAME 'k' (1, 25) (1, 26)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
301 OP ')' (1, 26) (1, 27)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
302 OP ':' (1, 27) (1, 28)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
303 NAME 'pass' (1, 29) (1, 33)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
304 >>> dump_tokens("def d01v_(a=1, *k, **w): pass")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
305 NAME 'def' (1, 0) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
306 NAME 'd01v_' (1, 4) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
307 OP '(' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
308 NAME 'a' (1, 10) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
309 OP '=' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
310 NUMBER '1' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
311 OP ',' (1, 13) (1, 14)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
312 OP '*' (1, 15) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
313 NAME 'k' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
314 OP ',' (1, 17) (1, 18)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
315 OP '**' (1, 19) (1, 21)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
316 NAME 'w' (1, 21) (1, 22)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
317 OP ')' (1, 22) (1, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
318 OP ':' (1, 23) (1, 24)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
319 NAME 'pass' (1, 25) (1, 29)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
320
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
321 Comparison
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
322
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
323 >>> dump_tokens("if 1 < 1 > 1 == 1 >= 5 <= 0x15 <= 0x12 != " +
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
324 ... "1 and 5 in 1 not in 1 is 1 or 5 is not 1: pass")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
325 NAME 'if' (1, 0) (1, 2)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
326 NUMBER '1' (1, 3) (1, 4)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
327 OP '<' (1, 5) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
328 NUMBER '1' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
329 OP '>' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
330 NUMBER '1' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
331 OP '==' (1, 13) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
332 NUMBER '1' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
333 OP '>=' (1, 18) (1, 20)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
334 NUMBER '5' (1, 21) (1, 22)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
335 OP '<=' (1, 23) (1, 25)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
336 NUMBER '0x15' (1, 26) (1, 30)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
337 OP '<=' (1, 31) (1, 33)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
338 NUMBER '0x12' (1, 34) (1, 38)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
339 OP '!=' (1, 39) (1, 41)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
340 NUMBER '1' (1, 42) (1, 43)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
341 NAME 'and' (1, 44) (1, 47)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
342 NUMBER '5' (1, 48) (1, 49)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
343 NAME 'in' (1, 50) (1, 52)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
344 NUMBER '1' (1, 53) (1, 54)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
345 NAME 'not' (1, 55) (1, 58)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
346 NAME 'in' (1, 59) (1, 61)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
347 NUMBER '1' (1, 62) (1, 63)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
348 NAME 'is' (1, 64) (1, 66)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
349 NUMBER '1' (1, 67) (1, 68)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
350 NAME 'or' (1, 69) (1, 71)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
351 NUMBER '5' (1, 72) (1, 73)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
352 NAME 'is' (1, 74) (1, 76)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
353 NAME 'not' (1, 77) (1, 80)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
354 NUMBER '1' (1, 81) (1, 82)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
355 OP ':' (1, 82) (1, 83)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
356 NAME 'pass' (1, 84) (1, 88)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
357
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
358 Shift
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
359
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
360 >>> dump_tokens("x = 1 << 1 >> 5")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
361 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
362 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
363 NUMBER '1' (1, 4) (1, 5)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
364 OP '<<' (1, 6) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
365 NUMBER '1' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
366 OP '>>' (1, 11) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
367 NUMBER '5' (1, 14) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
368
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
369 Additive
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
370
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
371 >>> dump_tokens("x = 1 - y + 15 - 01 + 0x124 + z + a[5]")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
372 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
373 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
374 NUMBER '1' (1, 4) (1, 5)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
375 OP '-' (1, 6) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
376 NAME 'y' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
377 OP '+' (1, 10) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
378 NUMBER '15' (1, 12) (1, 14)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
379 OP '-' (1, 15) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
380 NUMBER '01' (1, 17) (1, 19)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
381 OP '+' (1, 20) (1, 21)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
382 NUMBER '0x124' (1, 22) (1, 27)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
383 OP '+' (1, 28) (1, 29)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
384 NAME 'z' (1, 30) (1, 31)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
385 OP '+' (1, 32) (1, 33)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
386 NAME 'a' (1, 34) (1, 35)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
387 OP '[' (1, 35) (1, 36)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
388 NUMBER '5' (1, 36) (1, 37)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
389 OP ']' (1, 37) (1, 38)
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
390
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
391 Multiplicative
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
392
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
393 >>> dump_tokens("x = 1//1*1/5*12%0x12")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
394 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
395 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
396 NUMBER '1' (1, 4) (1, 5)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
397 OP '//' (1, 5) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
398 NUMBER '1' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
399 OP '*' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
400 NUMBER '1' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
401 OP '/' (1, 10) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
402 NUMBER '5' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
403 OP '*' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
404 NUMBER '12' (1, 13) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
405 OP '%' (1, 15) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
406 NUMBER '0x12' (1, 16) (1, 20)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
407
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
408 Unary
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
409
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
410 >>> dump_tokens("~1 ^ 1 & 1 |1 ^ -1")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
411 OP '~' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
412 NUMBER '1' (1, 1) (1, 2)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
413 OP '^' (1, 3) (1, 4)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
414 NUMBER '1' (1, 5) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
415 OP '&' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
416 NUMBER '1' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
417 OP '|' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
418 NUMBER '1' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
419 OP '^' (1, 14) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
420 OP '-' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
421 NUMBER '1' (1, 17) (1, 18)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
422 >>> dump_tokens("-1*1/1+1*1//1 - ---1**1")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
423 OP '-' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
424 NUMBER '1' (1, 1) (1, 2)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
425 OP '*' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
426 NUMBER '1' (1, 3) (1, 4)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
427 OP '/' (1, 4) (1, 5)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
428 NUMBER '1' (1, 5) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
429 OP '+' (1, 6) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
430 NUMBER '1' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
431 OP '*' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
432 NUMBER '1' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
433 OP '//' (1, 10) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
434 NUMBER '1' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
435 OP '-' (1, 14) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
436 OP '-' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
437 OP '-' (1, 17) (1, 18)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
438 OP '-' (1, 18) (1, 19)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
439 NUMBER '1' (1, 19) (1, 20)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
440 OP '**' (1, 20) (1, 22)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
441 NUMBER '1' (1, 22) (1, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
442
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
443 Selector
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
444
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
445 >>> dump_tokens("import sys, time\\nx = sys.modules['time'].time()")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
446 NAME 'import' (1, 0) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
447 NAME 'sys' (1, 7) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
448 OP ',' (1, 10) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
449 NAME 'time' (1, 12) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
450 NEWLINE '\\n' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
451 NAME 'x' (2, 0) (2, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
452 OP '=' (2, 2) (2, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
453 NAME 'sys' (2, 4) (2, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
454 OP '.' (2, 7) (2, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
455 NAME 'modules' (2, 8) (2, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
456 OP '[' (2, 15) (2, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
457 STRING "'time'" (2, 16) (2, 22)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
458 OP ']' (2, 22) (2, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
459 OP '.' (2, 23) (2, 24)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
460 NAME 'time' (2, 24) (2, 28)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
461 OP '(' (2, 28) (2, 29)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
462 OP ')' (2, 29) (2, 30)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
463
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
464 Methods
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
465
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
466 >>> dump_tokens("@staticmethod\\ndef foo(x,y): pass")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
467 OP '@' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
468 NAME 'staticmethod (1, 1) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
469 NEWLINE '\\n' (1, 13) (1, 14)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
470 NAME 'def' (2, 0) (2, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
471 NAME 'foo' (2, 4) (2, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
472 OP '(' (2, 7) (2, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
473 NAME 'x' (2, 8) (2, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
474 OP ',' (2, 9) (2, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
475 NAME 'y' (2, 10) (2, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
476 OP ')' (2, 11) (2, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
477 OP ':' (2, 12) (2, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
478 NAME 'pass' (2, 14) (2, 18)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
479
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
480 Backslash means line continuation, except for comments
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
481
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
482 >>> roundtrip("x=1+\\\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
483 ... "1\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
484 ... "# This is a comment\\\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
485 ... "# This also\\n")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
486 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
487 >>> roundtrip("# Comment \\\\nx = 0")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
488 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
489
46142
31ba3fa1585b Issue2495: tokenize.untokenize did not insert space between two consecutive string literals:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46139
diff changeset
490 Two string literals on the same line
31ba3fa1585b Issue2495: tokenize.untokenize did not insert space between two consecutive string literals:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46139
diff changeset
491
31ba3fa1585b Issue2495: tokenize.untokenize did not insert space between two consecutive string literals:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46139
diff changeset
492 >>> roundtrip("'' ''")
31ba3fa1585b Issue2495: tokenize.untokenize did not insert space between two consecutive string literals:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46139
diff changeset
493 True
31ba3fa1585b Issue2495: tokenize.untokenize did not insert space between two consecutive string literals:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46139
diff changeset
494
31ba3fa1585b Issue2495: tokenize.untokenize did not insert space between two consecutive string literals:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46139
diff changeset
495 Test roundtrip on random python modules.
31ba3fa1585b Issue2495: tokenize.untokenize did not insert space between two consecutive string literals:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46139
diff changeset
496 pass the '-ucompiler' option to process the full directory.
31ba3fa1585b Issue2495: tokenize.untokenize did not insert space between two consecutive string literals:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46139
diff changeset
497
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
498 >>>
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
499 >>> tempdir = os.path.dirname(f) or os.curdir
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
500 >>> testfiles = glob.glob(os.path.join(tempdir, "test*.py"))
46139
f1aec7dec05c Fixed tokenize tests
Christian Heimes <christian@cheimes.de>
parents: 45801
diff changeset
501
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
502 >>> if not test_support.is_resource_enabled("compiler"):
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
503 ... testfiles = random.sample(testfiles, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
504 ...
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
505 >>> for testfile in testfiles:
46139
f1aec7dec05c Fixed tokenize tests
Christian Heimes <christian@cheimes.de>
parents: 45801
diff changeset
506 ... if not roundtrip(open(testfile)):
f1aec7dec05c Fixed tokenize tests
Christian Heimes <christian@cheimes.de>
parents: 45801
diff changeset
507 ... print "Roundtrip failed for file %s" % testfile
f1aec7dec05c Fixed tokenize tests
Christian Heimes <christian@cheimes.de>
parents: 45801
diff changeset
508 ... break
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
509 ... else: True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
510 True
39638
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
511 """
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
512
6533
ba65372408dd Tests for tokenize.py (Ka-Ping Yee)
Guido van Rossum <guido@python.org>
parents:
diff changeset
513
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
514 from test import test_support
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
515 from tokenize import (tokenize, untokenize, generate_tokens, NUMBER, NAME, OP,
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
516 STRING, ENDMARKER, tok_name)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
517 from StringIO import StringIO
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
518 import os
35119
04868959f1fe SF bug #1224621: tokenize module does not detect inconsistent dedents
Raymond Hettinger <python@rcn.com>
parents: 35058
diff changeset
519
39638
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
520 def dump_tokens(s):
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
521 """Print out the tokens in s in a table format.
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
522
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
523 The ENDMARKER is omitted.
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
524 """
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
525 f = StringIO(s)
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
526 for type, token, start, end, line in generate_tokens(f.readline):
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
527 if type == ENDMARKER:
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
528 break
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
529 type = tok_name[type]
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
530 print("%(type)-10.10s %(token)-13.13r %(start)s %(end)s" % locals())
39638
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
531
37316
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
532 # This is an example from the docs, set up as a doctest.
35058
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
533 def decistmt(s):
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
534 """Substitute Decimals for floats in a string of statements.
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
535
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
536 >>> from decimal import Decimal
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
537 >>> s = 'print +21.3e-5*-.1234/81.7'
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
538 >>> decistmt(s)
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
539 "print +Decimal ('21.3e-5')*-Decimal ('.1234')/Decimal ('81.7')"
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
540
37316
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
541 The format of the exponent is inherited from the platform C library.
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
542 Known cases are "e-007" (Windows) and "e-07" (not Windows). Since
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
543 we're only showing 12 digits, and the 13th isn't close to 5, the
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
544 rest of the output should be platform-independent.
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
545
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
546 >>> exec(s) #doctest: +ELLIPSIS
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
547 -3.21716034272e-0...7
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
548
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
549 Output from calculations with Decimal should be identical across all
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
550 platforms.
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
551
35058
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
552 >>> exec(decistmt(s))
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
553 -3.217160342717258261933904529E-7
37316
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
554 """
35058
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
555
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
556 result = []
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
557 g = generate_tokens(StringIO(s).readline) # tokenize the string
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
558 for toknum, tokval, _, _, _ in g:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
559 if toknum == NUMBER and '.' in tokval: # replace NUMBER tokens
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
560 result.extend([
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
561 (NAME, 'Decimal'),
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
562 (OP, '('),
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
563 (STRING, repr(tokval)),
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
564 (OP, ')')
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
565 ])
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
566 else:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
567 result.append((toknum, tokval))
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
568 return untokenize(result)
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
569
39725
1ec7ef0f3b8d Hmm, this test has failed at least twice recently on the OpenBSD and
Neal Norwitz <nnorwitz@gmail.com>
parents: 39685
diff changeset
570
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
571 __test__ = {"doctests" : doctests, 'decistmt': decistmt}
37316
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
572
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
573
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
574 def test_main():
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
575 from test import test_tokenize
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
576 test_support.run_doctest(test_tokenize, True)
37316
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
577
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
578 if __name__ == "__main__":
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
579 test_main()