diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2016-07-08 13:22:53 +0200 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2016-07-15 08:22:26 +0200 |
commit | b5846e0cf0d26587ce59021daf15c1ffd09a5150 (patch) | |
tree | f49dee8c3ecb94a11bf4dff15579377a0ab84133 | |
parent | 7fa11da9a20cecab6e912c690d4ac33e69d4f986 (diff) | |
download | cython-b5846e0cf0d26587ce59021daf15c1ffd09a5150.tar.gz |
support "%%cython -3" cell magic in IPython
-rw-r--r-- | Cython/Build/IpythonMagic.py | 12 | ||||
-rw-r--r-- | Cython/Build/Tests/TestIpythonMagic.py | 19 |
2 files changed, 27 insertions, 4 deletions
diff --git a/Cython/Build/IpythonMagic.py b/Cython/Build/IpythonMagic.py index 88900960b..9ae39c82a 100644 --- a/Cython/Build/IpythonMagic.py +++ b/Cython/Build/IpythonMagic.py @@ -152,6 +152,10 @@ class CythonMagics(Magics): @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` " "Extension flag (can be specified multiple times)." @@ -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 diff --git a/Cython/Build/Tests/TestIpythonMagic.py b/Cython/Build/Tests/TestIpythonMagic.py index 5ec233670..8ee2d67ce 100644 --- a/Cython/Build/Tests/TestIpythonMagic.py +++ b/Cython/Build/Tests/TestIpythonMagic.py @@ -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(""" |