The new woob repository is here: https://gitlab.com/woob/woob. This gitlab will be removed soon.

Commit 8868ae22 authored by Vincent A's avatar Vincent A

weboob compat: override sys.meta_path to prevent package loaded twice

Use case: the core does "import woob.x" but some app does "import
weboob.x". This loaded modules twice and prevented isinstance() from
cooperating. This trick loads modules only once.
parent d02c2d66
......@@ -17,11 +17,43 @@
# You should have received a copy of the GNU Lesser General Public License
# along with woob. If not, see <http://www.gnu.org/licenses/>.
import sys
from importlib.abc import MetaPathFinder, Loader
import importlib
import woob
__path__ = woob.__path__
__title__ = woob.__title__
__version__ = woob.__version__
__author__ = woob.__author__
__copyright__ = woob.__copyright__
# Use case: the core does "import woob.x" but some app does "import weboob.x".
# If we merely set __path__, python will generate different modules
# though they have the same path. So the references will be different,
# isinstance(woob.X(), weboob.X) will fail, etc.
# Instead, we must return the same module to prevent Python from generating
# another one.
# Trick found at https://stackoverflow.com/a/56872393
class AliasLoader(Loader):
def module_repr(self, module):
return repr(module)
def load_module(self, fullname):
new_name = fullname.replace("weboob", "woob")
module = importlib.import_module(new_name)
sys.modules[fullname] = module
return module
class AliasImporter(MetaPathFinder):
def find_module(self, fullname, path=None):
root_name, _, __ = fullname.partition(".")
if root_name == "weboob":
return AliasLoader()
sys.meta_path.append(AliasImporter())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment