Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b31c7f7

Browse files
committed
* test_select.py: (some) tests for built-in select module
* test_grammar.py, testall.out: added test for funny things in string literals * token.py, symbol.py: definitions used with built-in parser module. * tokenize.py: added double-quote recognition
1 parent 52f2c05 commit b31c7f7

6 files changed

Lines changed: 155 additions & 1 deletion

File tree

Lib/symbol.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Non-terminal symbols of Python grammar (from "graminit.h")
2+
3+
single_input = 256
4+
file_input = 257
5+
eval_input = 258
6+
lambda_input = 259
7+
funcdef = 260
8+
parameters = 261
9+
varargslist = 262
10+
fpdef = 263
11+
fplist = 264
12+
stmt = 265
13+
simple_stmt = 266
14+
small_stmt = 267
15+
expr_stmt = 268
16+
print_stmt = 269
17+
del_stmt = 270
18+
pass_stmt = 271
19+
flow_stmt = 272
20+
break_stmt = 273
21+
continue_stmt = 274
22+
return_stmt = 275
23+
raise_stmt = 276
24+
import_stmt = 277
25+
global_stmt = 278
26+
access_stmt = 279
27+
accesstype = 280
28+
exec_stmt = 281
29+
compound_stmt = 282
30+
if_stmt = 283
31+
while_stmt = 284
32+
for_stmt = 285
33+
try_stmt = 286
34+
except_clause = 287
35+
suite = 288
36+
test = 289
37+
and_test = 290
38+
not_test = 291
39+
comparison = 292
40+
comp_op = 293
41+
expr = 294
42+
xor_expr = 295
43+
and_expr = 296
44+
shift_expr = 297
45+
arith_expr = 298
46+
term = 299
47+
factor = 300
48+
atom = 301
49+
trailer = 302
50+
subscript = 303
51+
exprlist = 304
52+
testlist = 305
53+
dictmaker = 306
54+
classdef = 307
55+
56+
names = dir()
57+
sym_name = {}
58+
for name in names:
59+
number = eval(name)
60+
sym_name[number] = name

Lib/test/test_grammar.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@
6262
x = .3e14
6363
x = 3.1e4
6464

65+
print '1.1.3 String literals'
66+
67+
def assert(s):
68+
if not s: raise TestFailed, 'see traceback'
69+
70+
x = ''; y = ""; assert(len(x) == 0 and x == y)
71+
x = '\''; y = "'"; assert(len(x) == 1 and x == y and ord(x) == 39)
72+
x = '"'; y = "\""; assert(len(x) == 1 and x == y and ord(x) == 34)
73+
x = "doesn't \"shrink\" does it"
74+
y = 'doesn\'t "shrink" does it'
75+
assert(len(x) == 24 and x == y)
76+
x = "doesn \"shrink\" doesn't it"
77+
y = 'doesn "shrink" doesn\'t it'
78+
assert(len(x) == 25 and x == y)
79+
80+
6581
print '1.2 Grammar'
6682

6783
print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE

Lib/test/test_select.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Testing select module
2+
3+
from test_support import *
4+
5+
def test():
6+
import select
7+
import os
8+
cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do date; sleep 3; done'
9+
p = os.popen(cmd, 'r')
10+
for tout in (0, 1, 2, 4, 8, 16) + (None,)*10:
11+
print 'timeout =', tout
12+
rfd, wfd, xfd = select.select([p], [], [], tout)
13+
print rfd, wfd, xfd
14+
if (rfd, wfd, xfd) == ([], [], []):
15+
continue
16+
if (rfd, wfd, xfd) == ([p], [], []):
17+
line = p.readline()
18+
print `line`
19+
if not line:
20+
print 'EOF'
21+
break
22+
continue
23+
print 'Heh?'
24+
25+
test()

Lib/test/testall.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ test_grammar
66
1.1.2.1 Plain integers
77
1.1.2.2 Long integers
88
1.1.2.3 Floating point
9+
1.1.3 String literals
910
1.2 Grammar
1011
single_input
1112
file_input

Lib/token.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Tokens (from "token.h")
2+
3+
ENDMARKER = 0
4+
NAME = 1
5+
NUMBER = 2
6+
STRING = 3
7+
NEWLINE = 4
8+
INDENT = 5
9+
DEDENT = 6
10+
LPAR = 7
11+
RPAR = 8
12+
LSQB = 9
13+
RSQB = 10
14+
COLON = 11
15+
COMMA = 12
16+
SEMI = 13
17+
PLUS = 14
18+
MINUS = 15
19+
STAR = 16
20+
SLASH = 17
21+
VBAR = 18
22+
AMPER = 19
23+
LESS = 20
24+
GREATER = 21
25+
EQUAL = 22
26+
DOT = 23
27+
PERCENT = 24
28+
BACKQUOTE = 25
29+
LBRACE = 26
30+
RBRACE = 27
31+
EQEQUAL = 28
32+
NOTEQUAL = 29
33+
LESSEQUAL = 30
34+
GREATEREQUAL = 31
35+
TILDE = 32
36+
CIRCUMFLEX = 33
37+
LEFTSHIFT = 34
38+
RIGHTSHIFT = 35
39+
OP = 36
40+
ERRORTOKEN = 37
41+
42+
names = dir()
43+
tok_name = {}
44+
for name in names:
45+
number = eval(name)
46+
tok_name[number] = name
47+
48+
N_TOKENS = 38 # Number of tokens including ERRORTOKEN
49+
50+
NT_OFFSET = 256 # Start of non-terminal symbols

Lib/tokenize.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
Floatnumber = Pointfloat + '\|' + Expfloat
2525
Number = Floatnumber + '\|' + Intnumber
2626

27-
String = '\'\(\\\\.\|[^\\\n\']\)*\''
27+
String = '\'\(\\\\.\|[^\\\n\']\)*\'' + '\|' + '"\(\\\\.\|[^\\\n"]\)*"'
28+
# Note: this module *recognizes* double quotes, but for backward
29+
# compatibility, it doesn't *use* them!
2830

2931
Operator = '~\|\+\|-\|\*\|/\|%\|\^\|&\||\|<<\|>>\|==\|<=\|<>\|!=\|>=\|=\|<\|>'
3032
Bracket = '[][(){}]'

0 commit comments

Comments
 (0)