|
1 | 1 | import functools
|
| 2 | +import sys |
2 | 3 | import textwrap
|
| 4 | +from types import ModuleType |
3 | 5 | import warnings
|
4 | 6 |
|
5 | 7 |
|
@@ -221,3 +223,38 @@ def wrapper(*args, **kwargs):
|
221 | 223 | return finalize(wrapper, new_doc)
|
222 | 224 |
|
223 | 225 | return deprecate
|
| 226 | + |
| 227 | + |
| 228 | +class _DeprecatorModuleType(ModuleType): |
| 229 | + def __getattr__(self, name): |
| 230 | + try: |
| 231 | + val, message = self._deprecated_dict[name] |
| 232 | + warnings.warn(message, MatplotlibDeprecationWarning, stacklevel=2) |
| 233 | + return val |
| 234 | + except KeyError: |
| 235 | + raise AttributeError( |
| 236 | + "Module {!r} has no attibute {!r}" |
| 237 | + .format(self.__name__, name)) from None |
| 238 | + |
| 239 | + |
| 240 | +def _deprecated_global( |
| 241 | + name, obj, since, *, |
| 242 | + message="", alternative="", pending=False, obj_type="object", |
| 243 | + addendum="", removal=""): |
| 244 | + frame = sys._getframe(1) |
| 245 | + if frame.f_locals is not frame.f_globals: |
| 246 | + raise RuntimeError( |
| 247 | + "Matplotlib internal error: cannot globally deprecate object from " |
| 248 | + "non-global frame") |
| 249 | + mod = sys.modules[frame.f_globals["__name__"]] |
| 250 | + if type(mod) == ModuleType: |
| 251 | + mod.__class__ = _DeprecatorModuleType |
| 252 | + elif mod.__class__ != _DeprecatorModuleType: |
| 253 | + warnings.warn("Matplotlib internal error: cannot deprecate global of " |
| 254 | + "patched module. Assigning attribute normally.") |
| 255 | + mod.__dict__[name] = obj |
| 256 | + return |
| 257 | + message = _generate_deprecation_message( |
| 258 | + since=since, message=message, name=name, alternative=alternative, |
| 259 | + pending=pending, obj_type=obj_type, removal=removal) |
| 260 | + mod.__dict__.setdefault("_deprecated_dict", {})[name] = (obj, message) |
0 commit comments