How to change layers priority?

In my case:

(Pdb) self.request.__provides__.__iro__[13]
<InterfaceClass other.package.interfaces.layer.IOriginalLayer>
(Pdb) self.request.__provides__.__iro__[14]
<InterfaceClass this.package.content.interfaces.IThisPackageContentLayer>

I want to have IThisPackageContentLayer before IOriginalLayer. How can I cange the layers order?

I have an upgrade step for importing browserlayer.xml (where I have only IThisPackageContentLayer). Can I make this layer to be prioritized against IOriginalLayer?

The code below shows an example how to change the iro (= "Interface Resolution Order"):

from zope.interface import Interface, alsoProvides

class I1(Interface): pass

class I2(Interface): pass

class C(object): pass

alsoProvides(c, I1)
alsoProvides(c, I2)
print c.__provides__.__iro__
c.__provides__ = type(c.__provides__)(
  *((I2,) + tuple(i for i in c.__provides__.__iro__ if i is not I2))
print c.__provides__.__iro__
1 Like

If you define IThisPackageContentLayer as a subclass of IOriginalLayer, it will be considered to have higher precedence and thus be listed earlier in the resolution order:

class IThisPackageContentLayer(IOriginalLayer):
    # ...

Thank you, @dieter.

This is awesome. It solved my problem.
Thank you very much, @davisagli.

I spent a lot of time trying to fix this. You saved my day.

1 Like

__iro__ because __mro__ was a good idea, and should be repeated. (?)