Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add blacklist for magic commands, since some break the kernel #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/scorep_jupyter/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class ScorepPythonKernel(IPythonKernel):
def __init__(self, **kwargs):
super().__init__(**kwargs)

self.whitelist_prefixes_cell = ['%%prun', '%%timeit', '%%capture', '%%python', '%%pypy']
self.whitelist_prefixes_line = ['%prun', '%time']

self.blacklist_prefixes = ['%lsmagic']

self.scorep_binding_args = []
self.scorep_env = {}

Expand Down Expand Up @@ -388,16 +393,18 @@ async def do_execute(self, code, silent, store_history=False, user_expressions=N
else:
# Some line/cell magics involve executing Python code, which must be parsed
# TODO: timeit, python, ...? do not save variables to globals()
whitelist_prefixes_cell = ['%%prun', '%%timeit', '%%capture', '%%python', '%%pypy']
whitelist_prefixes_line = ['%prun', '%time']

nomagic_code = '' # Code to be parsed for user variables
if not code.startswith(tuple(['%', '!'])): # No IPython magics and shell commands
nomagic_code = code
else:
if code.startswith(tuple(whitelist_prefixes_cell)): # Cell magic, remove first line
if code.startswith(tuple(self.blacklist_prefixes)):
self.cell_output(code + "is not supposed to be executed with this kernel due to compatibility "
"issues", 'stderr')
return self.standard_reply()

if code.startswith(tuple(self.whitelist_prefixes_cell)): # Cell magic, remove first line
nomagic_code = code.split("\n", 1)[1]
elif code.startswith(tuple(whitelist_prefixes_line)): # Line magic, remove first word
elif code.startswith(tuple(self.whitelist_prefixes_line)): # Line magic, remove first word
nomagic_code = code.split(" ", 1)[1]
if nomagic_code:
# Parsing for user variables might fail due to SyntaxError
Expand Down