diff --git a/jupytext/magics.py b/jupytext/magics.py index a946e96ce..9dbfc5d0e 100644 --- a/jupytext/magics.py +++ b/jupytext/magics.py @@ -18,7 +18,8 @@ # Commands starting with a question or exclamation mark have to be escaped _PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*(\?|!)\s*[A-Za-z]") -_PYTHON_MAGIC_CMD = re.compile(r"^(# |#)*({})(\s|$)".format('|'.join( +# A bash command not followed by an equal sign or a parenthesis is a magic command +_PYTHON_MAGIC_CMD = re.compile(r"^(# |#)*({})($|\s$|\s[^=,])".format('|'.join( # posix ['cat', 'cp', 'mv', 'rm', 'rmdir', 'mkdir'] + # windows diff --git a/tests/test_escape_magics.py b/tests/test_escape_magics.py index 21acab138..60a69c0ce 100644 --- a/tests/test_escape_magics.py +++ b/tests/test_escape_magics.py @@ -108,13 +108,15 @@ def test_force_comment_using_contents_manager(tmpdir): assert '%pylab inline' in stream.read().splitlines() -@pytest.mark.parametrize('magic_cmd', ['ls', '!ls', 'ls -al', '!whoami', '# ls', '# mv a b', '! mkdir tmp']) +@pytest.mark.parametrize('magic_cmd', ['ls', '!ls', 'ls -al', '!whoami', '# ls', '# mv a b', '! mkdir tmp', + 'cat', 'cat ', 'cat hello.txt', 'cat --option=value hello.txt']) def test_comment_bash_commands_in_python(magic_cmd): assert comment_magic([magic_cmd]) == ['# ' + magic_cmd] assert uncomment_magic(['# ' + magic_cmd]) == [magic_cmd] -@pytest.mark.parametrize('not_magic_cmd', ['copy(a)', 'copy.deepcopy']) +@pytest.mark.parametrize('not_magic_cmd', + ['copy(a)', 'copy.deepcopy', 'cat = 3', 'cat=5', 'cat, other = 5,3', 'cat(5)']) def test_do_not_comment_python_cmds(not_magic_cmd): assert comment_magic([not_magic_cmd]) == [not_magic_cmd] assert uncomment_magic([not_magic_cmd]) == [not_magic_cmd]