forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_co_consts.py
More file actions
112 lines (73 loc) · 2.63 KB
/
code_co_consts.py
File metadata and controls
112 lines (73 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Test co_consts behavior for Python 3.14+
In Python 3.14+:
- Functions with docstrings have the docstring as co_consts[0]
- CO_HAS_DOCSTRING flag (0x4000000) indicates docstring presence
- Functions without docstrings do NOT have None added as placeholder for docstring
Note: Other constants (small integers, code objects, etc.) may still appear in co_consts
depending on optimization level. This test focuses on docstring behavior.
"""
# Test function with docstring - docstring should be co_consts[0]
def with_doc():
"""This is a docstring"""
return 1
assert with_doc.__code__.co_consts[0] == "This is a docstring", (
with_doc.__code__.co_consts
)
assert with_doc.__doc__ == "This is a docstring"
# Check CO_HAS_DOCSTRING flag (0x4000000)
assert with_doc.__code__.co_flags & 0x4000000, hex(with_doc.__code__.co_flags)
# Test function without docstring - should NOT have HAS_DOCSTRING flag
def no_doc():
return 1
assert not (no_doc.__code__.co_flags & 0x4000000), hex(no_doc.__code__.co_flags)
assert no_doc.__doc__ is None
# Test async function with docstring
from asyncio import sleep
async def async_with_doc():
"""Async docstring"""
await sleep(1)
return 1
assert async_with_doc.__code__.co_consts[0] == "Async docstring", (
async_with_doc.__code__.co_consts
)
assert async_with_doc.__doc__ == "Async docstring"
assert async_with_doc.__code__.co_flags & 0x4000000
# Test async function without docstring
async def async_no_doc():
await sleep(1)
return 1
assert not (async_no_doc.__code__.co_flags & 0x4000000)
assert async_no_doc.__doc__ is None
# Test generator with docstring
def gen_with_doc():
"""Generator docstring"""
yield 1
yield 2
assert gen_with_doc.__code__.co_consts[0] == "Generator docstring"
assert gen_with_doc.__doc__ == "Generator docstring"
assert gen_with_doc.__code__.co_flags & 0x4000000
# Test generator without docstring
def gen_no_doc():
yield 1
yield 2
assert not (gen_no_doc.__code__.co_flags & 0x4000000)
assert gen_no_doc.__doc__ is None
# Test lambda - cannot have docstring
lambda_f = lambda: 0
assert not (lambda_f.__code__.co_flags & 0x4000000)
assert lambda_f.__doc__ is None
# Test class method with docstring
class cls_with_doc:
def method():
"""Method docstring"""
return 1
assert cls_with_doc.method.__code__.co_consts[0] == "Method docstring"
assert cls_with_doc.method.__doc__ == "Method docstring"
# Test class method without docstring
class cls_no_doc:
def method():
return 1
assert not (cls_no_doc.method.__code__.co_flags & 0x4000000)
assert cls_no_doc.method.__doc__ is None
print("All co_consts tests passed!")