diff options
author | Guido van Rossum <guido@python.org> | 2013-10-30 12:49:42 -0700 |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2013-10-30 12:49:42 -0700 |
commit | d53c1c7f83087904fdb0b1195f5dc439811da7d7 (patch) | |
tree | 6cba6116822f15432ca82fada1cd1fb7ec4239b1 /asyncio/selectors.py | |
parent | d005ef7caa7ae08271c050c0d0bba51d95c22763 (diff) | |
download | trollius-d53c1c7f83087904fdb0b1195f5dc439811da7d7.tar.gz |
Update selectors.py from cpython: add get_map() method (Natali + Pitrou).
Diffstat (limited to 'asyncio/selectors.py')
-rw-r--r-- | asyncio/selectors.py | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/asyncio/selectors.py b/asyncio/selectors.py index fe027f0..3e6c2ad 100644 --- a/asyncio/selectors.py +++ b/asyncio/selectors.py @@ -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 |