Skip to content

Commit

Permalink
Merge pull request #1637 from graingert/revert-loopingcall-delayedcal…
Browse files Browse the repository at this point in the history
…l-repr

10235  accept non-function callables in LoopingCall and DeferLater
  • Loading branch information
graingert authored and adiroiban committed Jul 23, 2021
1 parent c02a290 commit e4e41db
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/twisted/internet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,28 @@ def __repr__(self) -> str:
"""
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:
func = getattr(self.func, "__qualname__", None)
if func is None:
func = getattr(self.func, "__name__", None)
if func is not None:
imClass = getattr(self.func, "im_class", None)
if imClass is not None:
func = f"{imClass}.{func}"
if func is None:
func = reflect.safe_repr(self.func)
else:
func = None

now = self.seconds()
L = [
"<DelayedCall 0x%x [%ss] called=%s cancelled=%s"
% (id(self), self.time - now, self.called, self.cancelled)
]
if hasattr(self, "func"):
L.extend((" ", self.func.__qualname__, "("))
if func is not None:
L.extend((" ", func, "("))
if self.args:
L.append(", ".join([reflect.safe_repr(e) for e in self.args]))
if self.kw:
Expand Down
14 changes: 13 additions & 1 deletion src/twisted/internet/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,21 @@ def howLong() -> float:
self.call = self.clock.callLater(howLong(), self)

def __repr__(self) -> str:
# This code should be replaced by a utility function in reflect;
# see ticket #6066:
func = getattr(self.f, "__qualname__", None)
if func is None:
func = getattr(self.f, "__name__", None)
if func is not None:
imClass = getattr(self.f, "im_class", None)
if imClass is not None:
func = f"{imClass}.{func}"
if func is None:
func = reflect.safe_repr(self.f)

return "LoopingCall<{!r}>({}, *{}, **{})".format(
self.interval,
self.f.__qualname__,
func,
reflect.safe_repr(self.a),
reflect.safe_repr(self.kw),
)
Expand Down
1 change: 1 addition & 0 deletions src/twisted/newsfragments/10235.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
revert changes to ``DelayedCall.__repr__`` and ``LoopingCall.__repr__``

0 comments on commit e4e41db

Please sign in to comment.