Skip to content

Commit

Permalink
Allow specifying arguments to Cython cell_magic
Browse files Browse the repository at this point in the history
  • Loading branch information
user202729 committed Nov 9, 2024
1 parent 1b3f398 commit 0f3d239
Showing 1 changed file with 68 additions and 5 deletions.
73 changes: 68 additions & 5 deletions src/sage/repl/ipython_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,27 +342,90 @@ def cython(self, line, cell):
INPUT:
- ``line`` -- ignored
- ``line`` -- parsed as keyword arguments. See :func:`~sage.misc.cython.cython` for details.
- ``cell`` -- string; the Cython source code to process
OUTPUT: none; the Cython code is compiled and loaded
EXAMPLES::
sage: # needs sage.misc.cython
sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.run_cell( # needs sage.misc.cython
sage: shell.run_cell(
....: '''
....: %%cython
....: %%cython -v1 --annotate --no-sage-namespace
....: def f():
....: print('test')
....: ''')
sage: f() # needs sage.misc.cython
Compiling ....pyx because it changed.
[1/1] Cythonizing ....pyx
sage: f()
test
TESTS:
See :mod:`sage.repl.interpreter` for explanation of the dummy line.
Test unrecognized arguments::
sage: # needs sage.misc.cython
sage: print("dummy line"); shell.run_cell('''

Check failure on line 374 in src/sage/repl/ipython_extension.py

View workflow job for this annotation

GitHub Actions / test-new

Failed example:

Failed example:: Got: usage: sage-runtests [--verbose VERBOSE] [--compile-message] [--no-compile-message] [--use-cache] [--no-use-cache] [--create-local-c-file] [--no-create-local-c-file] [--annotate] [--no-annotate] [--sage-namespace] [--no-sage-namespace] [--create-local-so-file] [--no-create-local-so-file] sage-runtests: error: unrecognized arguments: --some-unrecognized-argument dummy line An exception has occurred, use %tb to see the full traceback. SystemExit: 2 doctest:warning File "<doctest sage.repl.ipython_extension.SageMagics.cython[4]>", line 1, in <module> print("dummy line"); shell.run_cell(''' File "/sage/src/sage/repl/interpreter.py", line 415, in run_cell super().run_cell(*args, **kwds) File "/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3048, in run_cell result = self._run_cell( File "/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3103, in _run_cell result = runner(coro) File "/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner coro.send(None) File "/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3308, in run_cell_async has_raised = await self.run_ast_nodes(code_ast.body, cell_name, File "/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3490, in run_ast_nodes if await self.run_code(code, result, async_=asy): File "/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3558, in run_code warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) File "/usr/lib/python3.10/warnings.py", line 109, in _showwarnmsg sw(msg.message, msg.category, msg.filename, msg.lineno, : UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
....: %%cython --some-unrecognized-argument
....: print(1)
....: ''')
dummy line
...
ArgumentError...Traceback (most recent call last)
...
ArgumentError: unrecognized arguments: --some-unrecognized-argument
Test ``--help`` is disabled::
sage: # needs sage.misc.cython
sage: print("dummy line"); shell.run_cell('''

Check failure on line 387 in src/sage/repl/ipython_extension.py

View workflow job for this annotation

GitHub Actions / test-new

Failed example:

Failed example:: Got: usage: sage-runtests [--verbose VERBOSE] [--compile-message] [--no-compile-message] [--use-cache] [--no-use-cache] [--create-local-c-file] [--no-create-local-c-file] [--annotate] [--no-annotate] [--sage-namespace] [--no-sage-namespace] [--create-local-so-file] [--no-create-local-so-file] sage-runtests: error: unrecognized arguments: --help dummy line An exception has occurred, use %tb to see the full traceback. SystemExit: 2
....: %%cython --help
....: print(1)
....: ''')
dummy line
...
ArgumentError...Traceback (most recent call last)
...
ArgumentError: unrecognized arguments: --help
Test invalid quotes::
sage: # needs sage.misc.cython
sage: print("dummy line"); shell.run_cell('''
....: %%cython --a='
....: print(1)
....: ''')
dummy line
...
ValueError...Traceback (most recent call last)
...
ValueError: No closing quotation
"""
from sage.misc.cython import cython_compile
return cython_compile(cell)
import shlex
import argparse

parser = argparse.ArgumentParser(add_help=False, exit_on_error=False)
parser.add_argument("--verbose", "-v", type=int)
for (arg, arg_short) in [
("compile-message", "m"),
("use-cache", "c"),
("create-local-c-file", "l"),
("annotate", "a"),
("sage-namespace", "s"),
("create-local-so-file", "o"),
]:
action = parser.add_argument(f"--{arg}", f"-{arg_short}", action="store_true", default=None)
parser.add_argument(f"--no-{arg}", action="store_false", dest=action.dest, default=None)

args = parser.parse_args(shlex.split(line))
return cython_compile(cell, **{k: v for k, v in args.__dict__.items() if v is not None})

@cell_magic
def fortran(self, line, cell):
Expand Down

0 comments on commit 0f3d239

Please sign in to comment.