Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide the XonshImportEventLoader with all the functions that pkg_re… #3636

Merged
merged 5 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/imphooks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Fixed import problems due to modules using deprecated pkg_resources methods by proxying calls to the underlying loader.

**Security:**

* <news item>
41 changes: 29 additions & 12 deletions xonsh/imphooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

This module registers the hooks it defines when it is imported.
"""
import builtins
import contextlib
import importlib
import os
import re
import sys
import types
import builtins
import contextlib
import importlib
from importlib.machinery import ModuleSpec
from importlib.abc import MetaPathFinder, SourceLoader, Loader
from importlib.machinery import ModuleSpec

from xonsh.events import events
from xonsh.execer import Execer
from xonsh.platform import ON_WINDOWS, scandir
from xonsh.lazyasd import lazyobject
from xonsh.platform import ON_WINDOWS, scandir


@lazyobject
Expand Down Expand Up @@ -270,6 +270,26 @@ def find_spec(self, fullname, path, target=None):
return spec


_XIEVL_WRAPPED_ATTRIBUTES = frozenset(
[
"load_module",
"module_repr",
"get_data",
"get_resource_filename",
"get_resource_stream",
"get_resource_string",
"has_resource",
"has_metadata",
"get_metadata",
"get_metadata_lines",
"resource_isdir",
"metadata_isdir",
"resource_listdir",
"metadata_listdir",
]
)


class XonshImportEventLoader(Loader):
"""A class that dispatches loader calls to another loader and fires relevant
xonsh events.
Expand All @@ -295,13 +315,10 @@ def exec_module(self, module):
events.on_import_post_exec_module.fire(module=module)
return rtn

def load_module(self, fullname):
"""Legacy module loading, provided for backwards compatibility."""
return self.loader.load_module(fullname)

def module_repr(self, module):
"""Legacy module repr, provided for backwards compatibility."""
return self.loader.module_repr(module)
def __getattr__(self, name):
if name in _XIEVL_WRAPPED_ATTRIBUTES:
return getattr(self.loader, name)
return object.__getattribute__(self, name)


def install_import_hooks():
Expand Down