Skip to content

Commit

Permalink
gh-38945: Allow specifying arguments to Cython cell_magic
Browse files Browse the repository at this point in the history
    
As seen in the code, this allows user to write code like

```
%%cython --verbose=1
def f():
    print(1)
```

and have the `verbose=1` passed to Cython compilation function.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion. (not aware of one)
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

------

Test failure is unrelated
`https://github.com/sagemath/sage/issues/33906` (fixed in newest
version)
    
URL: #38945
Reported by: user202729
Reviewer(s): Kwankyu Lee
  • Loading branch information
Release Manager committed Dec 8, 2024
2 parents 2fe0ab0 + 5784784 commit 16f3c14
Showing 1 changed file with 73 additions and 5 deletions.
78 changes: 73 additions & 5 deletions src/sage/repl/ipython_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,27 +342,95 @@ def cython(self, line, cell):
INPUT:
- ``line`` -- ignored
- ``line`` -- parsed as keyword arguments. The allowed arguments are:
- ``--verbose N`` / ``-v N``
- ``--compile-message``
- ``--use-cache``
- ``--create-local-c-file``
- ``--annotate``
- ``--sage-namespace``
- ``--create-local-so-file``
- ``--no-compile-message``, ``--no-use-cache``, etc.
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:
Test unrecognized arguments::
sage: # needs sage.misc.cython
sage: shell.run_cell('''
....: %%cython --some-unrecognized-argument
....: print(1)
....: ''')
UsageError: unrecognized arguments: --some-unrecognized-argument
Test ``--help`` is disabled::
sage: # needs sage.misc.cython
sage: shell.run_cell('''
....: %%cython --help
....: print(1)
....: ''')
UsageError: unrecognized arguments: --help
Test invalid quotes (see :mod:`sage.repl.interpreter` for explanation of the dummy line)::
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

class ExitCatchingArgumentParser(argparse.ArgumentParser):
def error(self, message):
# exit_on_error=False does not work completely in some Python versions
# see https://stackoverflow.com/q/67890157
# we raise UsageError to make the interface similar to what happens when e.g.
# IPython's ``%run`` gets unrecognized arguments
from IPython.core.error import UsageError
raise UsageError(message)

parser = ExitCatchingArgumentParser(prog="%%cython", add_help=False)
parser.add_argument("--verbose", "-v", type=int)
parser.add_argument("--compile-message", action=argparse.BooleanOptionalAction)
parser.add_argument("--use-cache", action=argparse.BooleanOptionalAction)
parser.add_argument("--create-local-c-file", action=argparse.BooleanOptionalAction)
parser.add_argument("--annotate", action=argparse.BooleanOptionalAction)
parser.add_argument("--sage-namespace", action=argparse.BooleanOptionalAction)
parser.add_argument("--create-local-so-file", action=argparse.BooleanOptionalAction)
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 16f3c14

Please sign in to comment.