/usr/lib/python2.7/dist-packages/traits/tests/test_regression.py is in python-traits 4.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | """ General regression tests for a variety of bugs. """
import gc
import sys
from ..has_traits import HasTraits, Property, on_trait_change
from ..trait_types import Bool, DelegatesTo, Instance, Int, List
from ..testing.unittest_tools import unittest
class Dummy(HasTraits):
x = Int(10)
def _create_subclass():
class Subclass(HasTraits):
pass
return Subclass
class Dummy2(HasTraits):
y = Int(20)
dummy = Instance(Dummy)
class DelegateMess(HasTraits):
dummy1 = Instance(Dummy, args=())
dummy2 = Instance(Dummy2)
y = DelegatesTo('dummy2')
handler_called = Bool(False)
def _dummy2_default(self):
# Create `self.dummy1`
return Dummy2(dummy=self.dummy1)
@on_trait_change('dummy1.x')
def _on_dummy1_x(self):
self.handler_called = True
def _init_trait_listeners(self):
""" Force the DelegatesTo listener to hook up first to exercise the
worst case.
"""
for name in ['y', '_on_dummy1_x']:
data = self.__class__.__listener_traits__[name]
getattr(self, '_init_trait_%s_listener' % data[0])(name, *data)
class DelegateLeak(HasTraits):
visible = Property(Bool, depends_on='can_enable')
can_enable = DelegatesTo('flag', prefix='x')
flag = Instance(Dummy, kw={'x': 42})
class Presenter(HasTraits):
obj = Instance(Dummy)
y = Property(Int(), depends_on='obj.x')
def _get_y(self):
return self.obj.x
class ListUpdatesTest(HasTraits):
a = List
b = List
events_received = Int(0)
@on_trait_change('a[], b[]')
def _receive_events(self):
self.events_received += 1
class TestRegression(unittest.TestCase):
def test_default_value_for_no_cache(self):
""" Make sure that CTrait.default_value_for() does not cache the
result.
"""
dummy = Dummy()
# Nothing in the __dict__ yet.
self.assertEqual(dummy.__dict__, {})
ctrait = dummy.trait('x')
default = ctrait.default_value_for(dummy, 'x')
self.assertEqual(default, 10)
self.assertEqual(dummy.__dict__, {})
def test_subclasses_weakref(self):
""" Make sure that dynamically created subclasses are not held
strongly by HasTraits.
"""
previous_subclasses = HasTraits.__subclasses__()
_create_subclass()
_create_subclass()
_create_subclass()
_create_subclass()
gc.collect()
self.assertEqual(previous_subclasses, HasTraits.__subclasses__())
def test_leaked_property_tuple(self):
""" the property ctrait constructor shouldn't leak a tuple. """
class A(HasTraits):
prop = Property()
a = A()
self.assertEqual(sys.getrefcount(a.trait('prop').property()), 1)
def test_delegate_initializer(self):
mess = DelegateMess()
self.assertFalse(mess.handler_called)
mess.dummy1.x = 20
self.assertTrue(mess.handler_called)
def test_no_leaking_notifiers(self):
""" Extended trait change notifications should not leaf
TraitChangeNotifyWrappers.
"""
dummy = Dummy()
ctrait = dummy._trait('x', 2)
self.assertEqual(len(ctrait._notifiers(1)), 0)
presenter = Presenter(obj=dummy)
self.assertEqual(len(ctrait._notifiers(1)), 1)
del presenter
self.assertEqual(len(ctrait._notifiers(1)), 0)
def test_init_list_depends(self):
""" Using two lists with bracket notation in extended name notation
should not raise an error.
"""
list_test = ListUpdatesTest()
# Updates to list items and the list trait itself should be counted.
list_test.a.append(0)
list_test.b = [1, 2, 3]
list_test.b[0] = 0
self.assertEqual(list_test.events_received, 3)
def test_has_traits_notifiers_refleak(self):
# Regression test for issue described in
# https://github.com/enthought/traits/pull/248
warmup = 5
cycles = 10
counts = []
def handler():
pass
def f():
obj = HasTraits()
obj.on_trait_change(handler)
# Warmup.
for _ in xrange(cycles):
f()
gc.collect()
counts.append(len(gc.get_objects()))
# All the counts beyond the warmup period should be the same.
self.assertEqual(counts[warmup:-1], counts[warmup+1:])
def test_delegation_refleak(self):
warmup = 5
cycles = 10
counts = []
for _ in xrange(cycles):
DelegateLeak()
gc.collect()
counts.append(len(gc.get_objects()))
# All the counts should be the same.
self.assertEqual(counts[warmup:-1], counts[warmup+1:])
if __name__ == '__main__':
unittest.main()
|