Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track KeyboardInterrupt protection per code object #1508

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion trio/_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from ._multierror import MultiError

from ._ki import (
enable_ki_protection, disable_ki_protection, currently_ki_protected
enable_ki_protection, disable_ki_protection, mark_ki_unsafe_as_leaf,
ki_allowed_if_safe, ki_forbidden, currently_ki_protected
)

# Imports that always exist
Expand Down
10 changes: 5 additions & 5 deletions trio/_core/_generated_io_epoll.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
# ******* WARNING: AUTOGENERATED! ALL EDITS WILL BE LOST ******
# *************************************************************
from ._run import GLOBAL_RUN_CONTEXT, _NO_SEND
from ._ki import LOCALS_KEY_KI_PROTECTION_ENABLED
from ._ki import enable_ki_protection




@enable_ki_protection
async def wait_readable(fd):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_readable(fd)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
async def wait_writable(fd):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_writable(fd)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def notify_closing(fd):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.io_manager.notify_closing(fd)
except AttributeError:
Expand Down
16 changes: 8 additions & 8 deletions trio/_core/_generated_io_kqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,47 @@
# ******* WARNING: AUTOGENERATED! ALL EDITS WILL BE LOST ******
# *************************************************************
from ._run import GLOBAL_RUN_CONTEXT, _NO_SEND
from ._ki import LOCALS_KEY_KI_PROTECTION_ENABLED
from ._ki import enable_ki_protection




@enable_ki_protection
def current_kqueue():
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.io_manager.current_kqueue()
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def monitor_kevent(ident, filter):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.io_manager.monitor_kevent(ident, filter)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
async def wait_kevent(ident, filter, abort_func):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_kevent(ident, filter, abort_func)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
async def wait_readable(fd):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_readable(fd)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
async def wait_writable(fd):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_writable(fd)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def notify_closing(fd):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.io_manager.notify_closing(fd)
except AttributeError:
Expand Down
22 changes: 11 additions & 11 deletions trio/_core/_generated_io_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,68 @@
# ******* WARNING: AUTOGENERATED! ALL EDITS WILL BE LOST ******
# *************************************************************
from ._run import GLOBAL_RUN_CONTEXT, _NO_SEND
from ._ki import LOCALS_KEY_KI_PROTECTION_ENABLED
from ._ki import enable_ki_protection




@enable_ki_protection
async def wait_readable(sock):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_readable(sock)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
async def wait_writable(sock):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_writable(sock)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def notify_closing(handle):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.io_manager.notify_closing(handle)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def register_with_iocp(handle):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.io_manager.register_with_iocp(handle)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
async def wait_overlapped(handle, lpOverlapped):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_overlapped(handle, lpOverlapped)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
async def write_overlapped(handle, data, file_offset=0):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.io_manager.write_overlapped(handle, data, file_offset)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
async def readinto_overlapped(handle, buffer, file_offset=0):
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.io_manager.readinto_overlapped(handle, buffer, file_offset)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def current_iocp():
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.io_manager.current_iocp()
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def monitor_completion_key():
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.io_manager.monitor_completion_key()
except AttributeError:
Expand Down
24 changes: 12 additions & 12 deletions trio/_core/_generated_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# ******* WARNING: AUTOGENERATED! ALL EDITS WILL BE LOST ******
# *************************************************************
from ._run import GLOBAL_RUN_CONTEXT, _NO_SEND
from ._ki import LOCALS_KEY_KI_PROTECTION_ENABLED
from ._ki import enable_ki_protection




@enable_ki_protection
def current_statistics():
"""Returns an object containing run-loop-level debugging information.

Expand All @@ -29,12 +30,12 @@ def current_statistics():
other attributes vary between backends.

"""
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.current_statistics()
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def current_time():
"""Returns the current time according to Trio's internal clock.

Expand All @@ -45,34 +46,34 @@ def current_time():
RuntimeError: if not inside a call to :func:`trio.run`.

"""
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.current_time()
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def current_clock():
"""Returns the current :class:`~trio.abc.Clock`.

"""
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.current_clock()
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def current_root_task():
"""Returns the current root :class:`Task`.

This is the task that is the ultimate parent of all other tasks.

"""
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.current_root_task()
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def reschedule(task, next_send=_NO_SEND):
"""Reschedule the given task with the given
:class:`outcome.Outcome`.
Expand All @@ -91,12 +92,12 @@ def reschedule(task, next_send=_NO_SEND):
raise) from :func:`wait_task_rescheduled`.

"""
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.reschedule(task, next_send)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def spawn_system_task(async_fn, *args, name=None):
"""Spawn a "system" task.

Expand Down Expand Up @@ -136,23 +137,23 @@ def spawn_system_task(async_fn, *args, name=None):
Task: the newly spawned task

"""
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.spawn_system_task(async_fn, *args, name=name)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def current_trio_token():
"""Retrieve the :class:`TrioToken` for the current call to
:func:`trio.run`.

"""
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.current_trio_token()
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
async def wait_all_tasks_blocked(cushion=0.0, tiebreaker=0):
"""Block until there are no runnable tasks.

Expand Down Expand Up @@ -213,12 +214,12 @@ async def test_lock_fairness():
print("FAIL")

"""
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return await GLOBAL_RUN_CONTEXT.runner.wait_all_tasks_blocked(cushion, tiebreaker)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def add_instrument(instrument):
"""Start instrumenting the current run loop with the given instrument.

Expand All @@ -228,12 +229,12 @@ def add_instrument(instrument):
If ``instrument`` is already active, does nothing.

"""
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.add_instrument(instrument)
except AttributeError:
raise RuntimeError('must be called from async context')

@enable_ki_protection
def remove_instrument(instrument):
"""Stop instrumenting the current run loop with the given instrument.

Expand All @@ -247,7 +248,6 @@ def remove_instrument(instrument):
deactivated.

"""
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
return GLOBAL_RUN_CONTEXT.runner.remove_instrument(instrument)
except AttributeError:
Expand Down
Loading