Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[smart_holder] Additional assert is_disowned() in test_class_sh_disowning.py #5234

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions tests/test_class_sh_disowning.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@
from pybind11_tests import class_sh_disowning as m


def is_disowned(obj):
try:
obj.get()
except ValueError:
return True
return False


def test_same_twice():
while True:
obj1a = m.Atype1(57)
obj1b = m.Atype1(62)
assert m.same_twice(obj1a, obj1b) == (57 * 10 + 1) * 100 + (62 * 10 + 1) * 10
assert is_disowned(obj1a)
assert is_disowned(obj1b)
obj1c = m.Atype1(0)
with pytest.raises(ValueError):
# Disowning works for one argument, but not both.
m.same_twice(obj1c, obj1c)
with pytest.raises(ValueError):
obj1c.get()
assert is_disowned(obj1c)
return # Comment out for manual leak checking (use `top` command).


Expand All @@ -25,6 +34,8 @@ def test_mixed():
obj1a = m.Atype1(90)
obj2a = m.Atype2(25)
assert m.mixed(obj1a, obj2a) == (90 * 10 + 1) * 200 + (25 * 10 + 2) * 20
assert is_disowned(obj1a)
assert is_disowned(obj2a)

# The C++ order of evaluation of function arguments is (unfortunately) unspecified:
# https://en.cppreference.com/w/cpp/language/eval_order
Expand All @@ -40,13 +51,6 @@ def test_mixed():
# the already disowned obj1a fails as expected.
m.mixed(obj1a, obj2b)

def is_disowned(obj):
try:
obj.get()
except ValueError:
return True
return False

# Either obj1b or obj2b was disowned in the expected failed m.mixed() calls above, but not
# both.
is_disowned_results = (is_disowned(obj1b), is_disowned(obj2b))
Expand Down