gh-126072: Set docstring attribute for module and class (#126231)

This commit is contained in:
Xuanteng Huang 2024-11-08 23:13:18 +08:00 committed by GitHub
parent b19d12f447
commit 6ec886531f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 77 additions and 20 deletions

View file

@ -178,6 +178,20 @@ freevars: ()
nlocals: 3
flags: 3
consts: ("'hello'", "'world'")
>>> class class_with_docstring:
... '''This is a docstring for class'''
... '''This line is not docstring'''
... pass
>>> print(class_with_docstring.__doc__)
This is a docstring for class
>>> class class_without_docstring:
... pass
>>> print(class_without_docstring.__doc__)
None
"""
import copy
@ -854,6 +868,33 @@ class CodeLocationTest(unittest.TestCase):
3 * [(42, 42, None, None)],
)
@cpython_only
def test_docstring_under_o2(self):
code = textwrap.dedent('''
def has_docstring(x, y):
"""This is a first-line doc string"""
"""This is a second-line doc string"""
a = x + y
b = x - y
return a, b
def no_docstring(x):
def g(y):
return x + y
return g
async def async_func():
"""asynf function doc string"""
pass
for func in [has_docstring, no_docstring(4), async_func]:
assert(func.__doc__ is None)
''')
rc, out, err = assert_python_ok('-OO', '-c', code)
if check_impl_detail(cpython=True) and ctypes is not None:
py = ctypes.pythonapi