Commit 8a99b502 authored by Raymond Hettinger's avatar Raymond Hettinger

SF patch #736962. Converted test_compile to unittest format.

parent bcc651a1
test_compile
1 2
1 2
3 4
1 2 3
1 2 3
2 3 4
from test.test_support import verbose, TestFailed import unittest
import warnings
if verbose: import sys
print "Testing whether compiler catches assignment to __debug__" from test import test_support
try: class TestSpecifics(unittest.TestCase):
compile('__debug__ = 1', '?', 'single')
except SyntaxError: def test_debug_assignment(self):
pass # catch assignments to __debug__
self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single')
import __builtin__ import __builtin__
prev = __builtin__.__debug__ prev = __builtin__.__debug__
setattr(__builtin__, '__debug__', 'sure') setattr(__builtin__, '__debug__', 'sure')
setattr(__builtin__, '__debug__', prev) setattr(__builtin__, '__debug__', prev)
if verbose: def test_argument_handling(self):
print 'Running tests on argument handling' # detect duplicate positional and keyword arguments
self.assertRaises(SyntaxError, eval, 'lambda a,a:0')
try: self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0')
exec 'def f(a, a): pass' self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0')
raise TestFailed, "duplicate arguments" try:
except SyntaxError: exec 'def f(a, a): pass'
pass self.fail("duplicate arguments")
except SyntaxError:
if verbose: pass
print "compiling string with syntax error" try:
exec 'def f(a = 0, a = 1): pass'
try: self.fail("duplicate keyword arguments")
compile("1+*3", "filename", "exec") except SyntaxError:
except SyntaxError, detail: pass
if not detail.filename == "filename": try:
raise TestFailed, "expected 'filename', got %r" % detail.filename exec 'def f(a): global a; a = 1'
self.fail("variable is global and local")
try: except SyntaxError:
exec 'def f(a = 0, a = 1): pass' pass
raise TestFailed, "duplicate keyword arguments"
except SyntaxError: def test_syntax_error(self):
pass self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec")
try: def test_duplicate_global_local(self):
exec 'def f(a): global a; a = 1' try:
raise TestFailed, "variable is global and local" exec 'def f(a): global a; a = 1'
except SyntaxError: self.fail("variable is global and local")
pass except SyntaxError:
pass
if verbose:
print "testing complex args" def test_complex_args(self):
def comp_args((a, b)): def comp_args((a, b)):
print a,b return a,b
self.assertEqual(comp_args((1, 2)), (1, 2))
comp_args((1, 2))
def comp_args((a, b)=(3, 4)):
def comp_args((a, b)=(3, 4)): return a, b
print a, b self.assertEqual(comp_args((1, 2)), (1, 2))
self.assertEqual(comp_args(), (3, 4))
comp_args((1, 2))
comp_args() def comp_args(a, (b, c)):
return a, b, c
def comp_args(a, (b, c)): self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3))
print a, b, c
def comp_args(a=2, (b, c)=(3, 4)):
comp_args(1, (2, 3)) return a, b, c
self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3))
def comp_args(a=2, (b, c)=(3, 4)): self.assertEqual(comp_args(), (2, 3, 4))
print a, b, c
def test_argument_order(self):
comp_args(1, (2, 3)) try:
comp_args() exec 'def f(a=1, (b, c)): pass'
self.fail("non-default args after default")
try: except SyntaxError:
exec 'def f(a=1, (b, c)): pass' pass
raise TestFailed, "non-default args after default"
except SyntaxError: def test_float_literals(self):
pass # testing bad float literals
self.assertRaises(SyntaxError, eval, "2e")
if verbose: self.assertRaises(SyntaxError, eval, "2.0e+")
print "testing bad float literals" self.assertRaises(SyntaxError, eval, "1e-")
self.assertRaises(SyntaxError, eval, "3-4e/21")
def expect_error(s):
try: def test_indentation(self):
eval(s) # testing compile() of indented block w/o trailing newline"
raise TestFailed("%r accepted" % s) s = """
except SyntaxError:
pass
expect_error("2e")
expect_error("2.0e+")
expect_error("1e-")
expect_error("3-4e/21")
if verbose:
print "testing compile() of indented block w/o trailing newline"
s = """
if 1: if 1:
if 2: if 2:
pass""" pass"""
compile(s, "<string>", "exec") compile(s, "<string>", "exec")
def test_literals_with_leading_zeroes(self):
if verbose: for arg in ["077787", "0xj", "0x.", "0e", "090000000000000",
print "testing literals with leading zeroes" "080000000000000", "000000000000009", "000000000000008"]:
self.assertRaises(SyntaxError, eval, arg)
def expect_same(test_source, expected):
got = eval(test_source) self.assertEqual(eval("0777"), 511)
if got != expected: self.assertEqual(eval("0777L"), 511)
raise TestFailed("eval(%r) gave %r, but expected %r" % self.assertEqual(eval("000777"), 511)
(test_source, got, expected)) self.assertEqual(eval("0xff"), 255)
self.assertEqual(eval("0xffL"), 255)
expect_error("077787") self.assertEqual(eval("0XfF"), 255)
expect_error("0xj") self.assertEqual(eval("0777."), 777)
expect_error("0x.") self.assertEqual(eval("0777.0"), 777)
expect_error("0e") self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777)
expect_same("0777", 511) self.assertEqual(eval("0777e1"), 7770)
expect_same("0777L", 511) self.assertEqual(eval("0e0"), 0)
expect_same("000777", 511) self.assertEqual(eval("0000E-012"), 0)
expect_same("0xff", 255) self.assertEqual(eval("09.5"), 9.5)
expect_same("0xffL", 255) self.assertEqual(eval("0777j"), 777j)
expect_same("0XfF", 255) self.assertEqual(eval("00j"), 0j)
expect_same("0777.", 777) self.assertEqual(eval("00.0"), 0)
expect_same("0777.0", 777) self.assertEqual(eval("0e3"), 0)
expect_same("000000000000000000000000000000000000000000000000000777e0", 777) self.assertEqual(eval("090000000000000."), 90000000000000.)
expect_same("0777e1", 7770) self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.)
expect_same("0e0", 0) self.assertEqual(eval("090000000000000e0"), 90000000000000.)
expect_same("0000E-012", 0) self.assertEqual(eval("090000000000000e-0"), 90000000000000.)
expect_same("09.5", 9.5) self.assertEqual(eval("090000000000000j"), 90000000000000j)
expect_same("0777j", 777j) self.assertEqual(eval("000000000000007"), 7)
expect_same("00j", 0j) self.assertEqual(eval("000000000000008."), 8.)
expect_same("00.0", 0) self.assertEqual(eval("000000000000009."), 9.)
expect_same("0e3", 0)
expect_same("090000000000000.", 90000000000000.) def test_unary_minus(self):
expect_same("090000000000000.0000000000000000000000", 90000000000000.) # Verify treatment of unary minus on negative numbers SF bug #660455
expect_same("090000000000000e0", 90000000000000.) warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning)
expect_same("090000000000000e-0", 90000000000000.) warnings.filterwarnings("ignore", "hex.* of negative int", FutureWarning)
expect_same("090000000000000j", 90000000000000j) # XXX Of course the following test will have to be changed in Python 2.4
expect_error("090000000000000") # plain octal literal w/ decimal digit # This test is in a <string> so the filterwarnings() can affect it
expect_error("080000000000000") # plain octal literal w/ decimal digit all_one_bits = '0xffffffff'
expect_error("000000000000009") # plain octal literal w/ decimal digit if sys.maxint != 2147483647:
expect_error("000000000000008") # plain octal literal w/ decimal digit all_one_bits = '0xffffffffffffffff'
expect_same("000000000000007", 7) self.assertEqual(eval(all_one_bits), -1)
expect_same("000000000000008.", 8.) self.assertEqual(eval("-" + all_one_bits), 1)
expect_same("000000000000009.", 9.)
def test_sequence_unpacking_error(self):
# Verify treatment of unary minus on negative numbers SF bug #660455 # Verify sequence packing/unpacking with "or". SF bug #757818
import warnings i,j = (1, -1) or (-1, 1)
warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning) self.assertEqual(i, 1)
warnings.filterwarnings("ignore", "hex.* of negative int", FutureWarning) self.assertEqual(j, -1)
# XXX Of course the following test will have to be changed in Python 2.4
# This test is in a <string> so the filterwarnings() can affect it
import sys def test_main():
all_one_bits = '0xffffffff' test_support.run_unittest(TestSpecifics)
if sys.maxint != 2147483647:
all_one_bits = '0xffffffffffffffff' if __name__ == "__main__":
exec """ test_main()
expect_same(all_one_bits, -1)
expect_same("-" + all_one_bits, 1)
"""
# Verify sequence packing/unpacking with "or". SF bug #757818
i,j = (1, -1) or (-1, 1)
if i != 1 or j != -1:
raise TestFailed, "Sequence packing/unpacking"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment