Commit 64132014 authored by Jason Madden's avatar Jason Madden

cleanup greenlet.py

parent 640867cb
...@@ -24,7 +24,7 @@ __all__ = ['Greenlet', ...@@ -24,7 +24,7 @@ __all__ = ['Greenlet',
if PYPY: if PYPY:
import _continuation import _continuation # pylint:disable=import-error
_continulet = _continuation.continulet _continulet = _continuation.continulet
...@@ -103,6 +103,7 @@ class _lazy(object): ...@@ -103,6 +103,7 @@ class _lazy(object):
class Greenlet(greenlet): class Greenlet(greenlet):
"""A light-weight cooperatively-scheduled execution unit. """A light-weight cooperatively-scheduled execution unit.
""" """
# pylint:disable=too-many-public-methods,too-many-instance-attributes
value = None value = None
_exc_info = () _exc_info = ()
...@@ -288,27 +289,29 @@ class Greenlet(greenlet): ...@@ -288,27 +289,29 @@ class Greenlet(greenlet):
result += ': ' + formatted result += ': ' + formatted
return result + '>' return result + '>'
_formatted_info = None
def _formatinfo(self): def _formatinfo(self):
try: info = self._formatted_info
return self._formatted_info if info is not None:
except AttributeError: return info
pass
try: try:
result = getfuncname(self.__dict__['_run']) result = getfuncname(self.__dict__['_run'])
except Exception: except Exception: # pylint:disable=broad-except
pass # Don't cache
else: return ''
args = []
if self.args: args = []
args = [repr(x)[:50] for x in self.args] if self.args:
if self._kwargs: args = [repr(x)[:50] for x in self.args]
args.extend(['%s=%s' % (key, repr(value)[:50]) for (key, value) in self._kwargs.items()]) if self._kwargs:
if args: args.extend(['%s=%s' % (key, repr(value)[:50]) for (key, value) in self._kwargs.items()])
result += '(' + ', '.join(args) + ')' if args:
# it is important to save the result here, because once the greenlet exits '_run' attribute will be removed result += '(' + ', '.join(args) + ')'
self._formatted_info = result # it is important to save the result here, because once the greenlet exits '_run' attribute will be removed
return result self._formatted_info = result
return '' return result
@property @property
def exception(self): def exception(self):
...@@ -532,7 +535,7 @@ class Greenlet(greenlet): ...@@ -532,7 +535,7 @@ class Greenlet(greenlet):
try: try:
result = self._run(*self.args, **self.kwargs) result = self._run(*self.args, **self.kwargs)
except: except: # pylint:disable=bare-except
self._report_error(sys.exc_info()) self._report_error(sys.exc_info())
return return
self._report_result(result) self._report_result(result)
...@@ -548,6 +551,8 @@ class Greenlet(greenlet): ...@@ -548,6 +551,8 @@ class Greenlet(greenlet):
Previously, if no callable object was passed to the constructor, the spawned greenlet would Previously, if no callable object was passed to the constructor, the spawned greenlet would
later fail with an AttributeError. later fail with an AttributeError.
""" """
# We usually override this in __init__
# pylint: disable=method-hidden
return return
def rawlink(self, callback): def rawlink(self, callback):
...@@ -559,7 +564,7 @@ class Greenlet(greenlet): ...@@ -559,7 +564,7 @@ class Greenlet(greenlet):
""" """
if not callable(callback): if not callable(callback):
raise TypeError('Expected callable: %r' % (callback, )) raise TypeError('Expected callable: %r' % (callback, ))
self._links.append(callback) self._links.append(callback) # pylint:disable=no-member
if self.ready() and self._links and not self._notifier: if self.ready() and self._links and not self._notifier:
self._notifier = self.parent.loop.run_callback(self._notify_links) self._notifier = self.parent.loop.run_callback(self._notify_links)
...@@ -569,29 +574,34 @@ class Greenlet(greenlet): ...@@ -569,29 +574,34 @@ class Greenlet(greenlet):
The *callback* will be called with this instance as an argument The *callback* will be called with this instance as an argument
once this greenlet's dead. A callable is called in its own greenlet. once this greenlet's dead. A callable is called in its own greenlet.
""" """
# XXX: Is the redefinition of SpawnedLink supposed to just be an
# optimization, or do people use it? It's not documented
# pylint:disable=redefined-outer-name
self.rawlink(SpawnedLink(callback)) self.rawlink(SpawnedLink(callback))
def unlink(self, callback): def unlink(self, callback):
"""Remove the callback set by :meth:`link` or :meth:`rawlink`""" """Remove the callback set by :meth:`link` or :meth:`rawlink`"""
try: try:
self._links.remove(callback) self._links.remove(callback) # pylint:disable=no-member
except ValueError: except ValueError:
pass pass
def link_value(self, callback, SpawnedLink=SuccessSpawnedLink): def link_value(self, callback, SpawnedLink=SuccessSpawnedLink):
"""Like :meth:`link` but *callback* is only notified when the greenlet has completed successfully.""" """Like :meth:`link` but *callback* is only notified when the greenlet has completed successfully."""
# pylint:disable=redefined-outer-name
self.link(callback, SpawnedLink=SpawnedLink) self.link(callback, SpawnedLink=SpawnedLink)
def link_exception(self, callback, SpawnedLink=FailureSpawnedLink): def link_exception(self, callback, SpawnedLink=FailureSpawnedLink):
"""Like :meth:`link` but *callback* is only notified when the greenlet dies because of an unhandled exception.""" """Like :meth:`link` but *callback* is only notified when the greenlet dies because of an unhandled exception."""
# pylint:disable=redefined-outer-name
self.link(callback, SpawnedLink=SpawnedLink) self.link(callback, SpawnedLink=SpawnedLink)
def _notify_links(self): def _notify_links(self):
while self._links: while self._links:
link = self._links.popleft() link = self._links.popleft() # pylint:disable=no-member
try: try:
link(self) link(self)
except: except: # pylint:disable=bare-except
self.parent.handle_error((link, self), *sys.exc_info()) self.parent.handle_error((link, self), *sys.exc_info())
...@@ -602,18 +612,21 @@ class _dummy_event(object): ...@@ -602,18 +612,21 @@ class _dummy_event(object):
def stop(self): def stop(self):
pass pass
def start(self, cb): # pylint:disable=unused-argument
raise AssertionError("Cannot start the dummy event")
_cancelled_start_event = _dummy_event() _cancelled_start_event = _dummy_event()
_start_completed_event = _dummy_event() _start_completed_event = _dummy_event()
del _dummy_event del _dummy_event
def _kill(greenlet, exception, waiter): def _kill(glet, exception, waiter):
try: try:
greenlet.throw(exception) glet.throw(exception)
except: except: # pylint:disable=bare-except
# XXX do we need this here? # XXX do we need this here?
greenlet.parent.handle_error(greenlet, *sys.exc_info()) glet.parent.handle_error(glet, *sys.exc_info())
if waiter is not None: if waiter is not None:
waiter.switch() waiter.switch()
...@@ -647,7 +660,7 @@ def _killall3(greenlets, exception, waiter): ...@@ -647,7 +660,7 @@ def _killall3(greenlets, exception, waiter):
if not g.dead: if not g.dead:
try: try:
g.throw(exception) g.throw(exception)
except: except: # pylint:disable=bare-except
g.parent.handle_error(g, *sys.exc_info()) g.parent.handle_error(g, *sys.exc_info())
if not g.dead: if not g.dead:
diehards.append(g) diehards.append(g)
...@@ -659,7 +672,7 @@ def _killall(greenlets, exception): ...@@ -659,7 +672,7 @@ def _killall(greenlets, exception):
if not g.dead: if not g.dead:
try: try:
g.throw(exception) g.throw(exception)
except: except: # pylint:disable=bare-except
g.parent.handle_error(g, *sys.exc_info()) g.parent.handle_error(g, *sys.exc_info())
......
[pep8] [pep8]
# N801: class names should use CapWords # N801: class names should use CapWords
# N802: function names should be lower-case; comes from Windows funcs # N802: function names should be lower-case; comes from Windows funcs
ignore=E702,E265,E402,E731,E266,E261,W503,E129,N801,N802 # N803: argument name should be lower-case; comes up with using the class name as a keyword-argument
ignore=E702,E265,E402,E731,E266,E261,W503,E129,N801,N802,N803
max_line_length=160 max_line_length=160
exclude=.runtimes,.eggs,.tox,.git,build,2.6,2.7,2.7pypy,3.3,3.5,test_support.py,test_queue.py,patched_tests_setup.py,test_threading_2.py,lock_tests.py,_sslgte279.py,3.4 exclude=.runtimes,.eggs,.tox,.git,build,2.6,2.7,2.7pypy,3.3,3.5,test_support.py,test_queue.py,patched_tests_setup.py,test_threading_2.py,lock_tests.py,_sslgte279.py,3.4
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment