From 66fdd91953ed995ef8fd8372037083f45c6f0de1 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 22 Jan 2020 19:21:10 +0100 Subject: [PATCH] Disable fixup_module_metadata when running Sphinx --- docs/conf.py | 4 ++++ fbchat/__init__.py | 33 +++------------------------- fbchat/_fix_module_metadata.py | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 fbchat/_fix_module_metadata.py diff --git a/docs/conf.py b/docs/conf.py index f6badf1..a3dadbe 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -11,8 +11,12 @@ import sys sys.path.insert(0, os.path.abspath("..")) +os.environ["_FBCHAT_DISABLE_FIX_MODULE_METADATA"] = "1" + import fbchat +del os.environ["_FBCHAT_DISABLE_FIX_MODULE_METADATA"] + # -- Project information ----------------------------------------------------- project = fbchat.__name__ diff --git a/fbchat/__init__.py b/fbchat/__init__.py index 99feaad..3d7c390 100644 --- a/fbchat/__init__.py +++ b/fbchat/__init__.py @@ -92,35 +92,8 @@ __version__ = "1.9.6" __all__ = ("Session", "Listener", "Client") -# Everything below is taken from the excellent trio project: +from . import _fix_module_metadata -def fixup_module_metadata(namespace): - def fix_one(qualname, name, obj): - mod = getattr(obj, "__module__", None) - if mod is not None and mod.startswith("fbchat."): - obj.__module__ = "fbchat" - # Modules, unlike everything else in Python, put fully-qualitied - # names into their __name__ attribute. We check for "." to avoid - # rewriting these. - if hasattr(obj, "__name__") and "." not in obj.__name__: - obj.__name__ = name - obj.__qualname__ = qualname - if isinstance(obj, type): - # Fix methods - for attr_name, attr_value in obj.__dict__.items(): - fix_one(objname + "." + attr_name, attr_name, attr_value) - - for objname, obj in namespace.items(): - if not objname.startswith("_"): # ignore private attributes - fix_one(objname, objname, obj) - - -# Having the public path in .__module__ attributes is important for: -# - exception names in printed tracebacks -# - sphinx :show-inheritance: -# - deprecation warnings -# - pickle -# - probably other stuff -fixup_module_metadata(globals()) -del fixup_module_metadata +_fix_module_metadata.fixup_module_metadata(globals()) +del _fix_module_metadata diff --git a/fbchat/_fix_module_metadata.py b/fbchat/_fix_module_metadata.py new file mode 100644 index 0000000..d2faa38 --- /dev/null +++ b/fbchat/_fix_module_metadata.py @@ -0,0 +1,39 @@ +"""Everything in this module is taken from the excellent trio project. + +Having the public path in .__module__ attributes is important for: +- exception names in printed tracebacks +- ~sphinx :show-inheritance:~ +- deprecation warnings +- pickle +- probably other stuff +""" + +import os + + +def fixup_module_metadata(namespace): + def fix_one(qualname, name, obj): + mod = getattr(obj, "__module__", None) + if mod is not None and mod.startswith("fbchat."): + obj.__module__ = "fbchat" + # Modules, unlike everything else in Python, put fully-qualitied + # names into their __name__ attribute. We check for "." to avoid + # rewriting these. + if hasattr(obj, "__name__") and "." not in obj.__name__: + obj.__name__ = name + obj.__qualname__ = qualname + if isinstance(obj, type): + # Fix methods + for attr_name, attr_value in obj.__dict__.items(): + fix_one(objname + "." + attr_name, attr_name, attr_value) + + for objname, obj in namespace.items(): + if not objname.startswith("_"): # ignore private attributes + fix_one(objname, objname, obj) + + +# Allow disabling this when running Sphinx +# This is done so that Sphinx autodoc can detect the file's source +# TODO: Find a better way to detect when we're running Sphinx! +if os.environ.get("_FBCHAT_DISABLE_FIX_MODULE_METADATA") == "1": + fixup_module_metadata = lambda namespace: None