Red-DiscordBot/redbot/core/_sharedlibdeprecation.py
jack1142 b457f8d1c1 [Core] Add deprecation warnings about removal of shared libraries. (#3106)
* feat: add deprecation warning when importing shared libs

* enhance(downloader): add shared libs deprecation warns

* enhance: add deprecation warning when (re)loading cogs

* docs(downloader): add deprecation note about shared libs

* chore(changelog): add towncrier entries

* style: split long tuple unpacks in multiple lines

* fix: argument to `humanize_list` has to be a sequence
2019-12-20 02:06:53 -05:00

30 lines
969 B
Python

from importlib.abc import MetaPathFinder
import warnings
class SharedLibDeprecationWarning(DeprecationWarning):
pass
warnings.simplefilter("always", SharedLibDeprecationWarning)
class SharedLibImportWarner(MetaPathFinder):
"""
Deprecation warner for shared libraries. This class sits on `sys.meta_path`
and prints warning if imported module is a shared library
"""
def find_spec(self, fullname, path, target=None) -> None:
"""This is only supposed to print warnings, it won't ever return module spec."""
parts = fullname.split(".")
if parts[0] != "cog_shared" or len(parts) != 2:
return None
msg = (
"One of cogs uses shared libraries which are"
" deprecated and scheduled for removal in Red 3.3.\n"
"You should inform author of the cog about this message."
)
warnings.warn(msg, SharedLibDeprecationWarning, stacklevel=2)
return None