File size: 20,507 Bytes
5a9e79d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index dca3841893..c68b9ce388 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -1352,14 +1352,11 @@ def test_multiline_list_comprehension(self):
and x != 50)]
""")
compiled_code, _ = self.check_positions_against_ast(snippet)
- compiled_code = compiled_code.co_consts[0]
self.assertIsInstance(compiled_code, types.CodeType)
self.assertOpcodeSourcePositionIs(compiled_code, 'LIST_APPEND',
line=1, end_line=2, column=1, end_column=8, occurrence=1)
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
line=1, end_line=2, column=1, end_column=8, occurrence=1)
- self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
- line=1, end_line=6, column=0, end_column=32, occurrence=1)
def test_multiline_async_list_comprehension(self):
snippet = textwrap.dedent("""\
@@ -1374,13 +1371,13 @@ def test_multiline_async_list_comprehension(self):
compiled_code, _ = self.check_positions_against_ast(snippet)
g = {}
eval(compiled_code, g)
- compiled_code = g['f'].__code__.co_consts[1]
+ compiled_code = g['f'].__code__
self.assertIsInstance(compiled_code, types.CodeType)
self.assertOpcodeSourcePositionIs(compiled_code, 'LIST_APPEND',
line=2, end_line=3, column=5, end_column=12, occurrence=1)
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
line=2, end_line=3, column=5, end_column=12, occurrence=1)
- self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
+ self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_CONST',
line=2, end_line=7, column=4, end_column=36, occurrence=1)
def test_multiline_set_comprehension(self):
@@ -1393,14 +1390,11 @@ def test_multiline_set_comprehension(self):
and x != 50)}
""")
compiled_code, _ = self.check_positions_against_ast(snippet)
- compiled_code = compiled_code.co_consts[0]
self.assertIsInstance(compiled_code, types.CodeType)
self.assertOpcodeSourcePositionIs(compiled_code, 'SET_ADD',
line=1, end_line=2, column=1, end_column=8, occurrence=1)
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
line=1, end_line=2, column=1, end_column=8, occurrence=1)
- self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
- line=1, end_line=6, column=0, end_column=32, occurrence=1)
def test_multiline_async_set_comprehension(self):
snippet = textwrap.dedent("""\
@@ -1415,13 +1409,13 @@ def test_multiline_async_set_comprehension(self):
compiled_code, _ = self.check_positions_against_ast(snippet)
g = {}
eval(compiled_code, g)
- compiled_code = g['f'].__code__.co_consts[1]
+ compiled_code = g['f'].__code__
self.assertIsInstance(compiled_code, types.CodeType)
self.assertOpcodeSourcePositionIs(compiled_code, 'SET_ADD',
line=2, end_line=3, column=5, end_column=12, occurrence=1)
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
line=2, end_line=3, column=5, end_column=12, occurrence=1)
- self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
+ self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_CONST',
line=2, end_line=7, column=4, end_column=36, occurrence=1)
def test_multiline_dict_comprehension(self):
@@ -1434,14 +1428,11 @@ def test_multiline_dict_comprehension(self):
and x != 50)}
""")
compiled_code, _ = self.check_positions_against_ast(snippet)
- compiled_code = compiled_code.co_consts[0]
self.assertIsInstance(compiled_code, types.CodeType)
self.assertOpcodeSourcePositionIs(compiled_code, 'MAP_ADD',
line=1, end_line=2, column=1, end_column=7, occurrence=1)
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
line=1, end_line=2, column=1, end_column=7, occurrence=1)
- self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
- line=1, end_line=6, column=0, end_column=32, occurrence=1)
def test_multiline_async_dict_comprehension(self):
snippet = textwrap.dedent("""\
@@ -1456,13 +1447,13 @@ def test_multiline_async_dict_comprehension(self):
compiled_code, _ = self.check_positions_against_ast(snippet)
g = {}
eval(compiled_code, g)
- compiled_code = g['f'].__code__.co_consts[1]
+ compiled_code = g['f'].__code__
self.assertIsInstance(compiled_code, types.CodeType)
self.assertOpcodeSourcePositionIs(compiled_code, 'MAP_ADD',
line=2, end_line=3, column=5, end_column=11, occurrence=1)
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
line=2, end_line=3, column=5, end_column=11, occurrence=1)
- self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
+ self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_CONST',
line=2, end_line=7, column=4, end_column=36, occurrence=1)
def test_matchcase_sequence(self):
@@ -1711,9 +1702,6 @@ def test_column_offset_deduplication(self):
for source in [
"lambda: a",
"(a for b in c)",
- "[a for b in c]",
- "{a for b in c}",
- "{a: b for c in d}",
]:
with self.subTest(source):
code = compile(f"{source}, {source}", "<test>", "eval")
diff --git a/Lib/test/test_compiler_assemble.py b/Lib/test/test_compiler_assemble.py
index 0bd7a09b00..3e2a127de7 100644
--- a/Lib/test/test_compiler_assemble.py
+++ b/Lib/test/test_compiler_assemble.py
@@ -16,7 +16,7 @@ def complete_metadata(self, metadata, filename="myfile.py"):
metadata.setdefault(key, key)
for key in ['consts']:
metadata.setdefault(key, [])
- for key in ['names', 'varnames', 'cellvars', 'freevars']:
+ for key in ['names', 'varnames', 'cellvars', 'freevars', 'fasthidden']:
metadata.setdefault(key, {})
for key in ['argcount', 'posonlyargcount', 'kwonlyargcount']:
metadata.setdefault(key, 0)
@@ -33,6 +33,9 @@ def assemble_test(self, insts, metadata, expected):
expected_metadata = {}
for key, value in metadata.items():
+ if key == "fasthidden":
+ # not exposed on code object
+ continue
if isinstance(value, list):
expected_metadata[key] = tuple(value)
elif isinstance(value, dict):
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index 2f5d67fde8..c90702a408 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -154,7 +154,7 @@ def bug708901():
def bug1333982(x=[]):
- assert 0, ([s for s in x] +
+ assert 0, ((s for s in x) +
1)
pass
@@ -162,7 +162,7 @@ def bug1333982(x=[]):
%3d RESUME 0
%3d LOAD_ASSERTION_ERROR
- LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
+ LOAD_CONST 1 (<code object <genexpr> at 0x..., file "%s", line %d>)
MAKE_FUNCTION 0
LOAD_FAST 0 (x)
GET_ITER
@@ -675,7 +675,7 @@ def _g(x):
def _h(y):
def foo(x):
'''funcdoc'''
- return [x + z for z in y]
+ return list(x + z for z in y)
return foo
dis_nested_0 = """\
@@ -705,13 +705,15 @@ def foo(x):
%3d RESUME 0
-%3d LOAD_CLOSURE 0 (x)
+%3d LOAD_GLOBAL 1 (NULL + list)
+ LOAD_CLOSURE 0 (x)
BUILD_TUPLE 1
- LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
+ LOAD_CONST 1 (<code object <genexpr> at 0x..., file "%s", line %d>)
MAKE_FUNCTION 8 (closure)
LOAD_DEREF 1 (y)
GET_ITER
CALL 0
+ CALL 1
RETURN_VALUE
""" % (dis_nested_0,
__file__,
@@ -723,21 +725,28 @@ def foo(x):
)
dis_nested_2 = """%s
-Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>:
+Disassembly of <code object <genexpr> at 0x..., file "%s", line %d>:
COPY_FREE_VARS 1
-%3d RESUME 0
- BUILD_LIST 0
+%3d RETURN_GENERATOR
+ POP_TOP
+ RESUME 0
LOAD_FAST 0 (.0)
- >> FOR_ITER 7 (to 26)
+ >> FOR_ITER 9 (to 32)
STORE_FAST 1 (z)
LOAD_DEREF 2 (x)
LOAD_FAST 1 (z)
BINARY_OP 0 (+)
- LIST_APPEND 2
- JUMP_BACKWARD 9 (to 8)
+ YIELD_VALUE 1
+ RESUME 1
+ POP_TOP
+ JUMP_BACKWARD 11 (to 10)
>> END_FOR
- RETURN_VALUE
+ RETURN_CONST 0 (None)
+ >> CALL_INTRINSIC_1 3 (INTRINSIC_STOPITERATION_ERROR)
+ RERAISE 1
+ExceptionTable:
+1 row
""" % (dis_nested_1,
__file__,
_h.__code__.co_firstlineno + 3,
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index d2b2f3171e..364f75db90 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -4280,14 +4280,14 @@ def test(*args, **kwargs):
@cpython_only
def test_signature_bind_implicit_arg(self):
- # Issue #19611: getcallargs should work with set comprehensions
+ # Issue #19611: getcallargs should work with comprehensions
def make_set():
- return {z * z for z in range(5)}
- setcomp_code = make_set.__code__.co_consts[1]
- setcomp_func = types.FunctionType(setcomp_code, {})
+ return set(z * z for z in range(5))
+ gencomp_code = make_set.__code__.co_consts[1]
+ gencomp_func = types.FunctionType(gencomp_code, {})
iterator = iter(range(5))
- self.assertEqual(self.call(setcomp_func, iterator), {0, 1, 4, 9, 16})
+ self.assertEqual(set(self.call(gencomp_func, iterator)), {0, 1, 4, 9, 16})
def test_signature_bind_posonly_kwargs(self):
def foo(bar, /, **kwargs):
diff --git a/Lib/test/test_listcomps.py b/Lib/test/test_listcomps.py
index 91bf2547ed..92fed98dd0 100644
--- a/Lib/test/test_listcomps.py
+++ b/Lib/test/test_listcomps.py
@@ -1,4 +1,5 @@
import doctest
+import textwrap
import unittest
@@ -87,63 +88,227 @@
>>> [None for i in range(10)]
[None, None, None, None, None, None, None, None, None, None]
-########### Tests for various scoping corner cases ############
-
-Return lambdas that use the iteration variable as a default argument
-
- >>> items = [(lambda i=i: i) for i in range(5)]
- >>> [x() for x in items]
- [0, 1, 2, 3, 4]
-
-Same again, only this time as a closure variable
-
- >>> items = [(lambda: i) for i in range(5)]
- >>> [x() for x in items]
- [4, 4, 4, 4, 4]
-
-Another way to test that the iteration variable is local to the list comp
-
- >>> items = [(lambda: i) for i in range(5)]
- >>> i = 20
- >>> [x() for x in items]
- [4, 4, 4, 4, 4]
-
-And confirm that a closure can jump over the list comp scope
-
- >>> items = [(lambda: y) for i in range(5)]
- >>> y = 2
- >>> [x() for x in items]
- [2, 2, 2, 2, 2]
-
-We also repeat each of the above scoping tests inside a function
-
- >>> def test_func():
- ... items = [(lambda i=i: i) for i in range(5)]
- ... return [x() for x in items]
- >>> test_func()
- [0, 1, 2, 3, 4]
+"""
- >>> def test_func():
- ... items = [(lambda: i) for i in range(5)]
- ... return [x() for x in items]
- >>> test_func()
- [4, 4, 4, 4, 4]
-
- >>> def test_func():
- ... items = [(lambda: i) for i in range(5)]
- ... i = 20
- ... return [x() for x in items]
- >>> test_func()
- [4, 4, 4, 4, 4]
-
- >>> def test_func():
- ... items = [(lambda: y) for i in range(5)]
- ... y = 2
- ... return [x() for x in items]
- >>> test_func()
- [2, 2, 2, 2, 2]
-"""
+class ListComprehensionTest(unittest.TestCase):
+ def _check_in_scopes(self, code, outputs=None, ns=None, scopes=None, raises=()):
+ code = textwrap.dedent(code)
+ scopes = scopes or ["module", "class", "function"]
+ for scope in scopes:
+ with self.subTest(scope=scope):
+ if scope == "class":
+ newcode = textwrap.dedent("""
+ class _C:
+ {code}
+ """).format(code=textwrap.indent(code, " "))
+ def get_output(moddict, name):
+ return getattr(moddict["_C"], name)
+ elif scope == "function":
+ newcode = textwrap.dedent("""
+ def _f():
+ {code}
+ return locals()
+ _out = _f()
+ """).format(code=textwrap.indent(code, " "))
+ def get_output(moddict, name):
+ return moddict["_out"][name]
+ else:
+ newcode = code
+ def get_output(moddict, name):
+ return moddict[name]
+ ns = ns or {}
+ try:
+ exec(newcode, ns)
+ except raises as e:
+ # We care about e.g. NameError vs UnboundLocalError
+ self.assertIs(type(e), raises)
+ else:
+ for k, v in (outputs or {}).items():
+ self.assertEqual(get_output(ns, k), v)
+
+ def test_lambdas_with_iteration_var_as_default(self):
+ code = """
+ items = [(lambda i=i: i) for i in range(5)]
+ y = [x() for x in items]
+ """
+ outputs = {"y": [0, 1, 2, 3, 4]}
+ self._check_in_scopes(code, outputs)
+
+ def test_lambdas_with_free_var(self):
+ code = """
+ items = [(lambda: i) for i in range(5)]
+ y = [x() for x in items]
+ """
+ outputs = {"y": [4, 4, 4, 4, 4]}
+ self._check_in_scopes(code, outputs)
+
+ def test_class_scope_free_var_with_class_cell(self):
+ class C:
+ def method(self):
+ super()
+ return __class__
+ items = [(lambda: i) for i in range(5)]
+ y = [x() for x in items]
+
+ self.assertEqual(C.y, [4, 4, 4, 4, 4])
+ self.assertIs(C().method(), C)
+
+ def test_inner_cell_shadows_outer(self):
+ code = """
+ items = [(lambda: i) for i in range(5)]
+ i = 20
+ y = [x() for x in items]
+ """
+ outputs = {"y": [4, 4, 4, 4, 4], "i": 20}
+ self._check_in_scopes(code, outputs)
+
+ def test_closure_can_jump_over_comp_scope(self):
+ code = """
+ items = [(lambda: y) for i in range(5)]
+ y = 2
+ z = [x() for x in items]
+ """
+ outputs = {"z": [2, 2, 2, 2, 2]}
+ self._check_in_scopes(code, outputs)
+
+ def test_inner_cell_shadows_outer_redefined(self):
+ code = """
+ y = 10
+ items = [(lambda: y) for y in range(5)]
+ x = y
+ y = 20
+ out = [z() for z in items]
+ """
+ outputs = {"x": 10, "out": [4, 4, 4, 4, 4]}
+ self._check_in_scopes(code, outputs)
+
+ def test_shadows_outer_cell(self):
+ code = """
+ def inner():
+ return g
+ [g for g in range(5)]
+ x = inner()
+ """
+ outputs = {"x": -1}
+ self._check_in_scopes(code, outputs, ns={"g": -1})
+
+ def test_assignment_expression(self):
+ code = """
+ x = -1
+ items = [(x:=y) for y in range(3)]
+ """
+ outputs = {"x": 2}
+ # assignment expression in comprehension is disallowed in class scope
+ self._check_in_scopes(code, outputs, scopes=["module", "function"])
+
+ def test_free_var_in_comp_child(self):
+ code = """
+ lst = range(3)
+ funcs = [lambda: x for x in lst]
+ inc = [x + 1 for x in lst]
+ [x for x in inc]
+ x = funcs[0]()
+ """
+ outputs = {"x": 2}
+ self._check_in_scopes(code, outputs)
+
+ def test_shadow_with_free_and_local(self):
+ code = """
+ lst = range(3)
+ x = -1
+ funcs = [lambda: x for x in lst]
+ items = [x + 1 for x in lst]
+ """
+ outputs = {"x": -1}
+ self._check_in_scopes(code, outputs)
+
+ def test_shadow_comp_iterable_name(self):
+ code = """
+ x = [1]
+ y = [x for x in x]
+ """
+ outputs = {"x": [1]}
+ self._check_in_scopes(code, outputs)
+
+ def test_nested_free(self):
+ code = """
+ x = 1
+ def g():
+ [x for x in range(3)]
+ return x
+ g()
+ """
+ outputs = {"x": 1}
+ self._check_in_scopes(code, outputs)
+
+ def test_introspecting_frame_locals(self):
+ code = """
+ import sys
+ [i for i in range(2)]
+ i = 20
+ sys._getframe().f_locals
+ """
+ outputs = {"i": 20}
+ self._check_in_scopes(code, outputs)
+
+ def test_nested(self):
+ code = """
+ l = [2, 3]
+ y = [[x ** 2 for x in range(x)] for x in l]
+ """
+ outputs = {"y": [[0, 1], [0, 1, 4]]}
+ self._check_in_scopes(code, outputs)
+
+ def test_nested_2(self):
+ code = """
+ l = [1, 2, 3]
+ x = 3
+ y = [x for [x ** x for x in range(x)][x - 1] in l]
+ """
+ outputs = {"y": [3, 3, 3]}
+ self._check_in_scopes(code, outputs)
+
+ def test_nested_3(self):
+ code = """
+ l = [(1, 2), (3, 4), (5, 6)]
+ y = [x for (x, [x ** x for x in range(x)][x - 1]) in l]
+ """
+ outputs = {"y": [1, 3, 5]}
+ self._check_in_scopes(code, outputs)
+
+ def test_nameerror(self):
+ code = """
+ [x for x in [1]]
+ x
+ """
+
+ self._check_in_scopes(code, raises=NameError)
+
+ def test_dunder_name(self):
+ code = """
+ y = [__x for __x in [1]]
+ """
+ outputs = {"y": [1]}
+ self._check_in_scopes(code, outputs)
+
+ def test_unbound_local_after_comprehension(self):
+ def f():
+ if False:
+ x = 0
+ [x for x in [1]]
+ return x
+
+ with self.assertRaises(UnboundLocalError):
+ f()
+
+ def test_unbound_local_inside_comprehension(self):
+ def f():
+ l = [None]
+ return [1 for (l[0], l) in [[1, 2]]]
+
+ with self.assertRaises(UnboundLocalError):
+ f()
__test__ = {'doctests' : doctests}
diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py
index fad2b3b837..73339ebdb7 100644
--- a/Lib/test/test_trace.py
+++ b/Lib/test/test_trace.py
@@ -187,9 +187,7 @@ def test_trace_list_comprehension(self):
firstlineno_called = get_firstlineno(traced_doubler)
expected = {
(self.my_py_filename, firstlineno_calling + 1): 1,
- # List comprehensions work differently in 3.x, so the count
- # below changed compared to 2.x.
- (self.my_py_filename, firstlineno_calling + 2): 12,
+ (self.my_py_filename, firstlineno_calling + 2): 11,
(self.my_py_filename, firstlineno_calling + 3): 1,
(self.my_py_filename, firstlineno_called + 1): 10,
}
|