File: test_codeop_jy.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (75 lines) | stat: -rw-r--r-- 2,288 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
 test compile. derived from test_codeop
"""
import codeop
import unittest
from test import test_support
from test.test_support import run_unittest


def compile_(source,name="<input>",symbol="single"):
    return compile(source,name,symbol)

class CompileTests(unittest.TestCase):

    def assertValid(self, str, symbol='single',values=None,value=None):
        '''succeed iff str is a valid piece of code'''
        code = compile_(str, "<input>", symbol)
        if values:
            d = {}
            exec code in d
            self.assertEquals(d,values)
        elif value is not None:
            self.assertEquals(eval(code,self.eval_d),value)
        else:
            self.assert_(code)

    def assertInvalid(self, str, symbol='single', is_syntax=1):
        '''succeed iff str is the start of an invalid piece of code'''
        try:
            compile_(str,symbol=symbol)
            self.fail("No exception thrown for invalid code")
        except SyntaxError:
            self.assert_(is_syntax)
        except OverflowError:
            self.assert_(not is_syntax)

    def test_valid(self):
        av = self.assertValid

        # Failed for Jython 2.5a2.  See http://bugs.jython.org/issue1116.
        # For some reason this tests fails when run from test_codeops#test_valid
        # when run from Jython (works in CPython).
        av("@a.b.c\ndef f():\n pass")

        # These tests pass on Jython, but fail on CPython.  Will need to investigate
        # to decide if we need to match CPython.
        av("\n\n")
        av("# a\n")
        av("\n\na = 1\n\n",values={'a':1})
        av("\n\nif 1: a=1\n\n",values={'a':1})
        av("def x():\n  pass\n ")
        av("def x():\n  pass\n  ")
        av("#a\n\n   \na=3\n",values={'a':3})
        av("def f():\n pass\n#foo")

    # these tests fail in Jython in test_codeop.py because PythonPartial.g
    # erroneously allows them through.  Once that is fixed, these tests
    # can be deleted.
    def test_invalid(self):
        ai = self.assertInvalid

        ai("del 1")
        ai("del ()")
        ai("del (1,)")
        ai("del [1]")
        ai("del '1'")
        ai("[i for i in range(10)] = (1, 2, 3)")


def test_main():
    run_unittest(CompileTests)


if __name__ == "__main__":
    test_main()