Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into break_the_lot
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl committed Oct 7, 2024
2 parents cc97cca + 5d9067b commit b81e297
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.7
rev: v0.6.8
hooks:
- id: ruff
types: [file]
Expand Down
10 changes: 5 additions & 5 deletions docs-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ beautifulsoup4==4.12.3
# via sphinx-codeautolink
certifi==2024.8.30
# via requests
cffi==1.17.0 ; platform_python_implementation != 'PyPy' or os_name == 'nt'
cffi==1.17.1 ; platform_python_implementation != 'PyPy' or os_name == 'nt'
# via
# -r docs-requirements.in
# cryptography
Expand All @@ -24,15 +24,15 @@ colorama==0.4.6 ; sys_platform == 'win32' or platform_system == 'Windows'
# via
# click
# sphinx
cryptography==43.0.0
cryptography==43.0.1
# via pyopenssl
docutils==0.20.1
# via
# sphinx
# sphinx-rtd-theme
exceptiongroup==1.2.2
# via -r docs-requirements.in
idna==3.8
idna==3.10
# via
# -r docs-requirements.in
# requests
Expand Down Expand Up @@ -77,7 +77,7 @@ sphinx==7.4.7
# sphinxcontrib-trio
sphinx-codeautolink==0.15.2
# via -r docs-requirements.in
sphinx-hoverxref==1.4.0
sphinx-hoverxref==1.4.1
# via -r docs-requirements.in
sphinx-rtd-theme==2.0.0
# via -r docs-requirements.in
Expand All @@ -102,5 +102,5 @@ sphinxcontrib-trio==1.1.2
# via -r docs-requirements.in
towncrier==24.8.0
# via -r docs-requirements.in
urllib3==2.2.2
urllib3==2.2.3
# via requests
2 changes: 2 additions & 0 deletions docs/source/reference-core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,8 @@ more features beyond the core channel interface:
.. autoclass:: MemoryReceiveChannel
:members:

.. autoclass:: MemoryChannelStatistics
:members:

