/usr/lib/python2.7/dist-packages/traits/tests/test_trait_list_dict.py is in python-traits 4.5.0-1ubuntu2.
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 | ##############################################################################
# Copyright 2014 Enthought, Inc.
##############################################################################
""" Test the persistence behavior of TraitListObjects, TraitDictObjects and
TraitSetObjects.
"""
from __future__ import absolute_import
import copy
from cPickle import dumps, loads
from ..has_traits import HasTraits, on_trait_change
from ..trait_types import Dict, List, Set, Str, Int, Instance
class A(HasTraits):
alist = List(Int, range(5))
adict = Dict(Str, Int, dict(a=1, b=2))
aset = Set(Int, range(5))
events = List()
@on_trait_change('alist_items,adict_items,aset_items')
def _receive_events(self, object, name, old, new):
self.events.append((name, new))
class B(HasTraits):
dict = Dict(Str, Instance(A))
def test_trait_list_object_persists():
a = A()
list = loads(dumps(a.alist))
assert list.object() is None
list.append(10)
assert len(a.events) == 0
a.alist.append(20)
assert len(a.events) == 1
list2 = loads(dumps(list))
assert list2.object() is None
def test_trait_dict_object_persists():
a = A()
dict = loads(dumps(a.adict))
assert dict.object() is None
dict['key'] = 10
assert len(a.events) == 0
a.adict['key'] = 10
assert len(a.events) == 1
dict2 = loads(dumps(dict))
assert dict2.object() is None
def test_trait_set_object_persists():
a = A()
set = loads(dumps(a.aset))
assert set.object() is None
set.add(10)
assert len(a.events) == 0
a.aset.add(20)
assert len(a.events) == 1
set2 = loads(dumps(set))
assert set2.object() is None
def test_trait_list_object_copies():
a = A()
list = copy.deepcopy(a.alist)
assert list.object() is None
list.append(10)
assert len(a.events) == 0
a.alist.append(20)
assert len(a.events) == 1
list2 = copy.deepcopy(list)
list2.append(30)
assert list2.object() is None
def test_trait_dict_object_copies():
a = A()
dict = copy.deepcopy(a.adict)
assert dict.object() is None
dict['key'] = 10
assert len(a.events) == 0
a.adict['key'] = 10
assert len(a.events) == 1
dict2 = copy.deepcopy(dict)
dict2['key2'] = 20
assert dict2.object() is None
def test_trait_set_object_copies():
a = A()
set1 = copy.deepcopy(a.aset)
assert set1.object() is None
set1.add(10)
assert len(a.events) == 0
a.aset.add(20)
assert len(a.events) == 1
set2 = copy.deepcopy(set1)
set2.add(30)
assert set2.object() is None
set3 = a.aset.copy()
assert type(set3) is set
# Should not raise an AttributeError:
set3.remove(20)
def test_pickle_whole():
a = A()
loads(dumps(a))
b = B(dict=dict(a=a))
loads(dumps(b))
|