def register(self, fileobj, events, data=None): if (not events) or (events & ~(EVENT_READ | EVENT_WRITE)): raise ValueError("Invalid events: {!r}".format(events)) key = selectors.SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data) if key.fd in self._fd_to_key: raise KeyError("{!r} (FD {}) is already registered".format( fileobj, key.fd)) self._fd_to_key[key.fd] = key if events & EVENT_READ: notifier = QtCore.QSocketNotifier(key.fd, QtCore.QSocketNotifier.Read) notifier.activated.connect(self.__on_read_activated) self.__read_notifiers[key.fd] = notifier if events & EVENT_WRITE: notifier = QtCore.QSocketNotifier(key.fd, QtCore.QSocketNotifier.Write) notifier.activated.connect(self.__on_write_activated) self.__write_notifiers[key.fd] = notifier return key
def test_getitem(self): s = FakeSelector() map = selectors._SelectorMapping(s) f = unittest.mock.Mock() f.fileno.return_value = 10 s.register(f, selectors.EVENT_READ, None) attended = selectors.SelectorKey(f, 10, selectors.EVENT_READ, None) self.assertEqual(attended, map.__getitem__(f))
def test_modify(self): fobj = unittest.mock.Mock() fobj.fileno.return_value = 10 s = FakeSelector() key = s.register(fobj, selectors.EVENT_READ) key2 = s.modify(fobj, selectors.EVENT_WRITE) self.assertNotEqual(key.events, key2.events) self.assertEqual( selectors.SelectorKey(fobj, 10, selectors.EVENT_WRITE, None), s.get_key(fobj))
def test_modify_data(self): fobj = unittest.mock.Mock() fobj.fileno.return_value = 10 d1 = object() d2 = object() s = FakeSelector() key = s.register(fobj, selectors.EVENT_READ, d1) key2 = s.modify(fobj, selectors.EVENT_READ, d2) self.assertEqual(key.events, key2.events) self.assertNotEqual(key.data, key2.data) self.assertEqual( selectors.SelectorKey(fobj, 10, selectors.EVENT_READ, d2), s.get_key(fobj))
def test_selector_key_repr(self): key = selectors.SelectorKey(10, 10, selectors.EVENT_READ, None) self.assertEqual("SelectorKey(fileobj=10, fd=10, events=1, data=None)", repr(key))