Commit a4440014 authored by Stefan Behnel's avatar Stefan Behnel

automatically enable language level 3 if jupyter kernel uses Python 3

parent 95768141
......@@ -53,6 +53,10 @@ Bugs fixed
Other changes
-------------
* The "%%cython" IPython/jupyter magic now defaults to the language level of
the current jupyter kernel. The language level can be set explicitly with
"%%cython -2" or "%%cython -3".
* Usage of ``Cython.Distutils.build_ext`` is now discouraged.
......
......@@ -152,8 +152,12 @@ class CythonMagics(Magics):
@magic_arguments.magic_arguments()
@magic_arguments.argument(
'-3', dest='cython3', action='store_true', default=False,
help="Switch to Python 3 syntax."
'-3', dest='language_level', action='store_const', const=3, default=None,
help="Select Python 3 syntax."
)
@magic_arguments.argument(
'-2', dest='language_level', action='store_const', const=2, default=None,
help="Select Python 2 syntax."
)
@magic_arguments.argument(
'-c', '--compile-args', action='append', default=[],
......@@ -272,7 +276,10 @@ class CythonMagics(Magics):
annotate=args.annotate,
force=True,
)
if args.cython3:
if args.language_level is not None:
assert args.language_level in (2, 3)
opts['language_level'] = args.language_level
elif sys.version_info[0] > 2:
opts['language_level'] = 3
build_extension.extensions = cythonize([extension], **opts)
except CompileError:
......
......@@ -28,7 +28,7 @@ def f(x):
""")
cython3_code = py3compat.str_to_unicode("""\
def f(x):
def f(int x):
return 2 / x
def call(x):
......@@ -87,13 +87,19 @@ class TestIPythonMagic(CythonTest):
self.assertEqual(ip.user_ns['g'], 20.0)
def test_cython3(self):
# The Cython module named 'mymodule' defines the function f.
# The Cython cell defines the functions f() and call().
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)
def test_cython2(self):
# The Cython cell defines the functions f() and call().
ip.run_cell_magic('cython', '-2', cython3_code)
ip.ex('g = f(10); h = call(10)')
self.assertEqual(ip.user_ns['g'], 2 // 10)
self.assertEqual(ip.user_ns['h'], 2 // 10)
@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