Debian Codegraph @

pypy/module/cpyext/test/test_api.py

raw symbols SCIP
1 import py, pytest
2 import contextlib
3 from rpython.rtyper.lltypesystem import lltype
4 from rpython.translator.c.database import LowLevelDatabase
5 from rpython.tool.cparser import parse_source
6 from pypy.interpreter.baseobjspace import W_Root
7 from pypy.module.cpyext.state import State
8 from pypy.module.cpyext.api import (
9 slot_function, cpython_api, copy_header_files, INTERPLEVEL_API,
10 Py_ssize_t, Py_ssize_tP, PyObject, cts, ApiFunction)
11 from pypy.module.cpyext.test.test_cpyext import (
12 freeze_refcnts, LeakCheckingTest)
13 from pypy.interpreter.error import OperationError
14 from rpython.rlib import rawrefcount
15 import os
16
17 @contextlib.contextmanager
18 def raises_w(space, expected_exc):
19 with pytest.raises(OperationError) as excinfo:
20 yield
21 operror = excinfo.value
22 assert operror.w_type is getattr(space, 'w_' + expected_exc.__name__)
23
24 class BaseApiTest(LeakCheckingTest):
25 def setup_class(cls):
26 space = cls.space
27 cls.preload_builtins(space)
28 cls.w_runappdirect = space.wrap(cls.runappdirect)
29
30 class CAPI:
31 def __repr__(self):
32 return '<%s.%s instance>' % (self.__class__.__module__,
33 self.__class__.__name__)
34
35 def __getattr__(self, name):
36 return getattr(cls.space, name)
37 cls.api = CAPI()
38 CAPI.__dict__.update(INTERPLEVEL_API)
39
40 def raises(self, space, api, expected_exc, f, *args):
41 if not callable(f):
42 raise Exception("%s is not callable" % (f,))
43 f(*args)
44 state = space.fromcache(State)
45 operror = state.get_exception()
46 if not operror:
47 raise Exception("DID NOT RAISE")
48 if getattr(space, 'w_' + expected_exc.__name__) is not operror.w_type:
49 raise Exception("Wrong exception")
50 return state.clear_exception()
51
52 def setup_method(self, func):
53 freeze_refcnts(self)
54
55 def teardown_method(self, func):
56 state = self.space.fromcache(State)
57 try:
58 state.check_and_raise_exception()
59 except OperationError as e:
60 print e.errorstr(self.space)
61 raise
62 self.cleanup()
63
64 @slot_function([PyObject], lltype.Void)
65 def PyPy_GetWrapped(space, w_arg):
66 assert isinstance(w_arg, W_Root)
67
68 @slot_function([PyObject], lltype.Void)
69 def PyPy_GetReference(space, arg):
70 assert lltype.typeOf(arg) == PyObject
71
72 @cpython_api([Py_ssize_t], Py_ssize_t, error=-1)
73 def PyPy_TypedefTest1(space, arg):
74 assert lltype.typeOf(arg) == Py_ssize_t
75 return 0
76
77 @cpython_api([Py_ssize_tP], Py_ssize_tP)
78 def PyPy_TypedefTest2(space, arg):
79 assert lltype.typeOf(arg) == Py_ssize_tP
80 return None
81
82 class TestConversion(BaseApiTest):
83 def test_conversions(self, space):
84 PyPy_GetWrapped(space, space.w_None)
85 PyPy_GetReference(space, space.w_None)
86
87 def test_typedef(self, space):
88 from rpython.translator.c.database import LowLevelDatabase
89 db = LowLevelDatabase()
90 assert PyPy_TypedefTest1.api_func.get_c_restype(db) == 'Signed'
91 assert PyPy_TypedefTest1.api_func.get_c_args(db) == 'Signed arg0'
92 assert PyPy_TypedefTest2.api_func.get_c_restype(db) == 'Signed *'
93 assert PyPy_TypedefTest2.api_func.get_c_args(db) == 'Signed *arg0'
94
95 PyPy_TypedefTest1(space, 0)
96 ppos = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw')
97 ppos[0] = 0
98 PyPy_TypedefTest2(space, ppos)
99 lltype.free(ppos, flavor='raw')
100
101 @pytest.mark.skipif(os.environ.get('USER')=='root',
102 reason='root can write to all files')
103 def test_copy_header_files(tmpdir):
104 copy_header_files(cts, tmpdir, True)
105 def check(name):
106 f = tmpdir.join(name)
107 assert f.check(file=True)
108 py.test.raises(py.error.EACCES, "f.open('w')") # check that it's not writable
109 check('Python.h')
110 check('modsupport.h')
111 check('pypy_decl.h')
112
113 def test_write_func():
114 db = LowLevelDatabase()
115 cdef = """
116 typedef ssize_t Py_ssize_t;
117 """
118 cts = parse_source(cdef)
119 cdecl = "Py_ssize_t * some_func(Py_ssize_t*)"
120 decl = cts.parse_func(cdecl)
121 api_function = ApiFunction(
122 decl.get_llargs(cts), decl.get_llresult(cts), lambda space, x: None,
123 cdecl=decl)
124 assert (api_function.get_api_decl('some_func', db)
125 == "PyAPI_FUNC(Py_ssize_t *) some_func(Py_ssize_t * arg0);")
126