[Twisted-Python] error in pollreactor.py

Jp Calderone exarkun at intarweb.us
Sun Aug 31 17:16:28 EDT 2003

On Sun, Aug 31, 2003 at 04:48:47PM -0400, Eric C. Newton wrote:
> Method _dictRemove of pollreactor.py:
>     def _dictRemove(self, selectable, mdict):
>         try:
>             # the easy way
>             fd = reader.fileno()
>         except:
>             # the hard way: necessary because fileno() may disappear at any
>             # moment, thanks to python's underlying sockets impl
>             for fd, fdes in selectables.items():
>                 if selectable is fdes:
>                     break
>             else:
>                 # Hmm, maybe not the right course of action?  This method can't
>                 # fail, because it happens inside error detection...
>                 return
>         if mdict.has_key(fd):
>             del mdict[fd]
>             self._updateRegistration(fd)
> First, "reader.fileno()" is just not going to work because there is no
> variable "reader" in the scope.  This error is hidden by the
> all-encompassing except clause.  Second, this comment about fileno()
> disappearing... I don't believe it.  Does anyone know if this is
> really true?

  What the comment really means, I believe, is that fileno() may return -1
at any time, without warning.  -1 will never be in the dict, so a lookup for
what the file descriptor used to be must be done.

> Code works (in my limited tests) if changed to:
>     def _dictRemove(self, selectable, mdict):
>         fd = selectable.fileno()
>         if mdict.has_key(fd):
>             del mdict[fd]
>             self._updateRegistration(fd)

  I think you're on the right track, but would use this instead:

    def _dictRemove(self, selectable, mdict):
        fd = selectable.fileno()
        if fd == -1:
            for fd, fdes in selectables.iteritems():
                if fdes is selectable:
                # This should probably be reported.  It should
                # never happen.
            del mdict[fd]
        except KeyError:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20030831/ebfbde78/attachment.pgp 

More information about the Twisted-Python mailing list