0001 import unittest 0002 import warnings 0003 import sys 0004 from test import test_support 0005 0006 class TestSpecifics(unittest.TestCase): 0007 0008 def test_debug_assignment(self): 0009 # catch assignments to __debug__ 0010 self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single') 0011 import __builtin__ 0012 prev = __builtin__.__debug__ 0013 setattr(__builtin__, '__debug__', 'sure') 0014 setattr(__builtin__, '__debug__', prev) 0015 0016 def test_argument_handling(self): 0017 # detect duplicate positional and keyword arguments 0018 self.assertRaises(SyntaxError, eval, 'lambda a,a:0') 0019 self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0') 0020 self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0') 0021 try: 0022 exec 'def f(a, a): pass' 0023 self.fail("duplicate arguments") 0024 except SyntaxError: 0025 pass 0026 try: 0027 exec 'def f(a = 0, a = 1): pass' 0028 self.fail("duplicate keyword arguments") 0029 except SyntaxError: 0030 pass 0031 try: 0032 exec 'def f(a): global a; a = 1' 0033 self.fail("variable is global and local") 0034 except SyntaxError: 0035 pass 0036 0037 def test_syntax_error(self): 0038 self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec") 0039 0040 def test_duplicate_global_local(self): 0041 try: 0042 exec 'def f(a): global a; a = 1' 0043 self.fail("variable is global and local") 0044 except SyntaxError: 0045 pass 0046 0047 def test_exec_with_general_mapping_for_locals(self): 0048 0049 class M: 0050 "Test mapping interface versus possible calls from eval()." 0051 def __getitem__(self, key): 0052 if key == 'a': 0053 return 12 0054 raise KeyError 0055 def __setitem__(self, key, value): 0056 self.results = (key, value) 0057 def keys(self): 0058 return list('xyz') 0059 0060 m = M() 0061 g = globals() 0062 exec 'z = a' in g, m 0063 self.assertEqual(m.results, ('z', 12)) 0064 try: 0065 exec 'z = b' in g, m 0066 except NameError: 0067 pass 0068 else: 0069 self.fail('Did not detect a KeyError') 0070 exec 'z = dir()' in g, m 0071 self.assertEqual(m.results, ('z', list('xyz'))) 0072 exec 'z = globals()' in g, m 0073 self.assertEqual(m.results, ('z', g)) 0074 exec 'z = locals()' in g, m 0075 self.assertEqual(m.results, ('z', m)) 0076 try: 0077 exec 'z = b' in m 0078 except TypeError: 0079 pass 0080 else: 0081 self.fail('Did not validate globals as a real dict') 0082 0083 class A: 0084 "Non-mapping" 0085 pass 0086 m = A() 0087 try: 0088 exec 'z = a' in g, m 0089 except TypeError: 0090 pass 0091 else: 0092 self.fail('Did not validate locals as a mapping') 0093 0094 # Verify that dict subclasses work as well 0095 class D(dict): 0096 def __getitem__(self, key): 0097 if key == 'a': 0098 return 12 0099 return dict.__getitem__(self, key) 0100 d = D() 0101 exec 'z = a' in g, d 0102 self.assertEqual(d['z'], 12) 0103 0104 def test_complex_args(self): 0105 0106 def comp_args((a, b)): 0107 return a,b 0108 self.assertEqual(comp_args((1, 2)), (1, 2)) 0109 0110 def comp_args((a, b)=(3, 4)): 0111 return a, b 0112 self.assertEqual(comp_args((1, 2)), (1, 2)) 0113 self.assertEqual(comp_args(), (3, 4)) 0114 0115 def comp_args(a, (b, c)): 0116 return a, b, c 0117 self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3)) 0118 0119 def comp_args(a=2, (b, c)=(3, 4)): 0120 return a, b, c 0121 self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3)) 0122 self.assertEqual(comp_args(), (2, 3, 4)) 0123 0124 def test_argument_order(self): 0125 try: 0126 exec 'def f(a=1, (b, c)): pass' 0127 self.fail("non-default args after default") 0128 except SyntaxError: 0129 pass 0130 0131 def test_float_literals(self): 0132 # testing bad float literals 0133 self.assertRaises(SyntaxError, eval, "2e") 0134 self.assertRaises(SyntaxError, eval, "2.0e+") 0135 self.assertRaises(SyntaxError, eval, "1e-") 0136 self.assertRaises(SyntaxError, eval, "3-4e/21") 0137 0138 def test_indentation(self): 0139 # testing compile() of indented block w/o trailing newline" 0140 s = """ 0141 if 1: 0142 if 2: 0143 pass""" 0144 compile(s, "<string>", "exec") 0145 0146 def test_literals_with_leading_zeroes(self): 0147 for arg in ["077787", "0xj", "0x.", "0e", "090000000000000", 0148 "080000000000000", "000000000000009", "000000000000008"]: 0149 self.assertRaises(SyntaxError, eval, arg) 0150 0151 self.assertEqual(eval("0777"), 511) 0152 self.assertEqual(eval("0777L"), 511) 0153 self.assertEqual(eval("000777"), 511) 0154 self.assertEqual(eval("0xff"), 255) 0155 self.assertEqual(eval("0xffL"), 255) 0156 self.assertEqual(eval("0XfF"), 255) 0157 self.assertEqual(eval("0777."), 777) 0158 self.assertEqual(eval("0777.0"), 777) 0159 self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777) 0160 self.assertEqual(eval("0777e1"), 7770) 0161 self.assertEqual(eval("0e0"), 0) 0162 self.assertEqual(eval("0000E-012"), 0) 0163 self.assertEqual(eval("09.5"), 9.5) 0164 self.assertEqual(eval("0777j"), 777j) 0165 self.assertEqual(eval("00j"), 0j) 0166 self.assertEqual(eval("00.0"), 0) 0167 self.assertEqual(eval("0e3"), 0) 0168 self.assertEqual(eval("090000000000000."), 90000000000000.) 0169 self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.) 0170 self.assertEqual(eval("090000000000000e0"), 90000000000000.) 0171 self.assertEqual(eval("090000000000000e-0"), 90000000000000.) 0172 self.assertEqual(eval("090000000000000j"), 90000000000000j) 0173 self.assertEqual(eval("000000000000007"), 7) 0174 self.assertEqual(eval("000000000000008."), 8.) 0175 self.assertEqual(eval("000000000000009."), 9.) 0176 0177 def test_unary_minus(self): 0178 # Verify treatment of unary minus on negative numbers SF bug #660455 0179 if sys.maxint == 2147483647: 0180 # 32-bit machine 0181 all_one_bits = '0xffffffff' 0182 self.assertEqual(eval(all_one_bits), 4294967295L) 0183 self.assertEqual(eval("-" + all_one_bits), -4294967295L) 0184 elif sys.maxint == 9223372036854775807: 0185 # 64-bit machine 0186 all_one_bits = '0xffffffffffffffff' 0187 self.assertEqual(eval(all_one_bits), 18446744073709551615L) 0188 self.assertEqual(eval("-" + all_one_bits), -18446744073709551615L) 0189 else: 0190 self.fail("How many bits *does* this machine have???") 0191 0192 def test_sequence_unpacking_error(self): 0193 # Verify sequence packing/unpacking with "or". SF bug #757818 0194 i,j = (1, -1) or (-1, 1) 0195 self.assertEqual(i, 1) 0196 self.assertEqual(j, -1) 0197 0198 def test_none_assignment(self): 0199 stmts = [ 0200 'None = 0', 0201 'None += 0', 0202 '__builtins__.None = 0', 0203 'def None(): pass', 0204 'class None: pass', 0205 '(a, None) = 0, 0', 0206 'for None in range(10): pass', 0207 'def f(None): pass', 0208 ] 0209 for stmt in stmts: 0210 stmt += "\n" 0211 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single') 0212 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec') 0213 0214 def test_import(self): 0215 succeed = [ 0216 'import sys', 0217 'import os, sys', 0218 'from __future__ import nested_scopes, generators', 0219 'from __future__ import (nested_scopes,\ngenerators)', 0220 'from __future__ import (nested_scopes,\ngenerators,)', 0221 'from sys import stdin, stderr, stdout', 0222 'from sys import (stdin, stderr,\nstdout)', 0223 'from sys import (stdin, stderr,\nstdout,)', 0224 'from sys import (stdin\n, stderr, stdout)', 0225 'from sys import (stdin\n, stderr, stdout,)', 0226 'from sys import stdin as si, stdout as so, stderr as se', 0227 'from sys import (stdin as si, stdout as so, stderr as se)', 0228 'from sys import (stdin as si, stdout as so, stderr as se,)', 0229 ] 0230 fail = [ 0231 'import (os, sys)', 0232 'import (os), (sys)', 0233 'import ((os), (sys))', 0234 'import (sys', 0235 'import sys)', 0236 'import (os,)', 0237 'from (sys) import stdin', 0238 'from __future__ import (nested_scopes', 0239 'from __future__ import nested_scopes)', 0240 'from __future__ import nested_scopes,\ngenerators', 0241 'from sys import (stdin', 0242 'from sys import stdin)', 0243 'from sys import stdin, stdout,\nstderr', 0244 'from sys import stdin si', 0245 'from sys import stdin,' 0246 'from sys import (*)', 0247 'from sys import (stdin,, stdout, stderr)', 0248 'from sys import (stdin, stdout),', 0249 ] 0250 for stmt in succeed: 0251 compile(stmt, 'tmp', 'exec') 0252 for stmt in fail: 0253 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec') 0254 0255 def test_for_distinct_code_objects(self): 0256 # SF bug 1048870 0257 def f(): 0258 f1 = lambda x=1: x 0259 f2 = lambda x=2: x 0260 return f1, f2 0261 f1, f2 = f() 0262 self.assertNotEqual(id(f1.func_code), id(f2.func_code)) 0263 0264 def test_main(): 0265 test_support.run_unittest(TestSpecifics) 0266 0267 if __name__ == "__main__": 0268 test_main() 0269
Generated by PyXR 0.9.4