Beispiel #1
0
def test_source_putaround():
    source = Source()
    source = source.putaround("""
        if 1:
            x=1
    """)
    assert str(source).strip() == "if 1:\n    x=1"
def test_source_putaround():
    source = Source()
    source = source.putaround("""
        if 1:
            x=1
    """)
    assert str(source).strip() == "if 1:\n    x=1"
 def test_getstatementrange_triple_quoted(self):
     #print str(self.source)
     source = Source("""hello('''
     ''')""")
     s = source.getstatement(0)
     assert s == str(source)
     s = source.getstatement(1)
     assert s == str(source)
Beispiel #4
0
 def test_getstatementrange_triple_quoted(self):
     #print str(self.source)
     source = Source("""hello('''
     ''')""")
     s = source.getstatement(0)
     assert s == str(source)
     s = source.getstatement(1)
     assert s == str(source)
Beispiel #5
0
def test_source_putaround_simple():
    source = Source("raise ValueError")
    source = source.putaround(
        "try:", """\
        except ValueError:
            x = 42
        else:
            x = 23""")
    assert str(source) == """\
def test_source_putaround_simple():
    source = Source("raise ValueError")
    source = source.putaround(
        "try:", """\
        except ValueError:
            x = 42
        else:
            x = 23""")
    assert str(source)=="""\
Beispiel #7
0
 def test_getstatementrange_bug(self):
     source = Source("""\
         try:
             x = (
                y +
                z)
         except:
             pass
     """)
     assert len(source) == 6
     assert source.getstatementrange(2) == (1, 4)
 def test_getstatementrange_bug(self):
     source = Source("""\
         try:
             x = (
                y +
                z)
         except:
             pass
     """)
     assert len(source) == 6
     assert source.getstatementrange(2) == (1, 4)
 def test_getstatementrange_bug2(self):
     source = Source("""\
         assert (
             33
             ==
             [
               X(3,
                   b=1, c=2
                ),
             ]
           )
     """)
     assert len(source) == 9
     assert source.getstatementrange(5) == (0, 9)
Beispiel #10
0
 def test_getstatementrange_bug2(self):
     source = Source("""\
         assert (
             33
             ==
             [
               X(3,
                   b=1, c=2
                ),
             ]
           )
     """)
     assert len(source) == 9
     assert source.getstatementrange(5) == (0, 9)
Beispiel #11
0
 def test_getstatementrange_bug2(self):
     py.test.skip("fix me (issue19)")
     source = Source("""\
         assert (
             33
             ==
             [
               X(3,
                   b=1, c=2
                ),
             ]
           )
     """)
     assert len(source) == 9
     assert source.getstatementrange(5) == (0, 9)
Beispiel #12
0
def test_comment_and_no_newline_at_end():
    from py._code.source import getstatementrange_ast
    source = Source(['def test_basic_complex():',
                     '    assert 1 == 2',
                     '# vim: filetype=pyopencl:fdm=marker'])
    ast, start, end = getstatementrange_ast(1, source)
    assert end == 2
Beispiel #13
0
def test_source_str_function():
    x = Source("3")
    assert str(x) == "3"

    x = Source("   3")
    assert str(x) == "3"

    x = Source("""
        3
    """, rstrip=False)
    assert str(x) == "\n3\n    "

    x = Source("""
        3
    """, rstrip=True)
    assert str(x) == "\n3"
def test_logsdir_cleanup(testdir, conftest_py, test_case_py):

    logsdir = outdir(testdir, 'myinilogs')

    makefile(
        testdir, ['pytest.ini'], """
        [pytest]
        logger_logsdir={0}
    """.format(logsdir))

    logsdir.ensure('tmpfile').write('\n'.join(Source('this shall be removed')))
    logsdir.join('tmpdir')

    result = testdir.runpytest('-s')
    assert result.ret == 0
    result.stdout.fnmatch_lines([
        '',
        'test_case.py .',
        '',
    ])

    assert ls(logsdir) == [test_case_py]
    assert ls(logsdir, test_case_py) == ['test_case']
    assert ls(logsdir, '{0}/test_case'.format(test_case_py)) == ['bar', 'foo']

    FileLineMatcher(logsdir,
                    '{0}/test_case/foo'.format(test_case_py)).fnmatch_lines([
                        '* foo: this is error',
                        '* foo: this is warning',
                    ])
    FileLineMatcher(logsdir,
                    '{0}/test_case/bar'.format(test_case_py)).fnmatch_lines([
                        '* bar: this is error',
                    ])
Beispiel #15
0
 def test_getstatementrange_within_constructs(self):
     source = Source("""\
         try:
             try: 
                 raise ValueError
             except SomeThing: 
                 pass 
         finally:
             42
     """)
     assert len(source) == 7
     assert source.getstatementrange(0) == (0, 7) 
     assert source.getstatementrange(1) == (1, 5) 
     assert source.getstatementrange(2) == (2, 3)
     assert source.getstatementrange(3) == (1, 5)
     assert source.getstatementrange(4) == (4, 5) 
     assert source.getstatementrange(5) == (0, 7)
     assert source.getstatementrange(6) == (6, 7) 
Beispiel #16
0
 def test_getstatementrange_within_constructs(self):
     source = Source("""\
         try:
             try:
                 raise ValueError
             except SomeThing:
                 pass
         finally:
             42
     """)
     assert len(source) == 7
     # check all lineno's that could occur in a traceback
     #assert source.getstatementrange(0) == (0, 7)
     #assert source.getstatementrange(1) == (1, 5)
     assert source.getstatementrange(2) == (2, 3)
     assert source.getstatementrange(3) == (3, 4)
     assert source.getstatementrange(4) == (4, 5)
     #assert source.getstatementrange(5) == (0, 7)
     assert source.getstatementrange(6) == (6, 7)
Beispiel #17
0
def test_unicode():
    try:
        unicode
    except NameError:
        return
    x = Source(unicode("4"))
    assert str(x) == "4"
    co = py.code.compile(unicode('u"\xc3\xa5"', 'utf8'), mode='eval')
    val = eval(co)
    assert isinstance(val, unicode)
 def test_getstatementrange_within_constructs(self):
     source = Source("""\
         try:
             try:
                 raise ValueError
             except SomeThing:
                 pass
         finally:
             42
     """)
     assert len(source) == 7
     # check all lineno's that could occur in a traceback
     #assert source.getstatementrange(0) == (0, 7)
     #assert source.getstatementrange(1) == (1, 5)
     assert source.getstatementrange(2) == (2, 3)
     assert source.getstatementrange(3) == (3, 4)
     assert source.getstatementrange(4) == (4, 5)
     #assert source.getstatementrange(5) == (0, 7)
     assert source.getstatementrange(6) == (6, 7)
Beispiel #19
0
    def test_getstatementrange_ast_issue58(self):
        source = Source("""\

            def test_some():
                for a in [a for a in
                    CAUSE_ERROR]: pass

            x = 3
        """)
        assert getstatement(2, source).lines == source.lines[2:3]
        assert getstatement(3, source).lines == source.lines[3:4]
Beispiel #20
0
def test_isparseable():
    assert Source("hello").isparseable()
    assert Source("if 1:\n  pass").isparseable()
    assert Source(" \nif 1:\n  pass").isparseable()
    assert not Source("if 1:\n").isparseable()
    assert not Source(" \nif 1:\npass").isparseable()
    assert not Source(chr(0)).isparseable()
Beispiel #21
0
 def setup_class(cls):
     # simply compile snippets just once
     src = str(Source(snippet))
     # truncate non-compilable stuff for now:
     p = src.index('Non compilable Functions')
     src = src[:p] + '\n'
     # put our ad into snippet
     exec cls.snippet_ad in snippet.__dict__
     src += cls.snippet_ad
     # just in case of trouble, we produce a tempfile
     ini, newsrc = translate_as_module(src,
                                       tmpname=str(
                                           udir.join("_geninterp_test.py")))
     cls.w_glob = ini(cls.space)
