mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03:00
Optimize BaseSelector.modify(). Patch by Arnaud Faure.
This commit is contained in:
parent
b7038817fe
commit
d0786a1a50
2 changed files with 16 additions and 3 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue