Additional assert is_disowned() in test_class_sh_disowning.py (#5234)

This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-07-09 13:27:31 -07:00 committed by GitHub
parent b044b4faef
commit 9193b8e714
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,17 +5,26 @@ import pytest
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).
@ -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
@ -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))