diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-01-13 09:00:55 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-01-13 09:00:55 (GMT) |
commit | 458fc6f98ca9a838af5dcdff64fd48ae3eb64fa9 (patch) | |
tree | 2f5ebe056493b3bc7ed8931fb9ca141fd150340d /Lib | |
parent | 17cba7daf5cdbe2a0e589be9ef507408b8dc07f1 (diff) | |
parent | 38dc250521d2c34f58537b25301a0ac9a4bb6c85 (diff) | |
download | cpython-458fc6f98ca9a838af5dcdff64fd48ae3eb64fa9.zip cpython-458fc6f98ca9a838af5dcdff64fd48ae3eb64fa9.tar.gz cpython-458fc6f98ca9a838af5dcdff64fd48ae3eb64fa9.tar.bz2 |
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.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/selectors.py | 3 | ||||
-rw-r--r-- | Lib/test/test_selectors.py | 11 |
2 files changed, 10 insertions, 4 deletions
diff --git a/Lib/selectors.py b/Lib/selectors.py index 598845d4..6d569c3 100644 --- a/Lib/selectors.py +++ b/Lib/selectors.py @@ -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 diff --git a/Lib/test/test_selectors.py b/Lib/test/test_selectors.py index 493a5a6..15f71c1 100644 --- a/Lib/test/test_selectors.py +++ b/Lib/test/test_selectors.py @@ -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() |