本文整理汇总了Python中test.support.check_syntax_error函数的典型用法代码示例。如果您正苦于以下问题:Python check_syntax_error函数的具体用法?Python check_syntax_error怎么用?Python check_syntax_error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了check_syntax_error函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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))")
开发者ID:johnyf,项目名称:cpython,代码行数:34,代码来源:test_grammar.py
示例2: test2
def test2(self):
prog_text_2 = """\
def wrong2():
print(x)
global x
"""
check_syntax_error(self, prog_text_2)
开发者ID:0jpq0,项目名称:kbengine,代码行数:7,代码来源:test_global.py
示例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
开发者ID:johnyf,项目名称:cpython,代码行数:34,代码来源:test_grammar.py
示例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)
开发者ID:1st1,项目名称:cpython,代码行数:7,代码来源:test_global.py
示例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")
开发者ID:johnyf,项目名称:cpython,代码行数:7,代码来源:test_grammar.py
示例6: test3
def test3(self):
prog_text_3 = """\
def wrong3():
print(x)
x = 2
global x
"""
check_syntax_error(self, prog_text_3)
开发者ID:0jpq0,项目名称:kbengine,代码行数:8,代码来源:test_global.py
示例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)
开发者ID:0jpq0,项目名称:kbengine,代码行数:9,代码来源:test_global.py
示例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")
开发者ID:johnyf,项目名称:cpython,代码行数:12,代码来源:test_grammar.py
示例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?
""")
开发者ID:10sr,项目名称:cpython,代码行数:33,代码来源:test_scope.py
示例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)
开发者ID:CaoYouXin,项目名称:myfirstapicloudapp,代码行数:17,代码来源:py3_test_grammar.py
示例11: testYield
def testYield(self):
check_syntax_error(self, "class foo:yield 1")
开发者ID:CaoYouXin,项目名称:myfirstapicloudapp,代码行数:2,代码来源:py3_test_grammar.py
示例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))")
开发者ID:Martiusweb,项目名称:cpython,代码行数:101,代码来源:test_grammar.py
示例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")
开发者ID:cpcloud,项目名称:cpython,代码行数:4,代码来源:test_support.py
示例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
开发者ID:johnyf,项目名称:cpython,代码行数:101,代码来源:test_grammar.py
示例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")
开发者ID:johnyf,项目名称:cpython,代码行数:19,代码来源:test_grammar.py
示例16: test_check_syntax_error
def test_check_syntax_error(self):
support.check_syntax_error(self, "def class", lineno=1, offset=9)
with self.assertRaises(AssertionError):
support.check_syntax_error(self, "x=1")
开发者ID:CCNITSilchar,项目名称:cpython,代码行数:4,代码来源:test_support.py
示例17: test_listcomps
def test_listcomps(self):
# list comprehension tests
nums = [1, 2, 3, 4, 5]
strs = ["Apple", "Banana", "Coconut"]
spcs = [" Apple", " Banana ", "Coco nut "]
self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])
self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
self.assertEqual([(i, s) for i in nums for s in strs],
[(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
(2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
(3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
(4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
(5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
[(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
(3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
(5, 'Banana'), (5, 'Coconut')])
self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
[[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
def test_in_func(l):
return [0 < x < 3 for x in l if x > 2]
self.assertEqual(test_in_func(nums), [False, False, False])
def test_nested_front():
self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
[[1, 2], [3, 4], [5, 6]])
test_nested_front()
check_syntax_error(self, "[i, s for i in nums for s in strs]")
check_syntax_error(self, "[x if y]")
suppliers = [
(1, "Boeing"),
(2, "Ford"),
(3, "Macdonalds")
]
parts = [
(10, "Airliner"),
(20, "Engine"),
(30, "Cheeseburger")
]
suppart = [
(1, 10), (1, 20), (2, 20), (3, 30)
]
x = [
(sname, pname)
for (sno, sname) in suppliers
for (pno, pname) in parts
for (sp_sno, sp_pno) in suppart
if sno == sp_sno and pno == sp_pno
]
self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
('Macdonalds', 'Cheeseburger')])
开发者ID:johnyf,项目名称:cpython,代码行数:62,代码来源:test_grammar.py
示例18: test_yield
def test_yield(self):
# Allowed as standalone statement
def g(): yield 1
def g(): yield from ()
# Allowed as RHS of assignment
def g(): x = yield 1
def g(): x = yield from ()
# Ordinary yield accepts implicit tuples
def g(): yield 1, 1
def g(): x = yield 1, 1
# 'yield from' does not
check_syntax_error(self, "def g(): yield from (), 1")
check_syntax_error(self, "def g(): x = yield from (), 1")
# Requires parentheses as subexpression
def g(): 1, (yield 1)
def g(): 1, (yield from ())
check_syntax_error(self, "def g(): 1, yield 1")
check_syntax_error(self, "def g(): 1, yield from ()")
# Requires parentheses as call argument
def g(): f((yield 1))
def g(): f((yield 1), 1)
def g(): f((yield from ()))
def g(): f((yield from ()), 1)
check_syntax_error(self, "def g(): f(yield 1)")
check_syntax_error(self, "def g(): f(yield 1, 1)")
check_syntax_error(self, "def g(): f(yield from ())")
check_syntax_error(self, "def g(): f(yield from (), 1)")
# Not allowed at top level
check_syntax_error(self, "yield")
check_syntax_error(self, "yield from")
# Not allowed at class scope
check_syntax_error(self, "class foo:yield 1")
check_syntax_error(self, "class foo:yield from ()")
# Check annotation refleak on SyntaxError
check_syntax_error(self, "def g(a:(yield)): pass")
开发者ID:johnyf,项目名称:cpython,代码行数:35,代码来源:test_grammar.py
示例19: test_check_syntax_error
def test_check_syntax_error(self):
support.check_syntax_error(self, "def class")
self.assertRaises(AssertionError, support.check_syntax_error, self, "1")
开发者ID:MYSHLIFE,项目名称:cpython-1,代码行数:3,代码来源:test_support.py
示例20: testUnoptimizedNamespaces
def testUnoptimizedNamespaces(self):
check_syntax_error(self, """\
def unoptimized_clash1(strip):
def f(s):
from string import *
return strip(s) # ambiguity: free or local
return f
""")
check_syntax_error(self, """\
def unoptimized_clash2():
from string import *
def f(s):
return strip(s) # ambiguity: global or local
return f
""")
check_syntax_error(self, """\
def unoptimized_clash2():
from string import *
def g():
def f(s):
return strip(s) # ambiguity: global or local
return f
""")
# XXX could allow this for exec with const argument, but what's the point
check_syntax_error(self, """\
def error(y):
exec "a = 1"
def f(x):
return x + y
return f
""")
check_syntax_error(self, """\
def f(x):
def g():
return x
del x # can't del name
""")
check_syntax_error(self, """\
def f():
def g():
from string import *
return strip # global or local?
""")
# and verify a few cases that should work
exec("""
def noproblem1():
from string import *
f = lambda x:x
def noproblem2():
from string import *
def f(x):
return x + 1
def noproblem3():
from string import *
def f(x):
global y
y = x
""")
开发者ID:isaiah,项目名称:jython3,代码行数:68,代码来源:test_scope.py
注:本文中的test.support.check_syntax_error函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论