Skip to content

Commit 0768cf8

Browse files
authored
Merge pull request #7440 from youknowone/pycode
Upgrade test_code and fix code bugs
2 parents f868f81 + c57f4de commit 0768cf8

File tree

10 files changed

+1281
-72
lines changed

10 files changed

+1281
-72
lines changed

Lib/test/_code_definitions.py

Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
2+
def simple_script():
3+
assert True
4+
5+
6+
def complex_script():
7+
obj = 'a string'
8+
pickle = __import__('pickle')
9+
def spam_minimal():
10+
pass
11+
spam_minimal()
12+
data = pickle.dumps(obj)
13+
res = pickle.loads(data)
14+
assert res == obj, (res, obj)
15+
16+
17+
def script_with_globals():
18+
obj1, obj2 = spam(42)
19+
assert obj1 == 42
20+
assert obj2 is None
21+
22+
23+
def script_with_explicit_empty_return():
24+
return None
25+
26+
27+
def script_with_return():
28+
return True
29+
30+
31+
def spam_minimal():
32+
# no arg defaults or kwarg defaults
33+
# no annotations
34+
# no local vars
35+
# no free vars
36+
# no globals
37+
# no builtins
38+
# no attr access (names)
39+
# no code
40+
return
41+
42+
43+
def spam_with_builtins():
44+
x = 42
45+
values = (42,)
46+
checks = tuple(callable(v) for v in values)
47+
res = callable(values), tuple(values), list(values), checks
48+
print(res)
49+
50+
51+
def spam_with_globals_and_builtins():
52+
func1 = spam
53+
func2 = spam_minimal
54+
funcs = (func1, func2)
55+
checks = tuple(callable(f) for f in funcs)
56+
res = callable(funcs), tuple(funcs), list(funcs), checks
57+
print(res)
58+
59+
60+
def spam_with_global_and_attr_same_name():
61+
try:
62+
spam_minimal.spam_minimal
63+
except AttributeError:
64+
pass
65+
66+
67+
def spam_full_args(a, b, /, c, d, *args, e, f, **kwargs):
68+
return (a, b, c, d, e, f, args, kwargs)
69+
70+
71+
def spam_full_args_with_defaults(a=-1, b=-2, /, c=-3, d=-4, *args,
72+
e=-5, f=-6, **kwargs):
73+
return (a, b, c, d, e, f, args, kwargs)
74+
75+
76+
def spam_args_attrs_and_builtins(a, b, /, c, d, *args, e, f, **kwargs):
77+
if args.__len__() > 2:
78+
return None
79+
return a, b, c, d, e, f, args, kwargs
80+
81+
82+
def spam_returns_arg(x):
83+
return x
84+
85+
86+
def spam_raises():
87+
raise Exception('spam!')
88+
89+
90+
def spam_with_inner_not_closure():
91+
def eggs():
92+
pass
93+
eggs()
94+
95+
96+
def spam_with_inner_closure():
97+
x = 42
98+
def eggs():
99+
print(x)
100+
eggs()
101+
102+
103+
def spam_annotated(a: int, b: str, c: object) -> tuple:
104+
return a, b, c
105+
106+
107+
def spam_full(a, b, /, c, d:int=1, *args, e, f:object=None, **kwargs) -> tuple:
108+
# arg defaults, kwarg defaults
109+
# annotations
110+
# all kinds of local vars, except cells
111+
# no free vars
112+
# some globals
113+
# some builtins
114+
# some attr access (names)
115+
x = args
116+
y = kwargs
117+
z = (a, b, c, d)
118+
kwargs['e'] = e
119+
kwargs['f'] = f
120+
extras = list((x, y, z, spam, spam.__name__))
121+
return tuple(a, b, c, d, e, f, args, kwargs), extras
122+
123+
124+
def spam(x):
125+
return x, None
126+
127+
128+
def spam_N(x):
129+
def eggs_nested(y):
130+
return None, y
131+
return eggs_nested, x
132+
133+
134+
def spam_C(x):
135+
a = 1
136+
def eggs_closure(y):
137+
return None, y, a, x
138+
return eggs_closure, a, x
139+
140+
141+
def spam_NN(x):
142+
def eggs_nested_N(y):
143+
def ham_nested(z):
144+
return None, z
145+
return ham_nested, y
146+
return eggs_nested_N, x
147+
148+
149+
def spam_NC(x):
150+
a = 1
151+
def eggs_nested_C(y):
152+
def ham_closure(z):
153+
return None, z, y, a, x
154+
return ham_closure, y
155+
return eggs_nested_C, a, x
156+
157+
158+
def spam_CN(x):
159+
a = 1
160+
def eggs_closure_N(y):
161+
def ham_C_nested(z):
162+
return None, z
163+
return ham_C_nested, y, a, x
164+
return eggs_closure_N, a, x
165+
166+
167+
def spam_CC(x):
168+
a = 1
169+
def eggs_closure_C(y):
170+
b = 2
171+
def ham_C_closure(z):
172+
return None, z, b, y, a, x
173+
return ham_C_closure, b, y, a, x
174+
return eggs_closure_C, a, x
175+
176+
177+
eggs_nested, *_ = spam_N(1)
178+
eggs_closure, *_ = spam_C(1)
179+
eggs_nested_N, *_ = spam_NN(1)
180+
eggs_nested_C, *_ = spam_NC(1)
181+
eggs_closure_N, *_ = spam_CN(1)
182+
eggs_closure_C, *_ = spam_CC(1)
183+
184+
ham_nested, *_ = eggs_nested_N(2)
185+
ham_closure, *_ = eggs_nested_C(2)
186+
ham_C_nested, *_ = eggs_closure_N(2)
187+
ham_C_closure, *_ = eggs_closure_C(2)
188+
189+
190+
TOP_FUNCTIONS = [
191+
# shallow
192+
simple_script,
193+
complex_script,
194+
script_with_globals,
195+
script_with_explicit_empty_return,
196+
script_with_return,
197+
spam_minimal,
198+
spam_with_builtins,
199+
spam_with_globals_and_builtins,
200+
spam_with_global_and_attr_same_name,
201+
spam_full_args,
202+
spam_full_args_with_defaults,
203+
spam_args_attrs_and_builtins,
204+
spam_returns_arg,
205+
spam_raises,
206+
spam_with_inner_not_closure,
207+
spam_with_inner_closure,
208+
spam_annotated,
209+
spam_full,
210+
spam,
211+
# outer func
212+
spam_N,
213+
spam_C,
214+
spam_NN,
215+
spam_NC,
216+
spam_CN,
217+
spam_CC,
218+
]
219+
NESTED_FUNCTIONS = [
220+
# inner func
221+
eggs_nested,
222+
eggs_closure,
223+
eggs_nested_N,
224+
eggs_nested_C,
225+
eggs_closure_N,
226+
eggs_closure_C,
227+
# inner inner func
228+
ham_nested,
229+
ham_closure,
230+
ham_C_nested,
231+
ham_C_closure,
232+
]
233+
FUNCTIONS = [
234+
*TOP_FUNCTIONS,
235+
*NESTED_FUNCTIONS,
236+
]
237+
238+
STATELESS_FUNCTIONS = [
239+
simple_script,
240+
complex_script,
241+
script_with_explicit_empty_return,
242+
script_with_return,
243+
spam,
244+
spam_minimal,
245+
spam_with_builtins,
246+
spam_full_args,
247+
spam_args_attrs_and_builtins,
248+
spam_returns_arg,
249+
spam_raises,
250+
spam_annotated,
251+
spam_with_inner_not_closure,
252+
spam_with_inner_closure,
253+
spam_N,
254+
spam_C,
255+
spam_NN,
256+
spam_NC,
257+
spam_CN,
258+
spam_CC,
259+
eggs_nested,
260+
eggs_nested_N,
261+
ham_nested,
262+
ham_C_nested
263+
]
264+
STATELESS_CODE = [
265+
*STATELESS_FUNCTIONS,
266+
script_with_globals,
267+
spam_full_args_with_defaults,
268+
spam_with_globals_and_builtins,
269+
spam_with_global_and_attr_same_name,
270+
spam_full,
271+
]
272+
273+
PURE_SCRIPT_FUNCTIONS = [
274+
simple_script,
275+
complex_script,
276+
script_with_explicit_empty_return,
277+
spam_minimal,
278+
spam_with_builtins,
279+
spam_raises,
280+
spam_with_inner_not_closure,
281+
spam_with_inner_closure,
282+
]
283+
SCRIPT_FUNCTIONS = [
284+
*PURE_SCRIPT_FUNCTIONS,
285+
script_with_globals,
286+
spam_with_globals_and_builtins,
287+
spam_with_global_and_attr_same_name,
288+
]
289+
290+
291+
# generators
292+
293+
def gen_spam_1(*args):
294+
for arg in args:
295+
yield arg
296+
297+
298+
def gen_spam_2(*args):
299+
yield from args
300+
301+
302+
async def async_spam():
303+
pass
304+
coro_spam = async_spam()
305+
coro_spam.close()
306+
307+
308+
async def asyncgen_spam(*args):
309+
for arg in args:
310+
yield arg
311+
asynccoro_spam = asyncgen_spam(1, 2, 3)
312+
313+
314+
FUNCTION_LIKE = [
315+
gen_spam_1,
316+
gen_spam_2,
317+
async_spam,
318+
asyncgen_spam,
319+
]
320+
FUNCTION_LIKE_APPLIED = [
321+
coro_spam, # actually FunctionType?
322+
asynccoro_spam, # actually FunctionType?
323+
]

0 commit comments

Comments
 (0)