mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Issue #19172: Add a get_map() method to selectors.
This commit is contained in:
parent
7613542a27
commit
4574b49703
3 changed files with 61 additions and 1 deletions
|
@ -6,7 +6,7 @@ This module allows high-level and efficient I/O multiplexing, built upon the
|
|||
|
||||
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from collections import namedtuple
|
||||
from collections import namedtuple, Mapping
|
||||
import functools
|
||||
import select
|
||||
import sys
|
||||
|
@ -44,6 +44,25 @@ SelectorKey = namedtuple('SelectorKey', ['fileobj', 'fd', 'events', 'data'])
|
|||
selected event mask and attached data."""
|
||||
|
||||
|
||||
class _SelectorMapping(Mapping):
|
||||
"""Mapping of file objects to selector keys."""
|
||||
|
||||
def __init__(self, selector):
|
||||
self._selector = selector
|
||||
|
||||
def __len__(self):
|
||||
return len(self._selector._fd_to_key)
|
||||
|
||||
def __getitem__(self, fileobj):
|
||||
try:
|
||||
return self._selector._fd_to_key[_fileobj_to_fd(fileobj)]
|
||||
except KeyError:
|
||||
raise KeyError("{!r} is not registered".format(fileobj)) from None
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._selector._fd_to_key)
|
||||
|
||||
|
||||
class BaseSelector(metaclass=ABCMeta):
|
||||
"""Base selector class.
|
||||
|
||||
|
@ -62,6 +81,8 @@ class BaseSelector(metaclass=ABCMeta):
|
|||
def __init__(self):
|
||||
# this maps file descriptors to keys
|
||||
self._fd_to_key = {}
|
||||
# read-only mapping returned by get_map()
|
||||
self._map = _SelectorMapping(self)
|
||||
|
||||
def register(self, fileobj, events, data=None):
|
||||
"""Register a file object.
|
||||
|
@ -162,6 +183,10 @@ class BaseSelector(metaclass=ABCMeta):
|
|||
except KeyError:
|
||||
raise KeyError("{!r} is not registered".format(fileobj)) from None
|
||||
|
||||
def get_map(self):
|
||||
"""Return a mapping of file objects to selector keys."""
|
||||
return self._map
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue