diff options
author | Guido van Rossum <guido@dropbox.com> | 2013-11-07 16:39:28 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@dropbox.com> | 2013-11-07 16:39:28 (GMT) |
commit | d0786a1a5036c06770f1c958323387fd682e789e (patch) | |
tree | 69a5d2bc9cdea0202c72144e852ad9ec2f4bc590 | |
parent | b7038817fee37fea81183d64c9dd957bab571fde (diff) | |
download | cpython-d0786a1a5036c06770f1c958323387fd682e789e.zip cpython-d0786a1a5036c06770f1c958323387fd682e789e.tar.gz cpython-d0786a1a5036c06770f1c958323387fd682e789e.tar.bz2 |
Optimize BaseSelector.modify(). Patch by Arnaud Faure.
-rw-r--r-- | Lib/selectors.py | 9 | ||||
-rw-r--r-- | Lib/test/test_selectors.py | 10 |
2 files changed, 16 insertions, 3 deletions
diff --git a/Lib/selectors.py b/Lib/selectors.py index 3638e85..3971502 100644 --- a/Lib/selectors.py +++ b/Lib/selectors.py @@ -138,11 +138,14 @@ class BaseSelector(metaclass=ABCMeta): key = self._fd_to_key[_fileobj_to_fd(fileobj)] except KeyError: raise KeyError("{!r} is not registered".format(fileobj)) from None - if events != key.events or data != key.data: - # TODO: If only the data changed, use a shortcut that only - # updates the data. + if events != key.events: self.unregister(fileobj) return self.register(fileobj, events, data) + elif data != key.data: + # Use a shortcut to update the data. + key = key._replace(data=data) + self._fd_to_key[key.fd] = key + return key else: return key diff --git a/Lib/test/test_selectors.py b/Lib/test/test_selectors.py index fd0481d..c64c87a 100644 --- a/Lib/test/test_selectors.py +++ b/Lib/test/test_selectors.py @@ -6,6 +6,7 @@ import socket from test import support from time import sleep import unittest +import unittest.mock try: from time import monotonic as time except ImportError: @@ -124,6 +125,15 @@ class BaseSelectorTestCase(unittest.TestCase): # modify unknown file obj self.assertRaises(KeyError, s.modify, 999999, selectors.EVENT_READ) + # modify use a shortcut + d3 = object() + s.register = unittest.mock.Mock() + s.unregister = unittest.mock.Mock() + + s.modify(rd, selectors.EVENT_READ, d3) + self.assertFalse(s.register.called) + self.assertFalse(s.unregister.called) + def test_close(self): s = self.SELECTOR() self.addCleanup(s.close) |