[Python-checkins] cpython (3.4): asyncio, Tulip issue 206: In debug mode, keep the callback in the

victor.stinner python-checkins at python.org
Wed Sep 17 23:26:55 CEST 2014


http://hg.python.org/cpython/rev/e2869887e6c2
changeset:   92456:e2869887e6c2
branch:      3.4
parent:      92454:ba86978c8ab5
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Sep 17 23:24:13 2014 +0200
summary:
  asyncio, Tulip issue 206: In debug mode, keep the callback in the
representation of Handle and TimerHandle after cancel().

files:
  Lib/asyncio/events.py                |  32 +++++++++------
  Lib/test/test_asyncio/test_events.py |   9 ++--
  2 files changed, 24 insertions(+), 17 deletions(-)


diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -73,7 +73,7 @@
     """Object returned by callback registration methods."""
 
     __slots__ = ('_callback', '_args', '_cancelled', '_loop',
-                 '_source_traceback', '__weakref__')
+                 '_source_traceback', '_repr', '__weakref__')
 
     def __init__(self, callback, args, loop):
         assert not isinstance(callback, Handle), 'A Handle is not a callback'
@@ -81,12 +81,13 @@
         self._callback = callback
         self._args = args
         self._cancelled = False
+        self._repr = None
         if self._loop.get_debug():
             self._source_traceback = traceback.extract_stack(sys._getframe(1))
         else:
             self._source_traceback = None
 
-    def __repr__(self):
+    def _repr_info(self):
         info = [self.__class__.__name__]
         if self._cancelled:
             info.append('cancelled')
@@ -95,10 +96,21 @@
         if self._source_traceback:
             frame = self._source_traceback[-1]
             info.append('created at %s:%s' % (frame[0], frame[1]))
+        return info
+
+    def __repr__(self):
+        if self._repr is not None:
+            return self._repr
+        info = self._repr_info()
         return '<%s>' % ' '.join(info)
 
     def cancel(self):
         self._cancelled = True
+        if self._loop.get_debug():
+            # Keep a representation in debug mode to keep callback and
+            # parameters. For example, to log the warning "Executing <Handle
+            # ...> took 2.5 second"
+            self._repr = repr(self)
         self._callback = None
         self._args = None
 
@@ -131,17 +143,11 @@
             del self._source_traceback[-1]
         self._when = when
 
-    def __repr__(self):
-        info = []
-        if self._cancelled:
-            info.append('cancelled')
-        info.append('when=%s' % self._when)
-        if self._callback is not None:
-            info.append(_format_callback(self._callback, self._args))
-        if self._source_traceback:
-            frame = self._source_traceback[-1]
-            info.append('created at %s:%s' % (frame[0], frame[1]))
-        return '<%s %s>' % (self.__class__.__name__, ' '.join(info))
+    def _repr_info(self):
+        info = super()._repr_info()
+        pos = 2 if self._cancelled else 1
+        info.insert(pos, 'when=%s' % self._when)
+        return info
 
     def __hash__(self):
         return hash(self._when)
diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -1891,8 +1891,8 @@
         # cancelled handle
         h.cancel()
         self.assertEqual(repr(h),
-                        '<Handle cancelled created at %s:%s>'
-                        % (create_filename, create_lineno))
+                        '<Handle cancelled noop(1, 2) at %s:%s created at %s:%s>'
+                        % (filename, lineno, create_filename, create_lineno))
 
     def test_handle_source_traceback(self):
         loop = asyncio.get_event_loop_policy().new_event_loop()
@@ -1987,8 +1987,9 @@
         # cancelled handle
         h.cancel()
         self.assertEqual(repr(h),
-                        '<TimerHandle cancelled when=123 created at %s:%s>'
-                        % (create_filename, create_lineno))
+                        '<TimerHandle cancelled when=123 noop() '
+                        'at %s:%s created at %s:%s>'
+                        % (filename, lineno, create_filename, create_lineno))
 
 
     def test_timer_comparison(self):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list