Content-Length: 318494 | pFad | http://github.com/python/cpython/pull/136478/commits/e4f8fa6d75c8649aff278a5f1825106dde954097

55 gh-136438: Make sure `test_compile` pass with all optimization levels by donBarbos · Pull Request #136478 · python/cpython · GitHub
Skip to content

gh-136438: Make sure test_compile pass with all optimization levels #136478

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Next Next commit
Make sure test_compile pass with all optimization levels
  • Loading branch information
donBarbos committed Jul 9, 2025
commit e4f8fa6d75c8649aff278a5f1825106dde954097
32 changes: 19 additions & 13 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,18 +583,19 @@ def test_compile_ast(self):
with open(fname, encoding='utf-8') as f:
fcontents = f.read()
sample_code = [
['<assign>', 'x = 5'],
['<ifblock>', """if True:\n pass\n"""],
['<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""],
['<deffunc>', """def foo():\n pass\nfoo()\n"""],
[fname, fcontents],
[2, '<assign>', 'x = 5'],
[2, '<ifblock>', """if True:\n pass\n"""],
[1, '<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""],
[1, '<deffunc>', """def foo():\n pass\nfoo()\n"""],
[0, fname, fcontents],
]

for fname, code in sample_code:
co1 = compile(code, '%s1' % fname, 'exec')
ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
for optval, fname, code in sample_code:
co1 = compile(code, '%s1' % fname, 'exec', optimize=optval)
ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST,
optimize=optval)
self.assertTrue(type(ast) == _ast.Module)
co2 = compile(ast, '%s3' % fname, 'exec')
co2 = compile(ast, '%s3' % fname, 'exec', optimize=optval)
self.assertEqual(co1, co2)
# the code object's filename comes from the second compilation step
self.assertEqual(co2.co_filename, '%s3' % fname)
Expand Down Expand Up @@ -823,8 +824,10 @@ def f():
else:
return "unused"

self.assertEqual(f.__code__.co_consts,
(f.__doc__, "used"))
if f.__doc__ is None:
self.assertEqual(f.__code__.co_consts, (True, "used"))
else:
self.assertEqual(f.__code__.co_consts, (f.__doc__, "used"))

@support.cpython_only
def test_remove_unused_consts_no_docstring(self):
Expand Down Expand Up @@ -862,14 +865,17 @@ def test_remove_unused_consts_extended_args(self):

# Stripping unused constants is not a strict requirement for the
# Python semantics, it's a more an implementation detail.
@support.cpython_only
def test_strip_unused_None(self):
# Python 3.10rc1 appended None to co_consts when None is not used
# at all. See bpo-45056.
def f1():
"docstring"
return 42
self.assertEqual(f1.__code__.co_consts, (f1.__doc__,))

if f1.__doc__ is None:
self.assertEqual(f1.__code__.co_consts, (42,))
else:
self.assertEqual(f1.__code__.co_consts, (f1.__doc__,))

# This is a regression test for a CPython specific peephole optimizer
# implementation bug present in a few releases. It's assertion verifies
Expand Down
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/pull/136478/commits/e4f8fa6d75c8649aff278a5f1825106dde954097

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy