New submission from David Beazley:

Not so much a bug, but an observation based on reviewing the implementation of 
the selectors.KqueueSelector class.  In that class there is the select() method:

        def select(self, timeout=None):
            timeout = None if timeout is None else max(timeout, 0)
            max_ev = len(self._fd_to_key)
            ready = []
            try:
                kev_list = self._kqueue.control(None, max_ev, timeout)
            except InterruptedError:
                return ready
            for kev in kev_list:
                fd = kev.ident
                flag = kev.filter
                events = 0
                if flag == select.KQ_FILTER_READ:
                    events |= EVENT_READ
                if flag == select.KQ_FILTER_WRITE:
                    events |= EVENT_WRITE

                key = self._key_from_fd(fd)
                if key:
                    ready.append((key, events & key.events))
            return ready

The for-loop looks like it might be checking flags against some kind of 
bit-mask in order to build events.  However, if so, the code just looks wrong.  
Wouldn't it use the '&' operator (or some variant) instead of '==' like this?

            for kev in kev_list:
                fd = kev.ident
                flag = kev.filter
                events = 0
                if flag & select.KQ_FILTER_READ:
                    events |= EVENT_READ
                if flag & select.KQ_FILTER_WRITE:
                    events |= EVENT_WRITE

If it's not a bit-mask, then wouldn't the code be simplified by something like 
this?

            for kev in kev_list:
                fd = kev.ident
                flag = kev.filter
                if flag == select.KQ_FILTER_READ:
                    events = EVENT_READ
                elif flag == select.KQ_FILTER_WRITE:
                    events = EVENT_WRITE


Again, not sure if this is a bug or not. It's just something that looks weirdly 
off.

----------
components: Library (Lib)
messages: 269676
nosy: dabeaz
priority: normal
severity: normal
status: open
title: Strange code in selectors.KqueueSelector
type: enhancement
versions: Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27436>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to