| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from test.test_support import run_unittest, check_syntax_error, \ |
| 5 |
check_py3k_warnings |
| 6 |
import unittest |
| 7 |
import sys |
| 8 |
import warnings |
| 9 |
|
| 10 |
from sys import * |
| 11 |
|
| 12 |
|
| 13 |
class TokenTests(unittest.TestCase): |
| 14 |
|
| 15 |
def test_backslash(self): |
| 16 |
|
| 17 |
x = 1 \ |
| 18 |
+ 1 |
| 19 |
self.assertEqual(x, 2, 'backslash for line continuation') |
| 20 |
|
| 21 |
|
| 22 |
x = 0 |
| 23 |
self.assertEqual(x, 0, 'backslash ending comment') |
| 24 |
|
| 25 |
def test_plain_integers(self): |
| 26 |
self.assertEqual(type(000), type(0)) |
| 27 |
self.assertEqual(0xff, 255) |
| 28 |
self.assertEqual(0377, 255) |
| 29 |
self.assertEqual(0o377, 255) |
| 30 |
self.assertEqual(2147483647, 017777777777) |
| 31 |
self.assertEqual(2147483647, 0o17777777777) |
| 32 |
self.assertEqual(0b1001, 9) |
| 33 |
|
| 34 |
self.assertRaises(SyntaxError, eval, "0x") |
| 35 |
from sys import maxint |
| 36 |
if maxint == 2147483647: |
| 37 |
self.assertEqual(-2147483647-1, -0o20000000000) |
| 38 |
|
| 39 |
self.assertTrue(037777777777 > 0) |
| 40 |
self.assertTrue(0o37777777777 > 0) |
| 41 |
self.assertTrue(0xffffffff > 0) |
| 42 |
self.assertTrue(0b1111111111111111111111111111111 > 0) |
| 43 |
for s in ('2147483648', '040000000000', '0o40000000000', |
| 44 |
'0x100000000', |
| 45 |
'0b10000000000000000000000000000000'): |
| 46 |
try: |
| 47 |
x = eval(s) |
| 48 |
except OverflowError: |
| 49 |
self.fail("OverflowError on huge integer literal %r" % s) |
| 50 |
elif maxint == 9223372036854775807: |
| 51 |
self.assertEqual(-9223372036854775807-1, -01000000000000000000000) |
| 52 |
self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000) |
| 53 |
self.assertTrue(01777777777777777777777 > 0) |
| 54 |
self.assertTrue(0o1777777777777777777777 > 0) |
| 55 |
self.assertTrue(0xffffffffffffffff > 0) |
| 56 |
self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0) |
| 57 |
for s in '9223372036854775808', '02000000000000000000000', \ |
| 58 |
'0o2000000000000000000000', \ |
| 59 |
'0x10000000000000000', \ |
| 60 |
'0b100000000000000000000000000000000000000000000000000000000000000': |
| 61 |
try: |
| 62 |
x = eval(s) |
| 63 |
except OverflowError: |
| 64 |
self.fail("OverflowError on huge integer literal %r" % s) |
| 65 |
else: |
| 66 |
self.fail('Weird maxint value %r' % maxint) |
| 67 |
|
| 68 |
def test_long_integers(self): |
| 69 |
x = 0L |
| 70 |
x = 0l |
| 71 |
x = 0xffffffffffffffffL |
| 72 |
x = 0xffffffffffffffffl |
| 73 |
x = 077777777777777777L |
| 74 |
x = 077777777777777777l |
| 75 |
x = 123456789012345678901234567890L |
| 76 |
x = 123456789012345678901234567890l |
| 77 |
|
| 78 |
def test_floats(self): |
| 79 |
x = 3.14 |
| 80 |
x = 314. |
| 81 |
x = 0.314 |
| 82 |
|
| 83 |
x = .314 |
| 84 |
x = 3e14 |
| 85 |
x = 3E14 |
| 86 |
x = 3e-14 |
| 87 |
x = 3e+14 |
| 88 |
x = 3.e14 |
| 89 |
x = .3e14 |
| 90 |
x = 3.1e4 |
| 91 |
|
| 92 |
def test_float_exponent_tokenization(self): |
| 93 |
|
| 94 |
self.assertEqual(1 if 1else 0, 1) |
| 95 |
self.assertEqual(1 if 0else 0, 0) |
| 96 |
self.assertRaises(SyntaxError, eval, "0 if 1Else 0") |
| 97 |
|
| 98 |
def test_string_literals(self): |
| 99 |
x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y) |
| 100 |
x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39) |
| 101 |
x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34) |
| 102 |
x = "doesn't \"shrink\" does it" |
| 103 |
y = 'doesn\'t "shrink" does it' |
| 104 |
self.assertTrue(len(x) == 24 and x == y) |
| 105 |
x = "does \"shrink\" doesn't it" |
| 106 |
y = 'does "shrink" doesn\'t it' |
| 107 |
self.assertTrue(len(x) == 24 and x == y) |
| 108 |
x = """ |
| 109 |
The "quick" |
| 110 |
brown fox |
| 111 |
jumps over |
| 112 |
the 'lazy' dog. |
| 113 |
""" |
| 114 |
y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |
| 115 |
self.assertEqual(x, y) |
| 116 |
y = ''' |
| 117 |
The "quick" |
| 118 |
brown fox |
| 119 |
jumps over |
| 120 |
the 'lazy' dog. |
| 121 |
''' |
| 122 |
self.assertEqual(x, y) |
| 123 |
y = "\n\ |
| 124 |
The \"quick\"\n\ |
| 125 |
brown fox\n\ |
| 126 |
jumps over\n\ |
| 127 |
the 'lazy' dog.\n\ |
| 128 |
" |
| 129 |
self.assertEqual(x, y) |
| 130 |
y = '\n\ |
| 131 |
The \"quick\"\n\ |
| 132 |
brown fox\n\ |
| 133 |
jumps over\n\ |
| 134 |
the \'lazy\' dog.\n\ |
| 135 |
' |
| 136 |
self.assertEqual(x, y) |
| 137 |
|
| 138 |
|
| 139 |
class GrammarTests(unittest.TestCase): |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
def test_eval_input(self): |
| 151 |
|
| 152 |
x = eval('1, 0 or 1') |
| 153 |
|
| 154 |
def test_funcdef(self): |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
def f1(): pass |
| 165 |
f1() |
| 166 |
f1(*()) |
| 167 |
f1(*(), **{}) |
| 168 |
def f2(one_argument): pass |
| 169 |
def f3(two, arguments): pass |
| 170 |
with check_py3k_warnings(('tuple parameter unpacking has been removed', |
| 171 |
SyntaxWarning)): |
| 172 |
exec('def f4(two, (compound, (argument, list))): pass') |
| 173 |
exec('def f5((compound, first), two): pass') |
| 174 |
self.assertEqual(f2.func_code.co_varnames, ('one_argument',)) |
| 175 |
self.assertEqual(f3.func_code.co_varnames, ('two', 'arguments')) |
| 176 |
if sys.platform.startswith('java'): |
| 177 |
self.assertEqual(f4.func_code.co_varnames, |
| 178 |
('two', '(compound, (argument, list))', 'compound', 'argument', |
| 179 |
'list',)) |
| 180 |
self.assertEqual(f5.func_code.co_varnames, |
| 181 |
('(compound, first)', 'two', 'compound', 'first')) |
| 182 |
else: |
| 183 |
self.assertEqual(f4.func_code.co_varnames, |
| 184 |
('two', '.1', 'compound', 'argument', 'list')) |
| 185 |
self.assertEqual(f5.func_code.co_varnames, |
| 186 |
('.0', 'two', 'compound', 'first')) |
| 187 |
def a1(one_arg,): pass |
| 188 |
def a2(two, args,): pass |
| 189 |
def v0(*rest): pass |
| 190 |
def v1(a, *rest): pass |
| 191 |
def v2(a, b, *rest): pass |
| 192 |
with check_py3k_warnings(('tuple parameter unpacking has been removed', |
| 193 |
SyntaxWarning)): |
| 194 |
exec('def v3(a, (b, c), *rest): return a, b, c, rest') |
| 195 |
|
| 196 |
f1() |
| 197 |
f2(1) |
| 198 |
f2(1,) |
| 199 |
f3(1, 2) |
| 200 |
f3(1, 2,) |
| 201 |
f4(1, (2, (3, 4))) |
| 202 |
v0() |
| 203 |
v0(1) |
| 204 |
v0(1,) |
| 205 |
v0(1,2) |
| 206 |
v0(1,2,3,4,5,6,7,8,9,0) |
| 207 |
v1(1) |
| 208 |
v1(1,) |
| 209 |
v1(1,2) |
| 210 |
v1(1,2,3) |
| 211 |
v1(1,2,3,4,5,6,7,8,9,0) |
| 212 |
v2(1,2) |
| 213 |
v2(1,2,3) |
| 214 |
v2(1,2,3,4) |
| 215 |
v2(1,2,3,4,5,6,7,8,9,0) |
| 216 |
v3(1,(2,3)) |
| 217 |
v3(1,(2,3),4) |
| 218 |
v3(1,(2,3),4,5,6,7,8,9,0) |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if sys.platform.startswith('java'): |
| 223 |
self.assertEqual(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c')) |
| 224 |
else: |
| 225 |
self.assertEqual(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c')) |
| 226 |
self.assertEqual(v3(1, (2, 3), 4), (1, 2, 3, (4,))) |
| 227 |
def d01(a=1): pass |
| 228 |
d01() |
| 229 |
d01(1) |
| 230 |
d01(*(1,)) |
| 231 |
d01(*[] or [2]) |
| 232 |
d01(**{'a':2}) |
| 233 |
d01(**{'a':2} or {}) |
| 234 |
def d11(a, b=1): pass |
| 235 |
d11(1) |
| 236 |
d11(1, 2) |
| 237 |
d11(1, **{'b':2}) |
| 238 |
def d21(a, b, c=1): pass |
| 239 |
d21(1, 2) |
| 240 |
d21(1, 2, 3) |
| 241 |
d21(*(1, 2, 3)) |
| 242 |
d21(1, *(2, 3)) |
| 243 |
d21(1, 2, *(3,)) |
| 244 |
d21(1, 2, **{'c':3}) |
| 245 |
def d02(a=1, b=2): pass |
| 246 |
d02() |
| 247 |
d02(1) |
| 248 |
d02(1, 2) |
| 249 |
d02(*(1, 2)) |
| 250 |
d02(1, *(2,)) |
| 251 |
d02(1, **{'b':2}) |
| 252 |
d02(**{'a': 1, 'b': 2}) |
| 253 |
def d12(a, b=1, c=2): pass |
| 254 |
d12(1) |
| 255 |
d12(1, 2) |
| 256 |
d12(1, 2, 3) |
| 257 |
def d22(a, b, c=1, d=2): pass |
| 258 |
d22(1, 2) |
| 259 |
d22(1, 2, 3) |
| 260 |
d22(1, 2, 3, 4) |
| 261 |
def d01v(a=1, *rest): pass |
| 262 |
d01v() |
| 263 |
d01v(1) |
| 264 |
d01v(1, 2) |
| 265 |
d01v(*(1, 2, 3, 4)) |
| 266 |
d01v(*(1,)) |
| 267 |
d01v(**{'a':2}) |
| 268 |
def d11v(a, b=1, *rest): pass |
| 269 |
d11v(1) |
| 270 |
d11v(1, 2) |
| 271 |
d11v(1, 2, 3) |
| 272 |
def d21v(a, b, c=1, *rest): pass |
| 273 |
d21v(1, 2) |
| 274 |
d21v(1, 2, 3) |
| 275 |
d21v(1, 2, 3, 4) |
| 276 |
d21v(*(1, 2, 3, 4)) |
| 277 |
d21v(1, 2, **{'c': 3}) |
| 278 |
def d02v(a=1, b=2, *rest): pass |
| 279 |
d02v() |
| 280 |
d02v(1) |
| 281 |
d02v(1, 2) |
| 282 |
d02v(1, 2, 3) |
| 283 |
d02v(1, *(2, 3, 4)) |
| 284 |
d02v(**{'a': 1, 'b': 2}) |
| 285 |
def d12v(a, b=1, c=2, *rest): pass |
| 286 |
d12v(1) |
| 287 |
d12v(1, 2) |
| 288 |
d12v(1, 2, 3) |
| 289 |
d12v(1, 2, 3, 4) |
| 290 |
d12v(*(1, 2, 3, 4)) |
| 291 |
d12v(1, 2, *(3, 4, 5)) |
| 292 |
d12v(1, *(2,), **{'c': 3}) |
| 293 |
def d22v(a, b, c=1, d=2, *rest): pass |
| 294 |
d22v(1, 2) |
| 295 |
d22v(1, 2, 3) |
| 296 |
d22v(1, 2, 3, 4) |
| 297 |
d22v(1, 2, 3, 4, 5) |
| 298 |
d22v(*(1, 2, 3, 4)) |
| 299 |
d22v(1, 2, *(3, 4, 5)) |
| 300 |
d22v(1, *(2, 3), **{'d': 4}) |
| 301 |
with check_py3k_warnings(('parenthesized argument names are invalid', |
| 302 |
SyntaxWarning)): |
| 303 |
exec('def d31v((x)): pass') |
| 304 |
with check_py3k_warnings(('tuple parameter unpacking has been removed', |
| 305 |
SyntaxWarning)): |
| 306 |
exec('def d32v((x,)): pass') |
| 307 |
d31v(1) |
| 308 |
d32v((1,)) |
| 309 |
|
| 310 |
|
| 311 |
def f(*args, **kwargs): |
| 312 |
return args, kwargs |
| 313 |
self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), |
| 314 |
{'x':2, 'y':5})) |
| 315 |
self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)") |
| 316 |
self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") |
| 317 |
self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}), |
| 318 |
((), {'eggs':'scrambled', 'spam':'fried'})) |
| 319 |
self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}), |
| 320 |
((), {'eggs':'scrambled', 'spam':'fried'})) |
| 321 |
|
| 322 |
|
| 323 |
check_syntax_error(self, "f(*g(1=2))") |
| 324 |
check_syntax_error(self, "f(**g(1=2))") |
| 325 |
|
| 326 |
|
| 327 |
def f(a,): pass |
| 328 |
|
| 329 |
def test_lambdef(self): |
| 330 |
|
| 331 |
l1 = lambda : 0 |
| 332 |
self.assertEqual(l1(), 0) |
| 333 |
l2 = lambda : a[d] |
| 334 |
l3 = lambda : [2 < x for x in [-1, 3, 0L]] |
| 335 |
self.assertEqual(l3(), [0, 1, 0]) |
| 336 |
l4 = lambda x = lambda y = lambda z=1 : z : y() : x() |
| 337 |
self.assertEqual(l4(), 1) |
| 338 |
l5 = lambda x, y, z=2: x + y + z |
| 339 |
self.assertEqual(l5(1, 2), 5) |
| 340 |
self.assertEqual(l5(1, 2, 3), 6) |
| 341 |
check_syntax_error(self, "lambda x: x = 2") |
| 342 |
with check_py3k_warnings(('tuple parameter unpacking has been removed', |
| 343 |
SyntaxWarning)): |
| 344 |
check_syntax_error(self, "lambda (None,): None") |
| 345 |
|
| 346 |
|
| 347 |
l10 = lambda a,: 0 |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
def test_simple_stmt(self): |
| 354 |
|
| 355 |
x = 1; pass; del x |
| 356 |
def foo(): |
| 357 |
|
| 358 |
x = 1; pass; del x; |
| 359 |
foo() |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
def test_expr_stmt(self): |
| 365 |
|
| 366 |
1 |
| 367 |
1, 2, 3 |
| 368 |
x = 1 |
| 369 |
x = 1, 2, 3 |
| 370 |
x = y = z = 1, 2, 3 |
| 371 |
x, y, z = 1, 2, 3 |
| 372 |
abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |
| 373 |
|
| 374 |
check_syntax_error(self, "x + 1 = 1") |
| 375 |
check_syntax_error(self, "a + 1 = b + 2") |
| 376 |
|
| 377 |
def test_print_stmt(self): |
| 378 |
|
| 379 |
import StringIO |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
save_stdout = sys.stdout |
| 384 |
sys.stdout = StringIO.StringIO() |
| 385 |
|
| 386 |
print 1, 2, 3 |
| 387 |
print 1, 2, 3, |
| 388 |
print |
| 389 |
print 0 or 1, 0 or 1, |
| 390 |
print 0 or 1 |
| 391 |
|
| 392 |
|
| 393 |
print >> sys.stdout, 1, 2, 3 |
| 394 |
print >> sys.stdout, 1, 2, 3, |
| 395 |
print >> sys.stdout |
| 396 |
print >> sys.stdout, 0 or 1, 0 or 1, |
| 397 |
print >> sys.stdout, 0 or 1 |
| 398 |
|
| 399 |
|
| 400 |
class Gulp: |
| 401 |
def write(self, msg): pass |
| 402 |
|
| 403 |
gulp = Gulp() |
| 404 |
print >> gulp, 1, 2, 3 |
| 405 |
print >> gulp, 1, 2, 3, |
| 406 |
print >> gulp |
| 407 |
print >> gulp, 0 or 1, 0 or 1, |
| 408 |
print >> gulp, 0 or 1 |
| 409 |
|
| 410 |
|
| 411 |
def driver(): |
| 412 |
oldstdout = sys.stdout |
| 413 |
sys.stdout = Gulp() |
| 414 |
try: |
| 415 |
tellme(Gulp()) |
| 416 |
tellme() |
| 417 |
finally: |
| 418 |
sys.stdout = oldstdout |
| 419 |
|
| 420 |
|
| 421 |
def tellme(file=sys.stdout): |
| 422 |
print >> file, 'hello world' |
| 423 |
|
| 424 |
driver() |
| 425 |
|
| 426 |
|
| 427 |
def tellme(file=None): |
| 428 |
print >> file, 'goodbye universe' |
| 429 |
|
| 430 |
driver() |
| 431 |
|
| 432 |
self.assertEqual(sys.stdout.getvalue(), '''\ |
| 433 |
1 2 3 |
| 434 |
1 2 3 |
| 435 |
1 1 1 |
| 436 |
1 2 3 |
| 437 |
1 2 3 |
| 438 |
1 1 1 |
| 439 |
hello world |
| 440 |
''') |
| 441 |
sys.stdout = save_stdout |
| 442 |
|
| 443 |
|
| 444 |
check_syntax_error(self, 'print ,') |
| 445 |
check_syntax_error(self, 'print >> x,') |
| 446 |
|
| 447 |
def test_del_stmt(self): |
| 448 |
|
| 449 |
abc = [1,2,3] |
| 450 |
x, y, z = abc |
| 451 |
xyz = x, y, z |
| 452 |
|
| 453 |
del abc |
| 454 |
del x, y, (z, xyz) |
| 455 |
|
| 456 |
def test_pass_stmt(self): |
| 457 |
|
| 458 |
pass |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
def test_break_stmt(self): |
| 464 |
|
| 465 |
while 1: break |
| 466 |
|
| 467 |
def test_continue_stmt(self): |
| 468 |
|
| 469 |
i = 1 |
| 470 |
while i: i = 0; continue |
| 471 |
|
| 472 |
msg = "" |
| 473 |
while not msg: |
| 474 |
msg = "ok" |
| 475 |
try: |
| 476 |
continue |
| 477 |
msg = "continue failed to continue inside try" |
| 478 |
except: |
| 479 |
msg = "continue inside try called except block" |
| 480 |
if msg != "ok": |
| 481 |
self.fail(msg) |
| 482 |
|
| 483 |
msg = "" |
| 484 |
while not msg: |
| 485 |
msg = "finally block not called" |
| 486 |
try: |
| 487 |
continue |
| 488 |
finally: |
| 489 |
msg = "ok" |
| 490 |
if msg != "ok": |
| 491 |
self.fail(msg) |
| 492 |
|
| 493 |
def test_break_continue_loop(self): |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
def test_inner(extra_burning_oil = 1, count=0): |
| 504 |
big_hippo = 2 |
| 505 |
while big_hippo: |
| 506 |
count += 1 |
| 507 |
try: |
| 508 |
if extra_burning_oil and big_hippo == 1: |
| 509 |
extra_burning_oil -= 1 |
| 510 |
break |
| 511 |
big_hippo -= 1 |
| 512 |
continue |
| 513 |
except: |
| 514 |
raise |
| 515 |
if count > 2 or big_hippo != 1: |
| 516 |
self.fail("continue then break in try/except in loop broken!") |
| 517 |
test_inner() |
| 518 |
|
| 519 |
def test_return(self): |
| 520 |
|
| 521 |
def g1(): return |
| 522 |
def g2(): return 1 |
| 523 |
g1() |
| 524 |
x = g2() |
| 525 |
check_syntax_error(self, "class foo:return 1") |
| 526 |
|
| 527 |
def test_break_in_finally(self): |
| 528 |
count = 0 |
| 529 |
while count < 2: |
| 530 |
count += 1 |
| 531 |
try: |
| 532 |
pass |
| 533 |
finally: |
| 534 |
break |
| 535 |
self.assertEqual(count, 1) |
| 536 |
|
| 537 |
count = 0 |
| 538 |
while count < 2: |
| 539 |
count += 1 |
| 540 |
try: |
| 541 |
continue |
| 542 |
finally: |
| 543 |
break |
| 544 |
self.assertEqual(count, 1) |
| 545 |
|
| 546 |
count = 0 |
| 547 |
while count < 2: |
| 548 |
count += 1 |
| 549 |
try: |
| 550 |
1.0/0.0 |
| 551 |
finally: |
| 552 |
break |
| 553 |
self.assertEqual(count, 1) |
| 554 |
|
| 555 |
for count in [0, 1]: |
| 556 |
self.assertEqual(count, 0) |
| 557 |
try: |
| 558 |
pass |
| 559 |
finally: |
| 560 |
break |
| 561 |
self.assertEqual(count, 0) |
| 562 |
|
| 563 |
for count in [0, 1]: |
| 564 |
self.assertEqual(count, 0) |
| 565 |
try: |
| 566 |
continue |
| 567 |
finally: |
| 568 |
break |
| 569 |
self.assertEqual(count, 0) |
| 570 |
|
| 571 |
for count in [0, 1]: |
| 572 |
self.assertEqual(count, 0) |
| 573 |
try: |
| 574 |
1.0/0.0 |
| 575 |
finally: |
| 576 |
break |
| 577 |
self.assertEqual(count, 0) |
| 578 |
|
| 579 |
def test_return_in_finally(self): |
| 580 |
def g1(): |
| 581 |
try: |
| 582 |
pass |
| 583 |
finally: |
| 584 |
return 1 |
| 585 |
self.assertEqual(g1(), 1) |
| 586 |
|
| 587 |
def g2(): |
| 588 |
try: |
| 589 |
return 2 |
| 590 |
finally: |
| 591 |
return 3 |
| 592 |
self.assertEqual(g2(), 3) |
| 593 |
|
| 594 |
def g3(): |
| 595 |
try: |
| 596 |
1.0/0.0 |
| 597 |
finally: |
| 598 |
return 4 |
| 599 |
self.assertEqual(g3(), 4) |
| 600 |
|
| 601 |
def test_yield(self): |
| 602 |
|
| 603 |
def g(): yield 1 |
| 604 |
|
| 605 |
def g(): x = yield 1 |
| 606 |
|
| 607 |
def g(): yield 1, 1 |
| 608 |
def g(): x = yield 1, 1 |
| 609 |
|
| 610 |
def g(): 1, (yield 1) |
| 611 |
check_syntax_error(self, "def g(): 1, yield 1") |
| 612 |
|
| 613 |
def g(): f((yield 1)) |
| 614 |
def g(): f((yield 1), 1) |
| 615 |
check_syntax_error(self, "def g(): f(yield 1)") |
| 616 |
check_syntax_error(self, "def g(): f(yield 1, 1)") |
| 617 |
|
| 618 |
check_syntax_error(self, "yield") |
| 619 |
|
| 620 |
check_syntax_error(self, "class foo:yield 1") |
| 621 |
|
| 622 |
check_syntax_error(self, "def g(a:(yield)): pass") |
| 623 |
|
| 624 |
def test_yield_in_comprehensions(self): |
| 625 |
|
| 626 |
def g(): [x for x in [(yield 1)]] |
| 627 |
|
| 628 |
def check(code, warntext): |
| 629 |
with check_py3k_warnings((warntext, DeprecationWarning)): |
| 630 |
compile(code, '<test string>', 'exec') |
| 631 |
if sys.py3kwarning: |
| 632 |
with warnings.catch_warnings(): |
| 633 |
warnings.filterwarnings('error', category=DeprecationWarning) |
| 634 |
with self.assertRaises(SyntaxError) as cm: |
| 635 |
compile(code, '<test string>', 'exec') |
| 636 |
self.assertIn(warntext, str(cm.exception)) |
| 637 |
|
| 638 |
check("def g(): [(yield x) for x in ()]", |
| 639 |
"'yield' inside list comprehension") |
| 640 |
check("def g(): [x for x in () if not (yield x)]", |
| 641 |
"'yield' inside list comprehension") |
| 642 |
check("def g(): [y for x in () for y in [(yield x)]]", |
| 643 |
"'yield' inside list comprehension") |
| 644 |
check("def g(): {(yield x) for x in ()}", |
| 645 |
"'yield' inside set comprehension") |
| 646 |
check("def g(): {(yield x): x for x in ()}", |
| 647 |
"'yield' inside dict comprehension") |
| 648 |
check("def g(): {x: (yield x) for x in ()}", |
| 649 |
"'yield' inside dict comprehension") |
| 650 |
check("def g(): ((yield x) for x in ())", |
| 651 |
"'yield' inside generator expression") |
| 652 |
with check_py3k_warnings(("'yield' inside list comprehension", |
| 653 |
DeprecationWarning)): |
| 654 |
check_syntax_error(self, "class C: [(yield x) for x in ()]") |
| 655 |
check("class C: ((yield x) for x in ())", |
| 656 |
"'yield' inside generator expression") |
| 657 |
with check_py3k_warnings(("'yield' inside list comprehension", |
| 658 |
DeprecationWarning)): |
| 659 |
check_syntax_error(self, "[(yield x) for x in ()]") |
| 660 |
check("((yield x) for x in ())", |
| 661 |
"'yield' inside generator expression") |
| 662 |
|
| 663 |
def test_raise(self): |
| 664 |
|
| 665 |
try: raise RuntimeError, 'just testing' |
| 666 |
except RuntimeError: pass |
| 667 |
try: raise KeyboardInterrupt |
| 668 |
except KeyboardInterrupt: pass |
| 669 |
|
| 670 |
def test_import(self): |
| 671 |
|
| 672 |
import sys |
| 673 |
import time, sys |
| 674 |
|
| 675 |
from time import time |
| 676 |
from time import (time) |
| 677 |
|
| 678 |
|
| 679 |
from sys import path, argv |
| 680 |
from sys import (path, argv) |
| 681 |
from sys import (path, argv,) |
| 682 |
|
| 683 |
def test_global(self): |
| 684 |
|
| 685 |
global a |
| 686 |
global a, b |
| 687 |
global one, two, three, four, five, six, seven, eight, nine, ten |
| 688 |
|
| 689 |
def test_exec(self): |
| 690 |
|
| 691 |
z = None |
| 692 |
del z |
| 693 |
exec 'z=1+1\n' |
| 694 |
if z != 2: self.fail('exec \'z=1+1\'\\n') |
| 695 |
del z |
| 696 |
exec 'z=1+1' |
| 697 |
if z != 2: self.fail('exec \'z=1+1\'') |
| 698 |
z = None |
| 699 |
del z |
| 700 |
import types |
| 701 |
if hasattr(types, "UnicodeType"): |
| 702 |
exec r"""if 1: |
| 703 |
exec u'z=1+1\n' |
| 704 |
if z != 2: self.fail('exec u\'z=1+1\'\\n') |
| 705 |
del z |
| 706 |
exec u'z=1+1' |
| 707 |
if z != 2: self.fail('exec u\'z=1+1\'')""" |
| 708 |
g = {} |
| 709 |
exec 'z = 1' in g |
| 710 |
if '__builtins__' in g: del g['__builtins__'] |
| 711 |
if g != {'z': 1}: self.fail('exec \'z = 1\' in g') |
| 712 |
g = {} |
| 713 |
l = {} |
| 714 |
|
| 715 |
exec 'global a; a = 1; b = 2' in g, l |
| 716 |
if '__builtins__' in g: del g['__builtins__'] |
| 717 |
if '__builtins__' in l: del l['__builtins__'] |
| 718 |
if (g, l) != ({'a':1}, {'b':2}): |
| 719 |
self.fail('exec ... in g (%s), l (%s)' %(g,l)) |
| 720 |
|
| 721 |
def test_assert(self): |
| 722 |
|
| 723 |
assert 1 |
| 724 |
assert 1, 1 |
| 725 |
assert lambda x:x |
| 726 |
assert 1, lambda x:x+1 |
| 727 |
|
| 728 |
try: |
| 729 |
assert True |
| 730 |
except AssertionError as e: |
| 731 |
self.fail("'assert True' should not have raised an AssertionError") |
| 732 |
|
| 733 |
try: |
| 734 |
assert True, 'this should always pass' |
| 735 |
except AssertionError as e: |
| 736 |
self.fail("'assert True, msg' should not have " |
| 737 |
"raised an AssertionError") |
| 738 |
|
| 739 |
|
| 740 |
@unittest.skipUnless(__debug__, "Won't work if __debug__ is False") |
| 741 |
def testAssert2(self): |
| 742 |
try: |
| 743 |
assert 0, "msg" |
| 744 |
except AssertionError, e: |
| 745 |
self.assertEqual(e.args[0], "msg") |
| 746 |
else: |
| 747 |
self.fail("AssertionError not raised by assert 0") |
| 748 |
|
| 749 |
try: |
| 750 |
assert False |
| 751 |
except AssertionError as e: |
| 752 |
self.assertEqual(len(e.args), 0) |
| 753 |
else: |
| 754 |
self.fail("AssertionError not raised by 'assert False'") |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
def test_if(self): |
| 761 |
|
| 762 |
if 1: pass |
| 763 |
if 1: pass |
| 764 |
else: pass |
| 765 |
if 0: pass |
| 766 |
elif 0: pass |
| 767 |
if 0: pass |
| 768 |
elif 0: pass |
| 769 |
elif 0: pass |
| 770 |
elif 0: pass |
| 771 |
else: pass |
| 772 |
|
| 773 |
def test_while(self): |
| 774 |
|
| 775 |
while 0: pass |
| 776 |
while 0: pass |
| 777 |
else: pass |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
x = 0 |
| 782 |
while 0: |
| 783 |
x = 1 |
| 784 |
else: |
| 785 |
x = 2 |
| 786 |
self.assertEqual(x, 2) |
| 787 |
|
| 788 |
def test_for(self): |
| 789 |
|
| 790 |
for i in 1, 2, 3: pass |
| 791 |
for i, j, k in (): pass |
| 792 |
else: pass |
| 793 |
class Squares: |
| 794 |
def __init__(self, max): |
| 795 |
self.max = max |
| 796 |
self.sofar = [] |
| 797 |
def __len__(self): return len(self.sofar) |
| 798 |
def __getitem__(self, i): |
| 799 |
if not 0 <= i < self.max: raise IndexError |
| 800 |
n = len(self.sofar) |
| 801 |
while n <= i: |
| 802 |
self.sofar.append(n*n) |
| 803 |
n = n+1 |
| 804 |
return self.sofar[i] |
| 805 |
n = 0 |
| 806 |
for x in Squares(10): n = n+x |
| 807 |
if n != 285: |
| 808 |
self.fail('for over growing sequence') |
| 809 |
|
| 810 |
result = [] |
| 811 |
for x, in [(1,), (2,), (3,)]: |
| 812 |
result.append(x) |
| 813 |
self.assertEqual(result, [1, 2, 3]) |
| 814 |
|
| 815 |
def test_try(self): |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
try: |
| 820 |
1/0.0 |
| 821 |
except ZeroDivisionError: |
| 822 |
pass |
| 823 |
else: |
| 824 |
pass |
| 825 |
try: 1/0.0 |
| 826 |
except EOFError: pass |
| 827 |
except TypeError as msg: pass |
| 828 |
except RuntimeError, msg: pass |
| 829 |
except: pass |
| 830 |
else: pass |
| 831 |
try: 1/0.0 |
| 832 |
except (EOFError, TypeError, ZeroDivisionError): pass |
| 833 |
try: 1/0.0 |
| 834 |
except (EOFError, TypeError, ZeroDivisionError), msg: pass |
| 835 |
try: pass |
| 836 |
finally: pass |
| 837 |
|
| 838 |
def test_suite(self): |
| 839 |
|
| 840 |
if 1: pass |
| 841 |
if 1: |
| 842 |
pass |
| 843 |
if 1: |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
pass |
| 848 |
pass |
| 849 |
|
| 850 |
pass |
| 851 |
|
| 852 |
|
| 853 |
def test_test(self): |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
if not 1: pass |
| 858 |
if 1 and 1: pass |
| 859 |
if 1 or 1: pass |
| 860 |
if not not not 1: pass |
| 861 |
if not 1 and 1 and 1: pass |
| 862 |
if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |
| 863 |
|
| 864 |
def test_comparison(self): |
| 865 |
|
| 866 |
|
| 867 |
if 1: pass |
| 868 |
x = (1 == 1) |
| 869 |
if 1 == 1: pass |
| 870 |
if 1 != 1: pass |
| 871 |
if 1 < 1: pass |
| 872 |
if 1 > 1: pass |
| 873 |
if 1 <= 1: pass |
| 874 |
if 1 >= 1: pass |
| 875 |
if 1 is 1: pass |
| 876 |
if 1 is not 1: pass |
| 877 |
if 1 in (): pass |
| 878 |
if 1 not in (): pass |
| 879 |
if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass |
| 880 |
with check_py3k_warnings(('<> not supported in 3.x; use !=', |
| 881 |
DeprecationWarning)): |
| 882 |
if eval('1 <> 1'): pass |
| 883 |
with check_py3k_warnings(('<> not supported in 3.x; use !=', |
| 884 |
DeprecationWarning)): |
| 885 |
if eval('1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1'): pass |
| 886 |
if sys.py3kwarning: |
| 887 |
with warnings.catch_warnings(): |
| 888 |
warnings.filterwarnings('error', category=DeprecationWarning) |
| 889 |
with self.assertRaises(DeprecationWarning) as cm: |
| 890 |
compile('1 <> 1', '<test string>', 'eval') |
| 891 |
self.assertIn('<> not supported in 3.x; use !=', |
| 892 |
str(cm.exception)) |
| 893 |
|
| 894 |
def test_binary_mask_ops(self): |
| 895 |
x = 1 & 1 |
| 896 |
x = 1 ^ 1 |
| 897 |
x = 1 | 1 |
| 898 |
|
| 899 |
def test_shift_ops(self): |
| 900 |
x = 1 << 1 |
| 901 |
x = 1 >> 1 |
| 902 |
x = 1 << 1 >> 1 |
| 903 |
|
| 904 |
def test_additive_ops(self): |
| 905 |
x = 1 |
| 906 |
x = 1 + 1 |
| 907 |
x = 1 - 1 - 1 |
| 908 |
x = 1 - 1 + 1 - 1 + 1 |
| 909 |
|
| 910 |
def test_multiplicative_ops(self): |
| 911 |
x = 1 * 1 |
| 912 |
with check_py3k_warnings(('classic int division', DeprecationWarning)): |
| 913 |
x = 1 / 1 |
| 914 |
x = 1 / 1.0 |
| 915 |
x = 1 % 1 |
| 916 |
with check_py3k_warnings(('classic int division', DeprecationWarning)): |
| 917 |
x = 1 / 1 * 1 % 1 |
| 918 |
x = 1 / 1.0 * 1 % 1 |
| 919 |
|
| 920 |
def test_unary_ops(self): |
| 921 |
x = +1 |
| 922 |
x = -1 |
| 923 |
x = ~1 |
| 924 |
x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
| 925 |
with check_py3k_warnings(('classic int division', DeprecationWarning)): |
| 926 |
x = -1*1/1 + 1*1 - ---1*1 |
| 927 |
x = -1*1/1.0 + 1*1 - ---1*1 |
| 928 |
|
| 929 |
def test_selectors(self): |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
import sys, time |
| 934 |
c = sys.path[0] |
| 935 |
x = time.time() |
| 936 |
x = sys.modules['time'].time() |
| 937 |
a = '01234' |
| 938 |
c = a[0] |
| 939 |
c = a[-1] |
| 940 |
s = a[0:5] |
| 941 |
s = a[:5] |
| 942 |
s = a[0:] |
| 943 |
s = a[:] |
| 944 |
s = a[-5:] |
| 945 |
s = a[:-1] |
| 946 |
s = a[-4:-3] |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
d = {} |
| 951 |
d[1] = 1 |
| 952 |
d[1,] = 2 |
| 953 |
d[1,2] = 3 |
| 954 |
d[1,2,3] = 4 |
| 955 |
L = list(d) |
| 956 |
L.sort(key=lambda x: (type(x).__name__, x)) |
| 957 |
self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') |
| 958 |
|
| 959 |
def test_atoms(self): |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
x = (1) |
| 964 |
x = (1 or 2 or 3) |
| 965 |
x = (1 or 2 or 3, 2, 3) |
| 966 |
|
| 967 |
x = [] |
| 968 |
x = [1] |
| 969 |
x = [1 or 2 or 3] |
| 970 |
x = [1 or 2 or 3, 2, 3] |
| 971 |
x = [] |
| 972 |
|
| 973 |
x = {} |
| 974 |
x = {'one': 1} |
| 975 |
x = {'one': 1,} |
| 976 |
x = {'one' or 'two': 1 or 2} |
| 977 |
x = {'one': 1, 'two': 2} |
| 978 |
x = {'one': 1, 'two': 2,} |
| 979 |
x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |
| 980 |
|
| 981 |
x = {'one'} |
| 982 |
x = {'one', 1,} |
| 983 |
x = {'one', 'two', 'three'} |
| 984 |
x = {2, 3, 4,} |
| 985 |
|
| 986 |
with check_py3k_warnings(('backquote not supported', SyntaxWarning)): |
| 987 |
x = eval('`x`') |
| 988 |
x = eval('`1 or 2 or 3`') |
| 989 |
self.assertEqual(eval('`1,2`'), '(1, 2)') |
| 990 |
|
| 991 |
x = x |
| 992 |
x = 'x' |
| 993 |
x = 123 |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
def test_classdef(self): |
| 1000 |
|
| 1001 |
class B: pass |
| 1002 |
class B2(): pass |
| 1003 |
class C1(B): pass |
| 1004 |
class C2(B): pass |
| 1005 |
class D(C1, C2, B): pass |
| 1006 |
class C: |
| 1007 |
def meth1(self): pass |
| 1008 |
def meth2(self, arg): pass |
| 1009 |
def meth3(self, a1, a2): pass |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
def class_decorator(x): |
| 1015 |
x.decorated = True |
| 1016 |
return x |
| 1017 |
@class_decorator |
| 1018 |
class G: |
| 1019 |
pass |
| 1020 |
self.assertEqual(G.decorated, True) |
| 1021 |
|
| 1022 |
def test_dictcomps(self): |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
nums = [1, 2, 3] |
| 1027 |
self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) |
| 1028 |
|
| 1029 |
def test_listcomps(self): |
| 1030 |
|
| 1031 |
nums = [1, 2, 3, 4, 5] |
| 1032 |
strs = ["Apple", "Banana", "Coconut"] |
| 1033 |
spcs = [" Apple", " Banana ", "Coco nut "] |
| 1034 |
|
| 1035 |
self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) |
| 1036 |
self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) |
| 1037 |
self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) |
| 1038 |
self.assertEqual([(i, s) for i in nums for s in strs], |
| 1039 |
[(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), |
| 1040 |
(2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), |
| 1041 |
(3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), |
| 1042 |
(4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), |
| 1043 |
(5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) |
| 1044 |
self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], |
| 1045 |
[(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), |
| 1046 |
(3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), |
| 1047 |
(5, 'Banana'), (5, 'Coconut')]) |
| 1048 |
self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], |
| 1049 |
[[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) |
| 1050 |
|
| 1051 |
def test_in_func(l): |
| 1052 |
return [0 < x < 3 for x in l if x > 2] |
| 1053 |
|
| 1054 |
self.assertEqual(test_in_func(nums), [False, False, False]) |
| 1055 |
|
| 1056 |
def test_nested_front(): |
| 1057 |
self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], |
| 1058 |
[[1, 2], [3, 4], [5, 6]]) |
| 1059 |
|
| 1060 |
test_nested_front() |
| 1061 |
|
| 1062 |
check_syntax_error(self, "[i, s for i in nums for s in strs]") |
| 1063 |
check_syntax_error(self, "[x if y]") |
| 1064 |
|
| 1065 |
suppliers = [ |
| 1066 |
(1, "Boeing"), |
| 1067 |
(2, "Ford"), |
| 1068 |
(3, "Macdonalds") |
| 1069 |
] |
| 1070 |
|
| 1071 |
parts = [ |
| 1072 |
(10, "Airliner"), |
| 1073 |
(20, "Engine"), |
| 1074 |
(30, "Cheeseburger") |
| 1075 |
] |
| 1076 |
|
| 1077 |
suppart = [ |
| 1078 |
(1, 10), (1, 20), (2, 20), (3, 30) |
| 1079 |
] |
| 1080 |
|
| 1081 |
x = [ |
| 1082 |
(sname, pname) |
| 1083 |
for (sno, sname) in suppliers |
| 1084 |
for (pno, pname) in parts |
| 1085 |
for (sp_sno, sp_pno) in suppart |
| 1086 |
if sno == sp_sno and pno == sp_pno |
| 1087 |
] |
| 1088 |
|
| 1089 |
self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), |
| 1090 |
('Macdonalds', 'Cheeseburger')]) |
| 1091 |
|
| 1092 |
def test_genexps(self): |
| 1093 |
|
| 1094 |
g = ([x for x in range(10)] for x in range(1)) |
| 1095 |
self.assertEqual(g.next(), [x for x in range(10)]) |
| 1096 |
try: |
| 1097 |
g.next() |
| 1098 |
self.fail('should produce StopIteration exception') |
| 1099 |
except StopIteration: |
| 1100 |
pass |
| 1101 |
|
| 1102 |
a = 1 |
| 1103 |
try: |
| 1104 |
g = (a for d in a) |
| 1105 |
g.next() |
| 1106 |
self.fail('should produce TypeError') |
| 1107 |
except TypeError: |
| 1108 |
pass |
| 1109 |
|
| 1110 |
self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) |
| 1111 |
self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) |
| 1112 |
|
| 1113 |
a = [x for x in range(10)] |
| 1114 |
b = (x for x in (y for y in a)) |
| 1115 |
self.assertEqual(sum(b), sum([x for x in range(10)])) |
| 1116 |
|
| 1117 |
self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) |
| 1118 |
self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) |
| 1119 |
self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) |
| 1120 |
self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) |
| 1121 |
self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) |
| 1122 |
self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)])) |
| 1123 |
self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) |
| 1124 |
check_syntax_error(self, "foo(x for x in range(10), 100)") |
| 1125 |
check_syntax_error(self, "foo(100, x for x in range(10))") |
| 1126 |
|
| 1127 |
def test_comprehension_specials(self): |
| 1128 |
|
| 1129 |
x = 10; g = (i for i in range(x)); x = 5 |
| 1130 |
self.assertEqual(len(list(g)), 10) |
| 1131 |
|
| 1132 |
|
| 1133 |
x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) |
| 1134 |
x = 5; t = True; |
| 1135 |
self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) |
| 1140 |
self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) |
| 1141 |
|
| 1142 |
|
| 1143 |
self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) |
| 1144 |
self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) |
| 1145 |
|
| 1146 |
def test_with_statement(self): |
| 1147 |
class manager(object): |
| 1148 |
def __enter__(self): |
| 1149 |
return (1, 2) |
| 1150 |
def __exit__(self, *args): |
| 1151 |
pass |
| 1152 |
|
| 1153 |
with manager(): |
| 1154 |
pass |
| 1155 |
with manager() as x: |
| 1156 |
pass |
| 1157 |
with manager() as (x, y): |
| 1158 |
pass |
| 1159 |
with manager(), manager(): |
| 1160 |
pass |
| 1161 |
with manager() as x, manager() as y: |
| 1162 |
pass |
| 1163 |
with manager() as x, manager(): |
| 1164 |
pass |
| 1165 |
|
| 1166 |
def test_if_else_expr(self): |
| 1167 |
|
| 1168 |
def _checkeval(msg, ret): |
| 1169 |
"helper to check that evaluation of expressions is done correctly" |
| 1170 |
print(msg) |
| 1171 |
return ret |
| 1172 |
|
| 1173 |
self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) |
| 1174 |
self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) |
| 1175 |
self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True]) |
| 1176 |
self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) |
| 1177 |
self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) |
| 1178 |
self.assertEqual((5 and 6 if 0 else 1), 1) |
| 1179 |
self.assertEqual(((5 and 6) if 0 else 1), 1) |
| 1180 |
self.assertEqual((5 and (6 if 1 else 1)), 6) |
| 1181 |
self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) |
| 1182 |
self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) |
| 1183 |
self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) |
| 1184 |
self.assertEqual((not 5 if 1 else 1), False) |
| 1185 |
self.assertEqual((not 5 if 0 else 1), 1) |
| 1186 |
self.assertEqual((6 + 1 if 1 else 2), 7) |
| 1187 |
self.assertEqual((6 - 1 if 1 else 2), 5) |
| 1188 |
self.assertEqual((6 * 2 if 1 else 4), 12) |
| 1189 |
with check_py3k_warnings(('classic int division', DeprecationWarning)): |
| 1190 |
self.assertEqual((6 / 2 if 1 else 3), 3) |
| 1191 |
self.assertEqual((6 < 4 if 0 else 2), 2) |
| 1192 |
|
| 1193 |
def test_paren_evaluation(self): |
| 1194 |
self.assertEqual(16 // (4 // 2), 8) |
| 1195 |
self.assertEqual((16 // 4) // 2, 2) |
| 1196 |
self.assertEqual(16 // 4 // 2, 2) |
| 1197 |
self.assertTrue(False is (2 is 3)) |
| 1198 |
self.assertFalse((False is 2) is 3) |
| 1199 |
self.assertFalse(False is 2 is 3) |
| 1200 |
|
| 1201 |
|
| 1202 |
def test_main(): |
| 1203 |
run_unittest(TokenTests, GrammarTests) |
| 1204 |
|
| 1205 |
if __name__ == '__main__': |
| 1206 |
test_main() |