diff options
Diffstat (limited to 'Lib/asyncio')
-rw-r--r-- | Lib/asyncio/__main__.py | 8 | ||||
-rw-r--r-- | Lib/asyncio/base_events.py | 2 | ||||
-rw-r--r-- | Lib/asyncio/taskgroups.py | 7 | ||||
-rw-r--r-- | Lib/asyncio/tasks.py | 14 | ||||
-rw-r--r-- | Lib/asyncio/tools.py | 16 |
5 files changed, 24 insertions, 23 deletions
diff --git a/Lib/asyncio/__main__.py b/Lib/asyncio/__main__.py index 7d980bc401a..21ca5c5f62a 100644 --- a/Lib/asyncio/__main__.py +++ b/Lib/asyncio/__main__.py @@ -12,7 +12,7 @@ import threading import types import warnings -from _colorize import can_colorize, ANSIColors # type: ignore[import-not-found] +from _colorize import get_theme from _pyrepl.console import InteractiveColoredConsole from . import futures @@ -103,8 +103,9 @@ class REPLThread(threading.Thread): exec(startup_code, console.locals) ps1 = getattr(sys, "ps1", ">>> ") - if can_colorize() and CAN_USE_PYREPL: - ps1 = f"{ANSIColors.BOLD_MAGENTA}{ps1}{ANSIColors.RESET}" + if CAN_USE_PYREPL: + theme = get_theme().syntax + ps1 = f"{theme.prompt}{ps1}{theme.reset}" console.write(f"{ps1}import asyncio\n") if CAN_USE_PYREPL: @@ -145,6 +146,7 @@ if __name__ == '__main__': parser = argparse.ArgumentParser( prog="python3 -m asyncio", description="Interactive asyncio shell and CLI tools", + color=True, ) subparsers = parser.add_subparsers(help="sub-commands", dest="command") ps = subparsers.add_parser( diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 29b872ce00e..04fb961e998 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -459,7 +459,7 @@ class BaseEventLoop(events.AbstractEventLoop): return futures.Future(loop=self) def create_task(self, coro, **kwargs): - """Schedule a coroutine object. + """Schedule or begin executing a coroutine object. Return a task object. """ diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py index 1633478d1c8..00e8f6d5d1a 100644 --- a/Lib/asyncio/taskgroups.py +++ b/Lib/asyncio/taskgroups.py @@ -179,7 +179,7 @@ class TaskGroup: exc = None - def create_task(self, coro, *, name=None, context=None): + def create_task(self, coro, **kwargs): """Create a new task in this group and return it. Similar to `asyncio.create_task`. @@ -193,10 +193,7 @@ class TaskGroup: if self._aborting: coro.close() raise RuntimeError(f"TaskGroup {self!r} is shutting down") - if context is None: - task = self._loop.create_task(coro, name=name) - else: - task = self._loop.create_task(coro, name=name, context=context) + task = self._loop.create_task(coro, **kwargs) futures.future_add_to_awaited_by(task, self._parent_task) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 825e91f5594..888615f8e5e 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -386,19 +386,13 @@ else: Task = _CTask = _asyncio.Task -def create_task(coro, *, name=None, context=None): +def create_task(coro, **kwargs): """Schedule the execution of a coroutine object in a spawn task. Return a Task object. """ loop = events.get_running_loop() - if context is None: - # Use legacy API if context is not needed - task = loop.create_task(coro, name=name) - else: - task = loop.create_task(coro, name=name, context=context) - - return task + return loop.create_task(coro, **kwargs) # wait() and as_completed() similar to those in PEP 3148. @@ -1030,9 +1024,9 @@ def create_eager_task_factory(custom_task_constructor): used. E.g. `loop.set_task_factory(asyncio.eager_task_factory)`. """ - def factory(loop, coro, *, name=None, context=None): + def factory(loop, coro, *, eager_start=True, **kwargs): return custom_task_constructor( - coro, loop=loop, name=name, context=context, eager_start=True) + coro, loop=loop, eager_start=eager_start, **kwargs) return factory diff --git a/Lib/asyncio/tools.py b/Lib/asyncio/tools.py index 6c1f725e777..bf1cb5e64cb 100644 --- a/Lib/asyncio/tools.py +++ b/Lib/asyncio/tools.py @@ -5,7 +5,7 @@ from collections import defaultdict from itertools import count from enum import Enum import sys -from _remotedebugging import get_all_awaited_by +from _remote_debugging import get_all_awaited_by class NodeType(Enum): @@ -21,13 +21,21 @@ class CycleFoundException(Exception): # ─── indexing helpers ─────────────────────────────────────────── +def _format_stack_entry(elem: tuple[str, str, int] | str) -> str: + if isinstance(elem, tuple): + fqname, path, line_no = elem + return f"{fqname} {path}:{line_no}" + + return elem + + def _index(result): id2name, awaits = {}, [] for _thr_id, tasks in result: for tid, tname, awaited in tasks: id2name[tid] = tname for stack, parent_id in awaited: - stack = [elem[0] if isinstance(elem, tuple) else elem for elem in stack] + stack = [_format_stack_entry(elem) for elem in stack] awaits.append((parent_id, stack, tid)) return id2name, awaits @@ -106,7 +114,7 @@ def _find_cycles(graph): # ─── PRINT TREE FUNCTION ─────────────────────────────────────── def build_async_tree(result, task_emoji="(T)", cor_emoji=""): """ - Build a list of strings for pretty-print a async call tree. + Build a list of strings for pretty-print an async call tree. The call tree is produced by `get_all_async_stacks()`, prefixing tasks with `task_emoji` and coroutine frames with `cor_emoji`. @@ -169,7 +177,7 @@ def build_task_table(result): return table def _print_cycle_exception(exception: CycleFoundException): - print("ERROR: await-graph contains cycles – cannot print a tree!", file=sys.stderr) + print("ERROR: await-graph contains cycles - cannot print a tree!", file=sys.stderr) print("", file=sys.stderr) for c in exception.cycles: inames = " → ".join(exception.id2name.get(tid, hex(tid)) for tid in c) |