Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from weakref import ref as wref
from gc import get_objects
from greenlet import greenlet
from gevent.exceptions import BlockingSwitchOutError
# In Cython, we define these as 'cdef inline' functions. The
# compilation unit cannot have a direct assignment to them (import
# is assignment) without generating a 'lvalue is not valid target'
# error.
locals()['getcurrent'] = __import__('greenlet').getcurrent
locals()['greenlet_init'] = lambda: None
locals()['_greenlet_switch'] = greenlet.switch
__all__ = [
'TrackedRawGreenlet',
'SwitchOutGreenletWithLoop',
]
class TrackedRawGreenlet(greenlet):
def __init__(self, function, parent):
greenlet.__init__(self, function, parent)
# See greenlet.py's Greenlet class. We capture the cheap
# parts to maintain the tree structure, but we do not capture
# the stack because that's too expensive for 'spawn_raw'.
current = getcurrent() # pylint:disable=undefined-variable
self.spawning_greenlet = wref(current)
def switch(self):
switch_out = getattr(getcurrent(), 'switch_out', None)
if switch_out is not None:
switch_out()
return greenlet.switch(self)
def switch(self):
switch_out = getattr(getcurrent(), 'switch_out', None)
if switch_out is not None:
switch_out()
return greenlet.switch(self)
def switch(self):
switch_out = getattr(getcurrent(), 'switch_out', None)
if switch_out is not None:
switch_out()
return greenlet.switch(self)
def switch(self, *a):
g = greenlet.getcurrent()
if (isinstance(g, GreenletWithDeferred) and
g.finished and g.parent == self):
# control will return to the parent anyway, and switching to it
# causes a memory leak (greenlets don't participate in gc).
if a:
return a[0]
return
return greenlet.greenlet.switch(self, *a)
def switch(self, *a):
g = greenlet.getcurrent()
if (isinstance(g, GreenletWithDeferred) and
g.finished and g.parent == self):
# control will return to the parent anyway, and switching to it
# causes a memory leak (greenlets don't participate in gc).
if a:
return a[0]
return
return greenlet.greenlet.switch(self, *a)