mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-26 23:22:01 +00:00
353615f77e
The instance registration for offset base types fails (under macOS, with a segfault) in the presense of virtual base types. The issue occurs when trying to `static_cast<Base *>(derived_ptr)` when `derived_ptr` has been allocated (via `operator new`) but not initialized. This commit fixes the issue by moving the addition to `registered_instances` into `init_holder` rather than immediately after value pointer allocation. This also renames it to `init_instance` since it does more than holder initialization now. (I also further renamed `init_holder_helper` to `init_holder` since `init_holder` isn't used anymore). Fixes #959.
181 lines
5.1 KiB
Python
181 lines
5.1 KiB
Python
import pytest
|
|
|
|
|
|
def test_keep_alive_argument(capture):
|
|
from pybind11_tests import Parent, Child, ConstructorStats
|
|
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
with capture:
|
|
p = Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.addChild(Child())
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 1
|
|
assert capture == """
|
|
Allocating child.
|
|
Releasing child.
|
|
"""
|
|
with capture:
|
|
del p
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == "Releasing parent."
|
|
|
|
with capture:
|
|
p = Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.addChildKeepAlive(Child())
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
|
assert capture == "Allocating child."
|
|
with capture:
|
|
del p
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == """
|
|
Releasing parent.
|
|
Releasing child.
|
|
"""
|
|
|
|
|
|
def test_keep_alive_return_value(capture):
|
|
from pybind11_tests import Parent, ConstructorStats
|
|
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
with capture:
|
|
p = Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.returnChild()
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 1
|
|
assert capture == """
|
|
Allocating child.
|
|
Releasing child.
|
|
"""
|
|
with capture:
|
|
del p
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == "Releasing parent."
|
|
|
|
with capture:
|
|
p = Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.returnChildKeepAlive()
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
|
assert capture == "Allocating child."
|
|
with capture:
|
|
del p
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == """
|
|
Releasing parent.
|
|
Releasing child.
|
|
"""
|
|
|
|
|
|
# https://bitbucket.org/pypy/pypy/issues/2447
|
|
@pytest.unsupported_on_pypy
|
|
def test_alive_gc(capture):
|
|
from pybind11_tests import ParentGC, Child, ConstructorStats
|
|
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
p = ParentGC()
|
|
p.addChildKeepAlive(Child())
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
|
lst = [p]
|
|
lst.append(lst) # creates a circular reference
|
|
with capture:
|
|
del p, lst
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == """
|
|
Releasing parent.
|
|
Releasing child.
|
|
"""
|
|
|
|
|
|
def test_alive_gc_derived(capture):
|
|
from pybind11_tests import Parent, Child, ConstructorStats
|
|
|
|
class Derived(Parent):
|
|
pass
|
|
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
p = Derived()
|
|
p.addChildKeepAlive(Child())
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 2
|
|
lst = [p]
|
|
lst.append(lst) # creates a circular reference
|
|
with capture:
|
|
del p, lst
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == """
|
|
Releasing parent.
|
|
Releasing child.
|
|
"""
|
|
|
|
|
|
def test_alive_gc_multi_derived(capture):
|
|
from pybind11_tests import Parent, Child, ConstructorStats
|
|
|
|
class Derived(Parent, Child):
|
|
def __init__(self):
|
|
Parent.__init__(self)
|
|
Child.__init__(self)
|
|
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
p = Derived()
|
|
p.addChildKeepAlive(Child())
|
|
# +3 rather than +2 because Derived corresponds to two registered instances
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 3
|
|
lst = [p]
|
|
lst.append(lst) # creates a circular reference
|
|
with capture:
|
|
del p, lst
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == """
|
|
Releasing parent.
|
|
Releasing child.
|
|
Releasing child.
|
|
"""
|
|
|
|
|
|
def test_return_none(capture):
|
|
from pybind11_tests import Parent, ConstructorStats
|
|
|
|
n_inst = ConstructorStats.detail_reg_inst()
|
|
with capture:
|
|
p = Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.returnNullChildKeepAliveChild()
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 1
|
|
assert capture == ""
|
|
with capture:
|
|
del p
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == "Releasing parent."
|
|
|
|
with capture:
|
|
p = Parent()
|
|
assert capture == "Allocating parent."
|
|
with capture:
|
|
p.returnNullChildKeepAliveParent()
|
|
assert ConstructorStats.detail_reg_inst() == n_inst + 1
|
|
assert capture == ""
|
|
with capture:
|
|
del p
|
|
assert ConstructorStats.detail_reg_inst() == n_inst
|
|
assert capture == "Releasing parent."
|
|
|
|
|
|
def test_call_guard():
|
|
from pybind11_tests import call_policies
|
|
|
|
assert call_policies.unguarded_call() == "unguarded"
|
|
assert call_policies.guarded_call() == "guarded"
|
|
|
|
assert call_policies.multiple_guards_correct_order() == "guarded & guarded"
|
|
assert call_policies.multiple_guards_wrong_order() == "unguarded & guarded"
|
|
|
|
if hasattr(call_policies, "with_gil"):
|
|
assert call_policies.with_gil() == "GIL held"
|
|
assert call_policies.without_gil() == "GIL released"
|