1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
from pypy.module.cpyext.test.test_api import BaseApiTest
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.module.cpyext.methodobject import PyMethodDef
from pypy.module.cpyext.api import ApiFunction
from pypy.module.cpyext.pyobject import PyObject, make_ref
from pypy.module.cpyext.methodobject import (
PyDescr_NewMethod, PyCFunction)
from rpython.rtyper.lltypesystem import rffi, lltype
class AppTestMethodObject(AppTestCpythonExtensionBase):
def test_call_METH_NOARGS(self):
mod = self.import_extension('MyModule', [
('getarg_NO', 'METH_NOARGS',
'''
if(args) {
Py_INCREF(args);
return args;
}
else {
Py_INCREF(Py_None);
return Py_None;
}
'''
),
])
assert mod.getarg_NO() is None
raises(TypeError, mod.getarg_NO, 1)
raises(TypeError, mod.getarg_NO, 1, 1)
def test_call_METH_O(self):
mod = self.import_extension('MyModule', [
('getarg_O', 'METH_O',
'''
Py_INCREF(args);
return args;
'''
),
])
assert mod.getarg_O(1) == 1
assert mod.getarg_O.__name__ == "getarg_O"
raises(TypeError, mod.getarg_O)
raises(TypeError, mod.getarg_O, 1, 1)
def test_call_METH_VARARGS(self):
mod = self.import_extension('MyModule', [
('getarg_VARARGS', 'METH_VARARGS',
'''
return Py_BuildValue("Ol", args, args->ob_refcnt);
'''
),
])
# check that we pass the expected tuple of arguments AND that the
# recnt is 1. In particular, on PyPy refcnt==1 means that we created
# the PyObject tuple directly, without passing from a w_tuple; as
# such, the tuple will be immediately freed after the call, without
# having to wait until the GC runs.
#
tup, refcnt = mod.getarg_VARARGS()
assert tup == ()
# the empty tuple is shared on CPython, so the refcnt will be >1. On
# PyPy it is not shared, though.
if not self.runappdirect:
assert refcnt == 1
#
tup, refcnt = mod.getarg_VARARGS(1)
assert tup == (1,)
assert refcnt == 1
#
tup, refcnt = mod.getarg_VARARGS(1, 2, 3)
assert tup == (1, 2, 3)
assert refcnt == 1
#
raises(TypeError, mod.getarg_VARARGS, k=1)
def test_call_METH_KEYWORDS(self):
mod = self.import_extension('MyModule', [
('getarg_KW', 'METH_VARARGS | METH_KEYWORDS',
'''
if (!kwargs) kwargs = Py_None;
return Py_BuildValue("OO", args, kwargs);
'''
),
])
assert mod.getarg_KW(1) == ((1,), None)
assert mod.getarg_KW(1, 2) == ((1, 2), None)
assert mod.getarg_KW(a=3, b=4) == ((), {'a': 3, 'b': 4})
assert mod.getarg_KW(1, 2, a=3, b=4) == ((1, 2), {'a': 3, 'b': 4})
assert mod.getarg_KW.__name__ == "getarg_KW"
def test_func_attributes(self):
mod = self.import_extension('MyModule', [
('isCFunction', 'METH_O',
'''
if(PyCFunction_Check(args)) {
PyCFunctionObject* func = (PyCFunctionObject*)args;
return PyUnicode_FromString(func->m_ml->ml_name);
}
else {
Py_RETURN_FALSE;
}
'''
),
('getModule', 'METH_O',
'''
if(PyCFunction_Check(args)) {
PyCFunctionObject* func = (PyCFunctionObject*)args;
Py_INCREF(func->m_module);
return func->m_module;
}
else {
Py_RETURN_FALSE;
}
'''
),
('isSameFunction', 'METH_O',
'''
PyCFunction ptr = PyCFunction_GetFunction(args);
if (!ptr) return NULL;
if (ptr == (PyCFunction)MyModule_getModule)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
'''
),
])
assert mod.isCFunction(mod.getModule) == "getModule"
assert mod.getModule(mod.getModule) == 'MyModule'
if self.runappdirect: # XXX: fails untranslated
assert mod.isSameFunction(mod.getModule)
raises(SystemError, mod.isSameFunction, 1)
def test_function_as_method(self):
# Unlike user functions, builtins don't become methods
mod = self.import_extension('foo', [
('f', 'METH_NOARGS',
'''
return PyLong_FromLong(42);
'''),
])
class A(object): pass
A.f = mod.f
A.g = lambda: 42
# Unbound method
assert A.f() == A.g() == 42
# Bound method
assert A().f() == 42
raises(TypeError, A().g)
def test_check(self):
mod = self.import_extension('foo', [
('check', 'METH_O',
'''
return PyLong_FromLong(PyCFunction_Check(args));
'''),
])
from math import degrees
assert mod.check(degrees) == 1
assert mod.check(list) == 0
assert mod.check(sorted) == 1
def func():
pass
class A(object):
def meth(self):
pass
@staticmethod
def stat():
pass
assert mod.check(func) == 0
assert mod.check(A) == 0
assert mod.check(A.meth) == 0
assert mod.check(A.stat) == 0
def test_module_attribute(self):
mod = self.import_extension('MyModule', [
('getarg_NO', 'METH_NOARGS',
'''
Py_INCREF(Py_None);
return Py_None;
'''
),
])
assert mod.getarg_NO() is None
assert mod.getarg_NO.__module__ == 'MyModule'
mod.getarg_NO.__module__ = 'foobar'
assert mod.getarg_NO.__module__ == 'foobar'
def test_text_signature(self):
mod = self.import_module('docstrings')
assert mod.no_doc.__doc__ is None
assert mod.no_doc.__text_signature__ is None
assert mod.empty_doc.__doc__ is None
assert mod.empty_doc.__text_signature__ is None
assert mod.no_sig.__doc__
assert mod.no_sig.__text_signature__ is None
assert mod.invalid_sig.__doc__
assert mod.invalid_sig.__text_signature__ is None
assert mod.invalid_sig2.__doc__
assert mod.invalid_sig2.__text_signature__ is None
assert mod.with_sig.__doc__
assert mod.with_sig.__text_signature__ == '($module, /, sig)'
assert mod.with_sig_but_no_doc.__doc__ is None
assert mod.with_sig_but_no_doc.__text_signature__ == '($module, /, sig)'
assert mod.with_signature_and_extra_newlines.__doc__
assert (mod.with_signature_and_extra_newlines.__text_signature__ ==
'($module, /, parameter)')
|