Beispiel #22
0
 def remote_exec(self, source, stdout=None, stderr=None): 
     """ return channel object and connect it to a remote
         execution thread where the given 'source' executes
         and has the sister 'channel' object in its global 
         namespace.  The callback functions 'stdout' and 
         'stderr' get called on receival of remote 
         stdout/stderr output strings. 
     """
     try:
         source = str(Source(source))
     except NameError: 
         try: 
             import py 
             source = str(py.code.Source(source))
         except ImportError: 
             pass 
     channel = self.newchannel() 
     outid = self._newredirectchannelid(stdout) 
     errid = self._newredirectchannelid(stderr) 
     self._send(Message.CHANNEL_OPEN(
                 channel.id, (source, outid, errid)))
     return channel 
Beispiel #23
0
class TestAccesses:
    source = Source("""\
        def f(x):
            pass
        def g(x):
            pass
    """)
    def test_getrange(self):
        x = self.source[0:2]
        assert x.isparseable()
        assert len(x.lines) == 2
        assert str(x) == "def f(x):\n    pass"

    def test_getline(self):
        x = self.source[0]
        assert x == "def f(x):"

    def test_len(self):
        assert len(self.source) == 4

    def test_iter(self):
        l = [x for x in self.source]
        assert len(l) == 4
Beispiel #24
0
 def test_getstatementrange_within_constructs(self):
     source = Source("""\
         try:
             try: 
                 raise ValueError
             except SomeThing: 
                 pass 
         finally:
             42
     """)
     assert len(source) == 7
     assert source.getstatementrange(0) == (0, 7)
     assert source.getstatementrange(1) == (1, 5)
     assert source.getstatementrange(2) == (2, 3)
     assert source.getstatementrange(3) == (1, 5)
     assert source.getstatementrange(4) == (4, 5)
     assert source.getstatementrange(5) == (0, 7)
     assert source.getstatementrange(6) == (6, 7)
 def test_compile_to_ast(self):
     import ast
     source = Source("x = 4")
     mod = source.compile(flag=ast.PyCF_ONLY_AST)
     assert isinstance(mod, ast.Module)
     compile(mod, "<filename>", "exec")
 def test_getstatementrange_with_syntaxerror_issue7(self):
     source = Source(":")
     py.test.raises(SyntaxError, lambda: source.getstatementrange(0))
 def test_getstatementrange_out_of_bounds_py3(self):
     source = Source("if xxx:\n   from .collections import something")
     r = source.getstatementrange(1)
     assert r == (1,2)
Beispiel #28
0
def test_source_strips():
    source = Source("")
    assert source == Source()
    assert str(source) == ''
    assert source.strip() == source
Beispiel #29
0
def test_source_strip_multiline():
    source = Source()
    source.lines = ["", " hello", "  "]
    source2 = source.strip()
    assert source2.lines == [" hello"]
