This file is indexed.

/usr/share/pyshared/protocols/tests/checks.py is in python-protocols 1.0a.svn20070625-5build1.

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
"""Basic test setups"""

__all__ = [
    'TestBase', 'ImplementationChecks', 'ProviderChecks',
    'InstanceImplementationChecks', 'makeClassTests', 'ClassProvidesChecks',
    'AdaptiveChecks', 'SimpleAdaptiveChecks', 'makeMetaClassProvidesTests',
    'BasicClassProvidesChecks',
]

from unittest import TestCase, makeSuite, TestSuite
from protocols import *


# Dummy interfaces and adapters used in tests

def a1(ob):
    return 'a1',ob

def a2(ob):
    return 'a2',ob





















class TestBase(TestCase):

    """Non-adapter instance tests"""

    a1 = staticmethod(a1)
    a2 = staticmethod(a2)

    def assertObProvidesOnlyA(self):
        assert self.IA(self.ob, None) is self.ob
        assert self.IB(self.ob, None) is None
        assert self.IA(None, None) is None
        assert adapt(self.IB, None, None) is None
        assert adapt(self.ob, None, None) is None

    def assertObProvidesAandB(self):
        assert self.IA(self.ob, None) is self.ob
        assert self.IB(self.ob, None) is self.ob
        assert adapt(self.IA, None, None) is None
        assert adapt(self.IB, None, None) is None
        assert adapt(self.ob, None, None) is None

    def assertAmbiguous(self, a1, a2, d1, d2, ifaces):
        try:
            self.declareObAdapts(a2,ifaces)
        except TypeError,v:
            assert v.args == ("Ambiguous adapter choice", a1, a2, d1, d2)

    def make(self,klass):
        # This is overridden by tests where 'klass' is a metaclass
        return klass()

    def assertObProvidesSubsetOfA(self):
        # Assert that self.ob provides a new subset of self.IA
        # (Caller must ensure that self.ob provides self.IA)
        class IC(self.Interface):
            advise(protocolIsSubsetOf=[self.IA])
            if 'self' in locals():
                del locals()['self']  # how the heck???
        assert IC(self.ob, None) is self.ob


    def setupBases(self,base):
        class M1(base): pass
        class M2(base): pass
        return M1, M2


    def assertM1ProvidesOnlyAandM2ProvidesB(self,M1,M2):
        assert self.IA(M1,None) is M1
        assert self.IB(M1,None) is None
        assert self.IB(M2,None) is M2


    def assertChangingBasesChangesInterface(self,M1,M2,m1,m2):
        try:
            M1.__bases__ = M2,
        except TypeError:   # XXX 2.2 doesn't let newstyle __bases__ change
            pass
        else:
            assert self.IA(m1,None) is m1
            assert self.IB(m1,None) is m1

    def assertObProvidesABCD(self,IC,ID):
        assert self.IA(self.ob, None) is self.ob
        assert self.IB(self.ob, None) is self.ob
        assert IC(self.ob, None) is self.ob
        assert ID(self.ob, None) is self.ob

    def assertObProvidesCandDnotAorB(self,IC,ID):
        assert self.IA(self.ob, None) is None
        assert self.IB(self.ob, None) is None
        assert IC(self.ob, None) is self.ob
        assert ID(self.ob, None) is self.ob









class ProviderChecks(TestBase):

    """Non-adapter specific-object-provides checks"""

    def declareObImplements(self,ifaces):
        adviseObject(self.ob, provides=ifaces)

    def declareObAdapts(self,factory,ifaces):
        declareAdapter(factory,provides=ifaces,forObjects=[self.ob])

    def checkSimpleRegister(self):
        self.declareObImplements([self.IA])
        self.assertObProvidesOnlyA()

    def checkImpliedRegister(self):
        self.declareObImplements([self.IB])
        self.assertObProvidesAandB()
























class SimpleAdaptiveChecks:

    """Simple adapter-oriented checks that Twisted can handle

    (Well, it handles them as long as all the interfaces are Twisted)"""

    def checkDelayedImplication(self):
        self.declareObImplements([self.IA])
        self.assertObProvidesSubsetOfA()


    def checkIndirectImplication(self):

        # Zope fails this because it does not support adding after-the-fact
        # implication.

        # IB->IA + ID->IC + IC->IB = ID->IA

        class IC(self.Interface): pass
        class ID(IC):             pass

        self.declareObImplements([ID])
        self.assertObProvidesCandDnotAorB(IC,ID)

        declareAdapter(
            NO_ADAPTER_NEEDED, provides=[self.IB], forProtocols=[IC]
        )

        self.assertObProvidesABCD(IC,ID)












