/usr/share/pyshared/zope/deprecation/deprecation.py is in python-zope.deprecation 4.0.2-1.
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 | ##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Deprecation Support
This module provides utilities to ease the development of backward-compatible
code.
"""
__docformat__ = "reStructuredText"
import sys
import types
import warnings
PY3 = sys.version_info[0] == 3
if PY3: #pragma NO COVER
str_and_sequence_types = (str, list, tuple)
else: #pragma NO COVER
str_and_sequence_types = (basestring, list, tuple)
class ShowSwitch(object):
"""Simple stack-based switch."""
def __init__(self):
self.stack = []
def on(self):
self.stack.pop()
def off(self):
self.stack.append(False)
def reset(self):
self.stack = []
def __call__(self):
return self.stack == []
def __repr__(self):
return '<ShowSwitch %s>' %(self() and 'on' or 'off')
# This attribute can be used to temporarly deactivate deprecation
# warnings, so that backward-compatibility code can import other
# backward-compatiblity components without warnings being produced.
__show__ = ShowSwitch()
ogetattr = object.__getattribute__
class DeprecationProxy(object):
def __init__(self, module):
self.__original_module = module
self.__deprecated = {}
def deprecate(self, names, message):
"""Deprecate the given names."""
if not isinstance(names, (tuple, list)):
names = (names,)
for name in names:
self.__deprecated[name] = message
def __getattribute__(self, name):
if name == 'deprecate' or name.startswith('_DeprecationProxy__'):
return ogetattr(self, name)
if name == '__class__':
return types.ModuleType
if name in ogetattr(self, '_DeprecationProxy__deprecated'):
if __show__():
warnings.warn(
name + ': ' + self.__deprecated[name],
DeprecationWarning, 2)
return getattr(ogetattr(self, '_DeprecationProxy__original_module'),
name)
def __setattr__(self, name, value):
if name.startswith('_DeprecationProxy__'):
return object.__setattr__(self, name, value)
setattr(self.__original_module, name, value)
def __delattr__(self, name):
if name.startswith('_DeprecationProxy__'):
return object.__delattr__(self, name)
delattr(self.__original_module, name)
class DeprecatedModule(object):
def __init__(self, module, msg):
self.__original_module = module
self.__msg = msg
def __getattribute__(self, name):
if name.startswith('_DeprecatedModule__'):
return ogetattr(self, name)
if name == '__class__':
return types.ModuleType
if __show__():
warnings.warn(self.__msg, DeprecationWarning, 2)
return getattr(ogetattr(self, '_DeprecatedModule__original_module'),
name)
def __setattr__(self, name, value):
if name.startswith('_DeprecatedModule__'):
return object.__setattr__(self, name, value)
setattr(self.__original_module, name, value)
def __delattr__(self, name):
if name.startswith('_DeprecatedModule__'):
return object.__delattr__(self, name)
delattr(self.__original_module, name)
class DeprecatedGetProperty(object):
def __init__(self, prop, message):
self.message = message
self.prop = prop
def __get__(self, inst, klass):
if __show__():
warnings.warn(self.message, DeprecationWarning, 2)
return self.prop.__get__(inst, klass)
class DeprecatedGetSetProperty(DeprecatedGetProperty):
def __set__(self, inst, prop):
if __show__():
warnings.warn(self.message, DeprecationWarning, 2)
self.prop.__set__(inst, prop)
class DeprecatedGetSetDeleteProperty(DeprecatedGetSetProperty):
def __delete__(self, inst):
if __show__():
warnings.warn(self.message, DeprecationWarning, 2)
self.prop.__delete__(inst)
def DeprecatedMethod(method, message):
def deprecated_method(self, *args, **kw):
if __show__():
warnings.warn(message, DeprecationWarning, 2)
return method(self, *args, **kw)
return deprecated_method
def deprecated(specifier, message):
"""Deprecate the given names."""
# A string specifier (or list of strings) means we're called
# top-level in a module and are to deprecate things inside this
# module
if isinstance(specifier, str_and_sequence_types):
globals = sys._getframe(1).f_globals
modname = globals['__name__']
if not isinstance(sys.modules[modname], DeprecationProxy):
sys.modules[modname] = DeprecationProxy(sys.modules[modname])
sys.modules[modname].deprecate(specifier, message)
# Anything else can mean the specifier is a function/method,
# module, or just an attribute of a class
elif isinstance(specifier, types.FunctionType):
return DeprecatedMethod(specifier, message)
elif isinstance(specifier, types.ModuleType):
return DeprecatedModule(specifier, message)
else:
prop = specifier
if hasattr(prop, '__get__') and hasattr(prop, '__set__') and \
hasattr(prop, '__delete__'):
return DeprecatedGetSetDeleteProperty(prop, message)
elif hasattr(prop, '__get__') and hasattr(prop, '__set__'):
return DeprecatedGetSetProperty(prop, message)
elif hasattr(prop, '__get__'):
return DeprecatedGetProperty(prop, message)
class deprecate(object):
"""Deprecation decorator"""
def __init__(self, msg):
self.msg = msg
def __call__(self, func):
return DeprecatedMethod(func, self.msg)
def moved(to_location, unsupported_in=None):
old = sys._getframe(1).f_globals['__name__']
message = '%s has moved to %s.' % (old, to_location)
if unsupported_in:
message += " Import of %s will become unsupported in %s" % (
old, unsupported_in)
warnings.warn(message, DeprecationWarning, 3)
__import__(to_location)
fromdict = sys.modules[to_location].__dict__
tomod = sys.modules[old]
tomod.__doc__ = message
for name, v in fromdict.items():
if name not in tomod.__dict__:
setattr(tomod, name, v)
|