This file is indexed.

/usr/lib/python2.7/dist-packages/doublex/doubles.py is in python-doublex 1.8.2-1build1.

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding:utf-8; tab-width:4; mode:python -*-

# doublex
#
# Copyright © 2012, 2013 David Villa Alises
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


import inspect

import hamcrest

from .internal import (ANY_ARG, OperationList, Method, MockBase, SpyBase,
                       AttributeFactory, WrongApiUsage)
from .proxy import create_proxy, get_class
from .matchers import MockIsExpectedInvocation


__all__ = ['Stub', 'Spy', 'ProxySpy', 'Mock', 'Mimic',
           'method_returning', 'method_raising',
           'ANY_ARG']


class Stub(object):
    _default_behavior = lambda x: None
    _new_attr_hooks = []

    def __new__(cls, collaborator=None):
        '''Creates a fresh class clone per instance. This is required due to
        ad-hoc stub properties are class attributes'''
        klass = cls._clone_class()
        return object.__new__(klass)

    @classmethod
    def _clone_class(cls):
        return type(cls.__name__, (cls,), dict(cls.__dict__))

    def __init__(self, collaborator=None):
        self._proxy = create_proxy(collaborator)
        self._stubs = OperationList()
        self._setting_up = False
        self._new_attr_hooks = self._new_attr_hooks[:]
        self._deactivate = False
        self.__class__.__setattr__ = self.__setattr__hook

    def _activate_next(self):
        self.__enter__()
        self._deactivate = True
        return self

    def __enter__(self):
        self._setting_up = True
        return self

    def __exit__(self, *args):
        self._setting_up = False

    def _manage_invocation(self, invocation):
        self._proxy.assure_signature_matches(invocation)

        if self._setting_up:
            self._stubs.append(invocation)
            return invocation

        self._prepare_invocation(invocation)

        stubbed_retval = self._default_behavior()
        if invocation in self._stubs:
            stubbed = self._stubs.lookup(invocation)
            stubbed_retval = stubbed._apply_stub(invocation)

        actual_retval = self._perform_invocation(invocation)

        retval = stubbed_retval if stubbed_retval is not None else actual_retval
        invocation.context.retval = retval
        return retval

    def _prepare_invocation(self, invocation):
        pass

    def _perform_invocation(self, invocation):
        return None

    def __getattr__(self, key):
        AttributeFactory.create(self, key)
        return object.__getattribute__(self, key)

    def __setattr__hook(self, key, value):
        if key in self.__dict__:
            object.__setattr__(self, key, value)
            return

        try:
            AttributeFactory.create(self, key)
        except AttributeError:
            #  collaborator has not attribute 'key', creaing it ad-hoc
            pass

        # descriptor protocol compliant
        object.__setattr__(self, key, value)

    def _classname(self):
        name = self._proxy.collaborator_classname()
        return name or self.__class__.__name__


class Spy(Stub, SpyBase):
    def __init__(self, collaborator=None):
        self._recorded = OperationList()
        super(Spy, self).__init__(collaborator)

    def _prepare_invocation(self, invocation):
        self._recorded.append(invocation)

    def _received_invocation(self, invocation, times, cmp_pred=None):
        return hamcrest.is_(times).matches(
            self._recorded.count(invocation, cmp_pred))

    def _get_invocations_to(self, name):
        return [i for i in self._recorded
                if self._proxy.same_method(name, i.name)]


class ProxySpy(Spy):
    def __init__(self, collaborator):
        self._assure_is_instance(collaborator)
        super(ProxySpy, self).__init__(collaborator)

    def _assure_is_instance(self, thing):
        if thing is None or inspect.isclass(thing):
            raise TypeError("ProxySpy takes an instance (got %s instead)" % thing)

    def _perform_invocation(self, invocation):
        return invocation._apply_on_collaborator()


class Mock(Spy, MockBase):
    def _prepare_invocation(self, invocation):
        hamcrest.assert_that(self, MockIsExpectedInvocation(invocation))
        super(Mock, self)._prepare_invocation(invocation)


def Mimic(double, collab):
    def __getattribute__hook(self, key):
        if key in ['__class__', '__dict__',
                   '_get_method', '_methods'] or \
                key in [x[0] for x in inspect.getmembers(double)] or \
                key in self.__dict__:
            return object.__getattribute__(self, key)

        return self._get_method(key)

    def _get_method(self, key):
        if key not in list(self._methods.keys()):
            typename = self._proxy.get_attr_typename(key)
            if typename not in ['instancemethod', 'function', 'method']:
                raise WrongApiUsage(
                    "Mimic does not support attribute '%s' (type '%s')" % (key, typename))

            method = Method(self, key)
            self._methods[key] = method

        return self._methods[key]

    assert issubclass(double, Stub), \
        "Mimic() takes a double class as first argument (got %s instead)" & double

    collab_class = get_class(collab)
    generated_class = type(
        "Mimic_%s_for_%s" % (double.__name__, collab_class.__name__),
        (double, collab_class) + collab_class.__bases__,
        dict(_methods = {},
             __getattribute__ = __getattribute__hook,
             _get_method = _get_method))
    return generated_class(collab)


def method_returning(value):
    with Spy() as stub:
        method = Method(stub, 'orphan')
        method(ANY_ARG).returns(value)
        return method


def method_raising(exception):
    with Spy() as stub:
        method = Method(stub, 'orphan')
        method(ANY_ARG).raises(exception)
        return method