Skip to content

Commit

Permalink
Merge pull request #3636 from Achimh3011/pkg_resources_missing_methods
Browse files Browse the repository at this point in the history
Provide the XonshImportEventLoader with all the functions that pkg_re…
  • Loading branch information
scopatz committed Jul 27, 2020
2 parents ebc756d + d15cd6a commit 674c417
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
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

0 comments on commit 674c417

Please sign in to comment.