##// END OF EJS Templates
Backport PR #2924: safe_run_module: Silence SystemExit codes 0 and None....
Backport PR #2924: safe_run_module: Silence SystemExit codes 0 and None. In `safe_execfile` we ignore SystemExit exceptions with codes 0 and 1. We don't do this for `safe_run_module` which leads to the following mismatch of tracebacks between Python and IPython: ``` $ cat > exit0.py import sys sys.exit(0) $ python -m exit0 $ ipython -m exit0 --------------------------------------------------------------------------- SystemExit Traceback (most recent call last) /usr/lib/python2.7/runpy.pyc in run_module(mod_name, init_globals, run_name, alter_sys) 174 if alter_sys: 175 return _run_module_code(code, init_globals, run_name, --> 176 fname, loader, pkg_name) 177 else: 178 # Leave the sys module alone /usr/lib/python2.7/runpy.pyc in _run_module_code(code, init_globals, mod_name, mod_fname, mod_loader, pkg_name) 80 mod_globals = temp_module.module.__dict__ 81 _run_code(code, mod_globals, init_globals, ---> 82 mod_name, mod_fname, mod_loader, pkg_name) 83 # Copy the globals of the temporary module, as they 84 # may be cleared when the temporary module goes away /usr/lib/python2.7/runpy.pyc in _run_code(code, run_globals, init_globals, mod_name, mod_fname, mod_loader, pkg_name) 70 __loader__ = mod_loader, 71 __package__ = pkg_name) ---> 72 exec code in run_globals 73 return run_globals 74 /tmp/exit0.py in <module>() 1 import sys ----> 2 sys.exit(0) SystemExit: 0 WARNING: Unknown failure executing module: <exit0> ``` The attached pull request silences SystemExit exceptions with codes 0 and None.

File last commit:

r7017:7fcb72be
r9972:8ab632a4
Show More
test_module_paths.py
135 lines | 3.9 KiB | text/x-python | PythonLexer
# encoding: utf-8
"""Tests for IPython.utils.path.py"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import with_statement
import os
import shutil
import sys
import tempfile
import StringIO
from os.path import join, abspath, split
import nose.tools as nt
from nose import with_setup
import IPython
from IPython.testing import decorators as dec
from IPython.testing.decorators import skip_if_not_win32, skip_win32
from IPython.testing.tools import make_tempfile
from IPython.utils import path, io
from IPython.utils import py3compat
import IPython.utils.module_paths as mp
env = os.environ
TEST_FILE_PATH = split(abspath(__file__))[0]
TMP_TEST_DIR = tempfile.mkdtemp()
#
# Setup/teardown functions/decorators
#
old_syspath = sys.path
def make_empty_file(fname):
f = open(fname, 'w')
f.close()
def setup():
"""Setup testenvironment for the module:
"""
# Do not mask exceptions here. In particular, catching WindowsError is a
# problem because that exception is only defined on Windows...
os.makedirs(join(TMP_TEST_DIR, "xmod"))
os.makedirs(join(TMP_TEST_DIR, "nomod"))
make_empty_file(join(TMP_TEST_DIR, "xmod/__init__.py"))
make_empty_file(join(TMP_TEST_DIR, "xmod/sub.py"))
make_empty_file(join(TMP_TEST_DIR, "pack.py"))
make_empty_file(join(TMP_TEST_DIR, "packpyc.pyc"))
sys.path = [TMP_TEST_DIR]
def teardown():
"""Teardown testenvironment for the module:
- Remove tempdir
- restore sys.path
"""
# Note: we remove the parent test dir, which is the root of all test
# subdirs we may have created. Use shutil instead of os.removedirs, so
# that non-empty directories are all recursively removed.
shutil.rmtree(TMP_TEST_DIR)
sys.path = old_syspath
def test_get_init_1():
"""See if get_init can find __init__.py in this testdir"""
with make_tempfile(join(TMP_TEST_DIR, "__init__.py")):
assert mp.get_init(TMP_TEST_DIR)
def test_get_init_2():
"""See if get_init can find __init__.pyw in this testdir"""
with make_tempfile(join(TMP_TEST_DIR, "__init__.pyw")):
assert mp.get_init(TMP_TEST_DIR)
def test_get_init_3():
"""get_init can't find __init__.pyc in this testdir"""
with make_tempfile(join(TMP_TEST_DIR, "__init__.pyc")):
assert mp.get_init(TMP_TEST_DIR) is None
def test_get_init_3():
"""get_init can't find __init__ in empty testdir"""
assert mp.get_init(TMP_TEST_DIR) is None
def test_find_mod_1():
modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
assert mp.find_mod("xmod") == modpath
def test_find_mod_2():
modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
assert mp.find_mod("xmod") == modpath
def test_find_mod_3():
modpath = join(TMP_TEST_DIR, "xmod", "sub.py")
assert mp.find_mod("xmod.sub") == modpath
def test_find_mod_4():
modpath = join(TMP_TEST_DIR, "pack.py")
assert mp.find_mod("pack") == modpath
def test_find_mod_5():
assert mp.find_mod("packpyc") is None
def test_find_module_1():
modpath = join(TMP_TEST_DIR, "xmod")
assert mp.find_module("xmod") == modpath
def test_find_module_2():
"""Testing sys.path that is empty"""
assert mp.find_module("xmod", []) is None
def test_find_module_3():
"""Testing sys.path that is empty"""
assert mp.find_module(None, None) is None
def test_find_module_4():
"""Testing sys.path that is empty"""
assert mp.find_module(None) is None
def test_find_module_5():
assert mp.find_module("xmod.nopack") is None