Skip to content

Commit 0017011

Browse files
committed
tests: avoid immortal objects in tests
Co-authored-by: Sam Gross <[email protected]> Signed-off-by: Henry Schreiner <[email protected]>
1 parent 7187894 commit 0017011

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

tests/test_kwargs_and_defaults.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22

3-
from pybind11_tests import PYBIND11_REFCNT_IMMORTAL
43
from pybind11_tests import kwargs_and_defaults as m
54

65

@@ -382,10 +381,10 @@ def test_args_refcount():
382381
arguments"""
383382
refcount = m.arg_refcount_h
384383

385-
myval = 54321
384+
myval = object()
386385
expected = refcount(myval)
387386
assert m.arg_refcount_h(myval) == expected
388-
assert m.arg_refcount_o(myval) in {expected + 1, PYBIND11_REFCNT_IMMORTAL}
387+
assert m.arg_refcount_o(myval) == expected + 1
389388
assert m.arg_refcount_h(myval) == expected
390389
assert refcount(myval) == expected
391390

@@ -421,7 +420,7 @@ def test_args_refcount():
421420
# for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
422421
# tuple without having to inc_ref the individual elements, but here we can't, hence the extra
423422
# refs.
424-
exp3_3 = PYBIND11_REFCNT_IMMORTAL if exp3 == PYBIND11_REFCNT_IMMORTAL else exp3 + 3
423+
exp3_3 = exp3 + 3
425424
assert m.mixed_args_refcount(myval, myval, myval) == (exp3_3, exp3_3, exp3_3)
426425

427426
assert m.class_default_argument() == "<class 'decimal.Decimal'>"

tests/test_pytypes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
import env
8-
from pybind11_tests import PYBIND11_REFCNT_IMMORTAL, detailed_error_messages_enabled
8+
from pybind11_tests import detailed_error_messages_enabled
99
from pybind11_tests import pytypes as m
1010

1111

@@ -631,11 +631,12 @@ def test_memoryview(method, args, fmt, expected_view):
631631
],
632632
)
633633
def test_memoryview_refcount(method):
634-
buf = b"\x0a\x0b\x0c\x0d"
634+
# Avoiding a literal to avoid an immortal object in free-threaded builds
635+
buf = "\x0a\x0b\x0c\x0d".encode("ascii")
635636
ref_before = sys.getrefcount(buf)
636637
view = method(buf)
637638
ref_after = sys.getrefcount(buf)
638-
assert ref_before < ref_after or ref_before == ref_after == PYBIND11_REFCNT_IMMORTAL
639+
assert ref_before < ref_after
639640
assert list(view) == list(buf)
640641

641642

0 commit comments

Comments
 (0)