Skip to content

Commit

Permalink
Merge pull request #1068 from twisted/9481-delayecall-repr
Browse files Browse the repository at this point in the history
Author: twm
Reviewer: altendky
Fixes: ticket:9481

Change DelayedCall repr to include details
  • Loading branch information
twm committed Sep 26, 2018
2 parents de25f95 + f1e94f6 commit 384eebf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/twisted/internet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DelayedCall:
# enable .debug to record creator call stack, and it will be logged if
# an exception occurs while the function is being run
debug = False
_str = None
_repr = None

def __init__(self, time, func, args, kw, cancel, reset,
seconds=runtimeSeconds):
Expand Down Expand Up @@ -101,7 +101,7 @@ def cancel(self):
self.canceller(self)
self.cancelled = 1
if self.debug:
self._str = str(self)
self._repr = repr(self)
del self.func, self.args, self.kw

def reset(self, secondsFromNow):
Expand Down Expand Up @@ -181,9 +181,15 @@ def __lt__(self, other):
return self.time < other.time


def __str__(self):
if self._str is not None:
return self._str
def __repr__(self):
"""
Implement C{repr()} for L{DelayedCall} instances.
@rtype: C{str}
@returns: String containing details of the L{DelayedCall}.
"""
if self._repr is not None:
return self._repr
if hasattr(self, 'func'):
# This code should be replaced by a utility function in reflect;
# see ticket #6066:
Expand Down
12 changes: 11 additions & 1 deletion src/twisted/internet/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,17 @@ def test_str(self):
self.assertEqual(
str(dc),
"<DelayedCall 0x%x [10.5s] called=0 cancelled=0 nothing(3, A=5)>"
% (id(dc),))
% (id(dc),),
)


def test_repr(self):
"""
The string representation of a L{DelayedCall} instance, as returned by
{repr}, is identical to that returned by L{str}.
"""
dc = DelayedCall(13, nothing, (6, ), {"A": 9}, None, None, lambda: 1.6)
self.assertEqual(str(dc), repr(dc))


def test_lt(self):
Expand Down
1 change: 1 addition & 0 deletions src/twisted/newsfragments/9481.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The repr() of a twisted.internet.base.DelayedCall now encodes the same information as its str(), exposing details of its scheduling and target callable.

0 comments on commit 384eebf

Please sign in to comment.