A simple channel example
++++++++++++++++++++++++
Expand Down
1 change: 1 addition & 0 deletions newsfragments/3095.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update :func:`trio.sleep_forever` to be `NoReturn`.
1 change: 1 addition & 0 deletions newsfragments/3101.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add docstrings for memory channels' ``statistics()`` and ``aclose`` methods.
1 change: 1 addition & 0 deletions src/trio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# Submodules imported by default
from . import abc, from_thread, lowlevel, socket, to_thread
from ._channel import (
MemoryChannelStatistics as MemoryChannelStatistics,
MemoryReceiveChannel as MemoryReceiveChannel,
MemorySendChannel as MemorySendChannel,
open_memory_channel as open_memory_channel,
Expand Down
20 changes: 15 additions & 5 deletions src/trio/_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __init__(self, max_buffer_size: int | float): # noqa: PYI041


@attrs.frozen
class MemoryChannelStats:
class MemoryChannelStatistics:
current_buffer_used: int
max_buffer_size: int | float
open_send_channels: int
Expand All @@ -132,8 +132,8 @@ class MemoryChannelState(Generic[T]):
# {task: None}
receive_tasks: OrderedDict[Task, None] = attrs.Factory(OrderedDict)

def statistics(self) -> MemoryChannelStats:
return MemoryChannelStats(
def statistics(self) -> MemoryChannelStatistics:
return MemoryChannelStatistics(
current_buffer_used=len(self.data),
max_buffer_size=self.max_buffer_size,
open_send_channels=self.open_send_channels,
Expand All @@ -159,7 +159,9 @@ def __attrs_post_init__(self) -> None:
def __repr__(self) -> str:
return f"<send channel at {id(self):#x}, using buffer at {id(self._state):#x}>"

def statistics(self) -> MemoryChannelStats:
def statistics(self) -> MemoryChannelStatistics:
"""Returns a `MemoryChannelStatistics` for the memory channel this is
associated with."""
# XX should we also report statistics specific to this object?
return self._state.statistics()

Expand Down Expand Up @@ -282,6 +284,9 @@ def close(self) -> None:

@enable_ki_protection
async def aclose(self) -> None:
"""Close this send channel object asynchronously.
See `MemorySendChannel.close`."""
self.close()
await trio.lowlevel.checkpoint()

Expand All @@ -296,7 +301,9 @@ class MemoryReceiveChannel(ReceiveChannel[ReceiveType], metaclass=NoPublicConstr
def __attrs_post_init__(self) -> None:
self._state.open_receive_channels += 1

def statistics(self) -> MemoryChannelStats:
def statistics(self) -> MemoryChannelStatistics:
"""Returns a `MemoryChannelStatistics` for the memory channel this is
associated with."""
return self._state.statistics()

def __repr__(self) -> str:
Expand Down Expand Up @@ -430,5 +437,8 @@ def close(self) -> None:

@enable_ki_protection
async def aclose(self) -> None:
"""Close this receive channel object asynchronously.
See `MemoryReceiveChannel.close`."""
self.close()
await trio.lowlevel.checkpoint()
7 changes: 2 additions & 5 deletions src/trio/_tests/_check_type_completeness.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@
"all": [
"No docstring found for class \"trio.MemoryReceiveChannel\"",
"No docstring found for class \"trio._channel.MemoryReceiveChannel\"",
"No docstring found for function \"trio._channel.MemoryReceiveChannel.statistics\"",
"No docstring found for class \"trio._channel.MemoryChannelStats\"",
"No docstring found for function \"trio._channel.MemoryReceiveChannel.aclose\"",
"No docstring found for class \"trio.MemoryChannelStatistics\"",
"No docstring found for class \"trio._channel.MemoryChannelStatistics\"",
"No docstring found for class \"trio.MemorySendChannel\"",
"No docstring found for class \"trio._channel.MemorySendChannel\"",
"No docstring found for function \"trio._channel.MemorySendChannel.statistics\"",
"No docstring found for function \"trio._channel.MemorySendChannel.aclose\"",
"No docstring found for class \"trio._core._run.Task\"",
"No docstring found for class \"trio._socket.SocketType\"",
"No docstring found for function \"trio._highlevel_socket.SocketStream.send_all\"",
Expand Down
14 changes: 14 additions & 0 deletions src/trio/_tests/test_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ async def sleep_3() -> None:
await check_takes_about(sleep_3, TARGET)


async def test_cannot_wake_sleep_forever() -> None:
# Test an error occurs if you manually wake sleep_forever().
task = trio.lowlevel.current_task()

async def wake_task() -> None:
await trio.lowlevel.checkpoint()
trio.lowlevel.reschedule(task, outcome.Value(None))

async with trio.open_nursery() as nursery:
nursery.start_soon(wake_task)
with pytest.raises(RuntimeError):
await trio.sleep_forever()


class TimeoutScope(Protocol):
def __call__(self, seconds: float, *, shield: bool) -> trio.CancelScope: ...

Expand Down
5 changes: 3 additions & 2 deletions src/trio/_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import math
import sys
from contextlib import contextmanager
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, NoReturn

import trio

Expand Down Expand Up @@ -58,13 +58,14 @@ def move_on_after(
)


async def sleep_forever() -> None:
async def sleep_forever() -> NoReturn:
"""Pause execution of the current task forever (or until cancelled).
Equivalent to calling ``await sleep(math.inf)``.
"""
await trio.lowlevel.wait_task_rescheduled(lambda _: trio.lowlevel.Abort.SUCCEEDED)
raise RuntimeError("Should never have been rescheduled!")


async def sleep_until(deadline: float) -> None:
Expand Down
31 changes: 16 additions & 15 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ black==24.8.0 ; implementation_name == 'cpython'
# via -r test-requirements.in
certifi==2024.8.30
# via requests
cffi==1.17.0 ; platform_python_implementation != 'PyPy' or os_name == 'nt'
cffi==1.17.1 ; platform_python_implementation != 'PyPy' or os_name == 'nt'
# via
# -r test-requirements.in
# cryptography
Expand All @@ -36,28 +36,28 @@ colorama==0.4.6 ; (implementation_name != 'cpython' and sys_platform == 'win32')
# sphinx
coverage==7.6.1
# via -r test-requirements.in
cryptography==43.0.0
cryptography==43.0.1
# via
# -r test-requirements.in
# pyopenssl
# trustme
# types-pyopenssl
dill==0.3.8
dill==0.3.9
# via pylint
docutils==0.20.1
# via sphinx
exceptiongroup==1.2.2 ; python_full_version < '3.11'
# via
# -r test-requirements.in
# pytest
idna==3.8
idna==3.10
# via
# -r test-requirements.in
# requests
# trustme
imagesize==1.4.1
# via sphinx
importlib-metadata==8.4.0 ; python_full_version < '3.10'
importlib-metadata==8.5.0 ; python_full_version < '3.10'
# via sphinx
iniconfig==2.0.0
# via pytest
Expand Down Expand Up @@ -91,7 +91,7 @@ parso==0.8.4 ; implementation_name == 'cpython'
# via jedi
pathspec==0.12.1 ; implementation_name == 'cpython'
# via black
platformdirs==4.2.2
platformdirs==4.3.6
# via
# black
# pylint
Expand All @@ -105,15 +105,15 @@ pylint==3.2.7
# via -r test-requirements.in
pyopenssl==24.2.1
# via -r test-requirements.in
pyright==1.1.381
pyright==1.1.382.post1
# via -r test-requirements.in
pytest==8.3.2
pytest==8.3.3
# via -r test-requirements.in
pytz==2024.1 ; python_full_version < '3.9'
pytz==2024.2 ; python_full_version < '3.9'
# via babel
requests==2.32.3
# via sphinx
ruff==0.6.3
ruff==0.6.8
# via -r test-requirements.in
sniffio==1.3.1
# via -r test-requirements.in
Expand Down Expand Up @@ -149,11 +149,11 @@ types-cffi==1.16.0.20240331
# via
# -r test-requirements.in
# types-pyopenssl
types-docutils==0.21.0.20240724
types-docutils==0.21.0.20240907
# via -r test-requirements.in
types-pyopenssl==24.1.0.20240722
# via -r test-requirements.in
types-setuptools==74.0.0.20240831
types-setuptools==75.1.0.20240917
# via types-cffi
typing-extensions==4.12.2
# via
Expand All @@ -162,9 +162,10 @@ typing-extensions==4.12.2
# black
# mypy
# pylint
urllib3==2.2.2
# pyright
urllib3==2.2.3
# via requests
uv==0.4.1
uv==0.4.17
# via -r test-requirements.in
zipp==3.20.1 ; python_full_version < '3.10'
zipp==3.20.2 ; python_full_version < '3.10'
# via importlib-metadata

0 comments on commit b81e297

Please sign in to comment.