Skip to content

Commit

Permalink
Mention pypy in docs and consolidate code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
rsokl committed Nov 18, 2022
1 parent 8b907fd commit fd1b90a
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions hypothesis-python/src/hypothesis/internal/entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,34 +91,26 @@ def register_random(r: RandomLike) -> None:
<https://docs.python.org/3/library/weakref.html#module-weakref>`_ to ``r``,
thus ``r`` will only be managed by Hypothesis as long as it has active
references elsewhere at runtime. The pattern ``register_random(MyRandom())``
will raise a ``ReferenceError`` to help protect users from this issue. That
being said, a pattern like
will raise a ``ReferenceError`` to help protect users from this issue.
This check does not occur for the PyPy interpreter. See the following example for
an illustration of this issue
.. code-block:: python
# contents of mylib/foo.py
def my_BROKEN_hook():
r = MyRandomLike()
def my_hook():
rng = MyRandomSingleton()
register_random(rng)
return None
must be refactored as
# `r` will be garbage collected after the hook resolved
# and Hypothesis will 'forget' that it was registered
register_random(r) # Hypothesis will emit a warning
.. code-block:: python
# contents of mylib/foo.py
rng = MyRandomLike()
rng = MyRandomSingleton()
def my_hook():
def my_WORKING_hook():
register_random(rng)
return None
in order for Hypothesis to continue managing the random instance after the hook
is called.
"""
if not (hasattr(r, "seed") and hasattr(r, "getstate") and hasattr(r, "setstate")):
raise InvalidArgument(f"r={r!r} does not have all the required methods")
Expand Down

0 comments on commit fd1b90a

Please sign in to comment.