/usr/share/pyshared/protocols/advice.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 | from __future__ import generators
from new import instancemethod
from types import ClassType, FunctionType, InstanceType
import sys
__all__ = [
'metamethod', 'supermeta', 'getMRO', 'classicMRO',
'mkRef', 'StrongRef',
# XXX these should be deprecated
'addClassAdvisor', 'isClassAdvisor', 'add_assignment_advisor',
'determineMetaclass', 'getFrameInfo', 'minimalBases',
]
# No sense duplicating all this functionality any more...
from peak.util import decorators
def addClassAdvisor(callback, depth=2,frame=None):
"protocols.advice.addClassAdvisor is deprecated, please use"
" peak.util.decorators.decorate_class instead"
from warnings import warn
warn(addClassAdvisor.__doc__, DeprecationWarning, 2)
return decorators.decorate_class(callback, (depth or 0)+1, frame)
def add_assignment_advisor(callback,depth=2,frame=None):
"protocols.advice.add_assignment_advisor is deprecated, please use"
" peak.util.decorators.decorate_assignment instead"
from warnings import warn
warn(add_assignment_advisor.__doc__, DeprecationWarning, 2)
return decorators.decorate_assignment(callback, (depth or 0)+1, frame)
def getFrameInfo(frame):
"protocols.advice.getFrameInfo is deprecated, please use"
" peak.util.decorators.frameinfo instead"
from warnings import warn
warn(getFrameInfo.__doc__, DeprecationWarning, 2)
return decorators.frameinfo(frame)
def determineMetaclass(bases, explicit_mc=None):
"protocols.advice.determineMetaclass is deprecated, please use"
" peak.util.decorators.metaclass_for_bases instead"
from warnings import warn
warn(determineMetaclass.__doc__, DeprecationWarning, 2)
return decorators.metaclass_for_bases(bases, explicit_mc)
def isClassAdvisor(ob):
"protocols.advice.isClassAdvisor is deprecated, please use"
" peak.util.decorators.metaclass_is_decorator instead"
from warnings import warn
warn(isClassAdvisor.__doc__, DeprecationWarning, 2)
return decorators.metaclass_is_decorator(ob)
def metamethod(func):
"""Wrapper for metaclass method that might be confused w/instance method"""
return property(lambda ob: func.__get__(ob,ob.__class__))
try:
from ExtensionClass import ExtensionClass
except ImportError:
ClassicTypes = ClassType
else:
ClassicTypes = ClassType, ExtensionClass
def classicMRO(ob, extendedClassic=False):
stack = []
push = stack.insert
pop = stack.pop
push(0,ob)
while stack:
cls = pop()
yield cls
p = len(stack)
for b in cls.__bases__: push(p,b)
if extendedClassic:
yield InstanceType
yield object
def getMRO(ob, extendedClassic=False):
if isinstance(ob,ClassicTypes):
return classicMRO(ob,extendedClassic)
elif isinstance(ob,type):
return ob.__mro__
return ob,
try:
from _speedups import metamethod, getMRO, classicMRO
except ImportError:
pass
# property-safe 'super()' for Python 2.2; 2.3 can use super() instead
def supermeta(typ,ob):
starttype = type(ob)
mro = starttype.__mro__
if typ not in mro:
starttype = ob
mro = starttype.__mro__
mro = iter(mro)
for cls in mro:
if cls is typ:
mro = [cls.__dict__ for cls in mro]
break
else:
raise TypeError("Not sub/supertypes:", starttype, typ)
typ = type(ob)
class theSuper(object):
def __getattribute__(self,name):
for d in mro:
if name in d:
descr = d[name]
try:
descr = descr.__get__
except AttributeError:
return descr
else:
return descr(ob,typ)
return object.__getattribute__(self,name)
return theSuper()
def minimalBases(classes):
"""DEPRECATED"""
from warnings import warn
warn("protocols.advice.minimalBases is deprecated; please do not use it",
DeprecationWarning, 2)
classes = [c for c in classes if c is not ClassType]
candidates = []
for m in classes:
for n in classes:
if issubclass(n,m) and m is not n:
break
else:
# m has no subclasses in 'classes'
if m in candidates:
candidates.remove(m) # ensure that we're later in the list
candidates.append(m)
return candidates
from weakref import ref
class StrongRef(object):
"""Like a weakref, but for non-weakrefable objects"""
__slots__ = 'referent'
def __init__(self,referent):
self.referent = referent
def __call__(self):
return self.referent
def __hash__(self):
return hash(self.referent)
def __eq__(self,other):
return self.referent==other
def __repr__(self):
return 'StrongRef(%r)' % self.referent
def mkRef(ob,*args):
"""Return either a weakref or a StrongRef for 'ob'
Note that extra args are forwarded to weakref.ref() if applicable."""
try:
return ref(ob,*args)
except TypeError:
return StrongRef(ob)
|