本文整理汇总了Python中pybind11_tests.ConstructorStats.get方法的典型用法代码示例。如果您正苦于以下问题:Python ConstructorStats.get方法的具体用法?Python ConstructorStats.get怎么用?Python ConstructorStats.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pybind11_tests.ConstructorStats
的用法示例。
在下文中一共展示了ConstructorStats.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_pointers
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_pointers(msg):
from pybind11_tests import (return_void_ptr, get_void_ptr_value, ExampleMandA,
print_opaque_list, return_null_str, get_null_str_value,
return_unique_ptr, ConstructorStats)
living_before = ConstructorStats.get(ExampleMandA).alive()
assert get_void_ptr_value(return_void_ptr()) == 0x1234
assert get_void_ptr_value(ExampleMandA()) # Should also work for other C++ types
assert ConstructorStats.get(ExampleMandA).alive() == living_before
with pytest.raises(TypeError) as excinfo:
get_void_ptr_value([1, 2, 3]) # This should not work
assert msg(excinfo.value) == """
get_void_ptr_value(): incompatible function arguments. The following argument types are supported:
1. (arg0: capsule) -> int
Invoked with: [1, 2, 3]
""" # noqa: E501 line too long
assert return_null_str() is None
assert get_null_str_value(return_null_str()) is not None
ptr = return_unique_ptr()
assert "StringList" in repr(ptr)
assert print_opaque_list(ptr) == "Opaque list: [some value]"
示例2: test_reference_internal
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_reference_internal():
from pybind11_tests import ConstructorStats
from pybind11_tests.submodule import A, B
b = B()
assert str(b.get_a1()) == "A[1]"
assert str(b.a1) == "A[1]"
assert str(b.get_a2()) == "A[2]"
assert str(b.a2) == "A[2]"
b.a1 = A(42)
b.a2 = A(43)
assert str(b.get_a1()) == "A[42]"
assert str(b.a1) == "A[42]"
assert str(b.get_a2()) == "A[43]"
assert str(b.a2) == "A[43]"
astats, bstats = ConstructorStats.get(A), ConstructorStats.get(B)
assert astats.alive() == 2
assert bstats.alive() == 1
del b
assert astats.alive() == 0
assert bstats.alive() == 0
assert astats.values() == ['1', '2', '42', '43']
assert bstats.values() == []
assert astats.default_constructions == 0
assert bstats.default_constructions == 1
assert astats.copy_constructions == 0
assert bstats.copy_constructions == 0
# assert astats.move_constructions >= 0 # Don't invoke any
# assert bstats.move_constructions >= 0 # Don't invoke any
assert astats.copy_assignments == 2
assert bstats.copy_assignments == 0
assert astats.move_assignments == 0
assert bstats.move_assignments == 0
示例3: test_unique_nodelete
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_unique_nodelete():
from pybind11_tests import MyObject4
o = MyObject4(23)
assert o.value == 23
cstats = ConstructorStats.get(MyObject4)
assert cstats.alive() == 1
del o
cstats = ConstructorStats.get(MyObject4)
assert cstats.alive() == 1 # Leak, but that's intentional
示例4: test_to_python
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_to_python():
mat = m.Matrix(5, 5)
assert memoryview(mat).shape == (5, 5)
assert mat[2, 3] == 0
mat[2, 3] = 4
assert mat[2, 3] == 4
mat2 = np.array(mat, copy=False)
assert mat2.shape == (5, 5)
assert abs(mat2).sum() == 4
assert mat2[2, 3] == 4
mat2[2, 3] = 5
assert mat2[2, 3] == 5
cstats = ConstructorStats.get(m.Matrix)
assert cstats.alive() == 1
del mat
pytest.gc_collect()
assert cstats.alive() == 1
del mat2 # holds a mat reference
pytest.gc_collect()
assert cstats.alive() == 0
assert cstats.values() == ["5x5 matrix"]
assert cstats.copy_constructions == 0
# assert cstats.move_constructions >= 0 # Don't invoke any
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0
示例5: test_to_python
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_to_python():
m = Matrix(5, 5)
assert m[2, 3] == 0
m[2, 3] = 4
assert m[2, 3] == 4
m2 = np.array(m, copy=False)
assert m2.shape == (5, 5)
assert abs(m2).sum() == 4
assert m2[2, 3] == 4
m2[2, 3] = 5
assert m2[2, 3] == 5
cstats = ConstructorStats.get(Matrix)
assert cstats.alive() == 1
del m
pytest.gc_collect()
assert cstats.alive() == 1
del m2 # holds an m reference
pytest.gc_collect()
assert cstats.alive() == 0
assert cstats.values() == ["5x5 matrix"]
assert cstats.copy_constructions == 0
# assert cstats.move_constructions >= 0 # Don't invoke any
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0
示例6: test_shared_ptr_and_references
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_shared_ptr_and_references():
from pybind11_tests.smart_ptr import SharedPtrRef, A
s = SharedPtrRef()
stats = ConstructorStats.get(A)
assert stats.alive() == 2
ref = s.ref # init_holder_helper(holder_ptr=false, owned=false)
assert stats.alive() == 2
assert s.set_ref(ref)
with pytest.raises(RuntimeError) as excinfo:
assert s.set_holder(ref)
assert "Unable to cast from non-held to held instance" in str(excinfo.value)
copy = s.copy # init_holder_helper(holder_ptr=false, owned=true)
assert stats.alive() == 3
assert s.set_ref(copy)
assert s.set_holder(copy)
holder_ref = s.holder_ref # init_holder_helper(holder_ptr=true, owned=false)
assert stats.alive() == 3
assert s.set_ref(holder_ref)
assert s.set_holder(holder_ref)
holder_copy = s.holder_copy # init_holder_helper(holder_ptr=true, owned=true)
assert stats.alive() == 3
assert s.set_ref(holder_copy)
assert s.set_holder(holder_copy)
del ref, copy, holder_ref, holder_copy, s
assert stats.alive() == 0
示例7: test_unique_nodelete
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_unique_nodelete():
o = m.MyObject4(23)
assert o.value == 23
cstats = ConstructorStats.get(m.MyObject4)
assert cstats.alive() == 1
del o
assert cstats.alive() == 1 # Leak, but that's intentional
示例8: test_stl_ownership
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_stl_ownership():
cstats = ConstructorStats.get(m.Placeholder)
assert cstats.alive() == 0
r = m.test_stl_ownership()
assert len(r) == 1
del r
assert cstats.alive() == 0
示例9: test_holder_with_addressof_operator
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_holder_with_addressof_operator():
# this test must not throw exception from c++
a = m.TypeForHolderWithAddressOf.make()
a.print_object_1()
a.print_object_2()
a.print_object_3()
a.print_object_4()
stats = ConstructorStats.get(m.TypeForHolderWithAddressOf)
assert stats.alive() == 1
np = m.TypeForHolderWithAddressOf.make()
assert stats.alive() == 2
del a
assert stats.alive() == 1
del np
assert stats.alive() == 0
b = m.TypeForHolderWithAddressOf.make()
c = b
assert b.get() is c.get()
assert stats.alive() == 1
del b
assert stats.alive() == 1
del c
assert stats.alive() == 0
示例10: test_large_holder
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_large_holder():
o = m.MyObject5(5)
assert o.value == 5
cstats = ConstructorStats.get(m.MyObject5)
assert cstats.alive() == 1
del o
assert cstats.alive() == 0
示例11: test_methods_and_attributes
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_methods_and_attributes():
instance1 = m.ExampleMandA()
instance2 = m.ExampleMandA(32)
instance1.add1(instance2)
instance1.add2(instance2)
instance1.add3(instance2)
instance1.add4(instance2)
instance1.add5(instance2)
instance1.add6(32)
instance1.add7(32)
instance1.add8(32)
instance1.add9(32)
instance1.add10(32)
assert str(instance1) == "ExampleMandA[value=320]"
assert str(instance2) == "ExampleMandA[value=32]"
assert str(instance1.self1()) == "ExampleMandA[value=320]"
assert str(instance1.self2()) == "ExampleMandA[value=320]"
assert str(instance1.self3()) == "ExampleMandA[value=320]"
assert str(instance1.self4()) == "ExampleMandA[value=320]"
assert str(instance1.self5()) == "ExampleMandA[value=320]"
assert instance1.internal1() == 320
assert instance1.internal2() == 320
assert instance1.internal3() == 320
assert instance1.internal4() == 320
assert instance1.internal5() == 320
assert instance1.overloaded() == "()"
assert instance1.overloaded(0) == "(int)"
assert instance1.overloaded(1, 1.0) == "(int, float)"
assert instance1.overloaded(2.0, 2) == "(float, int)"
assert instance1.overloaded(3, 3) == "(int, int)"
assert instance1.overloaded(4., 4.) == "(float, float)"
assert instance1.overloaded_const(-3) == "(int) const"
assert instance1.overloaded_const(5, 5.0) == "(int, float) const"
assert instance1.overloaded_const(6.0, 6) == "(float, int) const"
assert instance1.overloaded_const(7, 7) == "(int, int) const"
assert instance1.overloaded_const(8., 8.) == "(float, float) const"
assert instance1.overloaded_float(1, 1) == "(float, float)"
assert instance1.overloaded_float(1, 1.) == "(float, float)"
assert instance1.overloaded_float(1., 1) == "(float, float)"
assert instance1.overloaded_float(1., 1.) == "(float, float)"
assert instance1.value == 320
instance1.value = 100
assert str(instance1) == "ExampleMandA[value=100]"
cstats = ConstructorStats.get(m.ExampleMandA)
assert cstats.alive() == 2
del instance1, instance2
assert cstats.alive() == 0
assert cstats.values() == ["32"]
assert cstats.default_constructions == 1
assert cstats.copy_constructions == 3
assert cstats.move_constructions >= 1
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0
示例12: test_move_only_holder
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_move_only_holder():
from pybind11_tests.smart_ptr import TypeWithMoveOnlyHolder
a = TypeWithMoveOnlyHolder.make()
stats = ConstructorStats.get(TypeWithMoveOnlyHolder)
assert stats.alive() == 1
del a
assert stats.alive() == 0
示例13: test_move_support
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_move_support():
from pybind11_tests import NCVirt, NonCopyable, Movable
class NCVirtExt(NCVirt):
def get_noncopyable(self, a, b):
# Constructs and returns a new instance:
nc = NonCopyable(a * a, b * b)
return nc
def get_movable(self, a, b):
# Return a referenced copy
self.movable = Movable(a, b)
return self.movable
class NCVirtExt2(NCVirt):
def get_noncopyable(self, a, b):
# Keep a reference: this is going to throw an exception
self.nc = NonCopyable(a, b)
return self.nc
def get_movable(self, a, b):
# Return a new instance without storing it
return Movable(a, b)
ncv1 = NCVirtExt()
assert ncv1.print_nc(2, 3) == "36"
assert ncv1.print_movable(4, 5) == "9"
ncv2 = NCVirtExt2()
assert ncv2.print_movable(7, 7) == "14"
# Don't check the exception message here because it differs under debug/non-debug mode
with pytest.raises(RuntimeError):
ncv2.print_nc(9, 9)
nc_stats = ConstructorStats.get(NonCopyable)
mv_stats = ConstructorStats.get(Movable)
assert nc_stats.alive() == 1
assert mv_stats.alive() == 1
del ncv1, ncv2
assert nc_stats.alive() == 0
assert mv_stats.alive() == 0
assert nc_stats.values() == ['4', '9', '9', '9']
assert mv_stats.values() == ['4', '5', '7', '7']
assert nc_stats.copy_constructions == 0
assert mv_stats.copy_constructions == 1
assert nc_stats.move_constructions >= 0
assert mv_stats.move_constructions >= 0
示例14: test_large_holder
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_large_holder():
from pybind11_tests import MyObject5
o = MyObject5(5)
assert o.value == 5
cstats = ConstructorStats.get(MyObject5)
assert cstats.alive() == 1
del o
assert cstats.alive() == 0
示例15: test_sequence
# 需要导入模块: from pybind11_tests import ConstructorStats [as 别名]
# 或者: from pybind11_tests.ConstructorStats import get [as 别名]
def test_sequence():
from pybind11_tests import ConstructorStats
from pybind11_tests.sequences_and_iterators import Sequence
cstats = ConstructorStats.get(Sequence)
s = Sequence(5)
assert cstats.values() == ['of size', '5']
assert "Sequence" in repr(s)
assert len(s) == 5
assert s[0] == 0 and s[3] == 0
assert 12.34 not in s
s[0], s[3] = 12.34, 56.78
assert 12.34 in s
assert isclose(s[0], 12.34) and isclose(s[3], 56.78)
rev = reversed(s)
assert cstats.values() == ['of size', '5']
rev2 = s[::-1]
assert cstats.values() == ['of size', '5']
it = iter(Sequence(0))
for _ in range(3): # __next__ must continue to raise StopIteration
with pytest.raises(StopIteration):
next(it)
assert cstats.values() == ['of size', '0']
expected = [0, 56.78, 0, 0, 12.34]
assert allclose(rev, expected)
assert allclose(rev2, expected)
assert rev == rev2
rev[0::2] = Sequence([2.0, 2.0, 2.0])
assert cstats.values() == ['of size', '3', 'from std::vector']
assert allclose(rev, [2, 56.78, 2, 0, 2])
assert cstats.alive() == 4
del it
assert cstats.alive() == 3
del s
assert cstats.alive() == 2
del rev
assert cstats.alive() == 1
del rev2
assert cstats.alive() == 0
assert cstats.values() == []
assert cstats.default_constructions == 0
assert cstats.copy_constructions == 0
assert cstats.move_constructions >= 1
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0