| 1 |
from rpython.rtyper.lltypesystem import rffi |
| 2 |
from pypy.module.cpyext.test.test_api import BaseApiTest |
| 3 |
from pypy.module.cpyext.pyobject import PyObject, make_ref, from_ref, decref |
| 4 |
from pypy.module.cpyext.methodobject import PyClassMethod_New |
| 5 |
from pypy.module.cpyext.funcobject import ( |
| 6 |
PyFunctionObject, PyCodeObject, CODE_FLAGS, PyMethod_Function, |
| 7 |
PyMethod_Self, PyMethod_New, PyFunction_GetCode, PyFunction_GetModule, |
| 8 |
PyCode_NewEmpty, PyCode_Addr2Line) |
| 9 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 10 |
from pypy.interpreter.function import Function |
| 11 |
from pypy.interpreter.pycode import PyCode |
| 12 |
|
| 13 |
globals().update(CODE_FLAGS) |
| 14 |
|
| 15 |
class TestFunctionObject(BaseApiTest): |
| 16 |
def test_function(self, space): |
| 17 |
w_function = space.appexec([], """(): |
| 18 |
def f(): pass |
| 19 |
return f |
| 20 |
""") |
| 21 |
ref = make_ref(space, w_function) |
| 22 |
assert (from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) is |
| 23 |
space.gettypeobject(Function.typedef)) |
| 24 |
assert "f" == space.unwrap( |
| 25 |
from_ref(space, rffi.cast(PyFunctionObject, ref).c_func_name)) |
| 26 |
decref(space, ref) |
| 27 |
|
| 28 |
def test_method(self, space): |
| 29 |
w_method = space.appexec([], """(): |
| 30 |
class C(list): |
| 31 |
def method(self): pass |
| 32 |
return C().method |
| 33 |
""") |
| 34 |
|
| 35 |
w_function = space.getattr(w_method, space.wrap("__func__")) |
| 36 |
w_self = space.getattr(w_method, space.wrap("__self__")) |
| 37 |
|
| 38 |
assert space.is_w(PyMethod_Function(space, w_method), w_function) |
| 39 |
assert space.is_w(PyMethod_Self(space, w_method), w_self) |
| 40 |
|
| 41 |
w_method2 = PyMethod_New(space, w_function, w_self) |
| 42 |
assert space.eq_w(w_method, w_method2) |
| 43 |
|
| 44 |
def test_get_code_and_module(self, space): |
| 45 |
w_function = space.appexec([], """(): |
| 46 |
def func(x, y, z): return x |
| 47 |
func.__module__ = "abc" |
| 48 |
return func |
| 49 |
""", cache=False) |
| 50 |
w_code = PyFunction_GetCode(space, w_function) |
| 51 |
assert w_code.co_name == "func" |
| 52 |
|
| 53 |
ref = make_ref(space, w_code) |
| 54 |
assert (from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) is |
| 55 |
space.gettypeobject(PyCode.typedef)) |
| 56 |
assert "func" == space.unwrap( |
| 57 |
from_ref(space, rffi.cast(PyCodeObject, ref).c_co_name)) |
| 58 |
assert 3 == rffi.cast(PyCodeObject, ref).c_co_argcount |
| 59 |
decref(space, ref) |
| 60 |
|
| 61 |
w_module = PyFunction_GetModule(space,w_function) |
| 62 |
assert space.utf8_w(w_module) == "abc" |
| 63 |
|
| 64 |
def test_co_flags(self, space): |
| 65 |
def get_flags(signature, body="pass"): |
| 66 |
w_code = space.appexec([], """(): |
| 67 |
def func(%s): %s |
| 68 |
return func.__code__ |
| 69 |
""" % (signature, body), cache=False) |
| 70 |
ref = make_ref(space, w_code) |
| 71 |
co_flags = rffi.cast(PyCodeObject, ref).c_co_flags |
| 72 |
decref(space, ref) |
| 73 |
return co_flags |
| 74 |
assert get_flags("x") == CO_NESTED | CO_OPTIMIZED | CO_NEWLOCALS |
| 75 |
assert get_flags("x, *args") & CO_VARARGS |
| 76 |
assert get_flags("x, **kw") & CO_VARKEYWORDS |
| 77 |
assert get_flags("x", "yield x") & CO_GENERATOR |
| 78 |
|
| 79 |
def test_newcode(self, space): |
| 80 |
filename = rffi.str2charp('filename') |
| 81 |
funcname = rffi.str2charp('funcname') |
| 82 |
w_code = PyCode_NewEmpty(space, filename, funcname, 3) |
| 83 |
assert w_code.co_filename == 'filename' |
| 84 |
assert w_code.co_firstlineno == 3 |
| 85 |
|
| 86 |
ref = make_ref(space, w_code) |
| 87 |
assert "filename" == space.unwrap( |
| 88 |
from_ref(space, rffi.cast(PyCodeObject, ref).c_co_filename)) |
| 89 |
decref(space, ref) |
| 90 |
rffi.free_charp(filename) |
| 91 |
rffi.free_charp(funcname) |
| 92 |
|
| 93 |
def test_classmethod(self, space): |
| 94 |
w_function = space.appexec([], """(): |
| 95 |
def method(x): return x |
| 96 |
return method |
| 97 |
""") |
| 98 |
w_class = space.call_function(space.w_type, space.wrap("C"), |
| 99 |
space.newtuple([]), space.newdict()) |
| 100 |
w_instance = space.call_function(w_class) |
| 101 |
|
| 102 |
space.setattr(w_class, space.wrap("method"), w_function) |
| 103 |
assert space.is_w(space.call_method(w_instance, "method"), w_instance) |
| 104 |
|
| 105 |
w_classmethod = PyClassMethod_New(space, w_function) |
| 106 |
space.setattr(w_class, space.wrap("classmethod"), w_classmethod) |
| 107 |
assert space.is_w( |
| 108 |
space.call_method(w_instance, "classmethod"), w_class) |
| 109 |
|
| 110 |
def test_addr2line(self, space): |
| 111 |
w_function = space.appexec([], """(): |
| 112 |
def func(): |
| 113 |
x = a + b |
| 114 |
return x |
| 115 |
return func |
| 116 |
""", cache=False) |
| 117 |
w_code = PyFunction_GetCode(space, w_function) |
| 118 |
assert PyCode_Addr2Line(space, w_code, 0) == 3 |
| 119 |
assert PyCode_Addr2Line(space, w_code, 8) == 4 |
| 120 |
assert PyCode_Addr2Line(space, w_code, -1) == -1 |
| 121 |
assert PyCode_Addr2Line(space, w_code, 100) == -1 |
| 122 |
|
| 123 |
class AppTestPickleCpyextFunction(AppTestCpythonExtensionBase): |
| 124 |
def test_pickle_cpyext_function_directly(self): |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
module = self.import_extension('pickle_cpyext_test', [ |
| 131 |
("identity", "METH_O", |
| 132 |
""" |
| 133 |
Py_INCREF(args); |
| 134 |
return args; |
| 135 |
"""), |
| 136 |
]) |
| 137 |
import _pickle |
| 138 |
for proto in range(6): |
| 139 |
data = _pickle.dumps(module.identity, proto) |
| 140 |
result = _pickle.loads(data) |
| 141 |
assert result is module.identity |
| 142 |
|
| 143 |
def test_pickle_reduce_returning_cpyext_function(self): |
| 144 |
|
| 145 |
|
| 146 |
module = self.import_extension('pickle_cpyext_reduce_test', [ |
| 147 |
("add_one", "METH_O", |
| 148 |
""" |
| 149 |
long n = PyLong_AsLong(args); |
| 150 |
if (n == -1 && PyErr_Occurred()) return NULL; |
| 151 |
return PyLong_FromLong(n + 1); |
| 152 |
"""), |
| 153 |
]) |
| 154 |
import _pickle |
| 155 |
|
| 156 |
class Container: |
| 157 |
def __reduce__(self): |
| 158 |
return module.add_one, (41,) |
| 159 |
|
| 160 |
for proto in range(6): |
| 161 |
result = _pickle.loads(_pickle.dumps(Container(), proto)) |
| 162 |
assert result == 42 |
| 163 |
|
| 164 |
|
| 165 |
def test_pickle_method_descriptor(self): |
| 166 |
|
| 167 |
|
| 168 |
module = self.import_extension('pickle_method_desc', [ |
| 169 |
("new_obj", "METH_NOARGS", |
| 170 |
""" |
| 171 |
PyObject *obj = PyObject_New(PyObject, &Foo_Type); |
| 172 |
return obj; |
| 173 |
"""), |
| 174 |
], prologue=""" |
| 175 |
static PyObject * |
| 176 |
foo_greet(PyObject *self, PyObject *args) |
| 177 |
{ |
| 178 |
return PyLong_FromLong(42); |
| 179 |
} |
| 180 |
static PyMethodDef foo_methods[] = { |
| 181 |
{"greet", foo_greet, METH_NOARGS, NULL}, |
| 182 |
{NULL} |
| 183 |
}; |
| 184 |
static PyTypeObject Foo_Type = { |
| 185 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 186 |
"pickle_method_desc.Foo", |
| 187 |
}; |
| 188 |
""", more_init=""" |
| 189 |
Foo_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 190 |
Foo_Type.tp_methods = foo_methods; |
| 191 |
if (PyType_Ready(&Foo_Type) < 0) INITERROR; |
| 192 |
PyModule_AddObject(mod, "Foo", (PyObject *)&Foo_Type); |
| 193 |
""") |
| 194 |
import _pickle |
| 195 |
|
| 196 |
for proto in range(6): |
| 197 |
data = _pickle.dumps(module.Foo.greet, proto) |
| 198 |
result = _pickle.loads(data) |
| 199 |
assert result is module.Foo.greet |
| 200 |
|
| 201 |
def test_pickle_classmethod_descriptor(self): |
| 202 |
|
| 203 |
|
| 204 |
module = self.import_extension('pickle_classmethod_desc', [ |
| 205 |
("new_obj", "METH_NOARGS", |
| 206 |
""" |
| 207 |
PyObject *obj = PyObject_New(PyObject, &Foo_Type); |
| 208 |
return obj; |
| 209 |
"""), |
| 210 |
], prologue=""" |
| 211 |
static PyObject * |
| 212 |
foo_from_int(PyObject *cls, PyObject *args) |
| 213 |
{ |
| 214 |
return PyLong_FromLong(99); |
| 215 |
} |
| 216 |
static PyMethodDef foo_methods[] = { |
| 217 |
{"from_int", foo_from_int, METH_NOARGS | METH_CLASS, NULL}, |
| 218 |
{NULL} |
| 219 |
}; |
| 220 |
static PyTypeObject Foo_Type = { |
| 221 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 222 |
"pickle_classmethod_desc.Foo", |
| 223 |
}; |
| 224 |
""", more_init=""" |
| 225 |
Foo_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 226 |
Foo_Type.tp_methods = foo_methods; |
| 227 |
if (PyType_Ready(&Foo_Type) < 0) INITERROR; |
| 228 |
PyModule_AddObject(mod, "Foo", (PyObject *)&Foo_Type); |
| 229 |
""") |
| 230 |
import _pickle |
| 231 |
|
| 232 |
for proto in range(6): |
| 233 |
data = _pickle.dumps(module.Foo.from_int, proto) |
| 234 |
result = _pickle.loads(data) |
| 235 |
assert result.__qualname__ == "Foo.from_int" |
| 236 |
|
| 237 |
def test_pickle_wrapper_descriptor(self): |
| 238 |
|
| 239 |
|
| 240 |
module = self.import_extension('pickle_wrapper_desc', [ |
| 241 |
("new_obj", "METH_NOARGS", |
| 242 |
""" |
| 243 |
PyObject *obj = PyObject_New(PyObject, &Foo_Type); |
| 244 |
return obj; |
| 245 |
"""), |
| 246 |
], prologue=""" |
| 247 |
static PyObject * |
| 248 |
foo_repr(PyObject *self) |
| 249 |
{ |
| 250 |
return PyUnicode_FromString("<Foo>"); |
| 251 |
} |
| 252 |
static PyTypeObject Foo_Type = { |
| 253 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 254 |
"pickle_wrapper_desc.Foo", |
| 255 |
}; |
| 256 |
""", more_init=""" |
| 257 |
Foo_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 258 |
Foo_Type.tp_repr = foo_repr; |
| 259 |
if (PyType_Ready(&Foo_Type) < 0) INITERROR; |
| 260 |
PyModule_AddObject(mod, "Foo", (PyObject *)&Foo_Type); |
| 261 |
""") |
| 262 |
import _pickle |
| 263 |
|
| 264 |
for proto in range(6): |
| 265 |
data = _pickle.dumps(module.Foo.__repr__, proto) |
| 266 |
result = _pickle.loads(data) |
| 267 |
assert result is module.Foo.__repr__ |
| 268 |
|
| 269 |
|
| 270 |
class AppTestCall(AppTestCpythonExtensionBase): |
| 271 |
def test_code_new_empty(self): |
| 272 |
module = self.import_extension('foo', [ |
| 273 |
("code_newempty", "METH_VARARGS", |
| 274 |
""" |
| 275 |
const char *filename; |
| 276 |
const char *funcname; |
| 277 |
int firstlineno; |
| 278 |
|
| 279 |
if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 280 |
&filename, &funcname, &firstlineno)) |
| 281 |
return NULL; |
| 282 |
|
| 283 |
return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
| 284 |
"""), |
| 285 |
]) |
| 286 |
|
| 287 |
def f(): |
| 288 |
return args |
| 289 |
|
| 290 |
|
| 291 |
if not self.runappdirect: |
| 292 |
|
| 293 |
f.__code__ = module.code_newempty("abc", "def", 23) |
| 294 |
with raises(AssertionError) as info: |
| 295 |
f() |
| 296 |
|
| 297 |
lines = list(f.__code__.co_lines()) |
| 298 |
assert lines == [(0, 4, 23)] |
| 299 |
assert info.tb.tb_next.tb_lineno == 23 |
| 300 |
|
| 301 |
|
| 302 |
def test_get_xxx(self): |
| 303 |
module = self.import_extension('foo', [ |
| 304 |
("code_numfree", "METH_O", |
| 305 |
""" |
| 306 |
PyCodeObject *code = (PyCodeObject*)PyFunction_GetCode(args); |
| 307 |
if (code == NULL) { |
| 308 |
return NULL; |
| 309 |
} |
| 310 |
size_t n = PyCode_GetNumFree(code); |
| 311 |
return PyLong_FromSize_t(n); |
| 312 |
"""), |
| 313 |
("code_cellvars", "METH_O", |
| 314 |
""" |
| 315 |
PyCodeObject *code = (PyCodeObject*)PyFunction_GetCode(args); |
| 316 |
if (code == NULL) { |
| 317 |
return NULL; |
| 318 |
} |
| 319 |
return PyCode_GetCellvars(code); |
| 320 |
"""), |
| 321 |
("code_code", "METH_O", |
| 322 |
""" |
| 323 |
PyCodeObject *code = (PyCodeObject*)PyFunction_GetCode(args); |
| 324 |
if (code == NULL) { |
| 325 |
return NULL; |
| 326 |
} |
| 327 |
return PyCode_GetCode(code); |
| 328 |
"""), |
| 329 |
("code_freevars", "METH_O", |
| 330 |
""" |
| 331 |
PyCodeObject *code = (PyCodeObject*)PyFunction_GetCode(args); |
| 332 |
if (code == NULL) { |
| 333 |
return NULL; |
| 334 |
} |
| 335 |
return PyCode_GetFreevars(code); |
| 336 |
"""), |
| 337 |
("code_varnames", "METH_O", |
| 338 |
""" |
| 339 |
PyCodeObject *code = (PyCodeObject*)PyFunction_GetCode(args); |
| 340 |
if (code == NULL) { |
| 341 |
return NULL; |
| 342 |
} |
| 343 |
return PyCode_GetVarnames(code); |
| 344 |
"""), |
| 345 |
("func_globals", "METH_O", |
| 346 |
""" |
| 347 |
return PyFunction_GetGlobals(args); |
| 348 |
"""), |
| 349 |
("func_name", "METH_O", |
| 350 |
""" |
| 351 |
return PyUnicode_FromString(PyEval_GetFuncName(args)); |
| 352 |
"""), |
| 353 |
]) |
| 354 |
|
| 355 |
g = 42 |
| 356 |
def wrapper(x): |
| 357 |
a = 5 |
| 358 |
global g |
| 359 |
g = 12 |
| 360 |
def func(x): |
| 361 |
return a, x |
| 362 |
code = wrapper.__code__ |
| 363 |
|
| 364 |
class A: |
| 365 |
def method_example(self): pass |
| 366 |
|
| 367 |
|
| 368 |
assert module.code_cellvars(wrapper) == ('a', ) |
| 369 |
assert module.code_numfree(wrapper) == 0 |
| 370 |
assert module.code_code(wrapper) == code.co_code |
| 371 |
assert module.code_freevars(wrapper) == code.co_freevars |
| 372 |
assert module.code_varnames(wrapper) == code.co_varnames |
| 373 |
assert module.func_globals(wrapper) == wrapper.__globals__ |
| 374 |
assert module.func_name(sum) == "sum" |
| 375 |
assert module.func_name(wrapper) == "wrapper" |
| 376 |
assert module.func_name(A.method_example) == "method_example" |
| 377 |
assert module.func_name(A().method_example) == "method_example" |
| 378 |
assert module.func_name(A) == "type" |