RuntimeError if the selector is closed. And selectors.BaseSelector.close() now clears its internal reference to the selector mapping to break a reference cycle. Initial patch written by Martin Richard.
This commit is contained in:
commit
458fc6f98c
|
@ -174,6 +174,8 @@ class BaseSelector(metaclass=ABCMeta):
|
|||
SelectorKey for this file object
|
||||
"""
|
||||
mapping = self.get_map()
|
||||
if mapping is None:
|
||||
raise RuntimeError('Selector is closed')
|
||||
try:
|
||||
return mapping[fileobj]
|
||||
except KeyError:
|
||||
|
@ -256,6 +258,7 @@ class _BaseSelectorImpl(BaseSelector):
|
|||
|
||||
def close(self):
|
||||
self._fd_to_key.clear()
|
||||
self._map = None
|
||||
|
||||
def get_map(self):
|
||||
return self._map
|
||||
|
|
|
@ -178,14 +178,17 @@ class BaseSelectorTestCase(unittest.TestCase):
|
|||
s = self.SELECTOR()
|
||||
self.addCleanup(s.close)
|
||||
|
||||
mapping = s.get_map()
|
||||
rd, wr = self.make_socketpair()
|
||||
|
||||
s.register(rd, selectors.EVENT_READ)
|
||||
s.register(wr, selectors.EVENT_WRITE)
|
||||
|
||||
s.close()
|
||||
self.assertRaises(KeyError, s.get_key, rd)
|
||||
self.assertRaises(KeyError, s.get_key, wr)
|
||||
self.assertRaises(RuntimeError, s.get_key, rd)
|
||||
self.assertRaises(RuntimeError, s.get_key, wr)
|
||||
self.assertRaises(KeyError, mapping.__getitem__, rd)
|
||||
self.assertRaises(KeyError, mapping.__getitem__, wr)
|
||||
|
||||
def test_get_key(self):
|
||||
s = self.SELECTOR()
|
||||
|
@ -252,8 +255,8 @@ class BaseSelectorTestCase(unittest.TestCase):
|
|||
sel.register(rd, selectors.EVENT_READ)
|
||||
sel.register(wr, selectors.EVENT_WRITE)
|
||||
|
||||
self.assertRaises(KeyError, s.get_key, rd)
|
||||
self.assertRaises(KeyError, s.get_key, wr)
|
||||
self.assertRaises(RuntimeError, s.get_key, rd)
|
||||
self.assertRaises(RuntimeError, s.get_key, wr)
|
||||
|
||||
def test_fileno(self):
|
||||
s = self.SELECTOR()
|
||||
|
|
|
@ -203,6 +203,11 @@ Core and Builtins
|
|||
Library
|
||||
-------
|
||||
|
||||
- Issue #23209, #23225: selectors.BaseSelector.get_key() now raises a
|
||||
RuntimeError if the selector is closed. And selectors.BaseSelector.close()
|
||||
now clears its internal reference to the selector mapping to break a
|
||||
reference cycle. Initial patch written by Martin Richard.
|
||||
|
||||
- Issue #19777: Provide a home() classmethod on Path objects. Contributed
|
||||
by Victor Salgado and Mayank Tripathi.
|
||||
|
||||
|
|
Loading…
Reference in New Issue