annotate Lib/test/test_tokenize.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 35d3a8ed7997
children eb7ea51e658e
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
62619
2a8c68ba000b Spelling.
Mark Dickinson <dickinsm@gmail.com>
parents: 58817
diff changeset
59 There are some standard formatting practices 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
77474
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
281 >>> dump_tokens("b'abc' + B'abc'")
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
282 STRING "b'abc'" (1, 0) (1, 6)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
283 OP '+' (1, 7) (1, 8)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
284 STRING "B'abc'" (1, 9) (1, 15)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
285 >>> dump_tokens('b"abc" + B"abc"')
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
286 STRING 'b"abc"' (1, 0) (1, 6)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
287 OP '+' (1, 7) (1, 8)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
288 STRING 'B"abc"' (1, 9) (1, 15)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
289 >>> dump_tokens("br'abc' + bR'abc' + Br'abc' + BR'abc'")
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
290 STRING "br'abc'" (1, 0) (1, 7)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
291 OP '+' (1, 8) (1, 9)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
292 STRING "bR'abc'" (1, 10) (1, 17)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
293 OP '+' (1, 18) (1, 19)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
294 STRING "Br'abc'" (1, 20) (1, 27)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
295 OP '+' (1, 28) (1, 29)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
296 STRING "BR'abc'" (1, 30) (1, 37)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
297 >>> dump_tokens('br"abc" + bR"abc" + Br"abc" + BR"abc"')
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
298 STRING 'br"abc"' (1, 0) (1, 7)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
299 OP '+' (1, 8) (1, 9)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
300 STRING 'bR"abc"' (1, 10) (1, 17)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
301 OP '+' (1, 18) (1, 19)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
302 STRING 'Br"abc"' (1, 20) (1, 27)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
303 OP '+' (1, 28) (1, 29)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
304 STRING 'BR"abc"' (1, 30) (1, 37)
35d3a8ed7997 Issue #15054: Fix incorrect tokenization of 'b' and 'br' string literals.
Meador Inge <meadori@gmail.com>
parents: 65318
diff changeset
305
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
306 Operators
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
307
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
308 >>> 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
309 NAME 'def' (1, 0) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
310 NAME 'd22' (1, 4) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
311 OP '(' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
312 NAME 'a' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
313 OP ',' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
314 NAME 'b' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
315 OP ',' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
316 NAME 'c' (1, 14) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
317 OP '=' (1, 15) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
318 NUMBER '2' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
319 OP ',' (1, 17) (1, 18)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
320 NAME 'd' (1, 19) (1, 20)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
321 OP '=' (1, 20) (1, 21)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
322 NUMBER '2' (1, 21) (1, 22)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
323 OP ',' (1, 22) (1, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
324 OP '*' (1, 24) (1, 25)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
325 NAME 'k' (1, 25) (1, 26)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
326 OP ')' (1, 26) (1, 27)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
327 OP ':' (1, 27) (1, 28)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
328 NAME 'pass' (1, 29) (1, 33)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
329 >>> dump_tokens("def d01v_(a=1, *k, **w): pass")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
330 NAME 'def' (1, 0) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
331 NAME 'd01v_' (1, 4) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
332 OP '(' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
333 NAME 'a' (1, 10) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
334 OP '=' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
335 NUMBER '1' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
336 OP ',' (1, 13) (1, 14)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
337 OP '*' (1, 15) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
338 NAME 'k' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
339 OP ',' (1, 17) (1, 18)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
340 OP '**' (1, 19) (1, 21)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
341 NAME 'w' (1, 21) (1, 22)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
342 OP ')' (1, 22) (1, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
343 OP ':' (1, 23) (1, 24)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
344 NAME 'pass' (1, 25) (1, 29)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
345
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
346 Comparison
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
347
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
348 >>> 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
349 ... "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
350 NAME 'if' (1, 0) (1, 2)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
351 NUMBER '1' (1, 3) (1, 4)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
352 OP '<' (1, 5) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
353 NUMBER '1' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
354 OP '>' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
355 NUMBER '1' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
356 OP '==' (1, 13) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
357 NUMBER '1' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
358 OP '>=' (1, 18) (1, 20)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
359 NUMBER '5' (1, 21) (1, 22)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
360 OP '<=' (1, 23) (1, 25)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
361 NUMBER '0x15' (1, 26) (1, 30)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
362 OP '<=' (1, 31) (1, 33)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
363 NUMBER '0x12' (1, 34) (1, 38)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
364 OP '!=' (1, 39) (1, 41)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
365 NUMBER '1' (1, 42) (1, 43)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
366 NAME 'and' (1, 44) (1, 47)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
367 NUMBER '5' (1, 48) (1, 49)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
368 NAME 'in' (1, 50) (1, 52)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
369 NUMBER '1' (1, 53) (1, 54)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
370 NAME 'not' (1, 55) (1, 58)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
371 NAME 'in' (1, 59) (1, 61)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
372 NUMBER '1' (1, 62) (1, 63)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
373 NAME 'is' (1, 64) (1, 66)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
374 NUMBER '1' (1, 67) (1, 68)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
375 NAME 'or' (1, 69) (1, 71)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
376 NUMBER '5' (1, 72) (1, 73)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
377 NAME 'is' (1, 74) (1, 76)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
378 NAME 'not' (1, 77) (1, 80)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
379 NUMBER '1' (1, 81) (1, 82)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
380 OP ':' (1, 82) (1, 83)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
381 NAME 'pass' (1, 84) (1, 88)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
382
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
383 Shift
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
384
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
385 >>> dump_tokens("x = 1 << 1 >> 5")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
386 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
387 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
388 NUMBER '1' (1, 4) (1, 5)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
389 OP '<<' (1, 6) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
390 NUMBER '1' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
391 OP '>>' (1, 11) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
392 NUMBER '5' (1, 14) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
393
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
394 Additive
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
395
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
396 >>> 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
397 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
398 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
399 NUMBER '1' (1, 4) (1, 5)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
400 OP '-' (1, 6) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
401 NAME 'y' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
402 OP '+' (1, 10) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
403 NUMBER '15' (1, 12) (1, 14)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
404 OP '-' (1, 15) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
405 NUMBER '01' (1, 17) (1, 19)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
406 OP '+' (1, 20) (1, 21)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
407 NUMBER '0x124' (1, 22) (1, 27)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
408 OP '+' (1, 28) (1, 29)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
409 NAME 'z' (1, 30) (1, 31)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
410 OP '+' (1, 32) (1, 33)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
411 NAME 'a' (1, 34) (1, 35)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
412 OP '[' (1, 35) (1, 36)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
413 NUMBER '5' (1, 36) (1, 37)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
414 OP ']' (1, 37) (1, 38)
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
415
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
416 Multiplicative
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
417
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
418 >>> dump_tokens("x = 1//1*1/5*12%0x12")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
419 NAME 'x' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
420 OP '=' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
421 NUMBER '1' (1, 4) (1, 5)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
422 OP '//' (1, 5) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
423 NUMBER '1' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
424 OP '*' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
425 NUMBER '1' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
426 OP '/' (1, 10) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
427 NUMBER '5' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
428 OP '*' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
429 NUMBER '12' (1, 13) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
430 OP '%' (1, 15) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
431 NUMBER '0x12' (1, 16) (1, 20)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
432
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
433 Unary
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
434
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
435 >>> dump_tokens("~1 ^ 1 & 1 |1 ^ -1")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
436 OP '~' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
437 NUMBER '1' (1, 1) (1, 2)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
438 OP '^' (1, 3) (1, 4)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
439 NUMBER '1' (1, 5) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
440 OP '&' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
441 NUMBER '1' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
442 OP '|' (1, 11) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
443 NUMBER '1' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
444 OP '^' (1, 14) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
445 OP '-' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
446 NUMBER '1' (1, 17) (1, 18)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
447 >>> 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
448 OP '-' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
449 NUMBER '1' (1, 1) (1, 2)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
450 OP '*' (1, 2) (1, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
451 NUMBER '1' (1, 3) (1, 4)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
452 OP '/' (1, 4) (1, 5)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
453 NUMBER '1' (1, 5) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
454 OP '+' (1, 6) (1, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
455 NUMBER '1' (1, 7) (1, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
456 OP '*' (1, 8) (1, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
457 NUMBER '1' (1, 9) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
458 OP '//' (1, 10) (1, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
459 NUMBER '1' (1, 12) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
460 OP '-' (1, 14) (1, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
461 OP '-' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
462 OP '-' (1, 17) (1, 18)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
463 OP '-' (1, 18) (1, 19)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
464 NUMBER '1' (1, 19) (1, 20)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
465 OP '**' (1, 20) (1, 22)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
466 NUMBER '1' (1, 22) (1, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
467
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
468 Selector
39640
5cce028874bc Bug fixes large and small for tokenize.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 39638
diff changeset
469
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
470 >>> 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
471 NAME 'import' (1, 0) (1, 6)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
472 NAME 'sys' (1, 7) (1, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
473 OP ',' (1, 10) (1, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
474 NAME 'time' (1, 12) (1, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
475 NEWLINE '\\n' (1, 16) (1, 17)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
476 NAME 'x' (2, 0) (2, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
477 OP '=' (2, 2) (2, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
478 NAME 'sys' (2, 4) (2, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
479 OP '.' (2, 7) (2, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
480 NAME 'modules' (2, 8) (2, 15)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
481 OP '[' (2, 15) (2, 16)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
482 STRING "'time'" (2, 16) (2, 22)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
483 OP ']' (2, 22) (2, 23)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
484 OP '.' (2, 23) (2, 24)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
485 NAME 'time' (2, 24) (2, 28)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
486 OP '(' (2, 28) (2, 29)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
487 OP ')' (2, 29) (2, 30)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
488
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
489 Methods
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
490
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
491 >>> dump_tokens("@staticmethod\\ndef foo(x,y): pass")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
492 OP '@' (1, 0) (1, 1)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
493 NAME 'staticmethod (1, 1) (1, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
494 NEWLINE '\\n' (1, 13) (1, 14)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
495 NAME 'def' (2, 0) (2, 3)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
496 NAME 'foo' (2, 4) (2, 7)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
497 OP '(' (2, 7) (2, 8)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
498 NAME 'x' (2, 8) (2, 9)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
499 OP ',' (2, 9) (2, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
500 NAME 'y' (2, 10) (2, 11)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
501 OP ')' (2, 11) (2, 12)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
502 OP ':' (2, 12) (2, 13)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
503 NAME 'pass' (2, 14) (2, 18)
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 Backslash means line continuation, except for comments
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
506
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
507 >>> roundtrip("x=1+\\\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
508 ... "1\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
509 ... "# This is a comment\\\\n"
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
510 ... "# This also\\n")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
511 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
512 >>> roundtrip("# Comment \\\\nx = 0")
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
513 True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
514
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
515 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
516
31ba3fa1585b Issue2495: tokenize.untokenize did not insert space between two consecutive string literals:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46139
diff changeset
517 >>> 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
518 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
519
31ba3fa1585b Issue2495: tokenize.untokenize did not insert space between two consecutive string literals:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46139
diff changeset
520 Test roundtrip on random python modules.
65318
07b6bb76ee43 Merged revisions 85482 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62619
diff changeset
521 pass the '-ucpu' option to process the full directory.
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
522
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
523 >>>
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
524 >>> tempdir = os.path.dirname(f) or os.curdir
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
525 >>> testfiles = glob.glob(os.path.join(tempdir, "test*.py"))
46139
f1aec7dec05c Fixed tokenize tests
Christian Heimes <christian@cheimes.de>
parents: 45801
diff changeset
526
65318
07b6bb76ee43 Merged revisions 85482 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62619
diff changeset
527 >>> if not test_support.is_resource_enabled("cpu"):
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
528 ... testfiles = random.sample(testfiles, 10)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
529 ...
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
530 >>> for testfile in testfiles:
46139
f1aec7dec05c Fixed tokenize tests
Christian Heimes <christian@cheimes.de>
parents: 45801
diff changeset
531 ... if not roundtrip(open(testfile)):
f1aec7dec05c Fixed tokenize tests
Christian Heimes <christian@cheimes.de>
parents: 45801
diff changeset
532 ... print "Roundtrip failed for file %s" % testfile
f1aec7dec05c Fixed tokenize tests
Christian Heimes <christian@cheimes.de>
parents: 45801
diff changeset
533 ... break
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
534 ... else: True
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
535 True
56535
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
536
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
537 Evil tabs
56536
a4b1922a3074 change test to what I intended
Benjamin Peterson <benjamin@python.org>
parents: 56535
diff changeset
538 >>> dump_tokens("def f():\\n\\tif x\\n \\tpass")
56535
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
539 NAME 'def' (1, 0) (1, 3)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
540 NAME 'f' (1, 4) (1, 5)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
541 OP '(' (1, 5) (1, 6)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
542 OP ')' (1, 6) (1, 7)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
543 OP ':' (1, 7) (1, 8)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
544 NEWLINE '\\n' (1, 8) (1, 9)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
545 INDENT '\\t' (2, 0) (2, 1)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
546 NAME 'if' (2, 1) (2, 3)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
547 NAME 'x' (2, 4) (2, 5)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
548 NEWLINE '\\n' (2, 5) (2, 6)
56536
a4b1922a3074 change test to what I intended
Benjamin Peterson <benjamin@python.org>
parents: 56535
diff changeset
549 INDENT ' \\t' (3, 0) (3, 9)
56535
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
550 NAME 'pass' (3, 9) (3, 13)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
551 DEDENT '' (4, 0) (4, 0)
45b62725377a use floor division and add a test that exercises the tabsize codepath
Benjamin Peterson <benjamin@python.org>
parents: 46142
diff changeset
552 DEDENT '' (4, 0) (4, 0)
39638
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
553 """
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
554
6533
ba65372408dd Tests for tokenize.py (Ka-Ping Yee)
Guido van Rossum <guido@python.org>
parents:
diff changeset
555
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
556 from test import test_support
58817
70e115c34f97 Remove unused imports in test modules.
Georg Brandl <georg@python.org>
parents: 56536
diff changeset
557 from tokenize import (untokenize, generate_tokens, NUMBER, NAME, OP,
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
558 STRING, ENDMARKER, tok_name)
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
559 from StringIO import StringIO
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
560 import os
35119
04868959f1fe SF bug #1224621: tokenize module does not detect inconsistent dedents
Raymond Hettinger <python@rcn.com>
parents: 35058
diff changeset
561
39638
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
562 def dump_tokens(s):
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
563 """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
564
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
565 The ENDMARKER is omitted.
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
566 """
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
567 f = StringIO(s)
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
568 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
569 if type == ENDMARKER:
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
570 break
449ba0efb6bd Baby steps towards better tests for tokenize
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 37316
diff changeset
571 type = tok_name[type]
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
572 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
573
37316
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
574 # 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
575 def decistmt(s):
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
576 """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
577
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
578 >>> from decimal import Decimal
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
579 >>> 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
580 >>> decistmt(s)
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
581 "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
582
37316
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
583 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
584 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
585 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
586 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
587
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
588 >>> exec(s) #doctest: +ELLIPSIS
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
589 -3.21716034272e-0...7
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
590
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
591 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
592 platforms.
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
593
35058
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
594 >>> exec(decistmt(s))
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
595 -3.217160342717258261933904529E-7
37316
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
596 """
35058
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
597
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
598 result = []
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
599 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
600 for toknum, tokval, _, _, _ in g:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
601 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
602 result.extend([
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
603 (NAME, 'Decimal'),
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
604 (OP, '('),
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
605 (STRING, repr(tokval)),
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
606 (OP, ')')
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
607 ])
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
608 else:
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
609 result.append((toknum, tokval))
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
610 return untokenize(result)
8076424d455d Add untokenize() function to allow full round-trip tokenization.
Raymond Hettinger <python@rcn.com>
parents: 29086
diff changeset
611
39725
1ec7ef0f3b8d Hmm, this test has failed at least twice recently on the OpenBSD and
Neal Norwitz <nnorwitz@gmail.com>
parents: 39685
diff changeset
612
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
613 __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
614
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
615
45750
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
616 def test_main():
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
617 from test import test_tokenize
543e8dde361f Move test_tokenize to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 39725
diff changeset
618 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
619
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
620 if __name__ == "__main__":
049606933328 Repaired a number of errors in this test:
Tim Peters <tim.peters@gmail.com>
parents: 35119
diff changeset
621 test_main()