/usr/lib/python2.7/dist-packages/traits/tests/test_trait_cycle.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 | #------------------------------------------------------------------------------
#
# Copyright (c) 2007, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in /LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
#------------------------------------------------------------------------------
""" Test whether HasTraits objects with cycles can be garbage collected.
"""
from __future__ import absolute_import
import gc
import time
from traits.testing.unittest_tools import unittest
# Enthought library imports
from ..api import HasTraits, Any, DelegatesTo, Instance, Int
class TestCase(unittest.TestCase):
def _simple_cycle_helper(self, foo_class):
""" Can the garbage collector clean up a cycle with traits objects?
"""
# Create two Foo objects that refer to each other.
first = foo_class()
second = foo_class(child=first)
first.child = second
# get their ids
foo_ids = [id(first), id(second)]
# delete the items so that they can be garbage collected
del first, second
# tell the garbage collector to pick up the litter.
gc.collect()
# Now grab all objects in the process and ask for their ids
all_ids = [id(obj) for obj in gc.get_objects()]
# Ensure that neither of the Foo object ids are in this list
for foo_id in foo_ids:
self.assertTrue(foo_id not in all_ids)
def test_simple_cycle_oldstyle_class(self):
""" Can the garbage collector clean up a cycle with old style class?
"""
class Foo:
def __init__(self, child=None):
self.child = child
self._simple_cycle_helper(Foo)
def test_simple_cycle_newstyle_class(self):
""" Can the garbage collector clean up a cycle with new style class?
"""
class Foo(object):
def __init__(self, child=None):
self.child = child
self._simple_cycle_helper(Foo)
def test_simple_cycle_hastraits(self):
""" Can the garbage collector clean up a cycle with traits objects?
"""
class Foo(HasTraits):
child = Any
self._simple_cycle_helper(Foo)
def test_reference_to_trait_dict(self):
""" Does a HasTraits object refer to its __dict__ object?
This test may point to why the previous one fails. Even if it
doesn't, the functionality is needed for detecting problems
with memory in debug.memory_tracker
"""
class Foo(HasTraits):
child = Any
foo = Foo()
# It seems like foo sometimes has not finished construction yet, so
# the frame found by referrers is not _exactly_ the same as Foo(). For
# more information, see the gc doc: http://docs.python.org/lib/module-
# gc.html
#
# The documentation says that this (get_referrers) should be used for
# no purpose other than debugging, so this is really not a good way to
# test the code.
time.sleep(0.1)
referrers = gc.get_referrers(foo.__dict__)
self.assertTrue(len(referrers) > 0)
self.assertTrue(foo in referrers)
def test_delegates_to(self):
""" Tests if an object that delegates to another is freed.
"""
class Base(HasTraits):
""" Object we are delegating to. """
i = Int
class Delegates(HasTraits):
""" Object that delegates. """
b = Instance(Base)
i = DelegatesTo('b')
# Make a pair of object
b = Base()
d = Delegates(b=b)
# Delete d and thoroughly collect garbage
del d
for i in range(3):
gc.collect(2)
# See if we still have a Delegates
ds = [obj for obj in gc.get_objects() if isinstance(obj, Delegates)]
self.assertEqual(ds, [])
if __name__ == '__main__':
unittest.main()
|