| 1 |
from pypy.module.cpyext.test.test_api import BaseApiTest |
| 2 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 3 |
from pypy.module.cpyext.methodobject import PyMethodDef |
| 4 |
from pypy.module.cpyext.api import ApiFunction |
| 5 |
from pypy.module.cpyext.pyobject import PyObject, make_ref |
| 6 |
from pypy.module.cpyext.methodobject import ( |
| 7 |
PyDescr_NewMethod, PyCFunction) |
| 8 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 9 |
|
| 10 |
class AppTestMethodObject(AppTestCpythonExtensionBase): |
| 11 |
|
| 12 |
def test_call_METH_NOARGS(self): |
| 13 |
mod = self.import_extension('MyModule', [ |
| 14 |
('getarg_NO', 'METH_NOARGS', |
| 15 |
''' |
| 16 |
if(args) { |
| 17 |
Py_INCREF(args); |
| 18 |
return args; |
| 19 |
} |
| 20 |
else { |
| 21 |
Py_INCREF(Py_None); |
| 22 |
return Py_None; |
| 23 |
} |
| 24 |
''' |
| 25 |
), |
| 26 |
]) |
| 27 |
assert mod.getarg_NO() is None |
| 28 |
excinfo = raises(TypeError, mod.getarg_NO, 1) |
| 29 |
assert "(1 given)" in str(excinfo.value) |
| 30 |
raises(TypeError, mod.getarg_NO, 1, 1) |
| 31 |
|
| 32 |
def test_call_METH_O(self): |
| 33 |
mod = self.import_extension('MyModule', [ |
| 34 |
('getarg_O', 'METH_O', |
| 35 |
''' |
| 36 |
Py_INCREF(args); |
| 37 |
return args; |
| 38 |
''' |
| 39 |
), |
| 40 |
]) |
| 41 |
assert mod.getarg_O(1) == 1 |
| 42 |
assert mod.getarg_O.__name__ == "getarg_O" |
| 43 |
raises(TypeError, mod.getarg_O) |
| 44 |
raises(TypeError, mod.getarg_O, 1, 1) |
| 45 |
|
| 46 |
def test_call_METH_VARARGS(self): |
| 47 |
mod = self.import_extension('MyModule', [ |
| 48 |
('getarg_VARARGS', 'METH_VARARGS', |
| 49 |
''' |
| 50 |
return Py_BuildValue("Ol", args, args->ob_refcnt); |
| 51 |
''' |
| 52 |
), |
| 53 |
]) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
tup, refcnt = mod.getarg_VARARGS() |
| 61 |
assert tup == () |
| 62 |
|
| 63 |
|
| 64 |
if not self.runappdirect: |
| 65 |
assert refcnt == 1 |
| 66 |
|
| 67 |
tup, refcnt = mod.getarg_VARARGS(1) |
| 68 |
assert tup == (1,) |
| 69 |
assert refcnt == 1 |
| 70 |
|
| 71 |
tup, refcnt = mod.getarg_VARARGS(1, 2, 3) |
| 72 |
assert tup == (1, 2, 3) |
| 73 |
assert refcnt == 1 |
| 74 |
|
| 75 |
raises(TypeError, mod.getarg_VARARGS, k=1) |
| 76 |
|
| 77 |
def test_call_METH_VARARGS_FASTCALL(self): |
| 78 |
mod = self.import_extension('MyModule', [ |
| 79 |
('getarg_VA_FASTCALL', 'METH_VARARGS | METH_FASTCALL', |
| 80 |
''' |
| 81 |
PyObject *arg_tuple = PyTuple_New(len_args); |
| 82 |
for (Py_ssize_t i =0 ; i < len_args; i++) { |
| 83 |
Py_INCREF(args[i]); |
| 84 |
PyTuple_SetItem(arg_tuple, i, args[i]); |
| 85 |
} |
| 86 |
return arg_tuple; |
| 87 |
''' |
| 88 |
), |
| 89 |
]) |
| 90 |
|
| 91 |
tup = mod.getarg_VA_FASTCALL() |
| 92 |
assert tup == () |
| 93 |
|
| 94 |
tup = mod.getarg_VA_FASTCALL(1) |
| 95 |
assert tup == (1,) |
| 96 |
|
| 97 |
tup = mod.getarg_VA_FASTCALL(1, 2, 3) |
| 98 |
assert tup == (1, 2, 3) |
| 99 |
|
| 100 |
raises(TypeError, mod.getarg_VA_FASTCALL, k=1) |
| 101 |
|
| 102 |
def test_call_METH_KEYWORDS(self): |
| 103 |
mod = self.import_extension('MyModule', [ |
| 104 |
('getarg_KW', 'METH_VARARGS | METH_KEYWORDS', |
| 105 |
''' |
| 106 |
if (!kwargs) kwargs = Py_None; |
| 107 |
return Py_BuildValue("OO", args, kwargs); |
| 108 |
''' |
| 109 |
), |
| 110 |
]) |
| 111 |
assert mod.getarg_KW(1) == ((1,), None) |
| 112 |
assert mod.getarg_KW(1, 2) == ((1, 2), None) |
| 113 |
assert mod.getarg_KW(a=3, b=4) == ((), {'a': 3, 'b': 4}) |
| 114 |
assert mod.getarg_KW(1, 2, a=3, b=4) == ((1, 2), {'a': 3, 'b': 4}) |
| 115 |
assert mod.getarg_KW.__name__ == "getarg_KW" |
| 116 |
assert mod.getarg_KW(*(), **{}) == ((), {}) |
| 117 |
|
| 118 |
def test_call_METH_KEYWORDS_FASTCALL(self): |
| 119 |
mod = self.import_extension('MyModule', [ |
| 120 |
('getarg_KW_FASTCALL', 'METH_FASTCALL | METH_KEYWORDS', |
| 121 |
''' |
| 122 |
Py_ssize_t len_all_args = len_args; |
| 123 |
if (!kwnames) { |
| 124 |
kwnames = Py_None; |
| 125 |
} |
| 126 |
else { |
| 127 |
len_all_args += PyTuple_Size(kwnames); |
| 128 |
} |
| 129 |
PyObject *arg_tuple = PyTuple_New(len_all_args); |
| 130 |
for (Py_ssize_t i = 0 ; i < len_all_args; i++) { |
| 131 |
Py_INCREF(args[i]); |
| 132 |
PyTuple_SetItem(arg_tuple, i, args[i]); |
| 133 |
} |
| 134 |
return Py_BuildValue("NO", arg_tuple, kwnames); |
| 135 |
''' |
| 136 |
), |
| 137 |
]) |
| 138 |
assert mod.getarg_KW_FASTCALL(1) == ((1,), None) |
| 139 |
assert mod.getarg_KW_FASTCALL(1, 2) == ((1, 2), None) |
| 140 |
assert mod.getarg_KW_FASTCALL(a=3, b=4) == ((3, 4), ('a', 'b')) |
| 141 |
assert mod.getarg_KW_FASTCALL(1, 2, a=3, b=4) == ((1, 2, 3, 4), ('a', 'b')) |
| 142 |
assert mod.getarg_KW_FASTCALL.__name__ == "getarg_KW_FASTCALL" |
| 143 |
assert mod.getarg_KW_FASTCALL(*(), **{}) == ((), None) |
| 144 |
|
| 145 |
res = mod.getarg_KW_FASTCALL(1, 2, a=5, b=6, *[3, 4], **{'c': 7}) |
| 146 |
assert res[0][:4] == (1, 2, 3, 4) |
| 147 |
|
| 148 |
assert dict(zip(res[1], res[0][4:])) == {'a': 5, 'b': 6, 'c': 7} |
| 149 |
|
| 150 |
args = tuple(range(50)) |
| 151 |
kwargs = {str(i): i for i in range(100)} |
| 152 |
res = mod.getarg_KW_FASTCALL(*args, **kwargs) |
| 153 |
assert res[0][:len(args)] == args |
| 154 |
assert dict(zip(res[1], res[0][len(args):])) == kwargs |
| 155 |
|
| 156 |
def test_func_attributes(self): |
| 157 |
mod = self.import_extension('MyModule', [ |
| 158 |
('isCFunction', 'METH_O', |
| 159 |
''' |
| 160 |
if(PyCFunction_Check(args)) { |
| 161 |
PyCFunctionObject* func = (PyCFunctionObject*)args; |
| 162 |
return PyUnicode_FromString(func->m_ml->ml_name); |
| 163 |
} |
| 164 |
else { |
| 165 |
Py_RETURN_FALSE; |
| 166 |
} |
| 167 |
''' |
| 168 |
), |
| 169 |
('getModule', 'METH_O', |
| 170 |
''' |
| 171 |
if(PyCFunction_Check(args)) { |
| 172 |
PyCFunctionObject* func = (PyCFunctionObject*)args; |
| 173 |
Py_INCREF(func->m_module); |
| 174 |
return func->m_module; |
| 175 |
} |
| 176 |
else { |
| 177 |
Py_RETURN_FALSE; |
| 178 |
} |
| 179 |
''' |
| 180 |
), |
| 181 |
('isSameFunction', 'METH_O', |
| 182 |
''' |
| 183 |
PyCFunction ptr = PyCFunction_GetFunction(args); |
| 184 |
if (!ptr) return NULL; |
| 185 |
if (ptr == (PyCFunction)MyModule_getModule) |
| 186 |
Py_RETURN_TRUE; |
| 187 |
else |
| 188 |
Py_RETURN_FALSE; |
| 189 |
''' |
| 190 |
), |
| 191 |
]) |
| 192 |
assert mod.isCFunction(mod.getModule) == "getModule" |
| 193 |
assert mod.getModule(mod.getModule) == 'MyModule' |
| 194 |
if self.runappdirect: |
| 195 |
assert mod.isSameFunction(mod.getModule) |
| 196 |
raises(SystemError, mod.isSameFunction, 1) |
| 197 |
|
| 198 |
def test_function_as_method(self): |
| 199 |
|
| 200 |
mod = self.import_extension('foo', [ |
| 201 |
('f', 'METH_NOARGS', |
| 202 |
''' |
| 203 |
return PyLong_FromLong(42); |
| 204 |
'''), |
| 205 |
]) |
| 206 |
class A(object): pass |
| 207 |
A.f = mod.f |
| 208 |
A.g = lambda: 42 |
| 209 |
|
| 210 |
assert A.f() == A.g() == 42 |
| 211 |
|
| 212 |
assert A().f() == 42 |
| 213 |
raises(TypeError, A().g) |
| 214 |
|
| 215 |
def test_check(self): |
| 216 |
mod = self.import_extension('foo', [ |
| 217 |
('check', 'METH_O', |
| 218 |
''' |
| 219 |
return PyLong_FromLong(PyCFunction_Check(args)); |
| 220 |
'''), |
| 221 |
]) |
| 222 |
from math import degrees |
| 223 |
assert mod.check(degrees) == 1 |
| 224 |
assert mod.check(list) == 0 |
| 225 |
assert mod.check(sorted) == 1 |
| 226 |
def func(): |
| 227 |
pass |
| 228 |
class A(object): |
| 229 |
def meth(self): |
| 230 |
pass |
| 231 |
@staticmethod |
| 232 |
def stat(): |
| 233 |
pass |
| 234 |
assert mod.check(func) == 0 |
| 235 |
assert mod.check(A) == 0 |
| 236 |
assert mod.check(A.meth) == 0 |
| 237 |
assert mod.check(A.stat) == 0 |
| 238 |
|
| 239 |
def test_module_attribute(self): |
| 240 |
mod = self.import_extension('MyModule', [ |
| 241 |
('getarg_NO', 'METH_NOARGS', |
| 242 |
''' |
| 243 |
Py_INCREF(Py_None); |
| 244 |
return Py_None; |
| 245 |
''' |
| 246 |
), |
| 247 |
]) |
| 248 |
assert mod.getarg_NO() is None |
| 249 |
assert mod.getarg_NO.__module__ == 'MyModule' |
| 250 |
mod.getarg_NO.__module__ = 'foobar' |
| 251 |
assert mod.getarg_NO.__module__ == 'foobar' |
| 252 |
|
| 253 |
def test_callfunc(self): |
| 254 |
mod = self.import_extension('foo', [ |
| 255 |
('callfunc', 'METH_VARARGS', |
| 256 |
''' |
| 257 |
PyObject *func, *argseq=NULL, *kwargs=NULL; |
| 258 |
if (!PyArg_ParseTuple(args, "O|OO", &func, &argseq, &kwargs)) { |
| 259 |
return NULL; |
| 260 |
} |
| 261 |
if (!PyCFunction_Check(func)) { |
| 262 |
Py_RETURN_FALSE; |
| 263 |
} |
| 264 |
if (argseq == NULL) { |
| 265 |
return PyCFunction_Call(func, NULL, NULL); |
| 266 |
} |
| 267 |
if (kwargs == NULL) { |
| 268 |
return PyCFunction_Call(func, argseq, NULL); |
| 269 |
} |
| 270 |
return PyCFunction_Call(func, argseq, kwargs); |
| 271 |
''' |
| 272 |
), |
| 273 |
|
| 274 |
('func_NOARGS', 'METH_NOARGS', |
| 275 |
''' |
| 276 |
Py_RETURN_TRUE; |
| 277 |
'''), |
| 278 |
('func_KW', 'METH_VARARGS | METH_KEYWORDS', |
| 279 |
''' |
| 280 |
if (!kwargs) kwargs = Py_None; |
| 281 |
return Py_BuildValue("OO", args, kwargs); |
| 282 |
''' |
| 283 |
), |
| 284 |
]) |
| 285 |
ret = mod.callfunc(mod.func_NOARGS, ()) |
| 286 |
assert ret is True |
| 287 |
ret = mod.callfunc(mod.func_KW, (1, 2, 3), {'a':'a', 'b':'b', 'c':'c'}) |
| 288 |
assert ret == ((1, 2, 3), {'a':'a', 'b':'b', 'c':'c'}) |
| 289 |
with raises(TypeError): |
| 290 |
mod.callfunc(mod.func_NOARGS, (), {'a': 'a'}) |
| 291 |
with raises(TypeError): |
| 292 |
mod.callfunc(mod.func_NOARGS, (1, 2, 3)) |
| 293 |
|
| 294 |
def test_wrapper(self): |
| 295 |
|
| 296 |
mod = self.import_module(name="specmethdocstring") |
| 297 |
c = mod.C() |
| 298 |
assert c.__iter__.__doc__ == "usable docstring" |
| 299 |
assert mod.C.__qualname__ == "C" |
| 300 |
assert c.__iter__.__qualname__ == "C.__iter__" |
| 301 |
assert type(c.__iter__).__doc__.startswith('instancemethod(function, instance)') |
| 302 |
assert mod.C.__next__.__doc__ == "usable docstring" |
| 303 |
assert mod.C.__next__.__text_signature__ == '($self, /)' |
| 304 |
|
| 305 |
def test_module_name(self): |
| 306 |
|
| 307 |
mod = self.import_module(name="test_func") |
| 308 |
assert mod.f.__module__ == "test_func" |
| 309 |
|
| 310 |
def test_min_max(self): |
| 311 |
mod = self.import_module(name="builtin_min_max") |
| 312 |
mod.test_max2() |
| 313 |
|
| 314 |
|