class AdaptiveChecks(SimpleAdaptiveChecks):

    """General adapter/protocol implication checks

    Twisted can't handle these."""

    def checkAmbiguity(self):
        self.declareObAdapts(self.a1,[self.IA])
        self.assertAmbiguous(self.a1,self.a2,1,1,[self.IA])

    def checkOverrideDepth(self):
        self.declareObAdapts(self.a1,[self.IB])
        self.assertEquals(self.IA(self.ob,None), ('a1',self.ob))

        self.declareObAdapts(self.a2,[self.IA])
        self.assertEquals(self.IA(self.ob,None), ('a2',self.ob))


    def checkComposed(self):
        class IC(self.Interface): pass
        declareAdapter(self.a2,provides=[IC],forProtocols=[self.IA])
        self.declareObAdapts(self.a1,[self.IA])
        self.assertEqual(IC(self.ob,None), ('a2',('a1',self.ob)))


    def checkLateDefinition(self):
        # Zope fails this because it has different override semantics

        self.declareObAdapts(DOES_NOT_SUPPORT, [self.IA])
        assert self.IA(self.ob,None) is None

        self.declareObImplements([self.IA])
        assert self.IA(self.ob,None) is self.ob

        # NO_ADAPTER_NEEDED at same depth should override DOES_NOT_SUPPORT
        self.declareObAdapts(DOES_NOT_SUPPORT, [self.IA])
        assert self.IA(self.ob,None) is self.ob




class InstanceImplementationChecks(TestBase):

    """Non-adapter class-instances-provide checks"""

    # Everybody can handle these

    def declareObImplements(self,ifaces):
        declareImplementation(self.klass, ifaces)

    def declareObAdapts(self,factory,ifaces):
        declareAdapter(factory,provides=ifaces,forTypes=[self.klass])

    def checkSimpleRegister(self):
        self.declareObImplements([self.IA])
        self.assertObProvidesOnlyA()

    def checkImpliedRegister(self):
        self.declareObImplements([self.IB])
        self.assertObProvidesAandB()


class ImplementationChecks(InstanceImplementationChecks):

    """Non-adapter class-instances vs. class-provide checks"""

    # Twisted fails these tests because it has no way to distinguish the
    # interfaces an object provides from the interfaces its class provides

    def checkNoClassPassThru(self):
        self.declareObImplements([self.IA])
        assert self.IA(self.klass, None) is None

    def checkInheritedDeclaration(self):
        self.declareObImplements([self.IB])
        class Sub(self.klass): pass
        inst = self.make(Sub)
        assert self.IB(inst,None) is inst
        assert self.IA(inst,None) is inst
        assert self.IA(Sub,None) is None   # check not passed up to class
        assert self.IB(Sub,None) is None

    def checkRejectInheritanceAndReplace(self):
        self.declareObImplements([self.IB])

        class Sub(self.klass): advise(instancesDoNotProvide=[self.IB])
        inst = self.make(Sub)
        assert self.IA(inst,None) is inst
        assert self.IB(inst,None) is None

        declareImplementation(Sub, instancesProvide=[self.IB])
        assert self.IB(inst,None) is inst































class BasicClassProvidesChecks:

    """Object-provides checks for classes and types"""

    # Twisted doesn't support these because it makes no object/type distinction

    def checkNoInstancePassThru(self):
        inst = self.ob()
        adviseObject(self.ob, provides=[self.IA])
        assert self.IA(inst, None) is None

    def checkChangingBases(self):
        M1, M2 = self.setupBases(self.ob)
        adviseObject(M1, provides=[self.IA])
        adviseObject(M2, provides=[self.IB])
        self.assertM1ProvidesOnlyAandM2ProvidesB(M1,M2)
        self.assertChangingBasesChangesInterface(M1,M2,M1,M2)


