Repository: twisted/twisted

Issue:
AttributeError: 'DelayedCall' object has no attribute 'creator'
|<img alt="allenap's avatar" src="https://avatars.githubusercontent.com/u/0?s=50" width="50" height="50">| allenap reported|
|-|-|
|Trac ID|trac#8306|
|Type|defect|
|Created|2016-04-26 16:04:05Z|

```
Python 3.5.1+ (default, Mar 30 2016, 22:46:26) 
[GCC 5.3.1 20160330] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from twisted.internet.base import DelayedCall
>>> dc = DelayedCall(1, lambda: None, (), {}, lambda dc: None, lambda dc: None)
>>> dc.debug = True
>>> str(dc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../twisted/internet/base.py", line 206, in __str__
    L.append("\n\ntraceback at creation: \n\n%s" % ('    '.join(self.creator)))
AttributeError: 'DelayedCall' object has no attribute 'creator'
```

```
$ tail -n1 twisted/_version.py 
version = versions.Version('twisted', 16, 1, 1)
```


<details><summary>Searchable metadata</summary>

```
trac-id__8306 8306
type__defect defect
reporter__allenap allenap
priority__normal normal
milestone__None None
branch__ 
branch_author__ 
status__new new
resolution__None None
component__core core
keywords__None None
time__1461686645106304 1461686645106304
changetime__1462290530743285 1462290530743285
version__None None
owner__None None

```
</details>



Discussion:
|[<img alt="pawelmhm's avatar" src="https://avatars.githubusercontent.com/u/2700942?s=50" width="50" height="50">](https://github.com/pawelmhm)<a name="note_1"></a>|@pawelmhm commented|
|-|-|

seems like this is caused by [#8110](https://github.com/twisted/twisted/issues/8110) - when I remove dc.debug = True line I dont get any excception

Resolving patch:
diff --git a/src/twisted/internet/base.py b/src/twisted/internet/base.py
--- a/src/twisted/internet/base.py
+++ b/src/twisted/internet/base.py
@@ -80,6 +80,9 @@ class DelayedCall:
     debug = False
     _repr: Optional[str] = None
 
+    # In debug mode, the call stack at the time of instantiation.
+    creator: Optional[Sequence[str]] = None
+
     def __init__(
         self,
         time: float,
@@ -265,7 +268,7 @@ def __repr__(self) -> str:
                 )
             L.append(")")
 
-        if self.debug:
+        if self.creator is not None:
             L.append("\n\ntraceback at creation: \n\n%s" % ("    ".join(self.creator)))
         L.append(">")
 
@@ -990,8 +993,8 @@ def runUntilCurrent(self) -> None:
                 call.called = 1
                 call.func(*call.args, **call.kw)
             except BaseException:
-                log.deferr()
-                if hasattr(call, "creator"):
+                log.err()
+                if call.creator is not None:
                     e = "\n"
                     e += (
                         " C: previous exception occurred in "


Test patch:
diff --git a/src/twisted/internet/test/test_base.py b/src/twisted/internet/test/test_base.py
--- a/src/twisted/internet/test/test_base.py
+++ b/src/twisted/internet/test/test_base.py
@@ -356,6 +356,17 @@ def test_str(self):
         )
         self.assertEqual(str(dc), expected)
 
+    def test_switchToDebug(self):
+        """
+        If L{DelayedCall.debug} changes from C{0} to C{1} between
+        L{DelayeCall.__init__} and L{DelayedCall.__repr__} then
+        L{DelayedCall.__repr__} returns a string that does not include the
+        creator stack.
+        """
+        dc = DelayedCall(3, lambda: None, (), {}, nothing, nothing, lambda: 2)
+        dc.debug = 1
+        self.assertNotIn("traceback at creation", repr(dc))
+
 
 class DelayedCallDebugTests(DelayedCallMixin, TestCase):
     """
@@ -383,6 +394,17 @@ def test_str(self):
         )
         self.assertRegex(str(dc), expectedRegexp)
 
+    def test_switchFromDebug(self):
+        """
+        If L{DelayedCall.debug} changes from C{1} to C{0} between
+        L{DelayeCall.__init__} and L{DelayedCall.__repr__} then
+        L{DelayedCall.__repr__} returns a string that includes the creator
+        stack (we captured it, we might as well display it).
+        """
+        dc = DelayedCall(3, lambda: None, (), {}, nothing, nothing, lambda: 2)
+        dc.debug = 0
+        self.assertIn("traceback at creation", repr(dc))
+
 
 class TestSpySignalCapturingReactor(ReactorBase):