Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
add doc string co tests
  • Loading branch information
xuantengh committed Oct 30, 2024
commit 0450f96bdb2724749ae9e75a43689dff31158232
55 changes: 55 additions & 0 deletions Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,61 @@
flags: 3
consts: ('None',)

>>> def has_docstring(x: str):
... 'This is a one-line doc string'
... x += x
... x += "hello world"
... # co_flags should be 0x4000003 = 67108867
... return x

>>> dump(has_docstring.__code__)
name: has_docstring
argcount: 1
posonlyargcount: 0
kwonlyargcount: 0
names: ()
varnames: ('x',)
cellvars: ()
freevars: ()
nlocals: 1
flags: 67108867
consts: ("'This is a one-line doc string'", "'hello world'")

>>> async def async_func_docstring(x: str, y: str):
... "This is a docstring from async function"
... import asyncio
... await asyncio.sleep(1)
... # co_flags should be 0x4000083 = 67108995
... return x + y

>>> dump(async_func_docstring.__code__)
name: async_func_docstring
argcount: 2
posonlyargcount: 0
kwonlyargcount: 0
names: ('asyncio', 'sleep')
varnames: ('x', 'y', 'asyncio')
cellvars: ()
freevars: ()
nlocals: 3
flags: 67108995
consts: ("'This is a docstring from async function'", '0', 'None', '1')

>>> def no_docstring(x, y, z):
... return x + "hello" + y + z + "world"

>>> dump(no_docstring.__code__)
name: no_docstring
argcount: 3
posonlyargcount: 0
kwonlyargcount: 0
names: ()
varnames: ('x', 'y', 'z')
cellvars: ()
freevars: ()
nlocals: 3
flags: 3
consts: ("'hello'", "'world'")
"""

import copy
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ def test_remove_unused_consts_extended_args(self):
eval(compile(code, "file.py", "exec"), g)
exec(code, g)
f = g['f']
# Issue #126072: None is no longer the first element in co_consts
expected = tuple(['', 1] + [f't{i}' for i in range(N)])
self.assertEqual(f.__code__.co_consts, expected)
expected = "".join(expected[2:])
Expand Down Expand Up @@ -1244,6 +1245,7 @@ def return_genexp():
y)
genexp_lines = [0, 4, 2, 0, 4]

# Issue #126072: None is no longer the first element in co_consts
genexp_code = return_genexp.__code__.co_consts[0]
code_lines = self.get_code_lines(genexp_code)
self.assertEqual(genexp_lines, code_lines)
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_compiler_assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def inner():
return x
return inner() % 2

# Issue #126072: None is no longer the first element in co_consts
Comment thread
xuantengh marked this conversation as resolved.
Outdated
inner_code = mod_two.__code__.co_consts[0]
assert isinstance(inner_code, types.CodeType)

Expand Down