Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-126835: Move constant subscript folding to CFG #129568

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 10 commits into from
Feb 4, 2025
Merged
Changes from 1 commit
Commits
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
add more tests
  • Loading branch information
WolframAlph committed Feb 2, 2025
commit bfb913cab3d6e67e78d09fd9f80ad888602dbaef
34 changes: 34 additions & 0 deletions 34 Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,40 @@ def test_constant_folding(self):
self.assertFalse(instr.opname.startswith('BUILD_'))
self.check_lnotab(code)

def test_folding_subscript(self):
tests = [
# small ints
('(1, )[0]', True, False),
('(255, )[0]', True, False),
('(1, (1, 2))[1][1]', True, False),
('(1, 2)[2-1]', True, False),
('(1, (1, 2))[1][2-1]', True, False),
('(1, (1, 2))[1:6][0][2-1]', True, False),
# regular ints
('(256, )[0]', False, False),
('(1, (1, 1000))[1][1]', False, False),
('(1, 1000)[2-1]', False, False),
('(1, (1, 1000))[1][2-1]', False, False),
# errors
('(1, )[1]', True, True),
('(1, )[-2]', False, True),
('"a"[1]', True, True),
('"a"[-2]', False, True),
('(1, (1, 2))[2:6][0][2-1]', True, True),
]
for expr, has_small_int, has_error in tests:
with self.subTest(expr=expr, has_small_int=has_small_int, has_error=has_error):
WolframAlph marked this conversation as resolved.
Show resolved Hide resolved
code = compile(expr, '', 'single')
if not has_error:
self.assertNotInBytecode(code, 'BINARY_SUBSCR')
else:
self.assertInBytecode(code, 'BINARY_SUBSCR')
if has_small_int:
self.assertInBytecode(code, 'LOAD_SMALL_INT')
else:
self.assertNotInBytecode(code, 'LOAD_SMALL_INT')
self.check_lnotab(code)

def test_in_literal_list(self):
def containtest():
return x in [a, b]
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.