Beispiel #30
0
class TestSourceParsingAndCompiling:
    source = Source("""\
        def f(x):
            assert (x ==
                    3 +
                    4)
    """).strip()

    def test_compile(self):
        co = py.code.compile("x=3")
        d = {}
        exec(co, d)
        assert d['x'] == 3

    def test_compile_and_getsource_simple(self):
        co = py.code.compile("x=3")
        exec(co)
        source = py.code.Source(co)
        assert str(source) == "x=3"

    def test_compile_and_getsource_through_same_function(self):
        def gensource(source):
            return py.code.compile(source)

        co1 = gensource("""
            def f():
                raise KeyError()
        """)
        co2 = gensource("""
            def f():
                raise ValueError()
        """)
        source1 = inspect.getsource(co1)
        assert 'KeyError' in source1
        source2 = inspect.getsource(co2)
        assert 'ValueError' in source2

    def test_getstatement(self):
        #print str(self.source)
        ass = str(self.source[1:])
        for i in range(1, 4):
            #print "trying start in line %r" % self.source[i]
            s = self.source.getstatement(i)
            #x = s.deindent()
            assert str(s) == ass

    def test_getstatementrange_triple_quoted(self):
        #print str(self.source)
        source = Source("""hello('''
        ''')""")
        s = source.getstatement(0)
        assert s == str(source)
        s = source.getstatement(1)
        assert s == str(source)

    @astonly
    def test_getstatementrange_within_constructs(self):
        source = Source("""\
            try:
                try:
                    raise ValueError
                except SomeThing:
                    pass
            finally:
                42
        """)
        assert len(source) == 7
        # check all lineno's that could occur in a traceback
        #assert source.getstatementrange(0) == (0, 7)
        #assert source.getstatementrange(1) == (1, 5)
        assert source.getstatementrange(2) == (2, 3)
        assert source.getstatementrange(3) == (3, 4)
        assert source.getstatementrange(4) == (4, 5)
        #assert source.getstatementrange(5) == (0, 7)
        assert source.getstatementrange(6) == (6, 7)

    def test_getstatementrange_bug(self):
        source = Source("""\
            try:
                x = (
                   y +
                   z)
            except:
                pass
        """)
        assert len(source) == 6
        assert source.getstatementrange(2) == (1, 4)

    def test_getstatementrange_bug2(self):
        source = Source("""\
            assert (
                33
                ==
                [
                  X(3,
                      b=1, c=2
                   ),
                ]
              )
        """)
        assert len(source) == 9
        assert source.getstatementrange(5) == (0, 9)

    def test_getstatementrange_ast_issue58(self):
        source = Source("""\

            def test_some():
                for a in [a for a in
                    CAUSE_ERROR]: pass

            x = 3
        """)
        assert getstatement(2, source).lines == source.lines[2:3]
        assert getstatement(3, source).lines == source.lines[3:4]

    def test_getstatementrange_out_of_bounds_py3(self):
        source = Source("if xxx:\n   from .collections import something")
        r = source.getstatementrange(1)
        assert r == (1, 2)

    def test_getstatementrange_with_syntaxerror_issue7(self):
        source = Source(":")
        py.test.raises(SyntaxError, lambda: source.getstatementrange(0))

    def test_compile_to_ast(self):
        import ast
        source = Source("x = 4")
        mod = source.compile(flag=ast.PyCF_ONLY_AST)
        assert isinstance(mod, ast.Module)
        compile(mod, "<filename>", "exec")

    def test_compile_and_getsource(self):
        co = self.source.compile()
        py.builtin.exec_(co, globals())
        f(7)
        excinfo = py.test.raises(AssertionError, "f(6)")
        frame = excinfo.traceback[-1].frame
        stmt = frame.code.fullsource.getstatement(frame.lineno)
        #print "block", str(block)
        assert str(stmt).strip().startswith('assert')

    def test_compilefuncs_and_path_sanity(self):
        def check(comp, name):
            co = comp(self.source, name)
            if not name:
                expected = "codegen %s:%d>" % (mypath, mylineno + 2 + 1)
            else:
                expected = "codegen %r %s:%d>" % (name, mypath,
                                                  mylineno + 2 + 1)
            fn = co.co_filename
            assert fn.endswith(expected)

        mycode = py.code.Code(self.test_compilefuncs_and_path_sanity)
        mylineno = mycode.firstlineno
        mypath = mycode.path

        for comp in py.code.compile, py.code.Source.compile:
            for name in '', None, 'my':
                yield check, comp, name

    def test_offsetless_synerr(self):
        py.test.raises(SyntaxError,
                       py.code.compile,
                       "lambda a,a: 0",
                       mode='eval')
Beispiel #31
0
 def test_compile_to_ast(self):
     import ast
     source = Source("x = 4")
     mod = source.compile(flag=ast.PyCF_ONLY_AST)
     assert isinstance(mod, ast.Module)
     compile(mod, "<filename>", "exec")
Beispiel #32
0
 def test_getstatementrange_with_syntaxerror_issue7(self):
     source = Source(":")
     py.test.raises(SyntaxError, lambda: source.getstatementrange(0))
