This file is indexed.

/usr/lib/python2.7/dist-packages/traits/tests/test_dict.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
#  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
""" Test cases for dictionary (Dict) traits. """

from __future__ import absolute_import

from traits.testing.unittest_tools import unittest

from ..trait_types import Dict, Event, Str, TraitDictObject
from ..has_traits import HasTraits, on_trait_change
from ..trait_errors import TraitError


# fixme: We'd like to use a callable instance for the listener so that we
# can maintain state, but traits barfs trying to determine the signature 8^()
def create_listener():
    """ Create a listener for testing trait notifications. """

    def listener(obj, trait_name, old, new):

        listener.obj = obj
        listener.trait_name = trait_name
        listener.new = new
        listener.old = old
        listener.called += 1
        return

    listener.initialize = lambda: initialize_listener(listener)
    return initialize_listener(listener)


def initialize_listener(listener):
    """ Initialize a listener so it looks like it hasn't been called.

    This allows us to re-use the listener without having to create and
    wire-up a new one.

    """

    listener.obj = None
    listener.trait_name = None
    listener.old = None
    listener.new = None
    listener.called = 0

    return listener  # For convenience


class TestDict(unittest.TestCase):
    """ Test cases for dictionary (Dict) traits. """

    def test_modified_event(self):

        class Foo(HasTraits):
            name = Str
            modified = Event

            @on_trait_change('name')
            def _fire_modified_event(self):
                self.modified = True
                return

        class Bar(HasTraits):
            foos = Dict(Str, Foo)
            modified = Event

            @on_trait_change('foos_items,foos.modified')
            def _fire_modified_event(self, obj, trait_name, old, new):
                self.modified = True
                return

        bar = Bar()
        listener = create_listener()
        bar.on_trait_change(listener, 'modified')

        # Assign a completely new dictionary.
        bar.foos = {'dino': Foo(name='dino')}
        self.assertEqual(1, listener.called)
        self.assertEqual('modified', listener.trait_name)

        # Add an item to an existing dictionary.
        listener.initialize()
        fred = Foo(name='fred')
        bar.foos['fred'] = fred
        self.assertEqual(1, listener.called)
        self.assertEqual('modified', listener.trait_name)

        # Modify an item already in the dictionary.
        listener.initialize()
        fred.name = 'barney'
        self.assertEqual(1, listener.called)
        self.assertEqual('modified', listener.trait_name)

        # Overwrite an item in the dictionary. This is the one that fails!
        listener.initialize()
        bar.foos['fred'] = Foo(name='wilma')
        self.assertEqual(1, listener.called)
        self.assertEqual('modified', listener.trait_name)

        return

    def test_validate(self):
        """ Check the validation method.

        """
        foo = Dict()

        # invalid value
        with self.assertRaises(TraitError):
            foo.validate(object=HasTraits(), name='bar', value=None)

        # valid value
        result = foo.validate(object=HasTraits(), name='bar', value={})
        self.assertIsInstance(result, TraitDictObject)

        # object is None (check for issue #71)
        result = foo.validate(object=None, name='bar', value={})
        self.assertEqual(result, {})


if __name__ == '__main__':
    unittest.main()