Skip to content

Commit

Permalink
Add identifiers to ResourceWarnings from memory object streams
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Jun 11, 2024
1 parent a8ec745 commit 7b4f94e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This library adheres to `Semantic Versioning 2.0 <http:https://semver.org/>`_.

- Added support for the ``from_uri()``, ``full_match()``, ``parser`` methods/properties
in ``anyio.Path``, newly added in Python 3.13
- Changed the ``ResourceWarning`` from an unclosed memory object stream to include its
address for easier identification

**4.4.0**

Expand Down
4 changes: 2 additions & 2 deletions src/anyio/streams/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def __exit__(
def __del__(self) -> None:
if not self._closed:
warnings.warn(
f"Unclosed <{self.__class__.__name__}>",
f"Unclosed <{self.__class__.__name__} at {id(self):x}>",
ResourceWarning,
source=self,
)
Expand Down Expand Up @@ -305,7 +305,7 @@ def __exit__(
def __del__(self) -> None:
if not self._closed:
warnings.warn(
f"Unclosed <{self.__class__.__name__}>",
f"Unclosed <{self.__class__.__name__} at {id(self):x}>",
ResourceWarning,
source=self,
)
8 changes: 6 additions & 2 deletions tests/streams/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,15 @@ async def test_deprecated_item_type_parameter() -> None:
async def test_not_closed_warning() -> None:
send, receive = create_memory_object_stream[int]()

with pytest.warns(ResourceWarning, match="Unclosed <MemoryObjectSendStream>"):
with pytest.warns(
ResourceWarning, match="Unclosed <MemoryObjectSendStream at [0-9a-f]+>"
):
del send
gc.collect()

with pytest.warns(ResourceWarning, match="Unclosed <MemoryObjectReceiveStream>"):
with pytest.warns(
ResourceWarning, match="Unclosed <MemoryObjectReceiveStream at [0-9a-f]+>"
):
del receive
gc.collect()

Expand Down

0 comments on commit 7b4f94e

Please sign in to comment.