本文整理汇总了Python中test.support.check_syntax_error函数的典型用法代码示例。如果您正苦于以下问题:Python check_syntax_error函数的具体用法?Python check_syntax_error怎么用?Python check_syntax_error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了check_syntax_error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_genexps
def test_genexps(self):
# generator expression tests
g = ([x for x in range(10)] for x in range(1))
self.assertEqual(next(g), [x for x in range(10)])
try:
next(g)
self.fail('should produce StopIteration exception')
except StopIteration:
pass
a = 1
try:
g = (a for d in a)
next(g)
self.fail('should produce TypeError')
except TypeError:
pass
self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
a = [x for x in range(10)]
b = (x for x in (y for y in a))
self.assertEqual(sum(b), sum([x for x in range(10)]))
self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)]))
self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
check_syntax_error(self, "foo(x for x in range(10), 100)")
check_syntax_error(self, "foo(100, x for x in range(10))")
示例2: test2
def test2(self):
prog_text_2 = """\
def wrong2():
print(x)
global x
"""
check_syntax_error(self, prog_text_2)
示例3: test_lambdef
def test_lambdef(self):
### lambdef: 'lambda' [varargslist] ':' test
l1 = lambda : 0
self.assertEqual(l1(), 0)
l2 = lambda : a[d] # XXX just testing the expression
l3 = lambda : [2 < x for x in [-1, 3, 0]]
self.assertEqual(l3(), [0, 1, 0])
l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
self.assertEqual(l4(), 1)
l5 = lambda x, y, z=2: x + y + z
self.assertEqual(l5(1, 2), 5)
self.assertEqual(l5(1, 2, 3), 6)
check_syntax_error(self, "lambda x: x = 2")
check_syntax_error(self, "lambda (None,): None")
l6 = lambda x, y, *, k=20: x+y+k
self.assertEqual(l6(1,2), 1+2+20)
self.assertEqual(l6(1,2,k=10), 1+2+10)
# check that trailing commas are permitted
l10 = lambda a,: 0
l11 = lambda *args,: 0
l12 = lambda **kwds,: 0
l13 = lambda a, *args,: 0
l14 = lambda a, **kwds,: 0
l15 = lambda *args, b,: 0
l16 = lambda *, b,: 0
l17 = lambda *args, **kwds,: 0
l18 = lambda a, *args, b,: 0
l19 = lambda a, *, b,: 0
l20 = lambda a, *args, **kwds,: 0
l21 = lambda *args, b, **kwds,: 0
l22 = lambda *, b, **kwds,: 0
l23 = lambda a, *args, b, **kwds,: 0
l24 = lambda a, *, b, **kwds,: 0
示例4: test2
def test2(self):
prog_text_2 = """\
def wrong2():
print(x)
global x
"""
check_syntax_error(self, prog_text_2, lineno=3, offset=4)
示例5: test_return
def test_return(self):
# 'return' [testlist]
def g1(): return
def g2(): return 1
g1()
x = g2()
check_syntax_error(self, "class foo:return 1")
示例6: test3
def test3(self):
prog_text_3 = """\
def wrong3():
print(x)
x = 2
global x
"""
check_syntax_error(self, prog_text_3)
示例7: test1
def test1(self):
prog_text_1 = """\
def wrong1():
a = 1
b = 2
global a
global b
"""
check_syntax_error(self, prog_text_1)
示例8: test_expr_stmt
def test_expr_stmt(self):
# (exprlist '=')* exprlist
1
1, 2, 3
x = 1
x = 1, 2, 3
x = y = z = 1, 2, 3
x, y, z = 1, 2, 3
abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
check_syntax_error(self, "x + 1 = 1")
check_syntax_error(self, "a + 1 = b + 2")
示例9: testUnoptimizedNamespaces
def testUnoptimizedNamespaces(self):
check_syntax_error(self, """if 1:
def unoptimized_clash1(strip):
def f(s):
from sys import *
return getrefcount(s) # ambiguity: free or local
return f
""")
check_syntax_error(self, """if 1:
def unoptimized_clash2():
from sys import *
def f(s):
return getrefcount(s) # ambiguity: global or local
return f
""")
check_syntax_error(self, """if 1:
def unoptimized_clash2():
from sys import *
def g():
def f(s):
return getrefcount(s) # ambiguity: global or local
return f
""")
check_syntax_error(self, """if 1:
def f():
def g():
from sys import *
return getrefcount # global or local?
""")
示例10: testLambdef
def testLambdef(self):
### lambdef: 'lambda' [varargslist] ':' test
l1 = lambda : 0
self.assertEquals(l1(), 0)
l2 = lambda : a[d] # XXX just testing the expression
l3 = lambda : [2 < x for x in [-1, 3, 0]]
self.assertEquals(l3(), [0, 1, 0])
l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
self.assertEquals(l4(), 1)
l5 = lambda x, y, z=2: x + y + z
self.assertEquals(l5(1, 2), 5)
self.assertEquals(l5(1, 2, 3), 6)
check_syntax_error(self, "lambda x: x = 2")
check_syntax_error(self, "lambda (None,): None")
l6 = lambda x, y, *, k=20: x+y+k
self.assertEquals(l6(1,2), 1+2+20)
self.assertEquals(l6(1,2,k=10), 1+2+10)
示例11: testYield
def testYield(self):
check_syntax_error(self, "class foo:yield 1")
示例12: test_funcdef
#.........这里部分代码省略.........
def pos2key2(p1, p2, *, k1, k2=100):
return p1, p2, k1, k2
pos2key2(1, 2, k1=100)
pos2key2(1, 2, k1=100, k2=200)
pos2key2(1, 2, k2=100, k1=200)
def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg):
return p1, p2, k1, k2, kwarg
pos2key2dict(1, 2, k2=100, tokwarg1=100, tokwarg2=200)
pos2key2dict(1, 2, tokwarg1=100, tokwarg2=200, k2=100)
# keyword arguments after *arglist
def f(*args, **kwargs):
return args, kwargs
self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), {"x": 2, "y": 5}))
self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
# argument annotation tests
def f(x) -> list:
pass
self.assertEqual(f.__annotations__, {"return": list})
def f(x: int):
pass
self.assertEqual(f.__annotations__, {"x": int})
def f(*x: str):
pass
self.assertEqual(f.__annotations__, {"x": str})
def f(**x: float):
pass
self.assertEqual(f.__annotations__, {"x": float})
def f(x, y: 1 + 2):
pass
self.assertEqual(f.__annotations__, {"y": 3})
def f(a, b: 1, c: 2, d):
pass
self.assertEqual(f.__annotations__, {"b": 1, "c": 2})
def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6):
pass
self.assertEqual(f.__annotations__, {"b": 1, "c": 2, "e": 3, "g": 6})
def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10, **k: 11) -> 12:
pass
self.assertEqual(f.__annotations__, {"b": 1, "c": 2, "e": 3, "g": 6, "h": 7, "j": 9, "k": 11, "return": 12})
# Check for issue #20625 -- annotations mangling
class Spam:
def f(self, *, __kw: 1):
pass
class Ham(Spam):
pass
self.assertEqual(Spam.f.__annotations__, {"_Spam__kw": 1})
self.assertEqual(Ham.f.__annotations__, {"_Spam__kw": 1})
# Check for SF Bug #1697248 - mixing decorators and a return annotation
def null(x):
return x
@null
def f(x) -> list:
pass
self.assertEqual(f.__annotations__, {"return": list})
# test MAKE_CLOSURE with a variety of oparg's
closure = 1
def f():
return closure
def f(x=1):
return closure
def f(*, k=1):
return closure
def f() -> int:
return closure
# Check ast errors in *args and *kwargs
check_syntax_error(self, "f(*g(1=2))")
check_syntax_error(self, "f(**g(1=2))")
示例13: test_check_syntax_error
def test_check_syntax_error(self):
support.check_syntax_error(self, "def class")
with self.assertRaises(AssertionError):
support.check_syntax_error(self, "x=1")
示例14: test_funcdef
#.........这里部分代码省略.........
d22v(1, 2, 3, 4, 5)
d22v(*(1, 2, 3, 4))
d22v(1, 2, *(3, 4, 5))
d22v(1, *(2, 3), **{'d': 4})
# keyword argument type tests
try:
str('x', **{b'foo':1 })
except TypeError:
pass
else:
self.fail('Bytes should not work as keyword argument names')
# keyword only argument tests
def pos0key1(*, key): return key
pos0key1(key=100)
def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2
pos2key2(1, 2, k1=100)
pos2key2(1, 2, k1=100, k2=200)
pos2key2(1, 2, k2=100, k1=200)
def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg
pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
self.assertRaises(SyntaxError, eval, "def f(*): pass")
self.assertRaises(SyntaxError, eval, "def f(*,): pass")
self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass")
# keyword arguments after *arglist
def f(*args, **kwargs):
return args, kwargs
self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
{'x':2, 'y':5}))
self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {}))
self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}),
((), {'eggs':'scrambled', 'spam':'fried'}))
self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}),
((), {'eggs':'scrambled', 'spam':'fried'}))
# argument annotation tests
def f(x) -> list: pass
self.assertEqual(f.__annotations__, {'return': list})
def f(x: int): pass
self.assertEqual(f.__annotations__, {'x': int})
def f(*x: str): pass
self.assertEqual(f.__annotations__, {'x': str})
def f(**x: float): pass
self.assertEqual(f.__annotations__, {'x': float})
def f(x, y: 1+2): pass
self.assertEqual(f.__annotations__, {'y': 3})
def f(a, b: 1, c: 2, d): pass
self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass
self.assertEqual(f.__annotations__,
{'b': 1, 'c': 2, 'e': 3, 'g': 6})
def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10,
**k: 11) -> 12: pass
self.assertEqual(f.__annotations__,
{'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
'k': 11, 'return': 12})
# Check for issue #20625 -- annotations mangling
class Spam:
def f(self, *, __kw: 1):
pass
class Ham(Spam): pass
self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1})
self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1})
# Check for SF Bug #1697248 - mixing decorators and a return annotation
def null(x): return x
@null
def f(x) -> list: pass
self.assertEqual(f.__annotations__, {'return': list})
# test closures with a variety of opargs
closure = 1
def f(): return closure
def f(x=1): return closure
def f(*, k=1): return closure
def f() -> int: return closure
# Check ast errors in *args and *kwargs
check_syntax_error(self, "f(*g(1=2))")
check_syntax_error(self, "f(**g(1=2))")
# Check trailing commas are permitted in funcdef argument list
def f(a,): pass
def f(*args,): pass
def f(**kwds,): pass
def f(a, *args,): pass
def f(a, **kwds,): pass
def f(*args, b,): pass
def f(*, b,): pass
def f(*args, **kwds,): pass
def f(a, *args, b,): pass
def f(a, *, b,): pass
def f(a, *args, **kwds,): pass
def f(*args, b, **kwds,): pass
def f(*, b, **kwds,): pass
def f(a, *args, b, **kwds,): pass
def f(a, *, b, **kwds,): pass
示例15: test_var_annot_syntax_errors
def test_var_annot_syntax_errors(self):
# parser pass
check_syntax_error(self, "def f: int")
check_syntax_error(self, "x: int: str")
check_syntax_error(self, "def f():\n"
" nonlocal x: int\n")
# AST pass
check_syntax_error(self, "[x, 0]: int\n")
check_syntax_error(self, "f(): int\n")
check_syntax_error(self, "(x,): int")
check_syntax_error(self, "def f():\n"
" (x, y): int = (1, 2)\n")
# symtable pass
check_syntax_error(self, "def f():\n"
" x: int\n"
" global x\n")
check_syntax_error(self, "def f():\n"
" global x\n"
" x: int\n")