Debian Codegraph @

pypy/interpreter/test/test_function.py

raw symbols SCIP
1 from pypy.interpreter import eval
2 from pypy.interpreter.function import Function, Method
3 from pypy.interpreter.gateway import descr_function_get
4 from pypy.interpreter.pycode import PyCode
5 from pypy.interpreter.argument import Arguments
6
7
8 class TestMethod:
9 @classmethod
10 def compile(cls, src):
11 assert src.strip().startswith("def ")
12 compiler = cls.space.createcompiler()
13 code = compiler.compile(src, '<hello>', 'exec', 0).co_consts_w[0]
14 return Function(cls.space, code, cls.space.newdict())
15
16 def setup_class(cls):
17 src = """
18 def c(self, bar):
19 return bar
20 """
21 cls.fn = cls.compile(src)
22
23
24 def test_get(self):
25 space = self.space
26 w_meth = descr_function_get(space, self.fn, space.wrap(5), space.type(space.wrap(5)))
27 meth = space.unwrap(w_meth)
28 assert isinstance(meth, Method)
29
30 def test_call(self):
31 space = self.space
32 w_meth = descr_function_get(space, self.fn, space.wrap(5), space.type(space.wrap(5)))
33 meth = space.unwrap(w_meth)
34 w_result = meth.call_args(Arguments(space, [space.wrap(42)]))
35 assert space.unwrap(w_result) == 42
36
37 def test_fail_call(self):
38 space = self.space
39 w_meth = descr_function_get(space, self.fn, space.wrap(5), space.type(space.wrap(5)))
40 meth = space.unwrap(w_meth)
41 args = Arguments(space, [space.wrap("spam"), space.wrap("egg")])
42 self.space.raises_w(self.space.w_TypeError, meth.call_args, args)
43
44 def test_method_get(self):
45 space = self.space
46 # Create some function for this test only
47 func = self.compile("def m(self): return self")
48 # Some shorthands
49 obj1 = space.wrap(23)
50 obj2 = space.wrap(42)
51 args = Arguments(space, [])
52 # Check method returned from func.__get__()
53 w_meth1 = descr_function_get(space, func, obj1, space.type(obj1))
54 meth1 = space.unwrap(w_meth1)
55 assert isinstance(meth1, Method)
56 assert meth1.call_args(args) == obj1
57 # Check method returned from method.__get__()
58 # --- meth1 is already bound so meth1.__get__(*) is meth1.
59 w_meth2 = meth1.descr_method_get(obj2, space.type(obj2))
60 meth2 = space.unwrap(w_meth2)
61 assert isinstance(meth2, Method)
62 assert meth2.call_args(args) == obj1
63 # Check method returned from unbound_method.__get__()
64 w_meth3 = descr_function_get(space, func, space.w_None, space.type(obj2))
65 meth3 = space.unwrap(w_meth3)
66 assert meth3 is func
67
68 class TestShortcuts(object):
69 def compile(self, src):
70 assert src.strip().startswith("def ")
71 compiler = self.space.createcompiler()
72 code = compiler.compile(src, '<hello>', 'exec', 0).co_consts_w[0]
73 return Function(self.space, code, self.space.newdict())
74
75 def test_call_function(self):
76 space = self.space
77
78 d = {}
79 for i in range(10):
80 args = "(" + ''.join(["a%d," % a for a in range(i)]) + ")"
81 src = """
82 def f%s:
83 return %s
84 """ % (args, args)
85 exec src in d
86 f = d['f']
87 res = f(*range(i))
88 fn = self.compile(src)
89 code = fn.code
90
91 assert fn.code.fast_natural_arity == i|PyCode.FLATPYCALL
92 if i < 5:
93
94 def bomb(*args):
95 assert False, "shortcutting should have avoided this"
96
97 code.funcrun = bomb
98 code.funcrun_obj = bomb
99
100 args_w = map(space.wrap, range(i))
101 w_res = space.call_function(fn, *args_w)
102 check = space.is_true(space.eq(w_res, space.wrap(res)))
103 assert check
104
105 def test_flatcall(self):
106 space = self.space
107
108 src = """
109 def f(a):
110 return a"""
111 fn = self.compile(src)
112
113 assert fn.code.fast_natural_arity == 1|PyCode.FLATPYCALL
114
115 def bomb(*args):
116 assert False, "shortcutting should have avoided this"
117
118 fn.code.funcrun = bomb
119 fn.code.funcrun_obj = bomb
120
121 w_3 = space.newint(3)
122 w_res = space.call_function(fn, w_3)
123
124 assert w_res is w_3
125
126 w_res = space.appexec([fn, w_3], """(f, x):
127 return f(x)
128 """)
129
130 assert w_res is w_3
131
132 def test_flatcall_method(self):
133 space = self.space
134
135 src = """
136 def f(self, a):
137 return a
138 """
139 fn = self.compile(src)
140
141 assert fn.code.fast_natural_arity == 2|PyCode.FLATPYCALL
142
143 def bomb(*args):
144 assert False, "shortcutting should have avoided this"
145
146 fn.code.funcrun = bomb
147 fn.code.funcrun_obj = bomb
148
149 w_3 = space.newint(3)
150 w_res = space.appexec([fn, w_3], """(f, x):
151 class A(object):
152 m = f
153 y = A().m(x)
154 b = A().m
155 z = b(x)
156 return y is x and z is x
157 """)
158
159 assert space.is_true(w_res)
160
161 def test_flatcall_default_arg(self):
162 space = self.space
163
164 src = """
165 def f(a, b):
166 return a+b
167 """
168 code = self.compile(src).code
169 fn = Function(self.space, code, self.space.newdict(),
170 defs_w=[space.newint(1)])
171
172 assert fn.code.fast_natural_arity == 2|eval.Code.FLATPYCALL
173
174 def bomb(*args):
175 assert False, "shortcutting should have avoided this"
176
177 code.funcrun = bomb
178 code.funcrun_obj = bomb
179
180 w_3 = space.newint(3)
181 w_4 = space.newint(4)
182 # ignore this for now
183 #w_res = space.call_function(fn, w_3)
184 # assert space.eq_w(w_res, w_4)
185
186 w_res = space.appexec([fn, w_3], """(f, x):
187 return f(x)
188 """)
189
190 assert space.eq_w(w_res, w_4)
191
192 def test_flatcall_default_arg_method(self):
193 space = self.space
194
195 src = """
196 def f(self, a, b):
197 return a+b
198 """
199 code = self.compile(src).code
200 fn = Function(self.space, code, self.space.newdict(),
201 defs_w=[space.newint(1)])
202
203 assert fn.code.fast_natural_arity == 3|eval.Code.FLATPYCALL
204
205 def bomb(*args):
206 assert False, "shortcutting should have avoided this"
207
208 code.funcrun = bomb
209 code.funcrun_obj = bomb
210
211 w_3 = space.newint(3)
212
213 w_res = space.appexec([fn, w_3], """(f, x):
214 class A(object):
215 m = f
216 y = A().m(x)
217 b = A().m
218 z = b(x)
219 return y+10*z
220 """)
221
222 assert space.eq_w(w_res, space.wrap(44))
223
224
225 class TestFunction:
226
227 def test_doc_synthesized_from_text_signature(self):
228 """When a builtin has __text_signature__ but no __doc__, synthesize
229 a CPython-compatible docstring: 'name(sig)\n--\n\n'."""
230 from pypy.interpreter import gateway
231 space = self.space
232
233 # Builtin with a visible argument (auto-generates __text_signature__)
234 # but no docstring.
235 def g(space, w_x):
236 pass
237 app_g = gateway.interp2app_temp(g)
238 w_g = space.wrap(app_g)
239
240 w_tsig = space.getattr(w_g, space.wrap('__text_signature__'))
241 assert not space.is_w(w_tsig, space.w_None)
242
243 # The synthesized __doc__ must be 'name(sig)\n--\n\n'.
244 w_doc = space.getattr(w_g, space.wrap('__doc__'))
245 assert not space.is_w(w_doc, space.w_None)
246 doc = space.text_w(w_doc)
247 assert '\n--\n\n' in doc, repr(doc)
248 assert doc.startswith('g('), repr(doc)
249
250 def test_explicit_doc_not_overridden_by_text_signature(self):
251 """A builtin with an explicit __doc__ keeps it even if __text_signature__
252 is also set. Passes both before and after the fix."""
253 from pypy.interpreter import gateway
254 space = self.space
255
256 def g(space, w_x):
257 "My explicit docstring."
258 app_g = gateway.interp2app_temp(g)
259 w_g = space.wrap(app_g)
260
261 # Gateway still sets __text_signature__ automatically.
262 w_tsig = space.getattr(w_g, space.wrap('__text_signature__'))
263 assert not space.is_w(w_tsig, space.w_None)
264
265 # Explicit doc must be preserved unchanged.
266 w_doc = space.getattr(w_g, space.wrap('__doc__'))
267 doc = space.text_w(w_doc)
268 assert doc == "My explicit docstring.", repr(doc)
269
270 def test_text_signature_not_modified_when_synthesizing_doc(self):
271 """Synthesizing __doc__ must not touch __text_signature__.
272 Passes both before and after the fix (we never modify text_signature)."""
273 from pypy.interpreter import gateway
274 space = self.space
275
276 def g(space, w_x):
277 pass
278 app_g = gateway.interp2app_temp(g)
279 w_g = space.wrap(app_g)
280
281 w_tsig_before = space.getattr(w_g, space.wrap('__text_signature__'))
282 assert not space.is_w(w_tsig_before, space.w_None)
283 tsig_before = space.text_w(w_tsig_before)
284
285 # Access __doc__ (may synthesize it after the fix).
286 space.getattr(w_g, space.wrap('__doc__'))
287
288 # __text_signature__ must be identical.
289 w_tsig_after = space.getattr(w_g, space.wrap('__text_signature__'))
290 tsig_after = space.text_w(w_tsig_after)
291 assert tsig_before == tsig_after, (tsig_before, tsig_after)
292
293 def test_func_defaults(self):
294 from pypy.interpreter import gateway
295 def g(w_a=None):
296 pass
297 app_g = gateway.interp2app_temp(g)
298 space = self.space
299 w_g = space.wrap(app_g)
300 w_defs = space.getattr(w_g, space.wrap("__defaults__"))
301 assert space.is_w(w_defs, space.w_None)
302 w_count = space.getattr(w_g, space.wrap("__defaults_count__"))
303 assert space.unwrap(w_count) == 1