mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
gh-115347: avoid emitting redundant NOP for the docstring with -OO (#115494)
This commit is contained in:
parent
ad4f909e0e
commit
732faf17a6
3 changed files with 48 additions and 18 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue