8000 gh-97933: inline list/dict/set comprehensions by carljm · Pull Request #101441 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-97933: inline list/dict/set comprehensions #101441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 56 commits into from
May 9, 2023
Merged
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
72afa83
gh-97933: inline sync list/dict/set comprehensions
carljm Jan 30, 2023
8988234
simplify cell handling code slightly
carljm Jan 31, 2023
22c4a86
clarify comments
carljm Jan 31, 2023
c1b54f0
enable inlining async comprehensions also
carljm Jan 31, 2023
43db9b8
fix typo
carljm Jan 31, 2023
ed3209b
Merge branch 'main' into inlinecomp2
carljm Jan 31, 2023
aceb6c7
fix outer-cell, inner-local case
carljm Jan 31, 2023
e57c354
fix restoring NULL (unbound outer name) followed by load
carljm Jan 31, 2023
795d854
emit 1 x SWAP N+1 instead of N x SWAP 2
carljm Jan 31, 2023
686221a
remove stray dis.dis() call
carljm Jan 31, 2023
ac99697
fix compiler warning about Py_ssize_t -> int conversion
carljm Jan 31, 2023
db208d5
Merge branch 'main' into inlinecomp2
carljm Jan 31, 2023
8b76051
Merge branch 'main' into inlinecomp2
carljm Feb 1, 2023
4620856
add a couple more tests
carljm Feb 1, 2023
8773653
clear comp locals on entry, eval iter expr first
carljm Feb 1, 2023
be3becc
Merge branch 'main' into inlinecomp2
carljm Feb 9, 2023
f0c051e
fix double decref in error case
carljm Feb 9, 2023
142859a
adjust to RETURN_CONST
carljm Feb 9, 2023
568a470
fix up refcounting
carljm Feb 10, 2023
add772e
Merge branch 'main' into inlinecomp2
carljm Feb 10, 2023
8000
17d5d84
improve importlib comment
carljm Feb 10, 2023
b87d209
mark STORE_FAST_MAYBE_NULL as possibly NULLing a local
carljm Feb 10, 2023
36b2917
Merge branch 'main' into inlinecomp2
carljm Feb 10, 2023
ae0bd02
Merge branch 'main' into inlinecomp2
carljm Feb 13, 2023
9f0fc5b
Merge branch 'main' into inlinecomp2
carljm Feb 20, 2023
463c740
add test for NameError/UnboundLocalError
carljm Feb 28, 2023
67f50ba
fix case where iter var is free in outer scope
carljm Feb 28, 2023
73dc0ed
Merge branch 'main' into inlinecomp2
carljm Feb 28, 2023
ecb313c
Merge branch 'main' into inlinecomp2
carljm Mar 6, 2023
4109baa
add inlining of non-function-scope comprehensions
carljm Mar 7, 2023
d8802a1
simplify scope handling
carljm Mar 7, 2023
1c019a7
Merge branch 'main' into inlinecomp2
carljm Mar 7, 2023
24a9d9f
Merge branch 'main' into inlinecomp2
carljm Mar 8, 2023
b6a025b
add tests for comprehensions in class scope
carljm Mar 8, 2023
90b34de
run all listcomp scope tests in module, class, and func scope
carljm Mar 8, 2023
06db319
Merge branch 'main' into inlinecomp2
carljm Mar 8, 2023
b52046b
handle frame locals materialization in class/module scope
carljm Mar 14, 2023
6c5f269
Merge branch 'main' into inlinecomp2
carljm Mar 14, 2023
1a8f4a0
Merge branch 'main' into inlinecomp2
carljm Mar 17, 2023
1274e2b
Merge branch 'main' into inlinecomp2
carljm May 1, 2023
0727d6f
Merge branch 'main' into inlinecomp2
carljm May 2, 2023
51a1294
update comment
carljm May 2, 2023
8a78a36
Merge branch 'main' into inlinecomp2
carljm May 5, 2023
43722b4
review comments
carljm May 5, 2023
bf9e1f1
fix single backticks
carljm May 5, 2023
ca636a5
better nested test
carljm May 5, 2023
46c7a4f
fix u_fasthidden in nested case
carljm May 5, 2023
fb9f89e
fix refleak
carljm May 5, 2023
baacf5f
Merge branch 'main' into inlinecomp2
carljm May 5, 2023
5914d77
remove assumption that class scopes can't have cellvars
carljm May 5, 2023
ffae4e6
Apply suggestions from code review
carljm May 9, 2023
76077cd
Merge branch 'main' into inlinecomp2
carljm May 9, 2023
a8425a6
review comments
carljm May 9, 2023
656e46b
Apply suggestions from code review
carljm May 9, 2023
1402e7a
Apply suggestions from code review
carljm May 9, 2023
95401fe
Merge branch 'main' into inlinecomp2
carljm May 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
run all listcomp scope tests in module, class, and func scope
  • Loading branch information
carljm committed Mar 8, 2023
commit 90b34dea96d6304adb8a1abe2af001908086e63e
278 changes: 134 additions & 144 deletions Lib/test/test_listcomps.py
6D00
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import doctest
import textwrap
import unittest


Expand Down Expand Up @@ -87,154 +88,143 @@
>>> [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 f 10000 unction:

>>> 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]

And in class scope:

>>> class C:
... items = [(lambda i=i: i) for i in range(5)]
... ret = [x() for x in items]
>>> C.ret
[0, 1, 2, 3, 4]

>>> class C:
... items = [(lambda: i) for i in range(5)]
... ret = [x() for x in items]
>>> C.ret
[4, 4, 4, 4, 4]

>>> class C:
... items = [(lambda: i) for i in range(5)]
... i = 20
... ret = [x() for x in items]
>>> C.ret
[4, 4, 4, 4, 4]
>>> C.i
20

>>> class C:
... items = [(lambda: y) for i in range(5)]
... y = 2
... ret = [x() for x in items]
>>> C.ret
[2, 2, 2, 2, 2]

Some more tests for scoping edge cases, each in func/module/class scope:

>>> def test_func():
... y = 10
... items = [(lambda: y) for y in range(5)]
... x = y
... y = 20
... return x, [z() for z in items]
>>> test_func()
(10, [4, 4, 4, 4, 4])

>>> g = -1
>>> def test_func():
... def inner():
... return g
... [g for g in range(5)]
... return inner
>>> test_func()()
-1

>>> def test_func():
... x = -1
... items = [(x:=y) for y in range(3)]
... return x
>>> test_func()
2

>>> def test_func(lst):
... ret = [lambda: x for x in lst]
... inc = [x + 1 for x in lst]
... [x for x in inc]
... return ret
>>> test_func(range(3))[0]()
2

>>> def test_func(lst):
... x = -1
... funcs = [lambda: x for x in lst]
... items = [x + 1 for x in lst]
... return x
>>> test_func(range(3))
-1

>>> def test_func(x):
... return [x for x in x]
>>> test_func([1])
[1]

>>> def test_func():
... x = 1
... def g():
... [x for x in range(3)]
... return x
... g()
... return x
>>> test_func()
1

"""


class ListComprehensionTest(unittest.TestCase):
def _check_in_scopes(self, code, outputs, ns=None, scopes=None):
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()
""").format(code=textwrap.indent(code, " "))
def get_output(moddict, name):
return moddict["f"]()[name]
else:
newcode = code
def get_output(moddict, name):
return moddict[name]
ns = ns or {}
exec(newcode, ns)
for k, v in outputs.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_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]}
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_unbound_local_after_comprehension(self):
def f():
if False:
Expand Down
0