This file is indexed.

/usr/lib/python2.7/dist-packages/protocols/interfaces.py is in python-protocols 1.0a.svn20070625-7.

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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
"""Implement Interfaces and define the interfaces used by the package"""

from __future__ import generators

__all__ = [
    'Protocol', 'InterfaceClass', 'Interface',
    'AbstractBase', 'AbstractBaseMeta',
    'IAdapterFactory', 'IProtocol',
    'IAdaptingProtocol', 'IOpenProtocol', 'IOpenProvider',
    'IOpenImplementor', 'IImplicationListener', 'Attribute', 'Variation'
]

import api
from advice import metamethod, classicMRO, mkRef
from adapters import composeAdapters, updateWithSimplestAdapter
from adapters import NO_ADAPTER_NEEDED, DOES_NOT_SUPPORT

from types import InstanceType


# Thread locking support

try:
    from thread import allocate_lock

except ImportError:
    try:
        from dummy_thread import allocate_lock

    except ImportError:
        class allocate_lock(object):
            __slots__ = ()
            def acquire(*args): pass
            def release(*args): pass







# Trivial interface implementation

class Protocol:

    """Generic protocol w/type-based adapter registry"""

    def __init__(self):
        self.__adapters = {}
        self.__implies = {}
        self.__listeners = None
        self.__lock = allocate_lock()


    def getImpliedProtocols(self):

        # This is messy so it can clean out weakrefs, but this method is only
        # called for declaration activities and is thus not at all
        # speed-critical.  It's more important that we support weak refs to
        # implied protocols, so that dynamically created subset protocols can
        # be garbage collected.

        out = []
        add = out.append

        self.__lock.acquire()   # we might clean out dead weakrefs

        try:
            for k,v in self.__implies.items():
                proto = k()
                if proto is None:
                    del self.__implies[k]
                else:
                    add((proto,v))

            return out

        finally:
            self.__lock.release()



    def addImpliedProtocol(self,proto,adapter=NO_ADAPTER_NEEDED,depth=1):

        self.__lock.acquire()
        try:
            key = mkRef(proto)
            if not updateWithSimplestAdapter(
                self.__implies, key, adapter, depth
            ):
                return self.__implies[key][0]
        finally:
            self.__lock.release()

        # Always register implied protocol with classes, because they should
        # know if we break the implication link between two protocols
        for klass,(baseAdapter,d) in self.__adapters.items():
            api.declareAdapterForType(
                proto,composeAdapters(baseAdapter,self,adapter),klass,depth+d
            )

        if self.__listeners:
            for listener in self.__listeners.keys():    # Must use keys()!
                listener.newProtocolImplied(self, proto, adapter, depth)

        return adapter

    addImpliedProtocol = metamethod(addImpliedProtocol)















    def registerImplementation(self,klass,adapter=NO_ADAPTER_NEEDED,depth=1):

        self.__lock.acquire()
        try:
            if not updateWithSimplestAdapter(
                self.__adapters,klass,adapter,depth
            ):
                return self.__adapters[klass][0]
        finally:
            self.__lock.release()

        if adapter is DOES_NOT_SUPPORT:
            # Don't register non-support with implied protocols, because
            # "X implies Y" and "not X" doesn't imply "not Y".  In effect,
            # explicitly registering DOES_NOT_SUPPORT for a type is just a
            # way to "disinherit" a superclass' claim to support something.
            return adapter

        for proto, (extender,d) in self.getImpliedProtocols():
            api.declareAdapterForType(
                proto, composeAdapters(adapter,self,extender), klass, depth+d
            )

        return adapter

    registerImplementation = metamethod(registerImplementation)

    def registerObject(self, ob, adapter=NO_ADAPTER_NEEDED,depth=1):
        # Object needs to be able to handle registration
        if api.adapt(ob,IOpenProvider).declareProvides(self,adapter,depth):
            if adapter is DOES_NOT_SUPPORT:
                return  # non-support doesn't imply non-support of implied

            # Handle implied protocols
            for proto, (extender,d) in self.getImpliedProtocols():
                api.declareAdapterForObject(
                    proto, composeAdapters(adapter,self,extender), ob, depth+d
                )

    registerObject = metamethod(registerObject)

    def __adapt__(self, obj):

        get = self.__adapters.get

        try:
            typ = obj.__class__
        except AttributeError:
            typ = type(obj)

        try:
            mro = typ.__mro__
        except AttributeError:
            # Note: this adds 'InstanceType' and 'object' to end of MRO
            mro = classicMRO(typ,extendedClassic=True)

        for klass in mro:
            factory=get(klass)
            if factory is not None:
                return factory[0](obj)

    try:
        from _speedups import Protocol__adapt__ as __adapt__
    except ImportError:
        pass
    __adapt__ = metamethod(__adapt__)

    def addImplicationListener(self, listener):
        self.__lock.acquire()

        try:
            if self.__listeners is None:
                from weakref import WeakKeyDictionary
                self.__listeners = WeakKeyDictionary()

            self.__listeners[listener] = 1

        finally:
            self.__lock.release()

    addImplicationListener = metamethod(addImplicationListener)

    def __call__(self, ob, default=api._marker):
        """Adapt to this protocol"""
        return api.adapt(ob,self,default)


# Use faster __call__ method, if possible
# XXX it could be even faster if the __call__ were in the tp_call slot
# XXX directly, but Pyrex doesn't have a way to do that AFAIK.

try:
    from _speedups import Protocol__call__
except ImportError:
    pass
else:
    from new import instancemethod
    Protocol.__call__ = instancemethod(Protocol__call__, None, Protocol)

























