Skip to content

Commit

Permalink
let nullptr live on cppyy, rather than cppyy.gbl; add consistency che…
Browse files Browse the repository at this point in the history
…cks CPython <-> PyPy
  • Loading branch information
wlav committed Oct 12, 2017
1 parent 85d67a8 commit 6d02305
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
2 changes: 2 additions & 0 deletions python/cppyy/_pypy_cppyy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'gbl',
'addressof',
'bind_object',
'nullptr',
]

# first load the dependency libraries of the backend, then
Expand Down Expand Up @@ -44,6 +45,7 @@ def py59_compat(c):
for name in __all__:
setattr(_thismodule, name, getattr(_backend, name))
del name, sys
nullptr = _backend.nullptr

def load_reflection_info(name):
sc = _backend.gbl.gSystem.Load(name)
Expand Down
22 changes: 17 additions & 5 deletions test/test_cpp11features.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
from pytest import raises
from .support import setup_make

try:
import __pypy__
is_pypy = True
except ImportError:
is_pypy = False


currpath = py.path.local(__file__).dirpath()
test_dct = str(currpath.join("cpp11featuresDict.so"))

def setup_module(mod):
setup_make("cpp11featuresDict.so")


class TestCPP11FEATURES:
def setup_class(cls):
cls.test_dct = test_dct
import cppyy
cls.datatypes = cppyy.load_reflection_info(cls.test_dct)
cls.N = cppyy.gbl.N

def test01_shared_ptr(self):
"""Usage and access of std::shared_ptr<>"""
Expand Down Expand Up @@ -49,8 +54,9 @@ def test02_nullptr(self):

# test existence
nullptr = cppyy.nullptr
# assert not hasattr(cppyy.gbl, 'nullptr')

# TODO: test usage ...
# usage is tested in datatypes.py:test15_nullptr_passing

def test03_move(self):
"""Move construction, assignment, and methods"""
Expand All @@ -67,7 +73,10 @@ def moveit(T):
i2 = T(i1) # cctor
assert T.s_move_counter == 0

i3 = T(T()) # should call move, not memoized cctor
if is_pypy:
i3 = T(std.move(T())) # can't check ref-count
else:
i3 = T(T()) # should call move, not memoized cctor
assert T.s_move_counter == 1

i4 = T(std.move(i1))
Expand All @@ -77,7 +86,10 @@ def moveit(T):
i4.__assign__(i2)
assert T.s_move_counter == 2

i4.__assign__(T())
if is_pypy:
i4.__assign__(std.move(T())) # can't check ref-count
else:
i4.__assign__(T())
assert T.s_move_counter == 3

i4.__assign__(std.move(i2))
Expand Down
28 changes: 14 additions & 14 deletions test/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ def test03_array_passing(self):
raises(Exception, c.pass_array(0).__getitem__, 0) # raises SegfaultException
assert not c.pass_array(None)
raises(Exception, c.pass_array(None).__getitem__, 0) # id.
assert not c.pass_array(cppyy.gbl.nullptr)
raises(Exception, c.pass_array(cppyy.gbl.nullptr).__getitem__, 0) # id. id.
assert not c.pass_array(cppyy.nullptr)
raises(Exception, c.pass_array(cppyy.nullptr).__getitem__, 0) # id. id.

c.__destruct__()

Expand Down Expand Up @@ -692,22 +692,22 @@ def test20_voidp(self):

c = CppyyTestData()

assert not cppyy.gbl.nullptr
assert not cppyy.nullptr

assert c.s_voidp is cppyy.gbl.nullptr
assert CppyyTestData.s_voidp is cppyy.gbl.nullptr
assert c.s_voidp is cppyy.nullptr
assert CppyyTestData.s_voidp is cppyy.nullptr

assert c.m_voidp is cppyy.gbl.nullptr
assert c.get_voidp() is cppyy.gbl.nullptr
assert c.m_voidp is cppyy.nullptr
assert c.get_voidp() is cppyy.nullptr

c2 = CppyyTestData()
assert c2.m_voidp is cppyy.gbl.nullptr
assert c2.m_voidp is cppyy.nullptr
c.set_voidp(c2.m_voidp)
assert c.m_voidp is cppyy.gbl.nullptr
assert c.m_voidp is cppyy.nullptr
c.set_voidp(c2.get_voidp())
assert c.m_voidp is cppyy.gbl.nullptr
c.set_voidp(cppyy.gbl.nullptr)
assert c.m_voidp is cppyy.gbl.nullptr
assert c.m_voidp is cppyy.nullptr
c.set_voidp(cppyy.nullptr)
assert c.m_voidp is cppyy.nullptr

c.set_voidp(c2)
def address_equality_test(a, b):
Expand All @@ -722,8 +722,8 @@ def address_equality_test(a, b):

def null_test(null):
c.m_voidp = null
assert c.m_voidp is cppyy.gbl.nullptr
map(null_test, [0, None, cppyy.gbl.nullptr])
assert c.m_voidp is cppyy.nullptr
map(null_test, [0, None, cppyy.nullptr])

c.m_voidp = c2
address_equality_test(c.m_voidp, c2)
Expand Down
21 changes: 12 additions & 9 deletions test/test_fragile.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test05_wrong_arg_addressof(self):
# 0, None, and nullptr allowed
assert cppyy.addressof(0) == 0
assert cppyy.addressof(None) == 0
assert cppyy.addressof(cppyy.gbl.nullptr) == 0
assert cppyy.addressof(cppyy.nullptr) == 0

def test06_wrong_this(self):
"""Test that using an incorrect self argument raises"""
Expand Down Expand Up @@ -176,20 +176,23 @@ def test10_documentation(self):
assert 0
except TypeError as e:
assert "fragile::D::check()" in str(e)
assert "TypeError: wrong number of arguments" in str(e)
assert "TypeError: takes at most 0 arguments (1 given)" in str(e)
assert "TypeError: takes at least 2 arguments (1 given)" in str(e)

d.overload(None) # succeeds: accepted as nullptr on fragile::no_such_class*

try:
d.overload(None) # raises TypeError
d.overload(3.14) # raises TypeError
assert 0
except TypeError as e:
assert "fragile::D::overload()" in str(e)
assert "TypeError: wrong number of arguments" in str(e)
assert "TypeError: takes at most 0 arguments (1 given)" in str(e)
assert "fragile::D::overload(fragile::no_such_class*)" in str(e)
assert "TypeError: no converter available for 'fragile::no_such_class*'" in str(e)
assert "fragile::D::overload(char, int)" in str(e)
assert "TypeError: expected string, got NoneType object" in str(e)
assert "fragile::D::overload(int, fragile::no_such_class*)" in str(e)
assert "TypeError: expected integer, got NoneType object" in str(e)
assert "TypeError: could not convert argument 1 (no converter available for 'fragile::no_such_class*')" in str(e)
assert "void fragile::D::overload(char, int i = 0)" in str(e)
assert "TypeError: could not convert argument 1 (char or small int type expected)" in str(e)
assert "void fragile::D::overload(int, fragile::no_such_class* p = 0)" in str(e)
assert "TypeError: could not convert argument 1 (int/long conversion expects an integer object)" in str(e)

j = fragile.J()
assert fragile.J.method1.__doc__ == j.method1.__doc__
Expand Down

0 comments on commit 6d02305

Please sign in to comment.