class ClassProvidesChecks(BasicClassProvidesChecks):

    # Twisted doesn't support these because it makes no object/type distinction

    def checkInheritedDeclaration(self):
        class Sub(self.ob): pass
        adviseObject(self.ob, provides=[self.IB])
        assert self.IB(Sub, None) is Sub
        assert self.IA(Sub, None) is Sub


    def checkRejectInheritanceAndReplace(self):
        adviseObject(self.ob, provides=[self.IB])

        class Sub(self.ob): advise(classDoesNotProvide=[self.IB])

        assert self.IA(Sub,None) is Sub
        assert self.IB(Sub,None) is None

        adviseObject(Sub,provides=[self.IB])
        assert self.IB(Sub,None) is Sub

def makeInstanceTests(base,Picklable,NewStyle):

    """Generate a set of instance-oriented test classes using 'base'"""

    class AdviseFunction(base):
        __module__ = base.__module__
        def setUp(self):
            def aFunc(foo,bar):
                pass
            self.ob = aFunc
    AdviseFunction.__name__ = base.__name__+'.AdviseFunction'

    class AdviseModule(base):
        __module__ = base.__module__
        def setUp(self):
            from types import ModuleType
            self.ob = ModuleType('x')
    AdviseModule.__name__ = base.__name__+'.AdviseModule'

    class AdviseInstance(base):
        __module__ = base.__module__
        
        def setUp(self):
            self.ob = Picklable()

        def checkPickling(self):
            from cPickle import loads,dumps     # pickle has a bug!
            adviseObject(self.ob, provides=[self.IPure])
            newOb = loads(dumps(self.ob))
            assert self.IPure(newOb,None) is newOb
    AdviseInstance.__name__ = base.__name__+'.AdviseInstance'

    class AdviseNewInstance(AdviseInstance):
        __module__ = base.__module__
        def setUp(self):
            self.ob = NewStyle()
    AdviseNewInstance.__name__ = base.__name__+'.AdviseNewInstance'

    return AdviseFunction, AdviseModule, AdviseInstance, AdviseNewInstance


def makeClassProvidesTests(base):
    """Generate a set of class-provides-oriented test classes using 'base'"""

    class AdviseClass(base):
        def setUp(self):
            class Classic:
                pass
            self.ob = Classic

    class AdviseType(AdviseClass):
        def setUp(self):
            class Class(object):
                pass
            self.ob = Class

    return AdviseClass, AdviseType


def makeMetaClassProvidesTests(base):
    """Generate a set of class-provides-oriented test classes using 'base'"""

    # Notice that we don't test the *metaclass* of the next two configurations;
    # it would fail because the metaclass itself can't be adapted to an open
    # provider, because it has a __conform__ method (from ProviderMixin).  For
    # that to work, there'd have to be *another* metalevel.

    class AdviseMixinClass(base):
        def setUp(self):
            class Meta(ProviderMixin, type): pass
            class Test(object): __metaclass__ = Meta
            self.ob = Test

    class AdviseMixinMultiMeta2(base):
        def setUp(self):
            class Meta(ProviderMixin, type): pass
            class Test(ProviderMixin,object): __metaclass__ = Meta
            self.ob = Test

    return AdviseMixinClass, AdviseMixinMultiMeta2


def makeClassTests(base):
    """Generate a set of class-oriented test classes using 'base'"""

    class TestClassic(base):
        __module__ = base.__module__
        def setUp(self):
            class Classic: pass
            self.klass = Classic
            self.ob = Classic()
    TestClassic.__name__ = base.__name__+'.TestClassic'
    class TestBuiltin(base):
        __module__ = base.__module__
        def setUp(self):
            # Note: We need a type with a no-arguments constructor
            class Newstyle(list): __slots__ = ()
            self.klass = Newstyle
            self.ob = Newstyle()
    TestBuiltin.__name__ = base.__name__+'.TestBuiltin'
    class TestMetaclass(base):
        __module__ = base.__module__
        def setUp(self):
            class Meta(type): pass
            self.klass = Meta
            class Base(object): __metaclass__ = Meta
            self.ob = Base

        def make(self,klass):
            return klass('Dummy',(object,),{})
    TestMetaclass.__name__ = base.__name__+'.TestMetaclass'
    class TestMetaInstance(base):
        __module__ = base.__module__
        def setUp(self):
            class Meta(type): pass
            class Base(object): __metaclass__ = Meta
            self.klass = Base
            self.ob = Base()
    TestMetaInstance.__name__ = base.__name__+'.TestMetaInstance'

    return TestClassic, TestBuiltin, TestMetaclass, TestMetaInstance