Skip to content

Commit

Permalink
Merge pull request #3976 from ngoldbaum/free-threaded-random-fix
Browse files Browse the repository at this point in the history
Do not warn about possibly unreferenced RNGs on free-threaded CPython
  • Loading branch information
Zac-HD committed May 6, 2024
2 parents 26e8db7 + 00ea113 commit 8182432
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -130,6 +130,7 @@ their individual contributions.
* `Michel Alexandre Salim <https://github.com/michel-slm>`_ (michel@michel-slm.name)
* `mulkieran <https://www.github.com/mulkieran>`_
* `Munir Abdinur <https://www.github.com/mabdinur>`_
* `Nathan Goldbaum <https://www/github.com/ngoldbaum>`_
* `Nicholas Chammas <https://www.github.com/nchammas>`_
* `Nick Anyos <https://www.github.com/NickAnyos>`_
* `Nick Collins <https://github.com/nickcollins>` _
Expand Down
8 changes: 8 additions & 0 deletions hypothesis-python/RELEASE.rst
@@ -0,0 +1,8 @@
RELEASE_TYPE: patch

This patch turns off a check in :func:`~hypothesis.register_random` for possibly
unreferenced RNG instances on the free-threaded build of CPython 3.13 because
this check has a much higher false positive rate in the free-threaded build
(:issue:`3965`).

Thanks to Nathan Goldbaum for this patch.
3 changes: 3 additions & 0 deletions hypothesis-python/src/hypothesis/internal/compat.py
Expand Up @@ -14,6 +14,7 @@
import inspect
import platform
import sys
import sysconfig
import typing
from functools import partial
from typing import Any, ForwardRef, List, Optional, get_args
Expand Down Expand Up @@ -43,6 +44,8 @@
PYPY = platform.python_implementation() == "PyPy"
GRAALPY = platform.python_implementation() == "GraalVM"
WINDOWS = platform.system() == "Windows"
# First defined in CPython 3.13, defaults to False
FREE_THREADED_CPYTHON = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))


def add_note(exc, note):
Expand Down
11 changes: 8 additions & 3 deletions hypothesis-python/src/hypothesis/internal/entropy.py
Expand Up @@ -19,7 +19,7 @@

import hypothesis.core
from hypothesis.errors import HypothesisWarning, InvalidArgument
from hypothesis.internal.compat import GRAALPY, PYPY
from hypothesis.internal.compat import FREE_THREADED_CPYTHON, GRAALPY, PYPY

if TYPE_CHECKING:
from typing import Protocol
Expand Down Expand Up @@ -116,7 +116,7 @@ def my_WORKING_hook():
return

if not (PYPY or GRAALPY): # pragma: no branch
# PYPY and GRAALPY do not have `sys.getrefcount`
# PYPY and GRAALPY do not have `sys.getrefcount`.
gc.collect()
if not gc.get_referrers(r):
if sys.getrefcount(r) <= _PLATFORM_REF_COUNT:
Expand All @@ -127,7 +127,12 @@ def my_WORKING_hook():
"PRNG. See the docs for `register_random` for more "
"details."
)
else:
elif not FREE_THREADED_CPYTHON:
# On CPython, check for the free-threaded build because
# gc.get_referrers() ignores objects with immortal refcounts
# and objects are immortalized in the Python 3.13
# free-threading implementation at runtime.

warnings.warn(
"It looks like `register_random` was passed an object that could "
"be garbage collected immediately after `register_random` creates "
Expand Down

0 comments on commit 8182432

Please sign in to comment.