1

I ran into a peculiar issue with gevent and greenlet that I can't troubleshoot. I'm trying to overload an operator for convenience in a subclass of gevent.Greenlet:

import gevent

class Actor(gevent.Greenlet):

    # other stuff..

    def __or__(self, other):
        print "Hello from or!"

class Echo(Actor):
    pass

a = Actor()
b = Echo()

# This works!
print a.__or__(Echo())

# This doesn't!!
print a | b

This is the output:

$ python gtest.py
Hello from or!
None
Traceback (most recent call last):
  File "gtest.py", line 20, in <module>
    print a | b
TypeError: unsupported operand type(s) for |: 'Actor' and 'Echo'

I looked at the source code for gevent.Greenlet but couldn't see why it would disallow operator overloading. I feel like there is some meta-programming black magic going on. Does anyone have an idea?

user1496984
  • 10,957
  • 8
  • 37
  • 46
  • I'm stumped - but it does seem to be `greenlet` specific and oddly `a | a` and `b | b` work fine. Note that `gevent.Greenlet` subclasses `greenlet.greenlet` which is a C extension module which makes it a bit harder to debug. – Jason S Aug 09 '14 at 07:19
  • As long as the two operands are of the same type (both `Echo` or both `Actor`), it seems to work. But somehow it fails when the two are of different type.. – user1496984 Aug 09 '14 at 07:35

0 Answers0