Skip to content

Commit c12b4e7

Browse files
authored
Merge pull request #1924 from python-trio/token-weakref
2 parents b4a1bd6 + 4fdb36c commit c12b4e7

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

newsfragments/1924.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The :class:`~.lowlevel.TrioToken` class can now be used as a target of a weak reference.

trio/_core/_entry_queue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class TrioToken(metaclass=NoPublicConstructor):
145145
146146
"""
147147

148-
__slots__ = ("_reentry_queue",)
148+
__slots__ = ("_reentry_queue", "__weakref__")
149149

150150
def __init__(self, reentry_queue):
151151
self._reentry_queue = reentry_queue

trio/tests/test_threads.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import threading
22
import queue as stdlib_queue
33
import time
4+
import weakref
45

56
import pytest
7+
from trio._core import TrioToken, current_trio_token
68

79
from .. import _core
810
from .. import Event, CapacityLimiter, sleep
@@ -576,3 +578,10 @@ async def main():
576578

577579
_core.run(main)
578580
assert record == ["ok"]
581+
582+
583+
async def test_trio_token_weak_referenceable():
584+
token = current_trio_token()
585+
assert isinstance(token, TrioToken)
586+
weak_reference = weakref.ref(token)
587+
assert token is weak_reference()

0 commit comments

Comments
 (0)