gh-115347: avoid emitting redundant NOP for the docstring with -OO (#115494)

This commit is contained in:
Irit Katriel 2024-02-15 14:20:19 +00:00 committed by GitHub
parent ad4f909e0e
commit 732faf17a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 18 deletions

View file

@ -1,4 +1,6 @@
import contextlib
import dis
import io
import math
import os
import unittest
@ -812,6 +814,30 @@ class TestSpecifics(unittest.TestCase):
'RETURN_CONST',
list(dis.get_instructions(unused_code_at_end))[-1].opname)
@support.cpython_only
def test_docstring_omitted(self):
# See gh-115347
src = textwrap.dedent("""
def f():
"docstring1"
def h():
"docstring2"
return 42
class C:
"docstring3"
pass
return h
""")
for opt in [-1, 0, 1, 2]:
with self.subTest(opt=opt):
code = compile(src, "<test>", "exec", optimize=opt)
output = io.StringIO()
with contextlib.redirect_stdout(output):
dis.dis(code)
self.assertNotIn('NOP' , output.getvalue())
def test_dont_merge_constants(self):
# Issue #25843: compile() must not merge constants which are equal
# but have a different type.