Commit af5bb894 authored by Stefan Behnel's avatar Stefan Behnel

Avoid starting IPython in test module if we don't execute the tests.

parent 557a2a2a
...@@ -12,7 +12,7 @@ from Cython.Build import IpythonMagic ...@@ -12,7 +12,7 @@ from Cython.Build import IpythonMagic
from Cython.TestUtils import CythonTest from Cython.TestUtils import CythonTest
try: try:
from IPython.testing.globalipapp import get_ipython import IPython.testing.globalipapp
from IPython.utils import py3compat from IPython.utils import py3compat
except ImportError: except ImportError:
# Disable tests and fake helpers for initialisation below. # Disable tests and fake helpers for initialisation below.
...@@ -21,19 +21,15 @@ except ImportError: ...@@ -21,19 +21,15 @@ except ImportError:
return s return s
__test__ = {} __test__ = {}
get_ipython = lambda: None
py3compat = _py3compat() py3compat = _py3compat()
try: try:
# disable IPython history thread to avoid having to clean it up # disable IPython history thread before it gets started to avoid having to clean it up
from IPython.core.history import HistoryManager from IPython.core.history import HistoryManager
HistoryManager.enabled = False HistoryManager.enabled = False
except ImportError: except ImportError:
pass pass
# Initialise IPython after disabling history thread.
ip = get_ipython()
code = py3compat.str_to_unicode("""\ code = py3compat.str_to_unicode("""\
def f(x): def f(x):
return 2*x return 2*x
...@@ -75,17 +71,24 @@ else: ...@@ -75,17 +71,24 @@ else:
class TestIPythonMagic(CythonTest): class TestIPythonMagic(CythonTest):
@classmethod
def setUpClass(cls):
CythonTest.setUpClass()
cls._ip = IPython.testing.globalipapp.get_ipython()
def setUp(self): def setUp(self):
CythonTest.setUp(self) CythonTest.setUp(self)
ip.extension_manager.load_extension('cython') self._ip.extension_manager.load_extension('cython')
def test_cython_inline(self): def test_cython_inline(self):
ip = self._ip
ip.ex('a=10; b=20') ip.ex('a=10; b=20')
result = ip.run_cell_magic('cython_inline', '', 'return a+b') result = ip.run_cell_magic('cython_inline', '', 'return a+b')
self.assertEqual(result, 30) self.assertEqual(result, 30)
@skip_win32('Skip on Windows') @skip_win32('Skip on Windows')
def test_cython_pyximport(self): def test_cython_pyximport(self):
ip = self._ip
module_name = '_test_cython_pyximport' module_name = '_test_cython_pyximport'
ip.run_cell_magic('cython_pyximport', module_name, code) ip.run_cell_magic('cython_pyximport', module_name, code)
ip.ex('g = f(10)') ip.ex('g = f(10)')
...@@ -99,12 +102,14 @@ class TestIPythonMagic(CythonTest): ...@@ -99,12 +102,14 @@ class TestIPythonMagic(CythonTest):
pass pass
def test_cython(self): def test_cython(self):
ip = self._ip
ip.run_cell_magic('cython', '', code) ip.run_cell_magic('cython', '', code)
ip.ex('g = f(10)') ip.ex('g = f(10)')
self.assertEqual(ip.user_ns['g'], 20.0) self.assertEqual(ip.user_ns['g'], 20.0)
def test_cython_name(self): def test_cython_name(self):
# The Cython module named 'mymodule' defines the function f. # The Cython module named 'mymodule' defines the function f.
ip = self._ip
ip.run_cell_magic('cython', '--name=mymodule', code) ip.run_cell_magic('cython', '--name=mymodule', code)
# This module can now be imported in the interactive namespace. # This module can now be imported in the interactive namespace.
ip.ex('import mymodule; g = mymodule.f(10)') ip.ex('import mymodule; g = mymodule.f(10)')
...@@ -112,6 +117,7 @@ class TestIPythonMagic(CythonTest): ...@@ -112,6 +117,7 @@ class TestIPythonMagic(CythonTest):
def test_cython_language_level(self): def test_cython_language_level(self):
# The Cython cell defines the functions f() and call(). # The Cython cell defines the functions f() and call().
ip = self._ip
ip.run_cell_magic('cython', '', cython3_code) ip.run_cell_magic('cython', '', cython3_code)
ip.ex('g = f(10); h = call(10)') ip.ex('g = f(10); h = call(10)')
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
...@@ -123,6 +129,7 @@ class TestIPythonMagic(CythonTest): ...@@ -123,6 +129,7 @@ class TestIPythonMagic(CythonTest):
def test_cython3(self): def test_cython3(self):
# The Cython cell defines the functions f() and call(). # The Cython cell defines the functions f() and call().
ip = self._ip
ip.run_cell_magic('cython', '-3', cython3_code) ip.run_cell_magic('cython', '-3', cython3_code)
ip.ex('g = f(10); h = call(10)') ip.ex('g = f(10); h = call(10)')
self.assertEqual(ip.user_ns['g'], 2.0 / 10.0) self.assertEqual(ip.user_ns['g'], 2.0 / 10.0)
...@@ -130,6 +137,7 @@ class TestIPythonMagic(CythonTest): ...@@ -130,6 +137,7 @@ class TestIPythonMagic(CythonTest):
def test_cython2(self): def test_cython2(self):
# The Cython cell defines the functions f() and call(). # The Cython cell defines the functions f() and call().
ip = self._ip
ip.run_cell_magic('cython', '-2', cython3_code) ip.run_cell_magic('cython', '-2', cython3_code)
ip.ex('g = f(10); h = call(10)') ip.ex('g = f(10); h = call(10)')
self.assertEqual(ip.user_ns['g'], 2 // 10) self.assertEqual(ip.user_ns['g'], 2 // 10)
...@@ -138,6 +146,7 @@ class TestIPythonMagic(CythonTest): ...@@ -138,6 +146,7 @@ class TestIPythonMagic(CythonTest):
@skip_win32('Skip on Windows') @skip_win32('Skip on Windows')
def test_cython3_pgo(self): def test_cython3_pgo(self):
# The Cython cell defines the functions f() and call(). # The Cython cell defines the functions f() and call().
ip = self._ip
ip.run_cell_magic('cython', '-3 --pgo', pgo_cython3_code) ip.run_cell_magic('cython', '-3 --pgo', pgo_cython3_code)
ip.ex('g = f(10); h = call(10); main()') ip.ex('g = f(10); h = call(10); main()')
self.assertEqual(ip.user_ns['g'], 2.0 / 10.0) self.assertEqual(ip.user_ns['g'], 2.0 / 10.0)
...@@ -145,6 +154,7 @@ class TestIPythonMagic(CythonTest): ...@@ -145,6 +154,7 @@ class TestIPythonMagic(CythonTest):
@skip_win32('Skip on Windows') @skip_win32('Skip on Windows')
def test_extlibs(self): def test_extlibs(self):
ip = self._ip
code = py3compat.str_to_unicode(""" code = py3compat.str_to_unicode("""
from libc.math cimport sin from libc.math cimport sin
x = sin(0.0) x = sin(0.0)
...@@ -155,6 +165,7 @@ x = sin(0.0) ...@@ -155,6 +165,7 @@ x = sin(0.0)
def test_cython_verbose(self): def test_cython_verbose(self):
ip = self._ip
ip.run_cell_magic('cython', '--verbose', code) ip.run_cell_magic('cython', '--verbose', code)
ip.ex('g = f(10)') ip.ex('g = f(10)')
self.assertEqual(ip.user_ns['g'], 20.0) self.assertEqual(ip.user_ns['g'], 20.0)
...@@ -180,6 +191,7 @@ x = sin(0.0) ...@@ -180,6 +191,7 @@ x = sin(0.0)
finally: finally:
IpythonMagic.distutils.log = old_log IpythonMagic.distutils.log = old_log
ip = self._ip
with mock_distutils() as verbose_log: with mock_distutils() as verbose_log:
ip.run_cell_magic('cython', '--verbose', code) ip.run_cell_magic('cython', '--verbose', code)
ip.ex('g = f(10)') ip.ex('g = f(10)')
......
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