Commit b5846e0c authored by Stefan Behnel's avatar Stefan Behnel

support "%%cython -3" cell magic in IPython

parent 7fa11da9
......@@ -151,6 +151,10 @@ class CythonMagics(Magics):
self._import_all(module)
@magic_arguments.magic_arguments()
@magic_arguments.argument(
'-3', dest='cython3', action='store_true', default=False,
help="Switch to Python 3 syntax."
)
@magic_arguments.argument(
'-c', '--compile-args', action='append', default=[],
help="Extra flags to pass to compiler via the `extra_compile_args` "
......@@ -265,9 +269,11 @@ class CythonMagics(Magics):
try:
opts = dict(
quiet=quiet,
annotate = args.annotate,
force = True,
annotate=args.annotate,
force=True,
)
if args.cython3:
opts['language_level'] = 3
build_extension.extensions = cythonize([extension], **opts)
except CompileError:
return
......
......@@ -22,10 +22,19 @@ except ImportError:
from Cython.TestUtils import CythonTest
ip = get_ipython()
code = py3compat.str_to_unicode("""def f(x):
code = py3compat.str_to_unicode("""\
def f(x):
return 2*x
""")
cython3_code = py3compat.str_to_unicode("""\
def f(x):
return 2 / x
def call(x):
return f(*(x,))
""")
if sys.platform == 'win32':
# not using IPython's decorators here because they depend on "nose"
......@@ -77,6 +86,14 @@ class TestIPythonMagic(CythonTest):
ip.ex('import mymodule; g = mymodule.f(10)')
self.assertEqual(ip.user_ns['g'], 20.0)
def test_cython3(self):
# The Cython module named 'mymodule' defines the function f.
ip.run_cell_magic('cython', '-3', cython3_code)
# This module can now be imported in the interactive namespace.
ip.ex('g = f(10); h = call(10)')
self.assertEqual(ip.user_ns['g'], 2.0 / 10.0)
self.assertEqual(ip.user_ns['h'], 2.0 / 10.0)
@skip_win32
def test_extlibs(self):
code = py3compat.str_to_unicode("""
......
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