def test_source_strips():
    source = Source("")
    assert source == Source()
    assert str(source) == ''
    assert source.strip() == source
Beispiel #34
0
def test_unicode():
    x = Source(unicode("4"))
    assert str(x) == "4"
Beispiel #35
0
 def test_getstatementrange_out_of_bounds_py3(self):
     source = Source("if xxx:\n   from .collections import something")
     r = source.getstatementrange(1)
     assert r == (1, 2)
def makefile(testdir, path, content):
    return testdir.tmpdir.ensure(*path).write('\n'.join(Source(content)))
def test_source_strip_multiline():
    source = Source()
    source.lines = ["", " hello", "  "]
    source2 = source.strip()
    assert source2.lines == [" hello"]
Beispiel #38
0
class TestSourceParsingAndCompiling:
    source = Source("""\
        def f(x):
            assert (x ==
                    3 +
                    4)
    """).strip()

    def test_compile(self):
        co = py.code.compile("x=3")
        exec co
        assert x == 3

    def test_compile_unicode(self):
        co = py.code.compile(unicode('u"\xc3\xa5"', 'utf8'), mode='eval')
        val = eval(co)
        assert isinstance(val, unicode)

    def test_compile_and_getsource_simple(self):
        co = py.code.compile("x=3")
        exec co
        source = py.code.Source(co)
        assert str(source) == "x=3"

    def test_getstatement(self):
        #print str(self.source)
        ass = str(self.source[1:])
        for i in range(1, 4):
            #print "trying start in line %r" % self.source[i]
            s = self.source.getstatement(i)
            #x = s.deindent()
            assert str(s) == ass

    def test_getstatementrange_within_constructs(self):
        source = Source("""\
            try:
                try: 
                    raise ValueError
                except SomeThing: 
                    pass 
            finally:
                42
        """)
        assert len(source) == 7
        assert source.getstatementrange(0) == (0, 7)
        assert source.getstatementrange(1) == (1, 5)
        assert source.getstatementrange(2) == (2, 3)
        assert source.getstatementrange(3) == (1, 5)
        assert source.getstatementrange(4) == (4, 5)
        assert source.getstatementrange(5) == (0, 7)
        assert source.getstatementrange(6) == (6, 7)

    def test_getstatementrange_bug(self):
        source = Source("""\
            try:
                x = (
                   y +
                   z)
            except:
                pass
        """)
        assert len(source) == 6
        assert source.getstatementrange(2) == (1, 4)

    @py.test.mark.xfail
    def test_getstatementrange_bug2(self):
        source = Source("""\
            assert (
                33
                ==
                [
                  X(3,
                      b=1, c=2
                   ),
                ]
              )
        """)
        assert len(source) == 9
        assert source.getstatementrange(5) == (0, 9)

    def test_compile_and_getsource(self):
        co = self.source.compile()
        exec co
        f(7)
        excinfo = py.test.raises(AssertionError, "f(6)")
        frame = excinfo.traceback[-1].frame
        stmt = frame.code.fullsource.getstatement(frame.lineno)
        #print "block", str(block)
        assert str(stmt).strip().startswith('assert')

    def test_compilefuncs_and_path_sanity(self):
        def check(comp, name):
            co = comp(self.source, name)
            if not name:
                expected = "<codegen %s:%d>" % (mypath, mylineno + 2 + 1)
            else:
                expected = "<codegen %r %s:%d>" % (name, mypath,
                                                   mylineno + 2 + 1)
            fn = co.co_filename
            assert fn == expected

        mycode = py.code.Code(self.test_compilefuncs_and_path_sanity)
        mylineno = mycode.firstlineno
        mypath = mycode.path

        for comp in py.code.compile, py.code.Source.compile:
            for name in '', None, 'my':
                yield check, comp, name

    def test_offsetless_synerr(self):
        py.test.raises(SyntaxError,
                       py.code.compile,
                       "lambda a,a: 0",
                       mode='eval')