Skip to content

Commit 15d741e

Browse files
committed
add doc string co tests
1 parent 3b04e71 commit 15d741e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

Lib/test/test_code.py

+55
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,61 @@
123123
flags: 3
124124
consts: ('None',)
125125
126+
>>> def has_docstring(x: str):
127+
... 'This is a one-line doc string'
128+
... x += x
129+
... x += "hello world"
130+
... # co_flags should be 0x4000003 = 67108867
131+
... return x
132+
133+
>>> dump(has_docstring.__code__)
134+
name: has_docstring
135+
argcount: 1
136+
posonlyargcount: 0
137+
kwonlyargcount: 0
138+
names: ()
139+
varnames: ('x',)
140+
cellvars: ()
141+
freevars: ()
142+
nlocals: 1
143+
flags: 67108867
144+
consts: ("'This is a one-line doc string'", "'hello world'")
145+
146+
>>> async def async_func_docstring(x: str, y: str):
147+
... "This is a docstring from async function"
148+
... import asyncio
149+
... await asyncio.sleep(1)
150+
... # co_flags should be 0x4000083 = 67108995
151+
... return x + y
152+
153+
>>> dump(async_func_docstring.__code__)
154+
name: async_func_docstring
155+
argcount: 2
156+
posonlyargcount: 0
157+
kwonlyargcount: 0
158+
names: ('asyncio', 'sleep')
159+
varnames: ('x', 'y', 'asyncio')
160+
cellvars: ()
161+
freevars: ()
162+
nlocals: 3
163+
flags: 67108995
164+
consts: ("'This is a docstring from async function'", '0', 'None', '1')
165+
166+
>>> def no_docstring(x, y, z):
167+
... return x + "hello" + y + z + "world"
168+
169+
>>> dump(no_docstring.__code__)
170+
name: no_docstring
171+
argcount: 3
172+
posonlyargcount: 0
173+
kwonlyargcount: 0
174+
names: ()
175+
varnames: ('x', 'y', 'z')
176+
cellvars: ()
177+
freevars: ()
178+
nlocals: 3
179+
flags: 3
180+
consts: ("'hello'", "'world'")
126181
"""
127182

128183
import copy

Lib/test/test_compile.py

+2
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ def test_remove_unused_consts_extended_args(self):
852852
eval(compile(code, "file.py", "exec"), g)
853853
exec(code, g)
854854
f = g['f']
855+
# Issue #126072: None is no longer the first element in co_consts
855856
expected = tuple(['', 1] + [f't{i}' for i in range(N)])
856857
self.assertEqual(f.__code__.co_consts, expected)
857858
expected = "".join(expected[2:])
@@ -1244,6 +1245,7 @@ def return_genexp():
12441245
y)
12451246
genexp_lines = [0, 4, 2, 0, 4]
12461247

1248+
# Issue #126072: None is no longer the first element in co_consts
12471249
genexp_code = return_genexp.__code__.co_consts[0]
12481250
code_lines = self.get_code_lines(genexp_code)
12491251
self.assertEqual(genexp_lines, code_lines)

Lib/test/test_compiler_assemble.py

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def inner():
8484
return x
8585
return inner() % 2
8686

87+
# Issue #126072: None is no longer the first element in co_consts
8788
inner_code = mod_two.__code__.co_consts[0]
8889
assert isinstance(inner_code, types.CodeType)
8990

0 commit comments

Comments
 (0)