Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9481] Change DelayedCall repr to include details #1068

Merged
merged 9 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.