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

tests: avoid immortal objects in tests #5150

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ jobs:
fetch-depth: 0

- name: Prepare venv
run: python3.13 -m venv .venv
run: python3.13t -m venv .venv

- name: Install Python deps
run: .venv/bin/pip install -r tests/requirements.txt
Expand Down
7 changes: 3 additions & 4 deletions tests/test_kwargs_and_defaults.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest

from pybind11_tests import PYBIND11_REFCNT_IMMORTAL
from pybind11_tests import kwargs_and_defaults as m


Expand Down Expand Up @@ -382,10 +381,10 @@ def test_args_refcount():
arguments"""
refcount = m.arg_refcount_h

myval = 54321
myval = object()
expected = refcount(myval)
assert m.arg_refcount_h(myval) == expected
assert m.arg_refcount_o(myval) in {expected + 1, PYBIND11_REFCNT_IMMORTAL}
assert m.arg_refcount_o(myval) == expected + 1
assert m.arg_refcount_h(myval) == expected
assert refcount(myval) == expected

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

assert m.class_default_argument() == "<class 'decimal.Decimal'>"
7 changes: 4 additions & 3 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

import env
from pybind11_tests import PYBIND11_REFCNT_IMMORTAL, detailed_error_messages_enabled
from pybind11_tests import detailed_error_messages_enabled
from pybind11_tests import pytypes as m


Expand Down Expand Up @@ -631,11 +631,12 @@ def test_memoryview(method, args, fmt, expected_view):
],
)
def test_memoryview_refcount(method):
buf = b"\x0a\x0b\x0c\x0d"
# Avoiding a literal to avoid an immortal object in free-threaded builds
buf = "\x0a\x0b\x0c\x0d".encode("ascii")
ref_before = sys.getrefcount(buf)
view = method(buf)
ref_after = sys.getrefcount(buf)
assert ref_before < ref_after or ref_before == ref_after == PYBIND11_REFCNT_IMMORTAL
assert ref_before < ref_after
assert list(view) == list(buf)


Expand Down
Loading