Ejemplo n.º 1
0
def Xtest_indent_broken():

    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
try:
    stack = add_token(stack, grammar, token, label_index)
except ParseError as e:
    errors.append(e)
    tokens, i, stack = try_recover(grammar, stack, tokens, i)
    if i == -1:
        break
except Done as e:
%   self.root = e.node
    break

print 17

if x
    print 3
""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [5, 10]
        print(pyparse.format_messages(e))
Ejemplo n.º 2
0
def test_missing_comma_list():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
[1,
2
3,
5,
5,
3,
4,
5,
1,
2,
]

if a
    print 2

""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [4, 14]
        print(pyparse.format_messages(e))
Ejemplo n.º 3
0
    def test_encoding(self):
        info = pyparse.CompileInfo("<test>", "exec")
        tree = self.parse("""# coding: latin-1
stuff = "nothing"
""", info=info)
        assert tree.type == syms.file_input
        assert info.encoding == "iso-8859-1"
        sentence = u"u'Die Männer ärgern sich!'"
        input = (u"# coding: utf-7\nstuff = %s" % (sentence,)).encode("utf-7")
        tree = self.parse(input, info=info)
        assert info.encoding == "utf-7"
        input = b"# coding: iso-8859-15\nx"
        self.parse(input, info=info)
        assert info.encoding == "iso-8859-15"
        input = b"\xEF\xBB\xBF# coding: utf-8\nx"
        self.parse(input, info=info)
        assert info.encoding == "utf-8"
        input = b"# coding: utf-8\nx"
        info.flags |= consts.PyCF_SOURCE_IS_UTF8
        exc = py.test.raises(SyntaxError, self.parse, input, info=info).value
        info.flags &= ~consts.PyCF_SOURCE_IS_UTF8
        assert exc.msg == "coding declaration in unicode string"
        input = b"\xEF\xBB\xBF# coding: latin-1\nx"
        exc = py.test.raises(SyntaxError, self.parse, input).value
        assert exc.msg == "UTF-8 BOM with latin-1 coding cookie"
        input = b"# coding: not-here"
        exc = py.test.raises(SyntaxError, self.parse, input).value
        assert exc.msg == "Unknown encoding: not-here"
        input = u"# coding: ascii\n\xe2".encode('utf-8')
        exc = py.test.raises(SyntaxError, self.parse, input).value
        assert exc.msg == ("'ascii' codec can't decode byte 0xc3 "
                           "in position 16: ordinal not in range(128)")
Ejemplo n.º 4
0
def test_parse_all():
    # smoke test
    for fn in os.listdir(srcdir):
        if fn.endswith(".py"):
            fn = os.path.join(srcdir, fn)
            with open(fn, "rb") as f:
                s = f.read()
            info = pyparse.CompileInfo(fn, "exec")
            p = pyparse.PythonParser()
            st = p.parse_source(s, info)
Ejemplo n.º 5
0
def test_find_four_errors():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
if a
    print 2

x +=

print 4

x * * * * x

for i in range(10):
    print i

i += 1

if a
    print 5

""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 4
        assert [x.lineno for x in e.errors] == [2, 5, 9, 16]
        msg = pyparse.format_messages(e)
        assert msg == """\
if a
    ^
invalid syntax (expected ':') (line 2)


___
There were possibly further errors, but they are guesses:
(This is an experimental feature! if the errors are nonsense, please report a bug!)

x +=
    ^
invalid syntax (line 5)

x * * * * x
    ^
invalid syntax (line 9)

if a
    ^
invalid syntax (expected ':') (line 16)
"""
    else:
        assert 0, "should have raised"
Ejemplo n.º 6
0
def test_genexp_tuple():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
(a, b for a, b in zip([1, 2], [3, 4]))
print 1
if a
    print 3
""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [2, 4]
        print(pyparse.format_messages(e))
Ejemplo n.º 7
0
def test_genexp_keywordarg():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
dict(a = i for i in range(10))
print 1
if a
    print 3
""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [2, 4]
        print(pyparse.format_messages(e))
Ejemplo n.º 8
0
def test_missing_newline():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
if 1: def some_complicated_function(w, ith, many, tokens):
        if a:
            print 2
        else:
            print 3

if x
    print 3
""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [2, 8]
        print(pyparse.format_messages(e))
Ejemplo n.º 9
0
def test_lambda_with_newlines():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
lambda x:
    x + 1

print 2

if a
    print 2

""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [2, 7]
        print(pyparse.format_messages(e))
Ejemplo n.º 10
0
def test_random_comment():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
def add_tokens(self, tokens):
    from #yntaxerrors.recovery import try_recover
    grammar = self.grammar
    stack = StackEntry(None, grammar.dfas[self.start - 256], 0)

print 17

if x
    print 3
""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [3, 9]
        print(pyparse.format_messages(e))
Ejemplo n.º 11
0
def test_extra_dot():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
if 1:
    if errors:
        self.root = None
        if len(error.) == 1:
            raise errors[0]
        else:
            raise MultipleParseError(errors)

if x
    print 3
""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [5, 10]
        print(pyparse.format_messages(e))
Ejemplo n.º 12
0
def test_missing_pass():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
if x:
    # nothing
else:
    print 2

print 2 + 2

if a
    print 2

""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [4, 9]
        print(pyparse.format_messages(e))
Ejemplo n.º 13
0
def test_need_to_fix_earlier():
    import six
    if six.PY3:
        pytest.skip("later")
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
de% blub(self, tokens):
    grammar = self.grammar
    stack = StackEntry(None, grammar.dfas[self.start - 256], 0)

print 17

if x
    print 3
""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [2, 8]
        print(pyparse.format_messages(e))
Ejemplo n.º 14
0
def test_further_examples():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
def f(a, e.expected):
    bla

a.b(1, 2, 3):

def f(self):

print b

""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 3
        assert [x.lineno for x in e.errors] == [2, 5, 9]
        msg = pyparse.format_messages(e)
        assert msg == """\
def f(a, e.expected):
          ^
invalid syntax (expected ')') (line 2)


___
There were possibly further errors, but they are guesses:
(This is an experimental feature! if the errors are nonsense, please report a bug!)

a.b(1, 2, 3):
            ^
invalid syntax (line 5)

print b
^
expected an indented block (line 9)
"""
    else:
        assert 0, "should have raised"
Ejemplo n.º 15
0
def Xtest_random_newline():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    try:
        st = p.parse_source(b"""
try:
    1/0
exc
  ept ZeroDivisionError as e:
    foo.bar.baz
else:
    blub

print 17

if x
    print 3
""", info)
    except MultipleSyntaxErrors as e:
        assert len(e.errors) == 2
        assert [x.lineno for x in e.errors] == [5, 10]
        print(pyparse.format_messages(e))
Ejemplo n.º 16
0
 def parse(self, source, mode="exec", info=None):
     if not isinstance(source, bytes):
         source = source.encode("utf-8")
     if info is None:
         info = pyparse.CompileInfo("<test>", mode)
     return self.parser.parse_source(source, info)
Ejemplo n.º 17
0
def test_simple():
    info = pyparse.CompileInfo("<string>", "exec")
    p = pyparse.PythonParser()
    st = p.parse_source(b"x = 1\n", info)
Ejemplo n.º 18
0
 def test_dont_imply_dedent(self):
     info = pyparse.CompileInfo("<test>", "single",
                                consts.PyCF_DONT_IMPLY_DEDENT)
     self.parse('if 1:\n  x\n', info=info)
     self.parse('x = 5 ', info=info)