jaraco / irc

Full-featured Python IRC library for Python.
MIT License
392 stars 87 forks source link

PrioritizedHandler not sortable #14

Closed jaraco closed 8 years ago

jaraco commented 8 years ago

Playing around with event handlers, you may encounter this error:

File "\irc\client.py", line 292 in add_global_handler

bisect.insort(event_handlers, handler)

TypeError: unorderable types: method() < method()

To fix it, replace this:

#!python

PrioritizedHandler = collections.namedtuple('PrioritizedHandler',
    ('priority', 'callback'))

with:

#!python

PrioritizedHandlerBase = collections.namedtuple('PrioritizedHandlerBase',
    ('priority', 'callback'))

class PrioritizedHandler(PrioritizedHandlerBase):
    def __lt__(self, other):
        return (self.priority < other.priority)

jaraco commented 8 years ago

Aah. Good catch. If the priorities are the same, then the comparison will compare the callbacks. Thanks for the report.


Original comment by: Jason R. Coombs

jaraco commented 8 years ago

I believe this issue only exists on Python 3. On Python 2, functions and methods are orderable, so the comparison allowed the callback to be used in the comparison.


Original comment by: Jason R. Coombs

jaraco commented 8 years ago

Code for reproducing

#!python

import irc.client

class class1(object):
    def get_handler(self):
        return getattr(self, "_on_join")

    def _on_join(self, connection, event):
        pass

class class2(object):
    def get_handler(self):
        return getattr(self, "_on_join")

    def _on_join(self, connection, event):
        pass

c1 = class1()
c2 = class2()
client = irc.client.SimpleIRCClient()
client.ircobj.add_global_handler("join", c1.get_handler())
client.ircobj.add_global_handler("join", c2.get_handler())

Original comment by: coffeeburrito

jaraco commented 8 years ago

Also yes, Python3 :)


Original comment by: coffeeburrito

jaraco commented 8 years ago

Fix errors when handlers of the same priority are added under Python 3. Also fixes the unintended behavior of allowing handlers of the same priority to compare as unequal. Fixes #14.

→ <>


Original comment by: Jason R. Coombs

jaraco commented 8 years ago

Released as 8.0.1. Enjoy.


Original comment by: Jason R. Coombs

jaraco commented 8 years ago

Thanks bro


Original comment by: coffeeburrito