| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from test.support import check_syntax_error |
| 5 |
from test.support import import_helper, check_impl_detail |
| 6 |
from test.support.warnings_helper import check_syntax_warning |
| 7 |
import inspect |
| 8 |
import unittest |
| 9 |
import sys |
| 10 |
import warnings |
| 11 |
|
| 12 |
from sys import * |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import test.typinganndata.ann_module as ann_module |
| 17 |
import typing |
| 18 |
from test.typinganndata import ann_module2 |
| 19 |
import test |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
VALID_UNDERSCORE_LITERALS = [ |
| 26 |
'0_0_0', |
| 27 |
'4_2', |
| 28 |
'1_0000_0000', |
| 29 |
'0b1001_0100', |
| 30 |
'0xffff_ffff', |
| 31 |
'0o5_7_7', |
| 32 |
'1_00_00.5', |
| 33 |
'1_00_00.5e5', |
| 34 |
'1_00_00e5_1', |
| 35 |
'1e1_0', |
| 36 |
'.1_4', |
| 37 |
'.1_4e1', |
| 38 |
'0b_0', |
| 39 |
'0x_f', |
| 40 |
'0o_5', |
| 41 |
'1_00_00j', |
| 42 |
'1_00_00.5j', |
| 43 |
'1_00_00e5_1j', |
| 44 |
'.1_4j', |
| 45 |
'(1_2.5+3_3j)', |
| 46 |
'(.5_6j)', |
| 47 |
] |
| 48 |
INVALID_UNDERSCORE_LITERALS = [ |
| 49 |
|
| 50 |
'0_', |
| 51 |
'42_', |
| 52 |
'1.4j_', |
| 53 |
'0x_', |
| 54 |
'0b1_', |
| 55 |
'0xf_', |
| 56 |
'0o5_', |
| 57 |
'0 if 1_Else 1', |
| 58 |
|
| 59 |
'0_b0', |
| 60 |
'0_xf', |
| 61 |
'0_o5', |
| 62 |
|
| 63 |
'0_7', |
| 64 |
'09_99', |
| 65 |
|
| 66 |
'4_______2', |
| 67 |
'0.1__4', |
| 68 |
'0.1__4j', |
| 69 |
'0b1001__0100', |
| 70 |
'0xffff__ffff', |
| 71 |
'0x___', |
| 72 |
'0o5__77', |
| 73 |
'1e1__0', |
| 74 |
'1e1__0j', |
| 75 |
|
| 76 |
'1_.4', |
| 77 |
'1_.4j', |
| 78 |
|
| 79 |
'1._4', |
| 80 |
'1._4j', |
| 81 |
'._5', |
| 82 |
'._5j', |
| 83 |
|
| 84 |
'1.0e+_1', |
| 85 |
'1.0e+_1j', |
| 86 |
|
| 87 |
'1.4_j', |
| 88 |
'1.4e5_j', |
| 89 |
|
| 90 |
'1_e1', |
| 91 |
'1.4_e1', |
| 92 |
'1.4_e1j', |
| 93 |
|
| 94 |
'1e_1', |
| 95 |
'1.4e_1', |
| 96 |
'1.4e_1j', |
| 97 |
|
| 98 |
'(1+1.5_j_)', |
| 99 |
'(1+1.5_j)', |
| 100 |
] |
| 101 |
|
| 102 |
|
| 103 |
class TokenTests(unittest.TestCase): |
| 104 |
|
| 105 |
from test.support import check_syntax_error |
| 106 |
from test.support.warnings_helper import check_syntax_warning |
| 107 |
|
| 108 |
def test_backslash(self): |
| 109 |
|
| 110 |
x = 1 \ |
| 111 |
+ 1 |
| 112 |
self.assertEqual(x, 2, 'backslash for line continuation') |
| 113 |
|
| 114 |
|
| 115 |
x = 0 |
| 116 |
self.assertEqual(x, 0, 'backslash ending comment') |
| 117 |
|
| 118 |
def test_plain_integers(self): |
| 119 |
self.assertEqual(type(000), type(0)) |
| 120 |
self.assertEqual(0xff, 255) |
| 121 |
self.assertEqual(0o377, 255) |
| 122 |
self.assertEqual(2147483647, 0o17777777777) |
| 123 |
self.assertEqual(0b1001, 9) |
| 124 |
|
| 125 |
self.assertRaises(SyntaxError, eval, "0x") |
| 126 |
from sys import maxsize |
| 127 |
if maxsize == 2147483647: |
| 128 |
self.assertEqual(-2147483647-1, -0o20000000000) |
| 129 |
|
| 130 |
self.assertTrue(0o37777777777 > 0) |
| 131 |
self.assertTrue(0xffffffff > 0) |
| 132 |
self.assertTrue(0b1111111111111111111111111111111 > 0) |
| 133 |
for s in ('2147483648', '0o40000000000', '0x100000000', |
| 134 |
'0b10000000000000000000000000000000'): |
| 135 |
try: |
| 136 |
x = eval(s) |
| 137 |
except OverflowError: |
| 138 |
self.fail("OverflowError on huge integer literal %r" % s) |
| 139 |
elif maxsize == 9223372036854775807: |
| 140 |
self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000) |
| 141 |
self.assertTrue(0o1777777777777777777777 > 0) |
| 142 |
self.assertTrue(0xffffffffffffffff > 0) |
| 143 |
self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0) |
| 144 |
for s in '9223372036854775808', '0o2000000000000000000000', \ |
| 145 |
'0x10000000000000000', \ |
| 146 |
'0b100000000000000000000000000000000000000000000000000000000000000': |
| 147 |
try: |
| 148 |
x = eval(s) |
| 149 |
except OverflowError: |
| 150 |
self.fail("OverflowError on huge integer literal %r" % s) |
| 151 |
else: |
| 152 |
self.fail('Weird maxsize value %r' % maxsize) |
| 153 |
|
| 154 |
def test_long_integers(self): |
| 155 |
x = 0 |
| 156 |
x = 0xffffffffffffffff |
| 157 |
x = 0Xffffffffffffffff |
| 158 |
x = 0o77777777777777777 |
| 159 |
x = 0O77777777777777777 |
| 160 |
x = 123456789012345678901234567890 |
| 161 |
x = 0b100000000000000000000000000000000000000000000000000000000000000000000 |
| 162 |
x = 0B111111111111111111111111111111111111111111111111111111111111111111111 |
| 163 |
|
| 164 |
def test_floats(self): |
| 165 |
x = 3.14 |
| 166 |
x = 314. |
| 167 |
x = 0.314 |
| 168 |
|
| 169 |
x = .314 |
| 170 |
x = 3e14 |
| 171 |
x = 3E14 |
| 172 |
x = 3e-14 |
| 173 |
x = 3e+14 |
| 174 |
x = 3.e14 |
| 175 |
x = .3e14 |
| 176 |
x = 3.1e4 |
| 177 |
|
| 178 |
def test_float_exponent_tokenization(self): |
| 179 |
|
| 180 |
with warnings.catch_warnings(): |
| 181 |
warnings.simplefilter('ignore', SyntaxWarning) |
| 182 |
self.assertEqual(eval("1 if 1else 0"), 1) |
| 183 |
self.assertEqual(eval("1 if 0else 0"), 0) |
| 184 |
self.assertRaises(SyntaxError, eval, "0 if 1Else 0") |
| 185 |
|
| 186 |
def test_underscore_literals(self): |
| 187 |
for lit in VALID_UNDERSCORE_LITERALS: |
| 188 |
self.assertEqual(eval(lit), eval(lit.replace('_', ''))) |
| 189 |
for lit in INVALID_UNDERSCORE_LITERALS: |
| 190 |
self.assertRaises(SyntaxError, eval, lit) |
| 191 |
|
| 192 |
self.assertRaises(NameError, eval, "_0") |
| 193 |
|
| 194 |
def test_bad_numerical_literals(self): |
| 195 |
check = self.check_syntax_error |
| 196 |
check("0b12", "invalid digit '2' in binary literal") |
| 197 |
check("0b1_2", "invalid digit '2' in binary literal") |
| 198 |
check("0b2", "invalid digit '2' in binary literal") |
| 199 |
check("0b1_", "invalid binary literal") |
| 200 |
check("0b", "invalid binary literal") |
| 201 |
check("0o18", "invalid digit '8' in octal literal") |
| 202 |
check("0o1_8", "invalid digit '8' in octal literal") |
| 203 |
check("0o8", "invalid digit '8' in octal literal") |
| 204 |
check("0o1_", "invalid octal literal") |
| 205 |
check("0o", "invalid octal literal") |
| 206 |
check("0x1_", "invalid hexadecimal literal") |
| 207 |
check("0x", "invalid hexadecimal literal") |
| 208 |
check("1_", "invalid decimal literal") |
| 209 |
check("012", |
| 210 |
"leading zeros in decimal integer literals are not permitted; " |
| 211 |
"use an 0o prefix for octal integers") |
| 212 |
check("1.2_", "invalid decimal literal") |
| 213 |
check("1e2_", "invalid decimal literal") |
| 214 |
check("1e+", "invalid decimal literal") |
| 215 |
|
| 216 |
def test_end_of_numerical_literals(self): |
| 217 |
def check(test, error=False): |
| 218 |
with self.subTest(expr=test): |
| 219 |
if error: |
| 220 |
with warnings.catch_warnings(record=True) as w: |
| 221 |
with self.assertRaisesRegex(SyntaxError, |
| 222 |
r'invalid \w+ literal'): |
| 223 |
compile(test, "<testcase>", "eval") |
| 224 |
self.assertEqual(w, []) |
| 225 |
else: |
| 226 |
self.check_syntax_warning(test, |
| 227 |
errtext=r'invalid \w+ literal') |
| 228 |
|
| 229 |
for num in "0xf", "0o7", "0b1", "9", "0", "1.", "1e3", "1j": |
| 230 |
compile(num, "<testcase>", "eval") |
| 231 |
check(f"{num}and x", error=(num == "0xf")) |
| 232 |
check(f"{num}or x", error=(num == "0")) |
| 233 |
check(f"{num}in x") |
| 234 |
check(f"{num}not in x") |
| 235 |
check(f"{num}if x else y") |
| 236 |
check(f"x if {num}else y", error=(num == "0xf")) |
| 237 |
check(f"[{num}for x in ()]") |
| 238 |
check(f"{num}spam", error=True) |
| 239 |
|
| 240 |
|
| 241 |
with self.assertRaisesRegex(SyntaxError, r"invalid character '⁄' \(U\+2044\)"): |
| 242 |
compile(f"{num}⁄7", "<testcase>", "eval") |
| 243 |
|
| 244 |
with warnings.catch_warnings(): |
| 245 |
warnings.filterwarnings('ignore', '"is" with a literal', |
| 246 |
SyntaxWarning) |
| 247 |
with self.assertWarnsRegex(SyntaxWarning, |
| 248 |
r'invalid \w+ literal'): |
| 249 |
compile(f"{num}is x", "<testcase>", "eval") |
| 250 |
warnings.simplefilter('error', SyntaxWarning) |
| 251 |
with self.assertRaisesRegex(SyntaxError, |
| 252 |
r'invalid \w+ literal'): |
| 253 |
compile(f"{num}is x", "<testcase>", "eval") |
| 254 |
|
| 255 |
check("[0x1ffor x in ()]") |
| 256 |
check("[0x1for x in ()]") |
| 257 |
check("[0xfor x in ()]") |
| 258 |
|
| 259 |
def test_string_literals(self): |
| 260 |
x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y) |
| 261 |
x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39) |
| 262 |
x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34) |
| 263 |
x = "doesn't \"shrink\" does it" |
| 264 |
y = 'doesn\'t "shrink" does it' |
| 265 |
self.assertTrue(len(x) == 24 and x == y) |
| 266 |
x = "does \"shrink\" doesn't it" |
| 267 |
y = 'does "shrink" doesn\'t it' |
| 268 |
self.assertTrue(len(x) == 24 and x == y) |
| 269 |
x = """ |
| 270 |
The "quick" |
| 271 |
brown fox |
| 272 |
jumps over |
| 273 |
the 'lazy' dog. |
| 274 |
""" |
| 275 |
y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |
| 276 |
self.assertEqual(x, y) |
| 277 |
y = ''' |
| 278 |
The "quick" |
| 279 |
brown fox |
| 280 |
jumps over |
| 281 |
the 'lazy' dog. |
| 282 |
''' |
| 283 |
self.assertEqual(x, y) |
| 284 |
y = "\n\ |
| 285 |
The \"quick\"\n\ |
| 286 |
brown fox\n\ |
| 287 |
jumps over\n\ |
| 288 |
the 'lazy' dog.\n\ |
| 289 |
" |
| 290 |
self.assertEqual(x, y) |
| 291 |
y = '\n\ |
| 292 |
The \"quick\"\n\ |
| 293 |
brown fox\n\ |
| 294 |
jumps over\n\ |
| 295 |
the \'lazy\' dog.\n\ |
| 296 |
' |
| 297 |
self.assertEqual(x, y) |
| 298 |
|
| 299 |
def test_ellipsis(self): |
| 300 |
x = ... |
| 301 |
self.assertTrue(x is Ellipsis) |
| 302 |
self.assertRaises(SyntaxError, eval, ".. .") |
| 303 |
|
| 304 |
def test_eof_error(self): |
| 305 |
samples = ("def foo(", "\ndef foo(", "def foo(\n") |
| 306 |
for s in samples: |
| 307 |
with self.assertRaises(SyntaxError) as cm: |
| 308 |
compile(s, "<test>", "exec") |
| 309 |
self.assertIn("was never closed", str(cm.exception)) |
| 310 |
|
| 311 |
var_annot_global: int |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
class CNS: |
| 316 |
def __init__(self): |
| 317 |
self._dct = {} |
| 318 |
def __setitem__(self, item, value): |
| 319 |
self._dct[item.lower()] = value |
| 320 |
def __getitem__(self, item): |
| 321 |
return self._dct[item] |
| 322 |
|
| 323 |
|
| 324 |
class GrammarTests(unittest.TestCase): |
| 325 |
|
| 326 |
from test.support import check_syntax_error |
| 327 |
from test.support.warnings_helper import check_syntax_warning |
| 328 |
from test.support.warnings_helper import check_no_warnings |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
def test_eval_input(self): |
| 340 |
|
| 341 |
x = eval('1, 0 or 1') |
| 342 |
|
| 343 |
def test_var_annot_basics(self): |
| 344 |
|
| 345 |
var1: int = 5 |
| 346 |
var2: [int, str] |
| 347 |
my_lst = [42] |
| 348 |
def one(): |
| 349 |
return 1 |
| 350 |
int.new_attr: int |
| 351 |
[list][0]: type |
| 352 |
my_lst[one()-1]: int = 5 |
| 353 |
self.assertEqual(my_lst, [5]) |
| 354 |
|
| 355 |
def test_var_annot_syntax_errors(self): |
| 356 |
|
| 357 |
check_syntax_error(self, "def f: int") |
| 358 |
check_syntax_error(self, "x: int: str") |
| 359 |
check_syntax_error(self, "def f():\n" |
| 360 |
" nonlocal x: int\n") |
| 361 |
check_syntax_error(self, "def f():\n" |
| 362 |
" global x: int\n") |
| 363 |
check_syntax_error(self, "x: int = y = 1") |
| 364 |
check_syntax_error(self, "z = w: int = 1") |
| 365 |
check_syntax_error(self, "x: int = y: int = 1") |
| 366 |
|
| 367 |
check_syntax_error(self, "[x, 0]: int\n") |
| 368 |
check_syntax_error(self, "f(): int\n") |
| 369 |
check_syntax_error(self, "(x,): int") |
| 370 |
check_syntax_error(self, "def f():\n" |
| 371 |
" (x, y): int = (1, 2)\n") |
| 372 |
|
| 373 |
check_syntax_error(self, "def f():\n" |
| 374 |
" x: int\n" |
| 375 |
" global x\n") |
| 376 |
check_syntax_error(self, "def f():\n" |
| 377 |
" global x\n" |
| 378 |
" x: int\n") |
| 379 |
check_syntax_error(self, "def f():\n" |
| 380 |
" x: int\n" |
| 381 |
" nonlocal x\n") |
| 382 |
check_syntax_error(self, "def f():\n" |
| 383 |
" nonlocal x\n" |
| 384 |
" x: int\n") |
| 385 |
|
| 386 |
def test_var_annot_basic_semantics(self): |
| 387 |
|
| 388 |
with self.assertRaises(ZeroDivisionError): |
| 389 |
no_name[does_not_exist]: no_name_again = 1/0 |
| 390 |
with self.assertRaises(NameError): |
| 391 |
no_name[does_not_exist]: 1/0 = 0 |
| 392 |
global var_annot_global |
| 393 |
|
| 394 |
|
| 395 |
def f(): |
| 396 |
st: str = "Hello" |
| 397 |
a.b: int = (1, 2) |
| 398 |
return st |
| 399 |
self.assertEqual(f.__annotations__, {}) |
| 400 |
def f_OK(): |
| 401 |
x: 1/0 |
| 402 |
f_OK() |
| 403 |
def fbad(): |
| 404 |
x: int |
| 405 |
print(x) |
| 406 |
with self.assertRaises(UnboundLocalError): |
| 407 |
fbad() |
| 408 |
def f2bad(): |
| 409 |
(no_such_global): int |
| 410 |
print(no_such_global) |
| 411 |
try: |
| 412 |
f2bad() |
| 413 |
except Exception as e: |
| 414 |
self.assertIs(type(e), NameError) |
| 415 |
|
| 416 |
|
| 417 |
class C: |
| 418 |
__foo: int |
| 419 |
s: str = "attr" |
| 420 |
z = 2 |
| 421 |
def __init__(self, x): |
| 422 |
self.x: int = x |
| 423 |
self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str}) |
| 424 |
with self.assertRaises(NameError): |
| 425 |
class CBad: |
| 426 |
no_such_name_defined.attr: int = 0 |
| 427 |
with self.assertRaises(NameError): |
| 428 |
class Cbad2(C): |
| 429 |
x: int |
| 430 |
x.y: list = [] |
| 431 |
|
| 432 |
def test_annotations_inheritance(self): |
| 433 |
|
| 434 |
class A: |
| 435 |
attr: int |
| 436 |
class B(A): |
| 437 |
pass |
| 438 |
class C(A): |
| 439 |
attr: str |
| 440 |
class D: |
| 441 |
attr2: int |
| 442 |
class E(A, D): |
| 443 |
pass |
| 444 |
class F(C, A): |
| 445 |
pass |
| 446 |
self.assertEqual(A.__annotations__, {"attr": int}) |
| 447 |
self.assertEqual(B.__annotations__, {}) |
| 448 |
self.assertEqual(C.__annotations__, {"attr" : str}) |
| 449 |
self.assertEqual(D.__annotations__, {"attr2" : int}) |
| 450 |
self.assertEqual(E.__annotations__, {}) |
| 451 |
self.assertEqual(F.__annotations__, {}) |
| 452 |
|
| 453 |
|
| 454 |
def test_var_annot_metaclass_semantics(self): |
| 455 |
class CMeta(type): |
| 456 |
@classmethod |
| 457 |
def __prepare__(metacls, name, bases, **kwds): |
| 458 |
return {'__annotations__': CNS()} |
| 459 |
class CC(metaclass=CMeta): |
| 460 |
XX: 'ANNOT' |
| 461 |
self.assertEqual(CC.__annotations__['xx'], 'ANNOT') |
| 462 |
|
| 463 |
def test_var_annot_module_semantics(self): |
| 464 |
self.assertEqual(test.__annotations__, {}) |
| 465 |
self.assertEqual(ann_module.__annotations__, |
| 466 |
{1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int], 'u': int | float}) |
| 467 |
self.assertEqual(ann_module.M.__annotations__, |
| 468 |
{'123': 123, 'o': type}) |
| 469 |
self.assertEqual(ann_module2.__annotations__, {}) |
| 470 |
|
| 471 |
def test_var_annot_in_module(self): |
| 472 |
|
| 473 |
|
| 474 |
ann_module3 = import_helper.import_fresh_module("test.typinganndata.ann_module3") |
| 475 |
with self.assertRaises(NameError): |
| 476 |
ann_module3.f_bad_ann() |
| 477 |
with self.assertRaises(NameError): |
| 478 |
ann_module3.g_bad_ann() |
| 479 |
with self.assertRaises(NameError): |
| 480 |
ann_module3.D_bad_ann(5) |
| 481 |
|
| 482 |
def test_var_annot_simple_exec(self): |
| 483 |
gns = {}; lns= {} |
| 484 |
exec("'docstring'\n" |
| 485 |
"__annotations__[1] = 2\n" |
| 486 |
"x: int = 5\n", gns, lns) |
| 487 |
self.assertEqual(lns["__annotations__"], {1: 2, 'x': int}) |
| 488 |
with self.assertRaises(KeyError): |
| 489 |
gns['__annotations__'] |
| 490 |
|
| 491 |
def test_var_annot_custom_maps(self): |
| 492 |
|
| 493 |
ns = {'__annotations__': CNS()} |
| 494 |
exec('X: int; Z: str = "Z"; (w): complex = 1j', ns) |
| 495 |
self.assertEqual(ns['__annotations__']['x'], int) |
| 496 |
self.assertEqual(ns['__annotations__']['z'], str) |
| 497 |
with self.assertRaises(KeyError): |
| 498 |
ns['__annotations__']['w'] |
| 499 |
nonloc_ns = {} |
| 500 |
class CNS2: |
| 501 |
def __init__(self): |
| 502 |
self._dct = {} |
| 503 |
def __setitem__(self, item, value): |
| 504 |
nonlocal nonloc_ns |
| 505 |
self._dct[item] = value |
| 506 |
nonloc_ns[item] = value |
| 507 |
def __getitem__(self, item): |
| 508 |
return self._dct[item] |
| 509 |
exec('x: int = 1', {}, CNS2()) |
| 510 |
self.assertEqual(nonloc_ns['__annotations__']['x'], int) |
| 511 |
|
| 512 |
def test_var_annot_refleak(self): |
| 513 |
|
| 514 |
|
| 515 |
cns = CNS() |
| 516 |
nonloc_ns = {'__annotations__': cns} |
| 517 |
class CNS2: |
| 518 |
def __init__(self): |
| 519 |
self._dct = {'__annotations__': cns} |
| 520 |
def __setitem__(self, item, value): |
| 521 |
nonlocal nonloc_ns |
| 522 |
self._dct[item] = value |
| 523 |
nonloc_ns[item] = value |
| 524 |
def __getitem__(self, item): |
| 525 |
return self._dct[item] |
| 526 |
exec('X: str', {}, CNS2()) |
| 527 |
self.assertEqual(nonloc_ns['__annotations__']['x'], str) |
| 528 |
|
| 529 |
def test_var_annot_rhs(self): |
| 530 |
ns = {} |
| 531 |
exec('x: tuple = 1, 2', ns) |
| 532 |
self.assertEqual(ns['x'], (1, 2)) |
| 533 |
stmt = ('def f():\n' |
| 534 |
' x: int = yield') |
| 535 |
exec(stmt, ns) |
| 536 |
self.assertEqual(list(ns['f']()), [None]) |
| 537 |
|
| 538 |
ns = {"a": 1, 'b': (2, 3, 4), "c":5, "Tuple": typing.Tuple} |
| 539 |
exec('x: Tuple[int, ...] = a,*b,c', ns) |
| 540 |
self.assertEqual(ns['x'], (1, 2, 3, 4, 5)) |
| 541 |
|
| 542 |
def test_funcdef(self): |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
def f1(): pass |
| 556 |
f1() |
| 557 |
f1(*()) |
| 558 |
f1(*(), **{}) |
| 559 |
def f2(one_argument): pass |
| 560 |
def f3(two, arguments): pass |
| 561 |
self.assertEqual(f2.__code__.co_varnames, ('one_argument',)) |
| 562 |
self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments')) |
| 563 |
def a1(one_arg,): pass |
| 564 |
def a2(two, args,): pass |
| 565 |
def v0(*rest): pass |
| 566 |
def v1(a, *rest): pass |
| 567 |
def v2(a, b, *rest): pass |
| 568 |
|
| 569 |
f1() |
| 570 |
f2(1) |
| 571 |
f2(1,) |
| 572 |
f3(1, 2) |
| 573 |
f3(1, 2,) |
| 574 |
v0() |
| 575 |
v0(1) |
| 576 |
v0(1,) |
| 577 |
v0(1,2) |
| 578 |
v0(1,2,3,4,5,6,7,8,9,0) |
| 579 |
v1(1) |
| 580 |
v1(1,) |
| 581 |
v1(1,2) |
| 582 |
v1(1,2,3) |
| 583 |
v1(1,2,3,4,5,6,7,8,9,0) |
| 584 |
v2(1,2) |
| 585 |
v2(1,2,3) |
| 586 |
v2(1,2,3,4) |
| 587 |
v2(1,2,3,4,5,6,7,8,9,0) |
| 588 |
|
| 589 |
def d01(a=1): pass |
| 590 |
d01() |
| 591 |
d01(1) |
| 592 |
d01(*(1,)) |
| 593 |
d01(*[] or [2]) |
| 594 |
d01(*() or (), *{} and (), **() or {}) |
| 595 |
d01(**{'a':2}) |
| 596 |
d01(**{'a':2} or {}) |
| 597 |
def d11(a, b=1): pass |
| 598 |
d11(1) |
| 599 |
d11(1, 2) |
| 600 |
d11(1, **{'b':2}) |
| 601 |
def d21(a, b, c=1): pass |
| 602 |
d21(1, 2) |
| 603 |
d21(1, 2, 3) |
| 604 |
d21(*(1, 2, 3)) |
| 605 |
d21(1, *(2, 3)) |
| 606 |
d21(1, 2, *(3,)) |
| 607 |
d21(1, 2, **{'c':3}) |
| 608 |
def d02(a=1, b=2): pass |
| 609 |
d02() |
| 610 |
d02(1) |
| 611 |
d02(1, 2) |
| 612 |
d02(*(1, 2)) |
| 613 |
d02(1, *(2,)) |
| 614 |
d02(1, **{'b':2}) |
| 615 |
d02(**{'a': 1, 'b': 2}) |
| 616 |
def d12(a, b=1, c=2): pass |
| 617 |
d12(1) |
| 618 |
d12(1, 2) |
| 619 |
d12(1, 2, 3) |
| 620 |
def d22(a, b, c=1, d=2): pass |
| 621 |
d22(1, 2) |
| 622 |
d22(1, 2, 3) |
| 623 |
d22(1, 2, 3, 4) |
| 624 |
def d01v(a=1, *rest): pass |
| 625 |
d01v() |
| 626 |
d01v(1) |
| 627 |
d01v(1, 2) |
| 628 |
d01v(*(1, 2, 3, 4)) |
| 629 |
d01v(*(1,)) |
| 630 |
d01v(**{'a':2}) |
| 631 |
def d11v(a, b=1, *rest): pass |
| 632 |
d11v(1) |
| 633 |
d11v(1, 2) |
| 634 |
d11v(1, 2, 3) |
| 635 |
def d21v(a, b, c=1, *rest): pass |
| 636 |
d21v(1, 2) |
| 637 |
d21v(1, 2, 3) |
| 638 |
d21v(1, 2, 3, 4) |
| 639 |
d21v(*(1, 2, 3, 4)) |
| 640 |
d21v(1, 2, **{'c': 3}) |
| 641 |
def d02v(a=1, b=2, *rest): pass |
| 642 |
d02v() |
| 643 |
d02v(1) |
| 644 |
d02v(1, 2) |
| 645 |
d02v(1, 2, 3) |
| 646 |
d02v(1, *(2, 3, 4)) |
| 647 |
d02v(**{'a': 1, 'b': 2}) |
| 648 |
def d12v(a, b=1, c=2, *rest): pass |
| 649 |
d12v(1) |
| 650 |
d12v(1, 2) |
| 651 |
d12v(1, 2, 3) |
| 652 |
d12v(1, 2, 3, 4) |
| 653 |
d12v(*(1, 2, 3, 4)) |
| 654 |
d12v(1, 2, *(3, 4, 5)) |
| 655 |
d12v(1, *(2,), **{'c': 3}) |
| 656 |
def d22v(a, b, c=1, d=2, *rest): pass |
| 657 |
d22v(1, 2) |
| 658 |
d22v(1, 2, 3) |
| 659 |
d22v(1, 2, 3, 4) |
| 660 |
d22v(1, 2, 3, 4, 5) |
| 661 |
d22v(*(1, 2, 3, 4)) |
| 662 |
d22v(1, 2, *(3, 4, 5)) |
| 663 |
d22v(1, *(2, 3), **{'d': 4}) |
| 664 |
|
| 665 |
|
| 666 |
with warnings.catch_warnings(): |
| 667 |
warnings.simplefilter('ignore', BytesWarning) |
| 668 |
try: |
| 669 |
str('x', **{b'foo':1 }) |
| 670 |
except TypeError: |
| 671 |
pass |
| 672 |
else: |
| 673 |
self.fail('Bytes should not work as keyword argument names') |
| 674 |
|
| 675 |
def pos0key1(*, key): return key |
| 676 |
pos0key1(key=100) |
| 677 |
def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2 |
| 678 |
pos2key2(1, 2, k1=100) |
| 679 |
pos2key2(1, 2, k1=100, k2=200) |
| 680 |
pos2key2(1, 2, k2=100, k1=200) |
| 681 |
def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg |
| 682 |
pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200) |
| 683 |
pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100) |
| 684 |
|
| 685 |
self.assertRaises(SyntaxError, eval, "def f(*): pass") |
| 686 |
self.assertRaises(SyntaxError, eval, "def f(*,): pass") |
| 687 |
self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass") |
| 688 |
|
| 689 |
|
| 690 |
def f(*args, **kwargs): |
| 691 |
return args, kwargs |
| 692 |
self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), |
| 693 |
{'x':2, 'y':5})) |
| 694 |
self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {})) |
| 695 |
self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") |
| 696 |
self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}), |
| 697 |
((), {'eggs':'scrambled', 'spam':'fried'})) |
| 698 |
self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}), |
| 699 |
((), {'eggs':'scrambled', 'spam':'fried'})) |
| 700 |
|
| 701 |
|
| 702 |
check_syntax_error(self, "f(*g(1=2))") |
| 703 |
check_syntax_error(self, "f(**g(1=2))") |
| 704 |
|
| 705 |
|
| 706 |
def f(x) -> list: pass |
| 707 |
self.assertEqual(f.__annotations__, {'return': list}) |
| 708 |
def f(x: int): pass |
| 709 |
self.assertEqual(f.__annotations__, {'x': int}) |
| 710 |
def f(x: int, /): pass |
| 711 |
self.assertEqual(f.__annotations__, {'x': int}) |
| 712 |
def f(x: int = 34, /): pass |
| 713 |
self.assertEqual(f.__annotations__, {'x': int}) |
| 714 |
def f(*x: str): pass |
| 715 |
self.assertEqual(f.__annotations__, {'x': str}) |
| 716 |
def f(**x: float): pass |
| 717 |
self.assertEqual(f.__annotations__, {'x': float}) |
| 718 |
def f(x, y: 1+2): pass |
| 719 |
self.assertEqual(f.__annotations__, {'y': 3}) |
| 720 |
def f(x, y: 1+2, /): pass |
| 721 |
self.assertEqual(f.__annotations__, {'y': 3}) |
| 722 |
def f(a, b: 1, c: 2, d): pass |
| 723 |
self.assertEqual(f.__annotations__, {'b': 1, 'c': 2}) |
| 724 |
def f(a, b: 1, /, c: 2, d): pass |
| 725 |
self.assertEqual(f.__annotations__, {'b': 1, 'c': 2}) |
| 726 |
def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass |
| 727 |
self.assertEqual(f.__annotations__, |
| 728 |
{'b': 1, 'c': 2, 'e': 3, 'g': 6}) |
| 729 |
def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10, |
| 730 |
**k: 11) -> 12: pass |
| 731 |
self.assertEqual(f.__annotations__, |
| 732 |
{'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9, |
| 733 |
'k': 11, 'return': 12}) |
| 734 |
def f(a, b: 1, c: 2, d, e: 3 = 4, f: int = 5, /, *g: 6, h: 7, i=8, j: 9 = 10, |
| 735 |
**k: 11) -> 12: pass |
| 736 |
self.assertEqual(f.__annotations__, |
| 737 |
{'b': 1, 'c': 2, 'e': 3, 'f': int, 'g': 6, 'h': 7, 'j': 9, |
| 738 |
'k': 11, 'return': 12}) |
| 739 |
|
| 740 |
class Spam: |
| 741 |
def f(self, *, __kw: 1): |
| 742 |
pass |
| 743 |
class Ham(Spam): pass |
| 744 |
self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1}) |
| 745 |
self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1}) |
| 746 |
|
| 747 |
def null(x): return x |
| 748 |
@null |
| 749 |
def f(x) -> list: pass |
| 750 |
self.assertEqual(f.__annotations__, {'return': list}) |
| 751 |
|
| 752 |
|
| 753 |
@False or null |
| 754 |
def f(x): pass |
| 755 |
@d := null |
| 756 |
def f(x): pass |
| 757 |
@lambda f: null(f) |
| 758 |
def f(x): pass |
| 759 |
@[..., null, ...][1] |
| 760 |
def f(x): pass |
| 761 |
@null(null)(null) |
| 762 |
def f(x): pass |
| 763 |
@[null][0].__call__.__call__ |
| 764 |
def f(x): pass |
| 765 |
|
| 766 |
|
| 767 |
closure = 1 |
| 768 |
def f(): return closure |
| 769 |
def f(x=1): return closure |
| 770 |
def f(*, k=1): return closure |
| 771 |
def f() -> int: return closure |
| 772 |
|
| 773 |
|
| 774 |
def f(a,): pass |
| 775 |
def f(*args,): pass |
| 776 |
def f(**kwds,): pass |
| 777 |
def f(a, *args,): pass |
| 778 |
def f(a, **kwds,): pass |
| 779 |
def f(*args, b,): pass |
| 780 |
def f(*, b,): pass |
| 781 |
def f(*args, **kwds,): pass |
| 782 |
def f(a, *args, b,): pass |
| 783 |
def f(a, *, b,): pass |
| 784 |
def f(a, *args, **kwds,): pass |
| 785 |
def f(*args, b, **kwds,): pass |
| 786 |
def f(*, b, **kwds,): pass |
| 787 |
def f(a, *args, b, **kwds,): pass |
| 788 |
def f(a, *, b, **kwds,): pass |
| 789 |
|
| 790 |
def test_lambdef(self): |
| 791 |
|
| 792 |
l1 = lambda : 0 |
| 793 |
self.assertEqual(l1(), 0) |
| 794 |
l2 = lambda : a[d] |
| 795 |
l3 = lambda : [2 < x for x in [-1, 3, 0]] |
| 796 |
self.assertEqual(l3(), [0, 1, 0]) |
| 797 |
l4 = lambda x = lambda y = lambda z=1 : z : y() : x() |
| 798 |
self.assertEqual(l4(), 1) |
| 799 |
l5 = lambda x, y, z=2: x + y + z |
| 800 |
self.assertEqual(l5(1, 2), 5) |
| 801 |
self.assertEqual(l5(1, 2, 3), 6) |
| 802 |
check_syntax_error(self, "lambda x: x = 2") |
| 803 |
check_syntax_error(self, "lambda (None,): None") |
| 804 |
l6 = lambda x, y, *, k=20: x+y+k |
| 805 |
self.assertEqual(l6(1,2), 1+2+20) |
| 806 |
self.assertEqual(l6(1,2,k=10), 1+2+10) |
| 807 |
|
| 808 |
|
| 809 |
l10 = lambda a,: 0 |
| 810 |
l11 = lambda *args,: 0 |
| 811 |
l12 = lambda **kwds,: 0 |
| 812 |
l13 = lambda a, *args,: 0 |
| 813 |
l14 = lambda a, **kwds,: 0 |
| 814 |
l15 = lambda *args, b,: 0 |
| 815 |
l16 = lambda *, b,: 0 |
| 816 |
l17 = lambda *args, **kwds,: 0 |
| 817 |
l18 = lambda a, *args, b,: 0 |
| 818 |
l19 = lambda a, *, b,: 0 |
| 819 |
l20 = lambda a, *args, **kwds,: 0 |
| 820 |
l21 = lambda *args, b, **kwds,: 0 |
| 821 |
l22 = lambda *, b, **kwds,: 0 |
| 822 |
l23 = lambda a, *args, b, **kwds,: 0 |
| 823 |
l24 = lambda a, *, b, **kwds,: 0 |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
def test_simple_stmt(self): |
| 830 |
|
| 831 |
x = 1; pass; del x |
| 832 |
def foo(): |
| 833 |
|
| 834 |
x = 1; pass; del x; |
| 835 |
foo() |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
def test_expr_stmt(self): |
| 841 |
|
| 842 |
1 |
| 843 |
1, 2, 3 |
| 844 |
x = 1 |
| 845 |
x = 1, 2, 3 |
| 846 |
x = y = z = 1, 2, 3 |
| 847 |
x, y, z = 1, 2, 3 |
| 848 |
abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |
| 849 |
|
| 850 |
check_syntax_error(self, "x + 1 = 1") |
| 851 |
check_syntax_error(self, "a + 1 = b + 2") |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
def test_former_statements_refer_to_builtins(self): |
| 856 |
keywords = "print", "exec" |
| 857 |
|
| 858 |
cases = [ |
| 859 |
"{} foo", |
| 860 |
"{} {{1:foo}}", |
| 861 |
"if 1: {} foo", |
| 862 |
"if 1: {} {{1:foo}}", |
| 863 |
"if 1:\n {} foo", |
| 864 |
"if 1:\n {} {{1:foo}}", |
| 865 |
] |
| 866 |
for keyword in keywords: |
| 867 |
custom_msg = "call to '{}'".format(keyword) |
| 868 |
for case in cases: |
| 869 |
source = case.format(keyword) |
| 870 |
with self.subTest(source=source): |
| 871 |
with self.assertRaisesRegex(SyntaxError, custom_msg): |
| 872 |
exec(source) |
| 873 |
source = source.replace("foo", "(foo.)") |
| 874 |
with self.subTest(source=source): |
| 875 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 876 |
exec(source) |
| 877 |
|
| 878 |
def test_del_stmt(self): |
| 879 |
|
| 880 |
abc = [1,2,3] |
| 881 |
x, y, z = abc |
| 882 |
xyz = x, y, z |
| 883 |
|
| 884 |
del abc |
| 885 |
del x, y, (z, xyz) |
| 886 |
|
| 887 |
x, y, z = "xyz" |
| 888 |
del x |
| 889 |
del y, |
| 890 |
del (z) |
| 891 |
del () |
| 892 |
|
| 893 |
a, b, c, d, e, f, g = "abcdefg" |
| 894 |
del a, (b, c), (d, (e, f)) |
| 895 |
|
| 896 |
a, b, c, d, e, f, g = "abcdefg" |
| 897 |
del a, [b, c], (d, [e, f]) |
| 898 |
|
| 899 |
abcd = list("abcd") |
| 900 |
del abcd[1:2] |
| 901 |
|
| 902 |
compile("del a, (b[0].c, (d.e, f.g[1:2])), [h.i.j], ()", "<testcase>", "exec") |
| 903 |
|
| 904 |
def test_pass_stmt(self): |
| 905 |
|
| 906 |
pass |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
def test_break_stmt(self): |
| 912 |
|
| 913 |
while 1: break |
| 914 |
|
| 915 |
def test_continue_stmt(self): |
| 916 |
|
| 917 |
i = 1 |
| 918 |
while i: i = 0; continue |
| 919 |
|
| 920 |
msg = "" |
| 921 |
while not msg: |
| 922 |
msg = "ok" |
| 923 |
try: |
| 924 |
continue |
| 925 |
msg = "continue failed to continue inside try" |
| 926 |
except: |
| 927 |
msg = "continue inside try called except block" |
| 928 |
if msg != "ok": |
| 929 |
self.fail(msg) |
| 930 |
|
| 931 |
msg = "" |
| 932 |
while not msg: |
| 933 |
msg = "finally block not called" |
| 934 |
try: |
| 935 |
continue |
| 936 |
finally: |
| 937 |
msg = "ok" |
| 938 |
if msg != "ok": |
| 939 |
self.fail(msg) |
| 940 |
|
| 941 |
def test_break_continue_loop(self): |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
def test_inner(extra_burning_oil = 1, count=0): |
| 952 |
big_hippo = 2 |
| 953 |
while big_hippo: |
| 954 |
count += 1 |
| 955 |
try: |
| 956 |
if extra_burning_oil and big_hippo == 1: |
| 957 |
extra_burning_oil -= 1 |
| 958 |
break |
| 959 |
big_hippo -= 1 |
| 960 |
continue |
| 961 |
except: |
| 962 |
raise |
| 963 |
if count > 2 or big_hippo != 1: |
| 964 |
self.fail("continue then break in try/except in loop broken!") |
| 965 |
test_inner() |
| 966 |
|
| 967 |
def test_return(self): |
| 968 |
|
| 969 |
def g1(): return |
| 970 |
def g2(): return 1 |
| 971 |
def g3(): |
| 972 |
z = [2, 3] |
| 973 |
return 1, *z |
| 974 |
|
| 975 |
g1() |
| 976 |
x = g2() |
| 977 |
y = g3() |
| 978 |
self.assertEqual(y, (1, 2, 3), "unparenthesized star expr return") |
| 979 |
check_syntax_error(self, "class foo:return 1") |
| 980 |
|
| 981 |
def test_break_in_finally(self): |
| 982 |
count = 0 |
| 983 |
while count < 2: |
| 984 |
count += 1 |
| 985 |
try: |
| 986 |
pass |
| 987 |
finally: |
| 988 |
break |
| 989 |
self.assertEqual(count, 1) |
| 990 |
|
| 991 |
count = 0 |
| 992 |
while count < 2: |
| 993 |
count += 1 |
| 994 |
try: |
| 995 |
continue |
| 996 |
finally: |
| 997 |
break |
| 998 |
self.assertEqual(count, 1) |
| 999 |
|
| 1000 |
count = 0 |
| 1001 |
while count < 2: |
| 1002 |
count += 1 |
| 1003 |
try: |
| 1004 |
1/0 |
| 1005 |
finally: |
| 1006 |
break |
| 1007 |
self.assertEqual(count, 1) |
| 1008 |
|
| 1009 |
for count in [0, 1]: |
| 1010 |
self.assertEqual(count, 0) |
| 1011 |
try: |
| 1012 |
pass |
| 1013 |
finally: |
| 1014 |
break |
| 1015 |
self.assertEqual(count, 0) |
| 1016 |
|
| 1017 |
for count in [0, 1]: |
| 1018 |
self.assertEqual(count, 0) |
| 1019 |
try: |
| 1020 |
continue |
| 1021 |
finally: |
| 1022 |
break |
| 1023 |
self.assertEqual(count, 0) |
| 1024 |
|
| 1025 |
for count in [0, 1]: |
| 1026 |
self.assertEqual(count, 0) |
| 1027 |
try: |
| 1028 |
1/0 |
| 1029 |
finally: |
| 1030 |
break |
| 1031 |
self.assertEqual(count, 0) |
| 1032 |
|
| 1033 |
def test_continue_in_finally(self): |
| 1034 |
count = 0 |
| 1035 |
while count < 2: |
| 1036 |
count += 1 |
| 1037 |
try: |
| 1038 |
pass |
| 1039 |
finally: |
| 1040 |
continue |
| 1041 |
break |
| 1042 |
self.assertEqual(count, 2) |
| 1043 |
|
| 1044 |
count = 0 |
| 1045 |
while count < 2: |
| 1046 |
count += 1 |
| 1047 |
try: |
| 1048 |
break |
| 1049 |
finally: |
| 1050 |
continue |
| 1051 |
self.assertEqual(count, 2) |
| 1052 |
|
| 1053 |
count = 0 |
| 1054 |
while count < 2: |
| 1055 |
count += 1 |
| 1056 |
try: |
| 1057 |
1/0 |
| 1058 |
finally: |
| 1059 |
continue |
| 1060 |
break |
| 1061 |
self.assertEqual(count, 2) |
| 1062 |
|
| 1063 |
for count in [0, 1]: |
| 1064 |
try: |
| 1065 |
pass |
| 1066 |
finally: |
| 1067 |
continue |
| 1068 |
break |
| 1069 |
self.assertEqual(count, 1) |
| 1070 |
|
| 1071 |
for count in [0, 1]: |
| 1072 |
try: |
| 1073 |
break |
| 1074 |
finally: |
| 1075 |
continue |
| 1076 |
self.assertEqual(count, 1) |
| 1077 |
|
| 1078 |
for count in [0, 1]: |
| 1079 |
try: |
| 1080 |
1/0 |
| 1081 |
finally: |
| 1082 |
continue |
| 1083 |
break |
| 1084 |
self.assertEqual(count, 1) |
| 1085 |
|
| 1086 |
def test_return_in_finally(self): |
| 1087 |
def g1(): |
| 1088 |
try: |
| 1089 |
pass |
| 1090 |
finally: |
| 1091 |
return 1 |
| 1092 |
self.assertEqual(g1(), 1) |
| 1093 |
|
| 1094 |
def g2(): |
| 1095 |
try: |
| 1096 |
return 2 |
| 1097 |
finally: |
| 1098 |
return 3 |
| 1099 |
self.assertEqual(g2(), 3) |
| 1100 |
|
| 1101 |
def g3(): |
| 1102 |
try: |
| 1103 |
1/0 |
| 1104 |
finally: |
| 1105 |
return 4 |
| 1106 |
self.assertEqual(g3(), 4) |
| 1107 |
|
| 1108 |
def test_break_in_finally_after_return(self): |
| 1109 |
|
| 1110 |
def g1(x): |
| 1111 |
for count in [0, 1]: |
| 1112 |
count2 = 0 |
| 1113 |
while count2 < 20: |
| 1114 |
count2 += 10 |
| 1115 |
try: |
| 1116 |
return count + count2 |
| 1117 |
finally: |
| 1118 |
if x: |
| 1119 |
break |
| 1120 |
return 'end', count, count2 |
| 1121 |
self.assertEqual(g1(False), 10) |
| 1122 |
self.assertEqual(g1(True), ('end', 1, 10)) |
| 1123 |
|
| 1124 |
def g2(x): |
| 1125 |
for count in [0, 1]: |
| 1126 |
for count2 in [10, 20]: |
| 1127 |
try: |
| 1128 |
return count + count2 |
| 1129 |
finally: |
| 1130 |
if x: |
| 1131 |
break |
| 1132 |
return 'end', count, count2 |
| 1133 |
self.assertEqual(g2(False), 10) |
| 1134 |
self.assertEqual(g2(True), ('end', 1, 10)) |
| 1135 |
|
| 1136 |
def test_continue_in_finally_after_return(self): |
| 1137 |
|
| 1138 |
def g1(x): |
| 1139 |
count = 0 |
| 1140 |
while count < 100: |
| 1141 |
count += 1 |
| 1142 |
try: |
| 1143 |
return count |
| 1144 |
finally: |
| 1145 |
if x: |
| 1146 |
continue |
| 1147 |
return 'end', count |
| 1148 |
self.assertEqual(g1(False), 1) |
| 1149 |
self.assertEqual(g1(True), ('end', 100)) |
| 1150 |
|
| 1151 |
def g2(x): |
| 1152 |
for count in [0, 1]: |
| 1153 |
try: |
| 1154 |
return count |
| 1155 |
finally: |
| 1156 |
if x: |
| 1157 |
continue |
| 1158 |
return 'end', count |
| 1159 |
self.assertEqual(g2(False), 0) |
| 1160 |
self.assertEqual(g2(True), ('end', 1)) |
| 1161 |
|
| 1162 |
def test_yield(self): |
| 1163 |
|
| 1164 |
def g(): yield 1 |
| 1165 |
def g(): yield from () |
| 1166 |
|
| 1167 |
def g(): x = yield 1 |
| 1168 |
def g(): x = yield from () |
| 1169 |
|
| 1170 |
def g(): yield 1, 1 |
| 1171 |
def g(): x = yield 1, 1 |
| 1172 |
|
| 1173 |
check_syntax_error(self, "def g(): yield from (), 1") |
| 1174 |
check_syntax_error(self, "def g(): x = yield from (), 1") |
| 1175 |
|
| 1176 |
def g(): 1, (yield 1) |
| 1177 |
def g(): 1, (yield from ()) |
| 1178 |
check_syntax_error(self, "def g(): 1, yield 1") |
| 1179 |
check_syntax_error(self, "def g(): 1, yield from ()") |
| 1180 |
|
| 1181 |
def g(): f((yield 1)) |
| 1182 |
def g(): f((yield 1), 1) |
| 1183 |
def g(): f((yield from ())) |
| 1184 |
def g(): f((yield from ()), 1) |
| 1185 |
|
| 1186 |
def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest |
| 1187 |
self.assertEqual(list(g()), [(1, 2, 3, 4, 5, 6)]) |
| 1188 |
check_syntax_error(self, "def g(): f(yield 1)") |
| 1189 |
check_syntax_error(self, "def g(): f(yield 1, 1)") |
| 1190 |
check_syntax_error(self, "def g(): f(yield from ())") |
| 1191 |
check_syntax_error(self, "def g(): f(yield from (), 1)") |
| 1192 |
|
| 1193 |
check_syntax_error(self, "yield") |
| 1194 |
check_syntax_error(self, "yield from") |
| 1195 |
|
| 1196 |
check_syntax_error(self, "class foo:yield 1") |
| 1197 |
check_syntax_error(self, "class foo:yield from ()") |
| 1198 |
|
| 1199 |
check_syntax_error(self, "def g(a:(yield)): pass") |
| 1200 |
|
| 1201 |
def test_yield_in_comprehensions(self): |
| 1202 |
|
| 1203 |
def g(): [x for x in [(yield 1)]] |
| 1204 |
def g(): [x for x in [(yield from ())]] |
| 1205 |
|
| 1206 |
check = self.check_syntax_error |
| 1207 |
check("def g(): [(yield x) for x in ()]", |
| 1208 |
"'yield' inside list comprehension") |
| 1209 |
check("def g(): [x for x in () if not (yield x)]", |
| 1210 |
"'yield' inside list comprehension") |
| 1211 |
check("def g(): [y for x in () for y in [(yield x)]]", |
| 1212 |
"'yield' inside list comprehension") |
| 1213 |
check("def g(): {(yield x) for x in ()}", |
| 1214 |
"'yield' inside set comprehension") |
| 1215 |
check("def g(): {(yield x): x for x in ()}", |
| 1216 |
"'yield' inside dict comprehension") |
| 1217 |
check("def g(): {x: (yield x) for x in ()}", |
| 1218 |
"'yield' inside dict comprehension") |
| 1219 |
check("def g(): ((yield x) for x in ())", |
| 1220 |
"'yield' inside generator expression") |
| 1221 |
check("def g(): [(yield from x) for x in ()]", |
| 1222 |
"'yield' inside list comprehension") |
| 1223 |
check("class C: [(yield x) for x in ()]", |
| 1224 |
"'yield' inside list comprehension") |
| 1225 |
check("[(yield x) for x in ()]", |
| 1226 |
"'yield' inside list comprehension") |
| 1227 |
|
| 1228 |
def test_raise(self): |
| 1229 |
|
| 1230 |
try: raise RuntimeError('just testing') |
| 1231 |
except RuntimeError: pass |
| 1232 |
try: raise KeyboardInterrupt |
| 1233 |
except KeyboardInterrupt: pass |
| 1234 |
|
| 1235 |
def test_import(self): |
| 1236 |
|
| 1237 |
import sys |
| 1238 |
import time, sys |
| 1239 |
|
| 1240 |
from time import time |
| 1241 |
from time import (time) |
| 1242 |
|
| 1243 |
|
| 1244 |
from sys import path, argv |
| 1245 |
from sys import (path, argv) |
| 1246 |
from sys import (path, argv,) |
| 1247 |
|
| 1248 |
def test_global(self): |
| 1249 |
|
| 1250 |
global a |
| 1251 |
global a, b |
| 1252 |
global one, two, three, four, five, six, seven, eight, nine, ten |
| 1253 |
|
| 1254 |
def test_nonlocal(self): |
| 1255 |
|
| 1256 |
x = 0 |
| 1257 |
y = 0 |
| 1258 |
def f(): |
| 1259 |
nonlocal x |
| 1260 |
nonlocal x, y |
| 1261 |
|
| 1262 |
def test_assert(self): |
| 1263 |
|
| 1264 |
assert 1 |
| 1265 |
assert 1, 1 |
| 1266 |
assert lambda x:x |
| 1267 |
assert 1, lambda x:x+1 |
| 1268 |
|
| 1269 |
try: |
| 1270 |
assert True |
| 1271 |
except AssertionError as e: |
| 1272 |
self.fail("'assert True' should not have raised an AssertionError") |
| 1273 |
|
| 1274 |
try: |
| 1275 |
assert True, 'this should always pass' |
| 1276 |
except AssertionError as e: |
| 1277 |
self.fail("'assert True, msg' should not have " |
| 1278 |
"raised an AssertionError") |
| 1279 |
|
| 1280 |
|
| 1281 |
@unittest.skipUnless(__debug__, "Won't work if __debug__ is False") |
| 1282 |
def test_assert_failures(self): |
| 1283 |
try: |
| 1284 |
assert 0, "msg" |
| 1285 |
except AssertionError as e: |
| 1286 |
self.assertEqual(e.args[0], "msg") |
| 1287 |
else: |
| 1288 |
self.fail("AssertionError not raised by assert 0") |
| 1289 |
|
| 1290 |
try: |
| 1291 |
assert False |
| 1292 |
except AssertionError as e: |
| 1293 |
self.assertEqual(len(e.args), 0) |
| 1294 |
else: |
| 1295 |
self.fail("AssertionError not raised by 'assert False'") |
| 1296 |
|
| 1297 |
def test_assert_syntax_warnings(self): |
| 1298 |
|
| 1299 |
|
| 1300 |
self.check_syntax_warning('assert(x, "msg")', |
| 1301 |
'assertion is always true') |
| 1302 |
self.check_syntax_warning('assert(False, "msg")', |
| 1303 |
'assertion is always true') |
| 1304 |
self.check_syntax_warning('assert(False,)', |
| 1305 |
'assertion is always true') |
| 1306 |
|
| 1307 |
with self.check_no_warnings(category=SyntaxWarning): |
| 1308 |
compile('assert x, "msg"', '<testcase>', 'exec') |
| 1309 |
compile('assert False, "msg"', '<testcase>', 'exec') |
| 1310 |
|
| 1311 |
def test_assert_warning_promotes_to_syntax_error(self): |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
with warnings.catch_warnings(): |
| 1316 |
warnings.simplefilter('error', SyntaxWarning) |
| 1317 |
try: |
| 1318 |
compile('assert x, "msg" ', '<testcase>', 'exec') |
| 1319 |
except SyntaxError: |
| 1320 |
self.fail('SyntaxError incorrectly raised for \'assert x, "msg"\'') |
| 1321 |
with self.assertRaises(SyntaxError): |
| 1322 |
compile('assert(x, "msg")', '<testcase>', 'exec') |
| 1323 |
with self.assertRaises(SyntaxError): |
| 1324 |
compile('assert(False, "msg")', '<testcase>', 'exec') |
| 1325 |
with self.assertRaises(SyntaxError): |
| 1326 |
compile('assert(False,)', '<testcase>', 'exec') |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
def test_if(self): |
| 1333 |
|
| 1334 |
if 1: pass |
| 1335 |
if 1: pass |
| 1336 |
else: pass |
| 1337 |
if 0: pass |
| 1338 |
elif 0: pass |
| 1339 |
if 0: pass |
| 1340 |
elif 0: pass |
| 1341 |
elif 0: pass |
| 1342 |
elif 0: pass |
| 1343 |
else: pass |
| 1344 |
|
| 1345 |
def test_while(self): |
| 1346 |
|
| 1347 |
while 0: pass |
| 1348 |
while 0: pass |
| 1349 |
else: pass |
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
x = 0 |
| 1354 |
while 0: |
| 1355 |
x = 1 |
| 1356 |
else: |
| 1357 |
x = 2 |
| 1358 |
self.assertEqual(x, 2) |
| 1359 |
|
| 1360 |
def test_for(self): |
| 1361 |
|
| 1362 |
for i in 1, 2, 3: pass |
| 1363 |
for i, j, k in (): pass |
| 1364 |
else: pass |
| 1365 |
class Squares: |
| 1366 |
def __init__(self, max): |
| 1367 |
self.max = max |
| 1368 |
self.sofar = [] |
| 1369 |
def __len__(self): return len(self.sofar) |
| 1370 |
def __getitem__(self, i): |
| 1371 |
if not 0 <= i < self.max: raise IndexError |
| 1372 |
n = len(self.sofar) |
| 1373 |
while n <= i: |
| 1374 |
self.sofar.append(n*n) |
| 1375 |
n = n+1 |
| 1376 |
return self.sofar[i] |
| 1377 |
n = 0 |
| 1378 |
for x in Squares(10): n = n+x |
| 1379 |
if n != 285: |
| 1380 |
self.fail('for over growing sequence') |
| 1381 |
|
| 1382 |
result = [] |
| 1383 |
for x, in [(1,), (2,), (3,)]: |
| 1384 |
result.append(x) |
| 1385 |
self.assertEqual(result, [1, 2, 3]) |
| 1386 |
|
| 1387 |
result = [] |
| 1388 |
a = b = c = [1, 2, 3] |
| 1389 |
for x in *a, *b, *c: |
| 1390 |
result.append(x) |
| 1391 |
self.assertEqual(result, 3 * a) |
| 1392 |
|
| 1393 |
def test_try(self): |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
try: |
| 1398 |
1/0 |
| 1399 |
except ZeroDivisionError: |
| 1400 |
pass |
| 1401 |
else: |
| 1402 |
pass |
| 1403 |
try: 1/0 |
| 1404 |
except EOFError: pass |
| 1405 |
except TypeError as msg: pass |
| 1406 |
except: pass |
| 1407 |
else: pass |
| 1408 |
try: 1/0 |
| 1409 |
except (EOFError, TypeError, ZeroDivisionError): pass |
| 1410 |
try: 1/0 |
| 1411 |
except (EOFError, TypeError, ZeroDivisionError) as msg: pass |
| 1412 |
try: pass |
| 1413 |
finally: pass |
| 1414 |
with self.assertRaises(SyntaxError): |
| 1415 |
compile("try:\n pass\nexcept Exception as a.b:\n pass", "?", "exec") |
| 1416 |
compile("try:\n pass\nexcept Exception as a[b]:\n pass", "?", "exec") |
| 1417 |
|
| 1418 |
def test_try_star(self): |
| 1419 |
|
| 1420 |
|
| 1421 |
try: |
| 1422 |
1/0 |
| 1423 |
except* ZeroDivisionError: |
| 1424 |
pass |
| 1425 |
else: |
| 1426 |
pass |
| 1427 |
try: 1/0 |
| 1428 |
except* EOFError: pass |
| 1429 |
except* ZeroDivisionError as msg: pass |
| 1430 |
else: pass |
| 1431 |
try: 1/0 |
| 1432 |
except* (EOFError, TypeError, ZeroDivisionError): pass |
| 1433 |
try: 1/0 |
| 1434 |
except* (EOFError, TypeError, ZeroDivisionError) as msg: pass |
| 1435 |
try: pass |
| 1436 |
finally: pass |
| 1437 |
with self.assertRaises(SyntaxError): |
| 1438 |
compile("try:\n pass\nexcept* Exception as a.b:\n pass", "?", "exec") |
| 1439 |
compile("try:\n pass\nexcept* Exception as a[b]:\n pass", "?", "exec") |
| 1440 |
compile("try:\n pass\nexcept*:\n pass", "?", "exec") |
| 1441 |
|
| 1442 |
def test_suite(self): |
| 1443 |
|
| 1444 |
if 1: pass |
| 1445 |
if 1: |
| 1446 |
pass |
| 1447 |
if 1: |
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
pass |
| 1452 |
pass |
| 1453 |
|
| 1454 |
pass |
| 1455 |
|
| 1456 |
|
| 1457 |
def test_test(self): |
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
if not 1: pass |
| 1462 |
if 1 and 1: pass |
| 1463 |
if 1 or 1: pass |
| 1464 |
if not not not 1: pass |
| 1465 |
if not 1 and 1 and 1: pass |
| 1466 |
if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |
| 1467 |
|
| 1468 |
def test_comparison(self): |
| 1469 |
|
| 1470 |
|
| 1471 |
if 1: pass |
| 1472 |
x = (1 == 1) |
| 1473 |
if 1 == 1: pass |
| 1474 |
if 1 != 1: pass |
| 1475 |
if 1 < 1: pass |
| 1476 |
if 1 > 1: pass |
| 1477 |
if 1 <= 1: pass |
| 1478 |
if 1 >= 1: pass |
| 1479 |
if x is x: pass |
| 1480 |
if x is not x: pass |
| 1481 |
if 1 in (): pass |
| 1482 |
if 1 not in (): pass |
| 1483 |
if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in x is x is not x: pass |
| 1484 |
|
| 1485 |
def test_comparison_is_literal(self): |
| 1486 |
def check(test, msg='"is" with a literal'): |
| 1487 |
self.check_syntax_warning(test, msg) |
| 1488 |
|
| 1489 |
check('x is 1') |
| 1490 |
check('x is "thing"') |
| 1491 |
check('1 is x') |
| 1492 |
check('x is y is 1') |
| 1493 |
check('x is not 1', '"is not" with a literal') |
| 1494 |
|
| 1495 |
with warnings.catch_warnings(): |
| 1496 |
warnings.simplefilter('error', SyntaxWarning) |
| 1497 |
compile('x is None', '<testcase>', 'exec') |
| 1498 |
compile('x is False', '<testcase>', 'exec') |
| 1499 |
compile('x is True', '<testcase>', 'exec') |
| 1500 |
compile('x is ...', '<testcase>', 'exec') |
| 1501 |
|
| 1502 |
def test_warn_missed_comma(self): |
| 1503 |
def check(test): |
| 1504 |
self.check_syntax_warning(test, msg) |
| 1505 |
|
| 1506 |
msg=r'is not callable; perhaps you missed a comma\?' |
| 1507 |
check('[(1, 2) (3, 4)]') |
| 1508 |
check('[(x, y) (3, 4)]') |
| 1509 |
check('[[1, 2] (3, 4)]') |
| 1510 |
check('[{1, 2} (3, 4)]') |
| 1511 |
check('[{1: 2} (3, 4)]') |
| 1512 |
check('[[i for i in range(5)] (3, 4)]') |
| 1513 |
check('[{i for i in range(5)} (3, 4)]') |
| 1514 |
check('[(i for i in range(5)) (3, 4)]') |
| 1515 |
check('[{i: i for i in range(5)} (3, 4)]') |
| 1516 |
check('[f"{x}" (3, 4)]') |
| 1517 |
check('[f"x={x}" (3, 4)]') |
| 1518 |
check('["abc" (3, 4)]') |
| 1519 |
check('[b"abc" (3, 4)]') |
| 1520 |
check('[123 (3, 4)]') |
| 1521 |
check('[12.3 (3, 4)]') |
| 1522 |
check('[12.3j (3, 4)]') |
| 1523 |
check('[None (3, 4)]') |
| 1524 |
check('[True (3, 4)]') |
| 1525 |
check('[... (3, 4)]') |
| 1526 |
|
| 1527 |
msg=r'is not subscriptable; perhaps you missed a comma\?' |
| 1528 |
check('[{1, 2} [i, j]]') |
| 1529 |
check('[{i for i in range(5)} [i, j]]') |
| 1530 |
check('[(i for i in range(5)) [i, j]]') |
| 1531 |
check('[(lambda x, y: x) [i, j]]') |
| 1532 |
check('[123 [i, j]]') |
| 1533 |
check('[12.3 [i, j]]') |
| 1534 |
check('[12.3j [i, j]]') |
| 1535 |
check('[None [i, j]]') |
| 1536 |
check('[True [i, j]]') |
| 1537 |
check('[... [i, j]]') |
| 1538 |
|
| 1539 |
msg=r'indices must be integers or slices, not tuple; perhaps you missed a comma\?' |
| 1540 |
check('[(1, 2) [i, j]]') |
| 1541 |
check('[(x, y) [i, j]]') |
| 1542 |
check('[[1, 2] [i, j]]') |
| 1543 |
check('[[i for i in range(5)] [i, j]]') |
| 1544 |
check('[f"{x}" [i, j]]') |
| 1545 |
check('[f"x={x}" [i, j]]') |
| 1546 |
check('["abc" [i, j]]') |
| 1547 |
check('[b"abc" [i, j]]') |
| 1548 |
|
| 1549 |
msg=r'indices must be integers or slices, not tuple;' |
| 1550 |
check('[[1, 2] [3, 4]]') |
| 1551 |
msg=r'indices must be integers or slices, not list;' |
| 1552 |
check('[[1, 2] [[3, 4]]]') |
| 1553 |
check('[[1, 2] [[i for i in range(5)]]]') |
| 1554 |
msg=r'indices must be integers or slices, not set;' |
| 1555 |
check('[[1, 2] [{3, 4}]]') |
| 1556 |
check('[[1, 2] [{i for i in range(5)}]]') |
| 1557 |
msg=r'indices must be integers or slices, not dict;' |
| 1558 |
check('[[1, 2] [{3: 4}]]') |
| 1559 |
check('[[1, 2] [{i: i for i in range(5)}]]') |
| 1560 |
msg=r'indices must be integers or slices, not generator;' |
| 1561 |
check('[[1, 2] [(i for i in range(5))]]') |
| 1562 |
msg=r'indices must be integers or slices, not function;' |
| 1563 |
|
| 1564 |
|
| 1565 |
msg=r'indices must be integers or slices, not str;' |
| 1566 |
check('[[1, 2] [f"{x}"]]') |
| 1567 |
check('[[1, 2] [f"x={x}"]]') |
| 1568 |
check('[[1, 2] ["abc"]]') |
| 1569 |
msg=r'indices must be integers or slices, not' |
| 1570 |
check('[[1, 2] [b"abc"]]') |
| 1571 |
check('[[1, 2] [12.3]]') |
| 1572 |
check('[[1, 2] [12.3j]]') |
| 1573 |
check('[[1, 2] [None]]') |
| 1574 |
check('[[1, 2] [...]]') |
| 1575 |
|
| 1576 |
with warnings.catch_warnings(): |
| 1577 |
warnings.simplefilter('error', SyntaxWarning) |
| 1578 |
compile('[(lambda x, y: x) (3, 4)]', '<testcase>', 'exec') |
| 1579 |
compile('[[1, 2] [i]]', '<testcase>', 'exec') |
| 1580 |
compile('[[1, 2] [0]]', '<testcase>', 'exec') |
| 1581 |
compile('[[1, 2] [True]]', '<testcase>', 'exec') |
| 1582 |
compile('[[1, 2] [1:2]]', '<testcase>', 'exec') |
| 1583 |
compile('[{(1, 2): 3} [i, j]]', '<testcase>', 'exec') |
| 1584 |
|
| 1585 |
def test_binary_mask_ops(self): |
| 1586 |
x = 1 & 1 |
| 1587 |
x = 1 ^ 1 |
| 1588 |
x = 1 | 1 |
| 1589 |
|
| 1590 |
def test_shift_ops(self): |
| 1591 |
x = 1 << 1 |
| 1592 |
x = 1 >> 1 |
| 1593 |
x = 1 << 1 >> 1 |
| 1594 |
|
| 1595 |
def test_additive_ops(self): |
| 1596 |
x = 1 |
| 1597 |
x = 1 + 1 |
| 1598 |
x = 1 - 1 - 1 |
| 1599 |
x = 1 - 1 + 1 - 1 + 1 |
| 1600 |
|
| 1601 |
def test_multiplicative_ops(self): |
| 1602 |
x = 1 * 1 |
| 1603 |
x = 1 / 1 |
| 1604 |
x = 1 % 1 |
| 1605 |
x = 1 / 1 * 1 % 1 |
| 1606 |
|
| 1607 |
def test_unary_ops(self): |
| 1608 |
x = +1 |
| 1609 |
x = -1 |
| 1610 |
x = ~1 |
| 1611 |
x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
| 1612 |
x = -1*1/1 + 1*1 - ---1*1 |
| 1613 |
|
| 1614 |
def test_selectors(self): |
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
import sys, time |
| 1619 |
c = sys.path[0] |
| 1620 |
x = time.time() |
| 1621 |
x = sys.modules['time'].time() |
| 1622 |
a = '01234' |
| 1623 |
c = a[0] |
| 1624 |
c = a[-1] |
| 1625 |
s = a[0:5] |
| 1626 |
s = a[:5] |
| 1627 |
s = a[0:] |
| 1628 |
s = a[:] |
| 1629 |
s = a[-5:] |
| 1630 |
s = a[:-1] |
| 1631 |
s = a[-4:-3] |
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
d = {} |
| 1636 |
d[1] = 1 |
| 1637 |
d[1,] = 2 |
| 1638 |
d[1,2] = 3 |
| 1639 |
d[1,2,3] = 4 |
| 1640 |
L = list(d) |
| 1641 |
L.sort(key=lambda x: (type(x).__name__, x)) |
| 1642 |
self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') |
| 1643 |
|
| 1644 |
def test_atoms(self): |
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
x = (1) |
| 1649 |
x = (1 or 2 or 3) |
| 1650 |
x = (1 or 2 or 3, 2, 3) |
| 1651 |
|
| 1652 |
x = [] |
| 1653 |
x = [1] |
| 1654 |
x = [1 or 2 or 3] |
| 1655 |
x = [1 or 2 or 3, 2, 3] |
| 1656 |
x = [] |
| 1657 |
|
| 1658 |
x = {} |
| 1659 |
x = {'one': 1} |
| 1660 |
x = {'one': 1,} |
| 1661 |
x = {'one' or 'two': 1 or 2} |
| 1662 |
x = {'one': 1, 'two': 2} |
| 1663 |
x = {'one': 1, 'two': 2,} |
| 1664 |
x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |
| 1665 |
|
| 1666 |
x = {'one'} |
| 1667 |
x = {'one', 1,} |
| 1668 |
x = {'one', 'two', 'three'} |
| 1669 |
x = {2, 3, 4,} |
| 1670 |
|
| 1671 |
x = x |
| 1672 |
x = 'x' |
| 1673 |
x = 123 |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
def test_classdef(self): |
| 1680 |
|
| 1681 |
class B: pass |
| 1682 |
class B2(): pass |
| 1683 |
class C1(B): pass |
| 1684 |
class C2(B): pass |
| 1685 |
class D(C1, C2, B): pass |
| 1686 |
class C: |
| 1687 |
def meth1(self): pass |
| 1688 |
def meth2(self, arg): pass |
| 1689 |
def meth3(self, a1, a2): pass |
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
def class_decorator(x): return x |
| 1695 |
@class_decorator |
| 1696 |
class G: pass |
| 1697 |
|
| 1698 |
|
| 1699 |
@False or class_decorator |
| 1700 |
class H: pass |
| 1701 |
@d := class_decorator |
| 1702 |
class I: pass |
| 1703 |
@lambda c: class_decorator(c) |
| 1704 |
class J: pass |
| 1705 |
@[..., class_decorator, ...][1] |
| 1706 |
class K: pass |
| 1707 |
@class_decorator(class_decorator)(class_decorator) |
| 1708 |
class L: pass |
| 1709 |
@[class_decorator][0].__call__.__call__ |
| 1710 |
class M: pass |
| 1711 |
|
| 1712 |
def test_dictcomps(self): |
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
nums = [1, 2, 3] |
| 1717 |
self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) |
| 1718 |
|
| 1719 |
def test_listcomps(self): |
| 1720 |
|
| 1721 |
nums = [1, 2, 3, 4, 5] |
| 1722 |
strs = ["Apple", "Banana", "Coconut"] |
| 1723 |
spcs = [" Apple", " Banana ", "Coco nut "] |
| 1724 |
|
| 1725 |
self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) |
| 1726 |
self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) |
| 1727 |
self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) |
| 1728 |
self.assertEqual([(i, s) for i in nums for s in strs], |
| 1729 |
[(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), |
| 1730 |
(2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), |
| 1731 |
(3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), |
| 1732 |
(4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), |
| 1733 |
(5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) |
| 1734 |
self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], |
| 1735 |
[(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), |
| 1736 |
(3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), |
| 1737 |
(5, 'Banana'), (5, 'Coconut')]) |
| 1738 |
self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], |
| 1739 |
[[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) |
| 1740 |
|
| 1741 |
def test_in_func(l): |
| 1742 |
return [0 < x < 3 for x in l if x > 2] |
| 1743 |
|
| 1744 |
self.assertEqual(test_in_func(nums), [False, False, False]) |
| 1745 |
|
| 1746 |
def test_nested_front(): |
| 1747 |
self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], |
| 1748 |
[[1, 2], [3, 4], [5, 6]]) |
| 1749 |
|
| 1750 |
test_nested_front() |
| 1751 |
|
| 1752 |
check_syntax_error(self, "[i, s for i in nums for s in strs]") |
| 1753 |
check_syntax_error(self, "[x if y]") |
| 1754 |
|
| 1755 |
suppliers = [ |
| 1756 |
(1, "Boeing"), |
| 1757 |
(2, "Ford"), |
| 1758 |
(3, "Macdonalds") |
| 1759 |
] |
| 1760 |
|
| 1761 |
parts = [ |
| 1762 |
(10, "Airliner"), |
| 1763 |
(20, "Engine"), |
| 1764 |
(30, "Cheeseburger") |
| 1765 |
] |
| 1766 |
|
| 1767 |
suppart = [ |
| 1768 |
(1, 10), (1, 20), (2, 20), (3, 30) |
| 1769 |
] |
| 1770 |
|
| 1771 |
x = [ |
| 1772 |
(sname, pname) |
| 1773 |
for (sno, sname) in suppliers |
| 1774 |
for (pno, pname) in parts |
| 1775 |
for (sp_sno, sp_pno) in suppart |
| 1776 |
if sno == sp_sno and pno == sp_pno |
| 1777 |
] |
| 1778 |
|
| 1779 |
self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), |
| 1780 |
('Macdonalds', 'Cheeseburger')]) |
| 1781 |
|
| 1782 |
def test_genexps(self): |
| 1783 |
|
| 1784 |
g = ([x for x in range(10)] for x in range(1)) |
| 1785 |
self.assertEqual(next(g), [x for x in range(10)]) |
| 1786 |
try: |
| 1787 |
next(g) |
| 1788 |
self.fail('should produce StopIteration exception') |
| 1789 |
except StopIteration: |
| 1790 |
pass |
| 1791 |
|
| 1792 |
a = 1 |
| 1793 |
try: |
| 1794 |
g = (a for d in a) |
| 1795 |
next(g) |
| 1796 |
self.fail('should produce TypeError') |
| 1797 |
except TypeError: |
| 1798 |
pass |
| 1799 |
|
| 1800 |
self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) |
| 1801 |
self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) |
| 1802 |
|
| 1803 |
a = [x for x in range(10)] |
| 1804 |
b = (x for x in (y for y in a)) |
| 1805 |
self.assertEqual(sum(b), sum([x for x in range(10)])) |
| 1806 |
|
| 1807 |
self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) |
| 1808 |
self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) |
| 1809 |
self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) |
| 1810 |
self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) |
| 1811 |
self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) |
| 1812 |
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)])) |
| 1813 |
self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) |
| 1814 |
check_syntax_error(self, "foo(x for x in range(10), 100)") |
| 1815 |
check_syntax_error(self, "foo(100, x for x in range(10))") |
| 1816 |
|
| 1817 |
def test_comprehension_specials(self): |
| 1818 |
|
| 1819 |
x = 10; g = (i for i in range(x)); x = 5 |
| 1820 |
self.assertEqual(len(list(g)), 10) |
| 1821 |
|
| 1822 |
|
| 1823 |
x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) |
| 1824 |
x = 5; t = True; |
| 1825 |
self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) |
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) |
| 1830 |
self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) |
| 1831 |
|
| 1832 |
|
| 1833 |
self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) |
| 1834 |
self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) |
| 1835 |
|
| 1836 |
def test_with_statement(self): |
| 1837 |
class manager(object): |
| 1838 |
def __enter__(self): |
| 1839 |
return (1, 2) |
| 1840 |
def __exit__(self, *args): |
| 1841 |
pass |
| 1842 |
|
| 1843 |
with manager(): |
| 1844 |
pass |
| 1845 |
with manager() as x: |
| 1846 |
pass |
| 1847 |
with manager() as (x, y): |
| 1848 |
pass |
| 1849 |
with manager(), manager(): |
| 1850 |
pass |
| 1851 |
with manager() as x, manager() as y: |
| 1852 |
pass |
| 1853 |
with manager() as x, manager(): |
| 1854 |
pass |
| 1855 |
|
| 1856 |
with ( |
| 1857 |
manager() |
| 1858 |
): |
| 1859 |
pass |
| 1860 |
|
| 1861 |
with ( |
| 1862 |
manager() as x |
| 1863 |
): |
| 1864 |
pass |
| 1865 |
|
| 1866 |
with ( |
| 1867 |
manager() as (x, y), |
| 1868 |
manager() as z, |
| 1869 |
): |
| 1870 |
pass |
| 1871 |
|
| 1872 |
with ( |
| 1873 |
manager(), |
| 1874 |
manager() |
| 1875 |
): |
| 1876 |
pass |
| 1877 |
|
| 1878 |
with ( |
| 1879 |
manager() as x, |
| 1880 |
manager() as y |
| 1881 |
): |
| 1882 |
pass |
| 1883 |
|
| 1884 |
with ( |
| 1885 |
manager() as x, |
| 1886 |
manager() |
| 1887 |
): |
| 1888 |
pass |
| 1889 |
|
| 1890 |
with ( |
| 1891 |
manager() as x, |
| 1892 |
manager() as y, |
| 1893 |
manager() as z, |
| 1894 |
): |
| 1895 |
pass |
| 1896 |
|
| 1897 |
with ( |
| 1898 |
manager() as x, |
| 1899 |
manager() as y, |
| 1900 |
manager(), |
| 1901 |
): |
| 1902 |
pass |
| 1903 |
|
| 1904 |
def test_if_else_expr(self): |
| 1905 |
|
| 1906 |
def _checkeval(msg, ret): |
| 1907 |
"helper to check that evaluation of expressions is done correctly" |
| 1908 |
print(msg) |
| 1909 |
return ret |
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) |
| 1914 |
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]) |
| 1915 |
self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) |
| 1916 |
self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) |
| 1917 |
self.assertEqual((5 and 6 if 0 else 1), 1) |
| 1918 |
self.assertEqual(((5 and 6) if 0 else 1), 1) |
| 1919 |
self.assertEqual((5 and (6 if 1 else 1)), 6) |
| 1920 |
self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) |
| 1921 |
self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) |
| 1922 |
self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) |
| 1923 |
self.assertEqual((not 5 if 1 else 1), False) |
| 1924 |
self.assertEqual((not 5 if 0 else 1), 1) |
| 1925 |
self.assertEqual((6 + 1 if 1 else 2), 7) |
| 1926 |
self.assertEqual((6 - 1 if 1 else 2), 5) |
| 1927 |
self.assertEqual((6 * 2 if 1 else 4), 12) |
| 1928 |
self.assertEqual((6 / 2 if 1 else 3), 3) |
| 1929 |
self.assertEqual((6 < 4 if 0 else 2), 2) |
| 1930 |
|
| 1931 |
def test_paren_evaluation(self): |
| 1932 |
self.assertEqual(16 // (4 // 2), 8) |
| 1933 |
self.assertEqual((16 // 4) // 2, 2) |
| 1934 |
self.assertEqual(16 // 4 // 2, 2) |
| 1935 |
x = 2 |
| 1936 |
y = 3 |
| 1937 |
self.assertTrue(False is (x is y)) |
| 1938 |
self.assertFalse((False is x) is y) |
| 1939 |
self.assertFalse(False is x is y) |
| 1940 |
|
| 1941 |
def test_matrix_mul(self): |
| 1942 |
|
| 1943 |
|
| 1944 |
class M: |
| 1945 |
def __matmul__(self, o): |
| 1946 |
return 4 |
| 1947 |
def __imatmul__(self, o): |
| 1948 |
self.other = o |
| 1949 |
return self |
| 1950 |
m = M() |
| 1951 |
self.assertEqual(m @ m, 4) |
| 1952 |
m @= 42 |
| 1953 |
self.assertEqual(m.other, 42) |
| 1954 |
|
| 1955 |
def test_async_await(self): |
| 1956 |
async def test(): |
| 1957 |
def sum(): |
| 1958 |
pass |
| 1959 |
if 1: |
| 1960 |
await someobj() |
| 1961 |
|
| 1962 |
self.assertEqual(test.__name__, 'test') |
| 1963 |
self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE)) |
| 1964 |
|
| 1965 |
def decorator(func): |
| 1966 |
setattr(func, '_marked', True) |
| 1967 |
return func |
| 1968 |
|
| 1969 |
@decorator |
| 1970 |
async def test2(): |
| 1971 |
return 22 |
| 1972 |
self.assertTrue(test2._marked) |
| 1973 |
self.assertEqual(test2.__name__, 'test2') |
| 1974 |
self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE)) |
| 1975 |
|
| 1976 |
def test_async_for(self): |
| 1977 |
class Done(Exception): pass |
| 1978 |
|
| 1979 |
class AIter: |
| 1980 |
def __aiter__(self): |
| 1981 |
return self |
| 1982 |
async def __anext__(self): |
| 1983 |
raise StopAsyncIteration |
| 1984 |
|
| 1985 |
async def foo(): |
| 1986 |
async for i in AIter(): |
| 1987 |
pass |
| 1988 |
async for i, j in AIter(): |
| 1989 |
pass |
| 1990 |
async for i in AIter(): |
| 1991 |
pass |
| 1992 |
else: |
| 1993 |
pass |
| 1994 |
raise Done |
| 1995 |
|
| 1996 |
with self.assertRaises(Done): |
| 1997 |
foo().send(None) |
| 1998 |
|
| 1999 |
def test_async_with(self): |
| 2000 |
class Done(Exception): pass |
| 2001 |
|
| 2002 |
class manager: |
| 2003 |
async def __aenter__(self): |
| 2004 |
return (1, 2) |
| 2005 |
async def __aexit__(self, *exc): |
| 2006 |
return False |
| 2007 |
|
| 2008 |
async def foo(): |
| 2009 |
async with manager(): |
| 2010 |
pass |
| 2011 |
async with manager() as x: |
| 2012 |
pass |
| 2013 |
async with manager() as (x, y): |
| 2014 |
pass |
| 2015 |
async with manager(), manager(): |
| 2016 |
pass |
| 2017 |
async with manager() as x, manager() as y: |
| 2018 |
pass |
| 2019 |
async with manager() as x, manager(): |
| 2020 |
pass |
| 2021 |
raise Done |
| 2022 |
|
| 2023 |
with self.assertRaises(Done): |
| 2024 |
foo().send(None) |
| 2025 |
|
| 2026 |
|
| 2027 |
if __name__ == '__main__': |
| 2028 |
unittest.main() |