| 1 |
import py, os, sys |
| 2 |
from pytest import raises |
| 3 |
from .support import setup_make, soext |
| 4 |
|
| 5 |
currpath = py.path.local(__file__).dirpath() |
| 6 |
test_dct = str(currpath.join("pythonizablesDict"))+soext |
| 7 |
|
| 8 |
def setup_module(mod): |
| 9 |
setup_make("pythonizables") |
| 10 |
|
| 11 |
|
| 12 |
class AppTestPYTHONIZATION: |
| 13 |
spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools']) |
| 14 |
|
| 15 |
def setup_class(cls): |
| 16 |
cls.w_test_dct = cls.space.newtext(test_dct) |
| 17 |
cls.w_datatypes = cls.space.appexec([], """(): |
| 18 |
import ctypes, _cppyy |
| 19 |
_cppyy._post_import_startup() |
| 20 |
class py(object): |
| 21 |
pass |
| 22 |
py.add_pythonization = _cppyy.add_pythonization |
| 23 |
py.remove_pythonization = _cppyy.remove_pythonization |
| 24 |
py.pin_type = _cppyy._pin_type |
| 25 |
_cppyy.py = py |
| 26 |
return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, )) |
| 27 |
|
| 28 |
def test00_api(self): |
| 29 |
"""Test basic semantics of the pythonization API""" |
| 30 |
|
| 31 |
import _cppyy as cppyy |
| 32 |
|
| 33 |
raises(TypeError, cppyy.py.add_pythonization, 1) |
| 34 |
|
| 35 |
def pythonizor1(klass, name): |
| 36 |
pass |
| 37 |
|
| 38 |
def pythonizor2(klass, name): |
| 39 |
pass |
| 40 |
|
| 41 |
pythonizor3 = pythonizor1 |
| 42 |
|
| 43 |
cppyy.py.add_pythonization(pythonizor1) |
| 44 |
assert cppyy.py.remove_pythonization(pythonizor2) == False |
| 45 |
assert cppyy.py.remove_pythonization(pythonizor3) == True |
| 46 |
|
| 47 |
def pythonizor(klass, name): |
| 48 |
if name == 'pyzables::SomeDummy1': |
| 49 |
klass.test = 1 |
| 50 |
|
| 51 |
cppyy.py.add_pythonization(pythonizor) |
| 52 |
assert cppyy.gbl.pyzables.SomeDummy1.test == 1 |
| 53 |
|
| 54 |
def pythonizor(klass, name): |
| 55 |
if name == 'SomeDummy2': |
| 56 |
klass.test = 2 |
| 57 |
cppyy.py.add_pythonization(pythonizor, 'pyzables') |
| 58 |
|
| 59 |
def pythonizor(klass, name): |
| 60 |
if name == 'pyzables::SomeDummy2': |
| 61 |
klass.test = 3 |
| 62 |
cppyy.py.add_pythonization(pythonizor) |
| 63 |
|
| 64 |
assert cppyy.gbl.pyzables.SomeDummy2.test == 2 |
| 65 |
|
| 66 |
def test01_type_pinning(self): |
| 67 |
"""Verify pinnability of returns""" |
| 68 |
|
| 69 |
import _cppyy as cppyy |
| 70 |
|
| 71 |
cppyy.gbl.pyzables.GimeDerived.__creates__ = True |
| 72 |
|
| 73 |
result = cppyy.gbl.pyzables.GimeDerived() |
| 74 |
assert type(result) == cppyy.gbl.pyzables.MyDerived |
| 75 |
|
| 76 |
cppyy.py.pin_type(cppyy.gbl.pyzables.MyBase) |
| 77 |
assert type(result) == cppyy.gbl.pyzables.MyDerived |
| 78 |
|
| 79 |
def test02_transparency(self): |
| 80 |
"""Transparent use of smart pointers""" |
| 81 |
|
| 82 |
import _cppyy as cppyy |
| 83 |
|
| 84 |
Countable = cppyy.gbl.pyzables.Countable |
| 85 |
mine = cppyy.gbl.pyzables.mine |
| 86 |
|
| 87 |
assert type(mine) == Countable |
| 88 |
assert mine.m_check == 0xcdcdcdcd |
| 89 |
assert type(mine.__smartptr__()) == cppyy.gbl.std.shared_ptr(Countable) |
| 90 |
assert mine.__smartptr__().get().m_check == 0xcdcdcdcd |
| 91 |
assert mine.say_hi() == "Hi!" |
| 92 |
|
| 93 |
def test03_converters(self): |
| 94 |
"""Smart pointer argument passing""" |
| 95 |
|
| 96 |
import _cppyy as cppyy |
| 97 |
|
| 98 |
pz = cppyy.gbl.pyzables |
| 99 |
mine = pz.mine |
| 100 |
|
| 101 |
assert 0xcdcdcdcd == pz.pass_mine_rp_ptr(mine) |
| 102 |
assert 0xcdcdcdcd == pz.pass_mine_rp_ref(mine) |
| 103 |
assert 0xcdcdcdcd == pz.pass_mine_rp(mine) |
| 104 |
|
| 105 |
assert 0xcdcdcdcd == pz.pass_mine_sp_ptr(mine) |
| 106 |
assert 0xcdcdcdcd == pz.pass_mine_sp_ref(mine) |
| 107 |
|
| 108 |
assert 0xcdcdcdcd == pz.pass_mine_sp_ptr(mine.__smartptr__()) |
| 109 |
assert 0xcdcdcdcd == pz.pass_mine_sp_ref(mine.__smartptr__()) |
| 110 |
|
| 111 |
assert 0xcdcdcdcd == pz.pass_mine_sp(mine) |
| 112 |
assert 0xcdcdcdcd == pz.pass_mine_sp(mine.__smartptr__()) |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
pz.renew_mine() |
| 117 |
|
| 118 |
def test04_executors(self): |
| 119 |
"""Smart pointer return types""" |
| 120 |
|
| 121 |
import _cppyy as cppyy |
| 122 |
|
| 123 |
pz = cppyy.gbl.pyzables |
| 124 |
Countable = pz.Countable |
| 125 |
|
| 126 |
mine = pz.gime_mine_ptr() |
| 127 |
assert type(mine) == Countable |
| 128 |
assert mine.m_check == 0xcdcdcdcd |
| 129 |
assert type(mine.__smartptr__()) == cppyy.gbl.std.shared_ptr(Countable) |
| 130 |
assert mine.__smartptr__().get().m_check == 0xcdcdcdcd |
| 131 |
assert mine.say_hi() == "Hi!" |
| 132 |
|
| 133 |
mine = pz.gime_mine_ref() |
| 134 |
assert type(mine) == Countable |
| 135 |
assert mine.m_check == 0xcdcdcdcd |
| 136 |
assert type(mine.__smartptr__()) == cppyy.gbl.std.shared_ptr(Countable) |
| 137 |
assert mine.__smartptr__().get().m_check == 0xcdcdcdcd |
| 138 |
assert mine.say_hi() == "Hi!" |
| 139 |
|
| 140 |
mine = pz.gime_mine() |
| 141 |
assert type(mine) == Countable |
| 142 |
assert mine.m_check == 0xcdcdcdcd |
| 143 |
assert type(mine.__smartptr__()) == cppyy.gbl.std.shared_ptr(Countable) |
| 144 |
assert mine.__smartptr__().get().m_check == 0xcdcdcdcd |
| 145 |
assert mine.say_hi() == "Hi!" |
| 146 |
|
| 147 |
def test05_creates_flag(self): |
| 148 |
"""Effect of creates flag on return type""" |
| 149 |
|
| 150 |
import _cppyy as cppyy |
| 151 |
import gc |
| 152 |
|
| 153 |
pz = cppyy.gbl.pyzables |
| 154 |
Countable = pz.Countable |
| 155 |
|
| 156 |
gc.collect() |
| 157 |
oldcount = Countable.sInstances |
| 158 |
|
| 159 |
pz.gime_naked_countable.__creates__ = True |
| 160 |
for i in range(10): |
| 161 |
cnt = pz.gime_naked_countable() |
| 162 |
gc.collect() |
| 163 |
assert Countable.sInstances == oldcount + 1 |
| 164 |
del cnt |
| 165 |
gc.collect() |
| 166 |
|
| 167 |
assert Countable.sInstances == oldcount |
| 168 |
|
| 169 |
def test06_api_regression_test(self): |
| 170 |
"""Used to fail b/c klass touched in cppyy""" |
| 171 |
|
| 172 |
import _cppyy as cppyy |
| 173 |
|
| 174 |
def root_pythonizor(klass, name): |
| 175 |
if name == 'CppyyLegacy::TString': |
| 176 |
klass.__len__ = klass.Length |
| 177 |
|
| 178 |
cppyy.py.add_pythonization(root_pythonizor) |
| 179 |
|
| 180 |
assert len(cppyy.gbl.CppyyLegacy.TString("aap")) == 3 |