Debian Codegraph @

pypy/module/_cppyy/test/test_fragile.py

raw symbols SCIP
1 import py, os, sys
2 from .support import setup_make, soext
3
4 currpath = py.path.local(__file__).dirpath()
5 test_dct = str(currpath.join("fragileDict"))+soext
6
7 def setup_module(mod):
8 setup_make("fragile")
9
10
11 class AppTestFRAGILE:
12 spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])
13
14 def setup_class(cls):
15 cls.w_test_dct = cls.space.newtext(test_dct)
16 cls.w_fragile = cls.space.appexec([], """():
17 import ctypes, _cppyy
18 _cppyy._post_import_startup()
19 return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, ))
20
21 def test01_missing_classes(self):
22 """Test (non-)access to missing classes"""
23
24 import _cppyy as cppyy
25
26 raises(AttributeError, getattr, cppyy.gbl, "no_such_class")
27
28 assert cppyy.gbl.fragile == cppyy.gbl.fragile
29 fragile = cppyy.gbl.fragile
30
31 raises(AttributeError, getattr, fragile, "no_such_class")
32
33 assert fragile.C == fragile.C
34 assert fragile.C().check() == ord('C')
35
36 assert fragile.B == fragile.B
37 assert fragile.B().check() == ord('B')
38 assert not fragile.B().gime_no_such()
39
40 assert fragile.C == fragile.C
41 assert fragile.C().check() == ord('C')
42 raises(TypeError, fragile.C().use_no_such, None)
43
44 def test02_arguments(self):
45 """Test reporting when providing wrong arguments"""
46
47 import _cppyy as cppyy
48
49 assert cppyy.gbl.fragile == cppyy.gbl.fragile
50 fragile = cppyy.gbl.fragile
51
52 assert fragile.D == fragile.D
53 assert fragile.D().check() == ord('D')
54
55 d = fragile.D()
56 raises(TypeError, d.overload, None)
57 raises(TypeError, d.overload, None, None, None)
58
59 d.overload('a')
60 d.overload(1)
61
62 def test03_unsupported_arguments(self):
63 """Test arguments that are yet unsupported"""
64
65 import _cppyy as cppyy
66
67 assert cppyy.gbl.fragile == cppyy.gbl.fragile
68 fragile = cppyy.gbl.fragile
69
70 assert fragile.E == fragile.E
71 assert fragile.E().check() == ord('E')
72
73 e = fragile.E()
74 raises(TypeError, e.overload, None)
75 # allowing access to e.m_pp_no_such is debatable, but it provides a raw pointer
76 # which may be useful ...
77 assert e.m_pp_no_such[0] == 0xdead
78
79 def test04_wrong_arg_addressof(self):
80 """Test addressof() error reporting"""
81
82 import _cppyy as cppyy
83
84 assert cppyy.gbl.fragile == cppyy.gbl.fragile
85 fragile = cppyy.gbl.fragile
86
87 assert fragile.F == fragile.F
88 assert fragile.F().check() == ord('F')
89
90 f = fragile.F()
91 o = object()
92
93 cppyy.addressof(f)
94 raises(TypeError, cppyy.addressof, o)
95 raises(TypeError, cppyy.addressof, 1)
96 # see also test08_void_pointer_passing in test_advancedcpp.py
97
98 def test05_wrong_this(self):
99 """Test that using an incorrect self argument raises"""
100
101 import _cppyy as cppyy
102
103 assert cppyy.gbl.fragile == cppyy.gbl.fragile
104 fragile = cppyy.gbl.fragile
105
106 a = fragile.A()
107 assert fragile.A.check(a) == ord('A')
108
109 b = fragile.B()
110 assert fragile.B.check(b) == ord('B')
111 raises(TypeError, fragile.A.check, b)
112 raises(TypeError, fragile.B.check, a)
113
114 assert not a.gime_null()
115
116 assert isinstance(a.gime_null(), fragile.A)
117 raises(ReferenceError, fragile.A.check, a.gime_null())
118
119 def test06_unnamed_enum(self):
120 """Test that an unnamed enum does not cause infinite recursion"""
121
122 import _cppyy as cppyy
123
124 assert cppyy.gbl.fragile is cppyy.gbl.fragile
125 fragile = cppyy.gbl.fragile
126 assert cppyy.gbl.fragile is fragile
127
128 g = fragile.G()
129
130 def test07_unhandled_scoped_datamember(self):
131 """Test that an unhandled scoped data member does not cause infinite recursion"""
132
133 import _cppyy as cppyy
134
135 assert cppyy.gbl.fragile is cppyy.gbl.fragile
136 fragile = cppyy.gbl.fragile
137 assert cppyy.gbl.fragile is fragile
138
139 h = fragile.H()
140
141 def test08_operator_bool(self):
142 """Access to global vars with an operator bool() returning False"""
143
144 import _cppyy as cppyy
145
146 i = cppyy.gbl.fragile.I()
147 assert not i
148
149 g = cppyy.gbl.fragile.gI
150 assert not g
151
152 def test09_documentation(self):
153 """Check contents of documentation"""
154
155 import _cppyy as cppyy
156
157 assert cppyy.gbl.fragile == cppyy.gbl.fragile
158 fragile = cppyy.gbl.fragile
159
160 d = fragile.D()
161 try:
162 d.check(None) # raises TypeError
163 assert 0
164 except TypeError as e:
165 assert "fragile::D::check()" in str(e)
166 assert "TypeError: takes at most 0 arguments (1 given)" in str(e)
167 assert "TypeError: takes at least 2 arguments (1 given)" in str(e)
168
169 try:
170 d.overload(None) # raises TypeError
171 assert 0
172 except TypeError as e:
173 # TODO: pypy-c does not indicate which argument failed to convert, CPython does
174 # likewise there are still minor differences in descriptiveness of messages
175 assert "fragile::D::overload()" in str(e)
176 assert "TypeError: takes at most 0 arguments (1 given)" in str(e)
177 assert "fragile::D::overload(fragile::no_such_class*)" in str(e)
178 #assert "no converter available for 'fragile::no_such_class*'" in str(e)
179 assert "void fragile::D::overload(char, int i = 0)" in str(e)
180 #assert "char or small int type expected" in str(e)
181 assert "void fragile::D::overload(int, fragile::no_such_class* p = 0)" in str(e)
182 #assert "int/long conversion expects an integer object" in str(e)
183
184 j = fragile.J()
185 assert fragile.J.method1.__doc__ == j.method1.__doc__
186 assert j.method1.__doc__ == "int fragile::J::method1(int, double)"
187
188 f = fragile.fglobal
189 assert f.__doc__ == "void fragile::fglobal(int, double, char)"
190
191 try:
192 o = fragile.O() # raises TypeError
193 assert 0
194 except TypeError as e:
195 assert "cannot instantiate abstract class 'fragile::O'" in str(e)
196
197 def test10_dir(self):
198 """Test __dir__ method"""
199
200 import _cppyy as cppyy
201
202 members = dir(cppyy.gbl.fragile)
203 assert 'A' in members
204 assert 'B' in members
205 assert 'C' in members
206 assert 'D' in members # classes
207
208 assert 'nested1' in members # namespace
209
210 # TODO: think this through ... probably want this, but interferes with
211 # the (new) policy of lazy lookups
212 #assert 'fglobal' in members # function
213 #assert 'gI'in members # variable
214
215 def test11_imports(self):
216 """Test ability to import from namespace (or fail with ImportError)"""
217
218 import _cppyy as cppyy
219
220 # TODO: namespaces aren't loaded (and thus not added to sys.modules)
221 # with just the from ... import statement; actual use is needed
222
223 # TODO: this is really front-end testing, but the code is still in
224 # _cppyy, so we test it here until pythonify.py can be moved
225 import sys
226 sys.modules['cppyy'] = sys.modules['_cppyy']
227 from cppyy.gbl import fragile
228
229 def fail_import():
230 from cppyy.gbl import does_not_exist
231 raises(ImportError, fail_import)
232
233 from cppyy.gbl.fragile import A, B, C, D
234 assert cppyy.gbl.fragile.A is A
235 assert cppyy.gbl.fragile.B is B
236 assert cppyy.gbl.fragile.C is C
237 assert cppyy.gbl.fragile.D is D
238
239 # according to warnings, can't test "import *" ...
240
241 from cppyy.gbl.fragile import nested1
242 assert cppyy.gbl.fragile.nested1 is nested1
243 assert nested1.__name__ == 'nested1'
244 assert nested1.__module__ == 'cppyy.gbl.fragile'
245 assert nested1.__cpp_name__ == 'fragile::nested1'
246
247 from cppyy.gbl.fragile.nested1 import A, nested2
248 assert cppyy.gbl.fragile.nested1.A is A
249 assert A.__name__ == 'A'
250 assert A.__module__ == 'cppyy.gbl.fragile.nested1'
251 assert A.__cpp_name__ == 'fragile::nested1::A'
252 assert cppyy.gbl.fragile.nested1.nested2 is nested2
253 assert A.__name__ == 'A'
254 assert A.__module__ == 'cppyy.gbl.fragile.nested1'
255 assert nested2.__cpp_name__ == 'fragile::nested1::nested2'
256
257 from cppyy.gbl.fragile.nested1.nested2 import A, nested3
258 assert cppyy.gbl.fragile.nested1.nested2.A is A
259 assert A.__name__ == 'A'
260 assert A.__module__ == 'cppyy.gbl.fragile.nested1.nested2'
261 assert A.__cpp_name__ == 'fragile::nested1::nested2::A'
262 assert cppyy.gbl.fragile.nested1.nested2.nested3 is nested3
263 assert A.__name__ == 'A'
264 assert A.__module__ == 'cppyy.gbl.fragile.nested1.nested2'
265 assert nested3.__cpp_name__ == 'fragile::nested1::nested2::nested3'
266
267 from cppyy.gbl.fragile.nested1.nested2.nested3 import A
268 assert cppyy.gbl.fragile.nested1.nested2.nested3.A is nested3.A
269 assert A.__name__ == 'A'
270 assert A.__module__ == 'cppyy.gbl.fragile.nested1.nested2.nested3'
271 assert A.__cpp_name__ == 'fragile::nested1::nested2::nested3::A'
272
273 def test12_missing_casts(self):
274 """Test proper handling when a hierarchy is not fully available"""
275
276 import _cppyy as cppyy
277
278 k = cppyy.gbl.fragile.K()
279
280 assert k is k.GimeK(False)
281 assert k is not k.GimeK(True)
282
283 kd = k.GimeK(True)
284 assert kd is k.GimeK(True)
285 assert kd is not k.GimeK(False)
286
287 l = k.GimeL()
288 assert l is k.GimeL()
289
290 def test13_double_enum_trouble(self):
291 """Test a redefinition of enum in a derived class"""
292
293 return # don't bother; is fixed in cling-support
294
295 import _cppyy as cppyy
296
297 M = cppyy.gbl.fragile.M
298 N = cppyy.gbl.fragile.N
299
300 assert M.kOnce == N.kOnce
301 assert M.kTwice == N.kTwice
302 assert M.__dict__['kTwice'] is not N.__dict__['kTwice']