Warn when accessing deprecated attributes.

Previously, only calling a deprecated function issued a warning.
Attributes (like sphinx.util.pycompat.class_types) would never warn.
This commit is contained in:
Robert Lehmann 2015-02-14 11:11:37 +01:00
parent b61640959d
commit 67ea6ddb97

View File

@ -107,29 +107,32 @@ def execfile_(filepath, _globals, open=open):
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Internal module backwards-compatibility # Internal module backwards-compatibility
import functools
import warnings import warnings
def _deprecated(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
warnings.warn("sphinx.util.pycompat.{0.__name__} is deprecated, "
"please use the standard library version.".format(func),
DeprecationWarning, stacklevel=2)
func(*args, **kwargs)
return wrapped
from six import class_types from six import class_types
from six.moves import zip_longest from six.moves import zip_longest
from itertools import product from itertools import product
zip_longest = _deprecated(zip_longest) class _DeprecationWrapper(object):
product = _deprecated(product) def __init__(self, mod, deprecated):
self._mod = mod
self._deprecated = deprecated
all = _deprecated(all) def __getattr__(self, attr):
any = _deprecated(any) if attr in self._deprecated:
next = _deprecated(next) warnings.warn("sphinx.util.pycompat.%s is deprecated, "
open = _deprecated(open) "please use the standard library version." % attr,
DeprecationWarning)
return self._deprecated[attr]
return getattr(self._mod, attr)
base_exception = BaseException sys.modules[__name__] = _DeprecationWrapper(sys.modules[__name__], dict(
relpath = _deprecated(__import__('os').path.relpath) zip_longest = zip_longest,
product = product,
all = all,
any = any,
next = next,
open = open,
base_exception = BaseException,
relpath = __import__('os').path.relpath,
))