Victor Stinner | b757745 | 2017-06-09 22:28:32 +0200 | [diff] [blame] | 1 | import datetime |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 2 | import unittest |
Victor Stinner | b757745 | 2017-06-09 22:28:32 +0200 | [diff] [blame] | 3 | from test.support import cpython_only |
| 4 | try: |
| 5 | import _testcapi |
| 6 | except ImportError: |
| 7 | _testcapi = None |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 8 | |
| 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 | |
| 14 | class CFunctionCalls(unittest.TestCase): |
| 15 | |
| 16 | def test_varargs0(self): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 17 | self.assertRaises(TypeError, {}.__contains__) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 18 | |
| 19 | def test_varargs1(self): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 20 | {}.__contains__(0) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 21 | |
| 22 | def test_varargs2(self): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 23 | self.assertRaises(TypeError, {}.__contains__, 0, 1) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 24 | |
| 25 | def test_varargs0_ext(self): |
| 26 | try: |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 27 | {}.__contains__(*()) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 28 | except TypeError: |
| 29 | pass |
| 30 | |
| 31 | def test_varargs1_ext(self): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 32 | {}.__contains__(*(0,)) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 33 | |
| 34 | def test_varargs2_ext(self): |
| 35 | try: |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 36 | {}.__contains__(*(1, 2)) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 37 | except TypeError: |
| 38 | pass |
| 39 | else: |
| 40 | raise RuntimeError |
| 41 | |
| 42 | def test_varargs0_kw(self): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 43 | self.assertRaises(TypeError, {}.__contains__, x=2) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 44 | |
| 45 | def test_varargs1_kw(self): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 46 | self.assertRaises(TypeError, {}.__contains__, x=2) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 47 | |
| 48 | def test_varargs2_kw(self): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 49 | self.assertRaises(TypeError, {}.__contains__, x=2, y=2) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 50 | |
| 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 Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 94 | self.assertRaises(TypeError, [].count) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 95 | |
| 96 | def test_oldargs1_1(self): |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 97 | [].count(1) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 98 | |
| 99 | def test_oldargs1_2(self): |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 100 | self.assertRaises(TypeError, [].count, 1, 2) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 101 | |
| 102 | def test_oldargs1_0_ext(self): |
| 103 | try: |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 104 | [].count(*()) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 105 | except TypeError: |
| 106 | pass |
| 107 | else: |
| 108 | raise RuntimeError |
| 109 | |
| 110 | def test_oldargs1_1_ext(self): |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 111 | [].count(*(1,)) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 112 | |
| 113 | def test_oldargs1_2_ext(self): |
| 114 | try: |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 115 | [].count(*(1, 2)) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 116 | except TypeError: |
| 117 | pass |
| 118 | else: |
| 119 | raise RuntimeError |
| 120 | |
| 121 | def test_oldargs1_0_kw(self): |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 122 | self.assertRaises(TypeError, [].count, x=2) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 123 | |
| 124 | def test_oldargs1_1_kw(self): |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 125 | self.assertRaises(TypeError, [].count, {}, x=2) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 126 | |
| 127 | def test_oldargs1_2_kw(self): |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 128 | self.assertRaises(TypeError, [].count, x=2, y=2) |
Jeremy Hylton | bea3947 | 2001-05-29 16:26:20 +0000 | [diff] [blame] | 129 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 130 | |
Victor Stinner | b757745 | 2017-06-09 22:28:32 +0200 | [diff] [blame] | 131 | def pyfunc(arg1, arg2): |
| 132 | return [arg1, arg2] |
| 133 | |
| 134 | |
| 135 | def pyfunc_noarg(): |
| 136 | return "noarg" |
| 137 | |
| 138 | |
| 139 | class 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 | |
| 155 | PYTHON_INSTANCE = PythonClass() |
| 156 | |
| 157 | |
| 158 | IGNORE_RESULT = object() |
| 159 | |
| 160 | |
| 161 | @cpython_only |
| 162 | class 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 Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 301 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 302 | unittest.main() |