class AbstractBaseMeta(Protocol, type):

    """Metaclass for 'AbstractBase' - a protocol that's also a class

    (Note that this should not be used as an explicit metaclass - always
    subclass from 'AbstractBase' or 'Interface' instead.)
    """

    def __init__(self, __name__, __bases__, __dict__):

        type.__init__(self, __name__, __bases__, __dict__)
        Protocol.__init__(self)

        for b in __bases__:
            if isinstance(b,AbstractBaseMeta) and b.__bases__<>(object,):
                self.addImpliedProtocol(b)


    def __setattr__(self,attr,val):

        # We could probably support changing __bases__, as long as we checked
        # that no bases are *removed*.  But it'd be a pain, since we'd
        # have to do callbacks, remove entries from our __implies registry,
        # etc.  So just punt for now.

        if attr=='__bases__':
            raise TypeError(
                "Can't change interface __bases__", self
            )

        type.__setattr__(self,attr,val)

    __call__ = type.__call__


class AbstractBase(object):
    """Base class for a protocol that's a class"""

    __metaclass__ = AbstractBaseMeta


class InterfaceClass(AbstractBaseMeta):

    """Metaclass for 'Interface' - a non-instantiable protocol

    (Note that this should not be used as an explicit metaclass - always
    subclass from 'AbstractBase' or 'Interface' instead.)
    """

    def __call__(self, *__args, **__kw):
        if self.__init__ is Interface.__init__:
            return Protocol.__call__(self,*__args, **__kw)
        else:
            return type.__call__(self,*__args, **__kw)

    def getBases(self):
        return [
            b for b in self.__bases__
                if isinstance(b,AbstractBaseMeta) and b.__bases__<>(object,)
        ]


class Interface(object):
    __metaclass__ = InterfaceClass


















class Variation(Protocol):

    """A variation of a base protocol - "inherits" the base's adapters

    See the 'LocalProtocol' example in the reference manual for more info.
    """


    def __init__(self, baseProtocol, context = None):

        self.baseProtocol = baseProtocol
        self.context = context

        # Note: Protocol is a ``classic'' class, so we don't use super()
        Protocol.__init__(self)

        api.declareAdapterForProtocol(self,NO_ADAPTER_NEEDED,baseProtocol)


    def __repr__(self):

        if self.context is None:
            return "Variation(%r)" % self.baseProtocol

        return "Variation(%r,%r)" % (self.baseProtocol, self.context)
















# Semi-backward compatible 'interface.Attribute'

class Attribute(object):

    """Attribute declaration; should we get rid of this?"""

    def __init__(self,doc,name=None,value=None):
        self.__doc__ = doc
        self.name = name
        self.value = value

    def __get__(self,ob,typ=None):
        if ob is None:
            return self
        if not self.name:
            raise NotImplementedError("Abstract attribute")
        try:
            return ob.__dict__[self.name]
        except KeyError:
            return self.value

    def __set__(self,ob,val):
        if not self.name:
            raise NotImplementedError("Abstract attribute")
        ob.__dict__[self.name] = val

    def __delete__(self,ob):
        if not self.name:
            raise NotImplementedError("Abstract attribute")
        del ob.__dict__[self.name]

    def __repr__(self):
        return "Attribute: %s" % self.__doc__








# Interfaces and adapters for declaring protocol/type/object relationships

class IAdapterFactory(Interface):

    """Callable that can adapt an object to a protocol"""

    def __call__(ob):
        """Return an implementation of protocol for 'ob'"""


class IProtocol(Interface):

    """Object usable as a protocol by 'adapt()'"""

    def __hash__():
        """Protocols must be usable as dictionary keys"""

    def __eq__(other):
        """Protocols must be comparable with == and !="""

    def __ne__(other):
        """Protocols must be comparable with == and !="""


class IAdaptingProtocol(IProtocol):

    """A protocol that potentially knows how to adapt some object to itself"""

    def __adapt__(ob):
        """Return 'ob' adapted to protocol, or 'None'"""


class IConformingObject(Interface):

    """An object that potentially knows how to adapt to a protocol"""

    def __conform__(protocol):
        """Return an implementation of 'protocol' for self, or 'None'"""



class IOpenProvider(Interface):
    """An object that can be told how to adapt to protocols"""

    def declareProvides(protocol, adapter=NO_ADAPTER_NEEDED, depth=1):
        """Register 'adapter' as providing 'protocol' for this object

        Return a true value if the provided adapter is the "shortest path" to
        'protocol' for the object, or false if a shorter path already existed.
        """

class IOpenImplementor(Interface):
    """Object/type that can be told how its instances adapt to protocols"""

    def declareClassImplements(protocol, adapter=NO_ADAPTER_NEEDED, depth=1):
        """Register 'adapter' as implementing 'protocol' for instances"""


class IOpenProtocol(IAdaptingProtocol):
    """A protocol that be told what it implies, and what supports it

    Note that these methods are for the use of the declaration APIs only,
    and you should NEVER call them directly."""

    def addImpliedProtocol(proto, adapter=NO_ADAPTER_NEEDED, depth=1):
        """'adapter' provides conversion from this protocol to 'proto'"""

    def registerImplementation(klass, adapter=NO_ADAPTER_NEEDED, depth=1):
        """'adapter' provides protocol for instances of klass"""

    def registerObject(ob, adapter=NO_ADAPTER_NEEDED, depth=1):
        """'adapter' provides protocol for 'ob' directly"""

    def addImplicationListener(listener):
        """Notify 'listener' whenever protocol has new implied protocol"""


class IImplicationListener(Interface):

    def newProtocolImplied(srcProto, destProto, adapter, depth):
        """'srcProto' now implies 'destProto' via 'adapter' at 'depth'"""