blob: 2e8819be5dac68cd4c606186f12a81c410396214 [file] [log] [blame]
Victor Stinnerb7577452017-06-09 22:28:32 +02001import datetime
Jeremy Hyltonbea39472001-05-29 16:26:20 +00002import unittest
Victor Stinnerb7577452017-06-09 22:28:32 +02003from test.support import cpython_only
4try:
5 import _testcapi
6except ImportError:
7 _testcapi = None
Jeremy Hyltonbea39472001-05-29 16:26:20 +00008
9# The test cases here cover several paths through the function calling
10# code. They depend on the METH_XXX flag that is used to define a C
11# function, which can't be verified from Python. If the METH_XXX decl
12# for a C function changes, these tests may not cover the right paths.
13
14class CFunctionCalls(unittest.TestCase):
15
16 def test_varargs0(self):
Guido van Rossume2b70bc2006-08-18 22:13:04 +000017 self.assertRaises(TypeError, {}.__contains__)
Jeremy Hyltonbea39472001-05-29 16:26:20 +000018
19 def test_varargs1(self):
Guido van Rossume2b70bc2006-08-18 22:13:04 +000020 {}.__contains__(0)
Jeremy Hyltonbea39472001-05-29 16:26:20 +000021
22 def test_varargs2(self):
Guido van Rossume2b70bc2006-08-18 22:13:04 +000023 self.assertRaises(TypeError, {}.__contains__, 0, 1)
Jeremy Hyltonbea39472001-05-29 16:26:20 +000024
25 def test_varargs0_ext(self):
26 try:
Guido van Rossume2b70bc2006-08-18 22:13:04 +000027 {}.__contains__(*())
Jeremy Hyltonbea39472001-05-29 16:26:20 +000028 except TypeError:
29 pass
30
31 def test_varargs1_ext(self):
Guido van Rossume2b70bc2006-08-18 22:13:04 +000032 {}.__contains__(*(0,))
Jeremy Hyltonbea39472001-05-29 16:26:20 +000033
34 def test_varargs2_ext(self):
35 try:
Guido van Rossume2b70bc2006-08-18 22:13:04 +000036 {}.__contains__(*(1, 2))
Jeremy Hyltonbea39472001-05-29 16:26:20 +000037 except TypeError:
38 pass
39 else:
40 raise RuntimeError
41
42 def test_varargs0_kw(self):
Guido van Rossume2b70bc2006-08-18 22:13:04 +000043 self.assertRaises(TypeError, {}.__contains__, x=2)
Jeremy Hyltonbea39472001-05-29 16:26:20 +000044
45 def test_varargs1_kw(self):
Guido van Rossume2b70bc2006-08-18 22:13:04 +000046 self.assertRaises(TypeError, {}.__contains__, x=2)
Jeremy Hyltonbea39472001-05-29 16:26:20 +000047
48 def test_varargs2_kw(self):
Guido van Rossume2b70bc2006-08-18 22:13:04 +000049 self.assertRaises(TypeError, {}.__contains__, x=2, y=2)
Jeremy Hyltonbea39472001-05-29 16:26:20 +000050
51 def test_oldargs0_0(self):
52 {}.keys()
53
54 def test_oldargs0_1(self):
55 self.assertRaises(TypeError, {}.keys, 0)
56
57 def test_oldargs0_2(self):
58 self.assertRaises(TypeError, {}.keys, 0, 1)
59
60 def test_oldargs0_0_ext(self):
61 {}.keys(*())
62
63 def test_oldargs0_1_ext(self):
64 try:
65 {}.keys(*(0,))
66 except TypeError:
67 pass
68 else:
69 raise RuntimeError
70
71 def test_oldargs0_2_ext(self):
72 try:
73 {}.keys(*(1, 2))
74 except TypeError:
75 pass
76 else:
77 raise RuntimeError
78
79 def test_oldargs0_0_kw(self):
80 try:
81 {}.keys(x=2)
82 except TypeError:
83 pass
84 else:
85 raise RuntimeError
86
87 def test_oldargs0_1_kw(self):
88 self.assertRaises(TypeError, {}.keys, x=2)
89
90 def test_oldargs0_2_kw(self):
91 self.assertRaises(TypeError, {}.keys, x=2, y=2)
92
93 def test_oldargs1_0(self):
Raymond Hettinger31017ae2004-03-04 08:25:44 +000094 self.assertRaises(TypeError, [].count)
Jeremy Hyltonbea39472001-05-29 16:26:20 +000095
96 def test_oldargs1_1(self):
Raymond Hettinger31017ae2004-03-04 08:25:44 +000097 [].count(1)
Jeremy Hyltonbea39472001-05-29 16:26:20 +000098
99 def test_oldargs1_2(self):
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000100 self.assertRaises(TypeError, [].count, 1, 2)
Jeremy Hyltonbea39472001-05-29 16:26:20 +0000101
102 def test_oldargs1_0_ext(self):
103 try:
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000104 [].count(*())
Jeremy Hyltonbea39472001-05-29 16:26:20 +0000105 except TypeError:
106 pass
107 else:
108 raise RuntimeError
109
110 def test_oldargs1_1_ext(self):
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000111 [].count(*(1,))
Jeremy Hyltonbea39472001-05-29 16:26:20 +0000112
113 def test_oldargs1_2_ext(self):
114 try:
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000115 [].count(*(1, 2))
Jeremy Hyltonbea39472001-05-29 16:26:20 +0000116 except TypeError:
117 pass
118 else:
119 raise RuntimeError
120
121 def test_oldargs1_0_kw(self):
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000122 self.assertRaises(TypeError, [].count, x=2)
Jeremy Hyltonbea39472001-05-29 16:26:20 +0000123
124 def test_oldargs1_1_kw(self):
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000125 self.assertRaises(TypeError, [].count, {}, x=2)
Jeremy Hyltonbea39472001-05-29 16:26:20 +0000126
127 def test_oldargs1_2_kw(self):
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000128 self.assertRaises(TypeError, [].count, x=2, y=2)
Jeremy Hyltonbea39472001-05-29 16:26:20 +0000129
Fred Drake2e2be372001-09-20 21:33:42 +0000130
Victor Stinnerb7577452017-06-09 22:28:32 +0200131def pyfunc(arg1, arg2):
132 return [arg1, arg2]
133
134
135def pyfunc_noarg():
136 return "noarg"
137
138
139class PythonClass:
140 def method(self, arg1, arg2):
141 return [arg1, arg2]
142
143 def method_noarg(self):
144 return "noarg"
145
146 @classmethod
147 def class_method(cls):
148 return "classmethod"
149
150 @staticmethod
151 def static_method():
152 return "staticmethod"
153
154
155PYTHON_INSTANCE = PythonClass()
156
157
158IGNORE_RESULT = object()
159
160
161@cpython_only
162class FastCallTests(unittest.TestCase):
163 # Test calls with positional arguments
164 CALLS_POSARGS = (
165 # (func, args: tuple, result)
166
167 # Python function with 2 arguments
168 (pyfunc, (1, 2), [1, 2]),
169
170 # Python function without argument
171 (pyfunc_noarg, (), "noarg"),
172
173 # Python class methods
174 (PythonClass.class_method, (), "classmethod"),
175 (PythonClass.static_method, (), "staticmethod"),
176
177 # Python instance methods
178 (PYTHON_INSTANCE.method, (1, 2), [1, 2]),
179 (PYTHON_INSTANCE.method_noarg, (), "noarg"),
180 (PYTHON_INSTANCE.class_method, (), "classmethod"),
181 (PYTHON_INSTANCE.static_method, (), "staticmethod"),
182
183 # C function: METH_NOARGS
184 (globals, (), IGNORE_RESULT),
185
186 # C function: METH_O
187 (id, ("hello",), IGNORE_RESULT),
188
189 # C function: METH_VARARGS
190 (dir, (1,), IGNORE_RESULT),
191
192 # C function: METH_VARARGS | METH_KEYWORDS
193 (min, (5, 9), 5),
194
195 # C function: METH_FASTCALL
196 (divmod, (1000, 33), (30, 10)),
197
198 # C type static method: METH_FASTCALL | METH_CLASS
199 (int.from_bytes, (b'\x01\x00', 'little'), 1),
200
201 # bpo-30524: Test that calling a C type static method with no argument
202 # doesn't crash (ignore the result): METH_FASTCALL | METH_CLASS
203 (datetime.datetime.now, (), IGNORE_RESULT),
204 )
205
206 # Test calls with positional and keyword arguments
207 CALLS_KWARGS = (
208 # (func, args: tuple, kwargs: dict, result)
209
210 # Python function with 2 arguments
211 (pyfunc, (1,), {'arg2': 2}, [1, 2]),
212 (pyfunc, (), {'arg1': 1, 'arg2': 2}, [1, 2]),
213
214 # Python instance methods
215 (PYTHON_INSTANCE.method, (1,), {'arg2': 2}, [1, 2]),
216 (PYTHON_INSTANCE.method, (), {'arg1': 1, 'arg2': 2}, [1, 2]),
217
218 # C function: METH_VARARGS | METH_KEYWORDS
219 (max, ([],), {'default': 9}, 9),
220
221 # C type static method: METH_FASTCALL | METH_CLASS
222 (int.from_bytes, (b'\x01\x00',), {'byteorder': 'little'}, 1),
223 (int.from_bytes, (), {'bytes': b'\x01\x00', 'byteorder': 'little'}, 1),
224 )
225
226 def check_result(self, result, expected):
227 if expected is IGNORE_RESULT:
228 return
229 self.assertEqual(result, expected)
230
231 def test_fastcall(self):
232 # Test _PyObject_FastCall()
233
234 for func, args, expected in self.CALLS_POSARGS:
235 with self.subTest(func=func, args=args):
236 result = _testcapi.pyobject_fastcall(func, args)
237 self.check_result(result, expected)
238
239 if not args:
240 # args=NULL, nargs=0
241 result = _testcapi.pyobject_fastcall(func, None)
242 self.check_result(result, expected)
243
244 def test_fastcall_dict(self):
245 # Test _PyObject_FastCallDict()
246
247 for func, args, expected in self.CALLS_POSARGS:
248 with self.subTest(func=func, args=args):
249 # kwargs=NULL
250 result = _testcapi.pyobject_fastcalldict(func, args, None)
251 self.check_result(result, expected)
252
253 # kwargs={}
254 result = _testcapi.pyobject_fastcalldict(func, args, {})
255 self.check_result(result, expected)
256
257 if not args:
258 # args=NULL, nargs=0, kwargs=NULL
259 result = _testcapi.pyobject_fastcalldict(func, None, None)
260 self.check_result(result, expected)
261
262 # args=NULL, nargs=0, kwargs={}
263 result = _testcapi.pyobject_fastcalldict(func, None, {})
264 self.check_result(result, expected)
265
266 for func, args, kwargs, expected in self.CALLS_KWARGS:
267 with self.subTest(func=func, args=args, kwargs=kwargs):
268 result = _testcapi.pyobject_fastcalldict(func, args, kwargs)
269 self.check_result(result, expected)
270
271 def test_fastcall_keywords(self):
272 # Test _PyObject_FastCallKeywords()
273
274 for func, args, expected in self.CALLS_POSARGS:
275 with self.subTest(func=func, args=args):
276 # kwnames=NULL
277 result = _testcapi.pyobject_fastcallkeywords(func, args, None)
278 self.check_result(result, expected)
279
280 # kwnames=()
281 result = _testcapi.pyobject_fastcallkeywords(func, args, ())
282 self.check_result(result, expected)
283
284 if not args:
285 # kwnames=NULL
286 result = _testcapi.pyobject_fastcallkeywords(func, None, None)
287 self.check_result(result, expected)
288
289 # kwnames=()
290 result = _testcapi.pyobject_fastcallkeywords(func, None, ())
291 self.check_result(result, expected)
292
293 for func, args, kwargs, expected in self.CALLS_KWARGS:
294 with self.subTest(func=func, args=args, kwargs=kwargs):
295 kwnames = tuple(kwargs.keys())
296 args = args + tuple(kwargs.values())
297 result = _testcapi.pyobject_fastcallkeywords(func, args, kwnames)
298 self.check_result(result, expected)
299
300
Fred Drake2e2be372001-09-20 21:33:42 +0000301if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500302 unittest.main()