Fix tests, document, and make install_hook() install only once.

This commit is contained in:
Matthias Bussonnier 2016-08-10 18:20:54 -07:00
parent 619073573e
commit 3c7753f4be
3 changed files with 34 additions and 4 deletions

21
news/imphook.rst Normal file
View file

@ -0,0 +1,21 @@
**Added:** None
**Changed:**
* ``xonsh.imphooks`` does not install the import hooks automatically, you now
need to explicitly call the `install_hook()` method defined in this module.
For example: ``from xonsh.imphooks import install_hook; install_hook()``. The
``install_hook`` method can safely be called several times. If you need
compatibility with previous versions of Xonsh you can use the following::
from xonsh import imphooks
getattr(imphooks, 'install_hook', lambda:None)()
**Deprecated:** None
**Removed:** None
**Fixed:** None
**Security:** None

View file

@ -2,13 +2,13 @@
"""Testing xonsh import hooks"""
import pytest
from xonsh import imphooks # noqa
from xonsh import built_ins
from xonsh import imphooks
from xonsh.environ import Env
from xonsh.execer import Execer
from xonsh.built_ins import load_builtins, unload_builtins
import builtins
imphooks.install_hook()
@pytest.yield_fixture(autouse=True)
def imp_env(xonsh_execer):

View file

@ -85,4 +85,13 @@ class XonshImportHook(MetaPathFinder, SourceLoader):
def install_hook():
sys.meta_path.append(XonshImportHook())
"""
Install Xonsh import hook in `sys.metapath` in order for `.xsh` files to be
importable.
Can safely be called many times, will be no-op if a xonsh import hook is
already present.
"""
if XonshImportHook not in {type(hook) for hook in sys.meta_path}:
sys.meta_path.append(XonshImportHook())