[Twisted-Python] Trial __del__ behavior change - intentional?
Scott Lamb
slamb at slamb.org
Mon Oct 9 18:58:52 MDT 2006
I'm seeing a weird thing on Twisted 2.2.0. It does not happen with
older versions of Twisted (trunk revision 16674, which I had sitting
around for some reason). It appears I can't use reference handling to
clean up resources within a trial - the output code apparently keeps
a reference to the stack trace or something until after it's done.
That's too late for me - this resource is essentially a mutex, so if
I have more than one TestCase in a single trial run, my tests don't
work.
Is this change intentional?
My test code's inline, along with results on both versions. I'm not
quite sure how make proper unit tests of trial itself, so
unfortunately you must interpret the results manually.
$ cat resource_test.py
#!/usr/bin/env
"""A test of using __del__ for cleaning up resources within trial tests.
On trunk revision 16674, this works fine. On Twisted 2.2.0, something
holds
an extra reference to my resource object. Python actually exits without
ever calling my __del__.
"""
from twisted.trial import unittest
class MyResource:
def __init__(self, cleaned_up):
print '<<<INIT CALLED>>>'
self.cleaned_up = cleaned_up
def __del__(self):
print '<<<DEL CALLED>>>'
self.cleaned_up[0] = True
def raiseerror(self):
raise Exception('foo')
class ReferenceTest(unittest.TestCase):
def setUp(self):
# This is an array so that the callee can make changes that
alter
# our copy.
self.cleaned_up = [False]
self.my_resource = MyResource(self.cleaned_up)
def tearDown(self):
self.my_resource = None
self.assertEquals(self.cleaned_up[0], True)
def testError(self):
self.my_resource.raiseerror()
def testPass(self):
pass
[machine-a ~]$ svnversion svn/Twisted
16674
[machine-a ~]$ trial resource_test
resource_test
ReferenceTest
<<<INIT CALLED>>>
testError ... <<<DEL CALLED>>>
[ERROR]
<<<INIT CALLED>>>
testPass ... <<<DEL CALLED>>>
[OK]
========================================================================
=======
[ERROR]: resource_test.ReferenceTest.testError
File "twisted/internet/defer.py", line 109, in maybeDeferred
File "/Users/slamb/resource_test.py", line 35, in testError
self.my_resource.raiseerror()
File "/Users/slamb/resource_test.py", line 21, in raiseerror
raise Exception('foo')
exceptions.Exception: foo
------------------------------------------------------------------------
-------
Ran 2 tests in 0.030s
FAILED (errors=1, successes=1)
[machine-b ~]$ rpm -q python-twisted
python-twisted-2.2.0-1
[machine-b ~]$ trial resource_test
Running 2 tests.
resource_test
ReferenceTest
testError ... <<<INIT CALLED>>>
[ERROR]
[ERROR]
tearDown
['ERROR']
testPass ... <<<INIT CALLED>>>
<<<DEL CALLED>>>
[OK]
========================================================================
=======
[ERROR]: resource_test.ReferenceTest.testError
File "/home/slamb/resource_test.py", line 35, in testError
self.my_resource.raiseerror()
File "/home/slamb/resource_test.py", line 21, in raiseerror
raise Exception('foo')
exceptions.Exception: foo
========================================================================
=======
[ERROR]: resource_test.ReferenceTest.testError
File "/home/slamb/resource_test.py", line 32, in tearDown
self.assertEquals(self.cleaned_up[0], True)
twisted.trial.unittest.FailTest: False != True
------------------------------------------------------------------------
-------
Ran 2 tests in 0.055s
FAILED (errors=2, successes=1)
<<<DEL CALLED>>>
--
Scott Lamb <http://www.slamb.org/>
More information about the Twisted-Python
mailing list