test_grammar.py
来自「mallet是自然语言处理、机器学习领域的一个开源项目。」· Python 代码 · 共 733 行 · 第 1/2 页
PY
733 行
# Python test set -- part 1, grammar.# This just tests whether the parser accepts them all.from test_support import *import sysprint '1. Parser'print '1.1 Tokens'print '1.1.1 Backslashes'# Backslash means line continuation:x = 1 \+ 1if x != 2: raise TestFailed, 'backslash for line continuation'# Backslash does not means continuation in comments :\x = 0if x != 0: raise TestFailed, 'backslash ending comment'print '1.1.2 Numeric literals'print '1.1.2.1 Plain integers'if 0xff != 255: raise TestFailed, 'hex int'if 0377 != 255: raise TestFailed, 'octal int'if 2147483647 != 017777777777: raise TestFailed, 'large positive int'try: from sys import maxintexcept ImportError: maxint = 2147483647if maxint == 2147483647: if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int' # XXX -2147483648 if 037777777777 != -1: raise TestFailed, 'oct -1' if 0xffffffff != -1: raise TestFailed, 'hex -1' for s in '2147483648', '040000000000', '0x100000000': try: x = eval(s) except OverflowError: print "OverflowError on huge integer literal " + `s`elif eval('maxint == 9223372036854775807'): if eval('-9223372036854775807-1 != 01000000000000000000000'): raise TestFailed, 'max negative int' if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1' if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1' for s in '9223372036854775808', '02000000000000000000000', \ '0x10000000000000000': try: x = eval(s) except OverflowError: print "OverflowError on huge integer literal " + `s`else: print 'Weird maxint value', maxintprint '1.1.2.2 Long integers'x = 0Lx = 0lx = 0xffffffffffffffffLx = 0xfffffffffffffffflx = 077777777777777777Lx = 077777777777777777lx = 123456789012345678901234567890Lx = 123456789012345678901234567890lprint '1.1.2.3 Floating point'x = 3.14x = 314.x = 0.314# XXX x = 000.314x = .314x = 3e14x = 3E14x = 3e-14x = 3e+14x = 3.e14x = .3e14x = 3.1e4print '1.1.3 String literals'x = ''; y = ""; verify(len(x) == 0 and x == y)x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39)x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34)x = "doesn't \"shrink\" does it"y = 'doesn\'t "shrink" does it'verify(len(x) == 24 and x == y)x = "does \"shrink\" doesn't it"y = 'does "shrink" doesn\'t it'verify(len(x) == 24 and x == y)x = """The "quick"brown foxjumps overthe 'lazy' dog."""y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'verify(x == y)y = '''The "quick"brown foxjumps overthe 'lazy' dog.'''; verify(x == y)y = "\n\The \"quick\"\n\brown fox\n\jumps over\n\the 'lazy' dog.\n\"; verify(x == y)y = '\n\The \"quick\"\n\brown fox\n\jumps over\n\the \'lazy\' dog.\n\'; verify(x == y)print '1.2 Grammar'print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE# XXX can't test in a script -- this rule is only used when interactiveprint 'file_input' # (NEWLINE | stmt)* ENDMARKER# Being tested as this very moment this very moduleprint 'expr_input' # testlist NEWLINE# XXX Hard to test -- used only in calls to input()print 'eval_input' # testlist ENDMARKERx = eval('1, 0 or 1')print 'funcdef'### 'def' NAME parameters ':' suite### parameters: '(' [varargslist] ')'### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]### | ('**'|'*' '*') NAME)### | fpdef ['=' test] (',' fpdef ['=' test])* [',']### fpdef: NAME | '(' fplist ')'### fplist: fpdef (',' fpdef)* [',']### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)### argument: [test '='] test # Really [keyword '='] testdef f1(): passf1()f1(*())f1(*(), **{})def f2(one_argument): passdef f3(two, arguments): passdef f4(two, (compound, (argument, list))): passdef f5((compound, first), two): passverify(f2.func_code.co_varnames == ('one_argument',))verify(f3.func_code.co_varnames == ('two', 'arguments'))if sys.platform.startswith('java'): verify(f4.func_code.co_varnames == ('two', '(compound, (argument, list))', 'compound', 'argument', 'list',)) verify(f5.func_code.co_varnames == ('(compound, first)', 'two', 'compound', 'first'))else: verify(f4.func_code.co_varnames == ('two', '.2', 'compound', 'argument', 'list')) verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first'))def a1(one_arg,): passdef a2(two, args,): passdef v0(*rest): passdef v1(a, *rest): passdef v2(a, b, *rest): passdef v3(a, (b, c), *rest): return a, b, c, restif sys.platform.startswith('java'): verify(v3.func_code.co_varnames == ('a', '(b, c)', 'rest', 'b', 'c'))else: verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c'))verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))def d01(a=1): passd01()d01(1)d01(*(1,))d01(**{'a':2})def d11(a, b=1): passd11(1)d11(1, 2)d11(1, **{'b':2})def d21(a, b, c=1): passd21(1, 2)d21(1, 2, 3)d21(*(1, 2, 3))d21(1, *(2, 3))d21(1, 2, *(3,))d21(1, 2, **{'c':3})def d02(a=1, b=2): passd02()d02(1)d02(1, 2)d02(*(1, 2))d02(1, *(2,))d02(1, **{'b':2})d02(**{'a': 1, 'b': 2})def d12(a, b=1, c=2): passd12(1)d12(1, 2)d12(1, 2, 3)def d22(a, b, c=1, d=2): passd22(1, 2)d22(1, 2, 3)d22(1, 2, 3, 4)def d01v(a=1, *rest): passd01v()d01v(1)d01v(1, 2)d01v(*(1, 2, 3, 4))d01v(*(1,))d01v(**{'a':2})def d11v(a, b=1, *rest): passd11v(1)d11v(1, 2)d11v(1, 2, 3)def d21v(a, b, c=1, *rest): passd21v(1, 2)d21v(1, 2, 3)d21v(1, 2, 3, 4)d21v(*(1, 2, 3, 4))d21v(1, 2, **{'c': 3})def d02v(a=1, b=2, *rest): passd02v()d02v(1)d02v(1, 2)d02v(1, 2, 3)d02v(1, *(2, 3, 4))d02v(**{'a': 1, 'b': 2})def d12v(a, b=1, c=2, *rest): passd12v(1)d12v(1, 2)d12v(1, 2, 3)d12v(1, 2, 3, 4)d12v(*(1, 2, 3, 4))d12v(1, 2, *(3, 4, 5))d12v(1, *(2,), **{'c': 3})def d22v(a, b, c=1, d=2, *rest): passd22v(1, 2)d22v(1, 2, 3)d22v(1, 2, 3, 4)d22v(1, 2, 3, 4, 5)d22v(*(1, 2, 3, 4))d22v(1, 2, *(3, 4, 5))d22v(1, *(2, 3), **{'d': 4})### lambdef: 'lambda' [varargslist] ':' testprint 'lambdef'l1 = lambda : 0verify(l1() == 0)l2 = lambda : a[d] # XXX just testing the expressionl3 = lambda : [2 < x for x in [-1, 3, 0L]]verify(l3() == [0, 1, 0])l4 = lambda x = lambda y = lambda z=1 : z : y() : x()verify(l4() == 1)l5 = lambda x, y, z=2: x + y + zverify(l5(1, 2) == 5)verify(l5(1, 2, 3) == 6)check_syntax("lambda x: x = 2")### stmt: simple_stmt | compound_stmt# Tested below### simple_stmt: small_stmt (';' small_stmt)* [';']print 'simple_stmt'x = 1; pass; del x### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt# Tested belowprint 'expr_stmt' # (exprlist '=')* exprlist11, 2, 3x = 1x = 1, 2, 3x = y = z = 1, 2, 3x, y, z = 1, 2, 3abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)# NB these variables are deleted belowcheck_syntax("x + 1 = 1")check_syntax("a + 1 = b + 2")print 'print_stmt' # 'print' (test ',')* [test]print 1, 2, 3print 1, 2, 3,printprint 0 or 1, 0 or 1,print 0 or 1print 'extended print_stmt' # 'print' '>>' test ','import sysprint >> sys.stdout, 1, 2, 3print >> sys.stdout, 1, 2, 3,print >> sys.stdoutprint >> sys.stdout, 0 or 1, 0 or 1,print >> sys.stdout, 0 or 1# test printing to an instanceclass Gulp: def write(self, msg): passgulp = Gulp()print >> gulp, 1, 2, 3print >> gulp, 1, 2, 3,print >> gulpprint >> gulp, 0 or 1, 0 or 1,print >> gulp, 0 or 1# test print >> Nonedef driver(): oldstdout = sys.stdout sys.stdout = Gulp() try: tellme(Gulp()) tellme() finally: sys.stdout = oldstdout# we should see this oncedef tellme(file=sys.stdout): print >> file, 'hello world'driver()# we should not see this at alldef tellme(file=None): print >> file, 'goodbye universe'driver()# syntax errorscheck_syntax('print ,')check_syntax('print >> x,')print 'del_stmt' # 'del' exprlistdel abcdel x, y, (z, xyz)print 'pass_stmt' # 'pass'passprint 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt# Tested belowprint 'break_stmt' # 'break'while 1: breakprint 'continue_stmt' # 'continue'i = 1while i: i = 0; continuemsg = ""while not msg: msg = "continue + try/except ok" try: continue msg = "continue failed to continue inside try" except: msg = "continue inside try called except block"print msgmsg = ""while not msg: msg = "finally block not called" try: continue
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?