Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: unparsable cell is not idempotent (#7583)

Myles Scolnickc29b963d4da1
diff --git a/marimo/_ast/cell_manager.py b/marimo/_ast/cell_manager.py
index 73ff712f90..1e0b75994c 100644
--- a/marimo/_ast/cell_manager.py
+++ b/marimo/_ast/cell_manager.py
@@ -22,6 +22,7 @@ from marimo._ast.compiler import (
)
from marimo._ast.models import CellData
from marimo._ast.names import DEFAULT_CELL_NAME, SETUP_CELL_NAME
+from marimo._ast.parse import fixed_dedent
from marimo._ast.pytest import process_for_pytest
from marimo._schemas.serialization import (
CellDef,
@@ -268,18 +269,9 @@ class CellManager:
else:
cell_id = self.create_cell_id()
- # - code.split("\n")[1:-1] disregards first and last lines, which are
- # empty
- # - line[4:] removes leading indent in multiline string
- # - replace(...) unescapes double quotes
- # - rstrip() removes an extra newline
- code = "\n".join(
- [line[4:].replace('\\"', '"') for line in code.split("\n")[1:-1]]
- )
-
self.register_cell(
cell_id=cell_id,
- code=code,
+ code=fixed_dedent(code).strip(),
config=cell_config,
name=name or DEFAULT_CELL_NAME,
cell=None,
diff --git a/marimo/_ast/codegen.py b/marimo/_ast/codegen.py
index 9fd8dbabee..0f44ce7c96 100644
--- a/marimo/_ast/codegen.py
+++ b/marimo/_ast/codegen.py
@@ -365,11 +365,15 @@ def generate_unparsable_cell(
code: str, name: Optional[str], config: CellConfig
) -> str:
text = ["app._unparsable_cell("]
- # escape double quotes to not interfere with string
- quote_escaped_code = code.replace('"', '\\"')
- # use r-string to handle backslashes (don't want to write
- # escape characters, want to actually write backslash characters)
- code_as_str = f'r"""\n{quote_escaped_code}\n"""'
+ # If code contains triple quotes, we can't use raw strings with delimiters
+ # Instead, use a normal string with proper escaping
+ if '"""' in code:
+ # Use normal string with escaping for backslashes and quotes
+ escaped_code = code.replace("\\", "\\\\").replace('"', '\\"')
+ code_as_str = f'"""\n{escaped_code}\n"""'
+ else:
+ # Use raw string to preserve backslashes and other special chars
+ code_as_str = f'r"""\n{code}\n"""'
flags = {}
if config != CellConfig():
diff --git a/marimo/_ast/parse.py b/marimo/_ast/parse.py
index e98ebd9354..84b4a1376d 100644
--- a/marimo/_ast/parse.py
+++ b/marimo/_ast/parse.py
@@ -380,9 +380,25 @@ class Extractor:
# but mypy is struggling.
kwargs, _violations = _eval_kwargs(node.value.keywords) # type: ignore
violations.extend(_violations)
+ raw_string_node = node.value.args[0] # type: ignore
+ if not isinstance(raw_string_node, ast.Constant) or not isinstance(
+ raw_string_node.value, str
+ ):
+ violations.append(
+ Violation(
+ f"Expected string constant in unparsable cell, got {type(raw_string_node).__name__}",
+ lineno=node.lineno,
+ col_offset=node.col_offset,
+ )
+ )
+ unparsable_code = ""
+ else:
+ unparsable = raw_string_node.value
+ unparsable_code = fixed_dedent(unparsable).strip()
+
return ParseResult(
UnparsableCell(
- code=node.value.args[0].value, # type: ignore
+ code=unparsable_code,
options=kwargs,
_ast=node,
),
diff --git a/tests/_ast/codegen_data/test_generate_filecontents_with_syntax_error.py b/tests/_ast/codegen_data/test_generate_filecontents_with_syntax_error.py
index 56f41dba5a..c8150626a8 100644
--- a/tests/_ast/codegen_data/test_generate_filecontents_with_syntax_error.py
+++ b/tests/_ast/codegen_data/test_generate_filecontents_with_syntax_error.py
@@ -25,11 +25,11 @@ def _():
app._unparsable_cell(
- r"""
+ """
_ another_error
_ and \"\"\"another\"\"\"
- \t
+ \\t
""",
name="_"
)
diff --git a/tests/_ast/codegen_data/test_unparsable_cell_with_triple_quotes.py b/tests/_ast/codegen_data/test_unparsable_cell_with_triple_quotes.py
new file mode 100644
index 0000000000..92668fcfa0
--- /dev/null
+++ b/tests/_ast/codegen_data/test_unparsable_cell_with_triple_quotes.py
@@ -0,0 +1,32 @@
+import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def _():
+ x = 1
+ return
+
+
+app._unparsable_cell(
+ """
+ \"\"\"
+ ```python {.marimo}
+ print(\"Hello, World!\")
+ """,
+ name="_"
+)
+
+
+app._unparsable_cell(
+ r"""
+ it's an unparsable cell
+ """,
+ name="_"
+)
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/test_codegen.py b/tests/_ast/test_codegen.py
index 3d36632801..2d1e3cfb06 100644
--- a/tests/_ast/test_codegen.py
+++ b/tests/_ast/test_codegen.py
@@ -239,8 +239,220 @@ class TestGeneration:
# Verify the config is included in the output
assert "hide_code=True" in raw
- # Verify the code is properly escaped and included
- assert 'mo.md(\\"markdown in marimo\\")' in raw
+ # Verify the code is properly included (raw string for code without triple quotes)
+ assert 'mo.md("markdown in marimo")' in raw
+
+ @staticmethod
+ def test_generate_unparsable_cell_snapshots() -> None:
+ def generate(
+ code: str,
+ name: str | None = None,
+ config: CellConfig | None = None,
+ ) -> str:
+ result = codegen.generate_unparsable_cell(
+ code, name, config or CellConfig()
+ )
+ # Make sure it's valid Python
+ ast.parse(result)
+ return result
+
+ # Simple code without special characters
+ assert generate("x = 1") == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ x = 1
+ """
+)\
+'''
+ )
+
+ # Code with a name
+ assert generate("x = 1", "my_cell", CellConfig()) == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ x = 1
+ """,
+ name="my_cell"
+)\
+'''
+ )
+
+ # Code with escaped quotes (no triple quotes)
+ assert generate('x = "hello \\"world\\""') == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ x = "hello \\"world\\""
+ """
+)\
+'''
+ )
+
+ # Code with triple quotes - should use escaped string format
+ assert generate('x = """hello"""') == snapshot(
+ '''\
+app._unparsable_cell(
+ """
+ x = \\"\\"\\"hello\\"\\"\\"
+ """
+)\
+'''
+ )
+
+ # Code with backslashes (no triple quotes) - raw string preserves them
+ assert generate("path = 'C:\\\\Users\\\\test'") == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ path = 'C:\\\\Users\\\\test'
+ """
+)\
+'''
+ )
+
+ # Code with backslashes AND triple quotes - escaped string format
+ assert generate('path = """C:\\Users\\test"""') == snapshot(
+ '''\
+app._unparsable_cell(
+ """
+ path = \\"\\"\\"C:\\\\Users\\\\test\\"\\"\\"
+ """
+)\
+'''
+ )
+
+ # Code with all config options
+ assert generate(
+ "x = 1",
+ "named_cell",
+ CellConfig(disabled=True, hide_code=True, column=1),
+ ) == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ x = 1
+ """,
+ column=1, disabled=True, hide_code=True, name="named_cell"
+)\
+'''
+ )
+
+ # Multiline code
+ assert generate("x = 1\ny = 2\nz = x + y") == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ x = 1
+ y = 2
+ z = x + y
+ """
+)\
+'''
+ )
+
+ # Code with newlines and indentation
+ assert generate("def foo():\n return 1\n") == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ def foo():
+ return 1
+
+ """
+)\
+'''
+ )
+
+ # Empty code
+ assert generate("") == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+
+ """
+)\
+'''
+ )
+
+ # Code with only whitespace
+ assert generate(" ") == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ \n\
+ """
+)\
+'''
+ )
+
+ # Code with tabs
+ assert generate("x = 1\n\ty = 2") == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ x = 1
+ y = 2
+ """
+)\
+''' # noqa: E101
+ )
+
+ # Code with syntax error (common use case)
+ assert generate("return\nx.") == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ return
+ x.
+ """
+)\
+'''
+ )
+
+ # Code with unicode characters
+ assert generate('x = "héllo wörld 🎉"') == snapshot(
+ '''\
+app._unparsable_cell(
+ r"""
+ x = "héllo wörld 🎉"
+ """
+)\
+'''
+ )
+
+ # Edge cases that may produce invalid Python
+ assert generate('x = 1\n"""') == snapshot(
+ '''\
+app._unparsable_cell(
+ """
+ x = 1
+ \\"\\"\\"
+ """
+)\
+'''
+ )
+
+ assert generate('""")') == snapshot(
+ '''\
+app._unparsable_cell(
+ """
+ \\"\\"\\")
+ """
+)\
+'''
+ )
+
+ assert generate('app._unparsable_cell(r"""code""")') == snapshot(
+ '''\
+app._unparsable_cell(
+ """
+ app._unparsable_cell(r\\"\\"\\"code\\"\\"\\")
+ """
+)\
+'''
+ )
@staticmethod
def test_long_line_in_main() -> None:
@@ -610,6 +822,21 @@ class TestGeneration:
)
assert "import marimo" in source
+ @staticmethod
+ async def test_unparsable_cell_with_triple_quotes_roundtrip() -> None:
+ """Test that unparsable cells with triple quotes round-trip correctly.
+
+ This tests the idempotency of the codegen for unparsable cells that
+ contain triple quotes, which require escaped strings instead of raw
+ strings.
+ """
+ source = await get_idempotent_marimo_source(
+ "test_unparsable_cell_with_triple_quotes"
+ )
+ assert "import marimo" in source
+ # Verify the escaped triple quotes pattern is preserved
+ assert '"""' in source or '\\"\\"\\"' in source
+
@pytest.fixture
def marimo_app() -> App:
diff --git a/tests/_ast/test_parse.py b/tests/_ast/test_parse.py
index ce496a5e56..5d3f5473f9 100644
--- a/tests/_ast/test_parse.py
+++ b/tests/_ast/test_parse.py
@@ -4,7 +4,14 @@ from __future__ import annotations
import ast
from pathlib import Path
-from marimo._ast.parse import Parser, _eval_kwargs, parse_notebook
+from inline_snapshot import snapshot
+
+from marimo._ast.parse import (
+ Parser,
+ _eval_kwargs,
+ fixed_dedent,
+ parse_notebook,
+)
DIR_PATH = Path(__file__).parent
@@ -209,3 +216,158 @@ class TestParser:
assert "@my_decorator" in function_cell.code, (
f"Expected @my_decorator in extracted code, but got: {function_cell.code!r}"
)
+
+ @staticmethod
+ def test_unparsable_cell_with_non_string_creates_violation() -> None:
+ """Test that unparsable cells with non-string args create violations."""
+ code = """
+import marimo
+__generated_with = "0.0.0"
+app = marimo.App()
+
+app._unparsable_cell(123, name="_")
+
+if __name__ == "__main__":
+ app.run()
+"""
+ notebook = parse_notebook(code)
+ assert notebook is not None
+ assert len(notebook.violations) == 1
+ assert "Expected string constant" in notebook.violations[0].description
+
+
+def test_fixed_dedent() -> None:
+ # Basic dedent
+ assert fixed_dedent(" x = 1\n y = 2") == snapshot("""\
+x = 1
+y = 2\
+""")
+
+ # No indentation
+ assert fixed_dedent("x = 1\ny = 2") == snapshot("""\
+x = 1
+y = 2\
+""")
+
+ # Mixed indentation levels
+ assert fixed_dedent(" def foo():\n return 1") == snapshot("""\
+def foo():
+ return 1\
+""")
+
+ # Inconsistent indentation (main difference from textwrap.dedent)
+ assert fixed_dedent(" x = 1\ny = 2") == snapshot("""\
+x = 1
+y = 2\
+""")
+
+ # Empty string
+ assert fixed_dedent("") == snapshot("")
+
+ # Only whitespace
+ assert fixed_dedent(" ") == snapshot("")
+
+ # Blank lines in middle
+ assert fixed_dedent(" x = 1\n\n y = 2") == snapshot("""\
+x = 1
+
+y = 2\
+""")
+
+ # Leading blank lines
+ assert fixed_dedent("\n\n x = 1") == snapshot("""\
+
+
+x = 1\
+""")
+
+ # Trailing blank lines
+ assert fixed_dedent(" x = 1\n\n") == snapshot("x = 1\n")
+
+ # Tabs as indentation
+ assert fixed_dedent("\tx = 1\n\ty = 2") == snapshot("""\
+x = 1
+y = 2\
+""")
+
+ # Mixed tabs and spaces
+ assert fixed_dedent("\t x = 1\n\t y = 2") == snapshot("""\
+x = 1
+y = 2\
+""")
+
+ # Deeply nested code
+ assert fixed_dedent(
+ " def foo():\n if True:\n return 1"
+ ) == snapshot("""\
+def foo():
+ if True:
+ return 1\
+""")
+
+ # Multiline string preserved
+ assert fixed_dedent(' x = """hello\n world"""') == snapshot('''\
+x = """hello
+world"""\
+''')
+
+ # Code with comments
+ assert fixed_dedent(" # comment\n x = 1") == snapshot("""\
+# comment
+x = 1\
+""")
+
+ # Comment line without expected indent
+ assert fixed_dedent(
+ " x = 1\n# comment with no indent\n y = 2"
+ ) == snapshot("""\
+x = 1
+# comment with no indent
+y = 2\
+""")
+
+ # Escaped characters preserved
+ assert fixed_dedent(' x = "hello\\nworld"') == snapshot(
+ 'x = "hello\\nworld"'
+ )
+
+ # Unicode content
+ assert fixed_dedent(' x = "héllo wörld 🎉"') == snapshot(
+ 'x = "héllo wörld 🎉"'
+ )
+
+ # Docstring style content
+ assert fixed_dedent(
+ ' """This is a docstring.\n\n Args:\n x: something\n """'
+ ) == snapshot('''\
+"""This is a docstring.
+
+Args:
+ x: something
+"""\
+''')
+
+ # Single line with indentation
+ assert fixed_dedent(" single_line_code()") == snapshot(
+ "single_line_code()"
+ )
+
+ # Only blank lines
+ assert fixed_dedent("\n\n\n") == snapshot("\n\n\n")
+
+ # AI-generated code pattern with inconsistent indentation
+ assert fixed_dedent(" x = 1\n if True:\nreturn x") == snapshot(
+ "x = 1\nif True:\nreturn x"
+ )
+
+ # Class definition
+ assert fixed_dedent(
+ " class MyClass:\n def method(self):\n return 1"
+ ) == snapshot("""\
+class MyClass:
+ def method(self):
+ return 1\
+""")
+
+ # Two-space indentation
+ assert fixed_dedent(" x = 1\n y = 2") == snapshot("x = 1\ny = 2")
diff --git a/tests/_ast/test_transformers.py b/tests/_ast/test_transformers.py
index 82b474b8fa..5ca8bb295b 100644
--- a/tests/_ast/test_transformers.py
+++ b/tests/_ast/test_transformers.py
@@ -169,6 +169,9 @@ def test_compiled_ast() -> None:
)
),
]
+ # Fix missing lineno/col_offset required for compiling AST nodes
+ for stmt in statements:
+ ast.fix_missing_locations(stmt)
result = compiled_ast(statements)
assert isinstance(result, ast.Module)
@@ -210,6 +213,10 @@ def test_clean_to_modules() -> None:
value=ast.Constant(value=1),
)
]
+ # Fix missing lineno/col_offset required for compiling AST nodes
+ ast.fix_missing_locations(with_block)
+ for stmt in pre_block:
+ ast.fix_missing_locations(stmt)
pre_module, with_module = clean_to_modules(pre_block, with_block)
@@ -234,6 +241,8 @@ def test_clean_to_modules_no_optional_vars() -> None:
],
body=[ast.Expr(value=ast.Constant(value="hello"))],
)
+ # Fix missing lineno/col_offset required for compiling AST nodes
+ ast.fix_missing_locations(with_block)
pre_block = []
pre_module, with_module = clean_to_modules(pre_block, with_block)
diff --git a/tests/_convert/snapshots/dataflow.md.txt b/tests/_convert/snapshots/dataflow.md.txt
index f3603f082f..59c192ea35 100644
--- a/tests/_convert/snapshots/dataflow.md.txt
+++ b/tests/_convert/snapshots/dataflow.md.txt
@@ -444,4 +444,4 @@ tips = {
```python {.marimo}
import marimo as mo
-```
+```
\ No newline at end of file
diff --git a/tests/_convert/snapshots/marimo_for_jupyter_users.md.txt b/tests/_convert/snapshots/marimo_for_jupyter_users.md.txt
index dc1cf71c32..54e509c2ff 100644
--- a/tests/_convert/snapshots/marimo_for_jupyter_users.md.txt
+++ b/tests/_convert/snapshots/marimo_for_jupyter_users.md.txt
@@ -172,4 +172,4 @@ Check out [our docs](https://docs.marimo.io/) to learn more!
_This guide was adapted from [Pluto for Jupyter
users](https://featured.plutojl.org/basic/pluto%20for%20jupyter%20users).
-We ❤️ Pluto.jl!_
+We ❤️ Pluto.jl!_
\ No newline at end of file
diff --git a/tests/_convert/snapshots/sql.md.txt b/tests/_convert/snapshots/sql.md.txt
index 607a9a7e3a..96d42e0b28 100644
--- a/tests/_convert/snapshots/sql.md.txt
+++ b/tests/_convert/snapshots/sql.md.txt
@@ -327,4 +327,4 @@ import random
```python {.marimo hide_code="true"}
import string
-```
+```
\ No newline at end of file
diff --git a/tests/_convert/snapshots/unparsable_cell_with_escaped_quotes.py.txt b/tests/_convert/snapshots/unparsable_cell_with_escaped_quotes.py.txt
new file mode 100644
index 0000000000..10bc1d9d87
--- /dev/null
+++ b/tests/_convert/snapshots/unparsable_cell_with_escaped_quotes.py.txt
@@ -0,0 +1,47 @@
+import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+app._unparsable_cell(
+ r"""
+ return
+ """,
+ name="_"
+)
+
+
+app._unparsable_cell(
+ r"""
+ # Single quote string
+ x = "hello \"world\"
+ x.
+ """,
+ name="_"
+)
+
+
+app._unparsable_cell(
+ r"""
+ # Triple quote string
+ x = "hello \"\"\"world\"\"\""
+ x.
+ """,
+ name="_"
+)
+
+
+app._unparsable_cell(
+ r"""
+ # Triple quote string with other slashes
+ x = "hello \"\"\"world\"\"\"
+ path = "C:\\Users\\test"
+ x.
+ """,
+ name="_"
+)
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_convert/snapshots/unsafe-doc-old.md.txt b/tests/_convert/snapshots/unsafe-doc-old.md.txt
deleted file mode 100644
index 42c99eee78..0000000000
--- a/tests/_convert/snapshots/unsafe-doc-old.md.txt
+++ /dev/null
@@ -1,84 +0,0 @@
----
-title: Casually malicious md
-marimo-version: 0.0.0
----
-
-What happens if I just leave a \"\"\"
-" ' ! @ # $ % ^ & * ( ) + = - _ [ ] { } | \ /
-
-# Notebook
-<!--
-\
-
-```python {.marimo}
-print("Hello, World!")
-```
-
-````python {.marimo hide_code="true"}
-mo.md(r"""
--->
-
-```marimo run convert document.md```
-""")
-````
-
-```python {.marimo unparsable="true"}
-it's an unparsable cell
-```
-
-````python {.marimo hide_code="true"}
-mo.md(r"""
-<!-- Actually markdown -->
-```{python} `
- print("Hello, World!")
-
-<!-- Disabled code block -->
-""")
-````
-
-```python {.marimo disabled="true"}
-1 + 1
-```
-
-<!-- Hidden code block -->
-
-```python {.marimo hide_code="true"}
-1 + 1
-```
-
-<!-- Empty code block -->
-
-```python {.marimo}
-
-```
-
-<!-- Improperly nested code block -->
-
-````python {.marimo unparsable="true"}
-"""
-```{python}
-print("Hello, World!")
-````
-
-\"\"\"
-```
-
-<!-- Improperly nested code block -->
-```{python}
-````{python}
-print("Hello, World!")
-````
-```
-
--->
-
-<!-- from the notebook, should remain unchanged -->
-
-````python {.marimo}
-mo.md("""
-This is a markdown cell with an execution block in it
-```{python}
-# To ambiguous to convert
-```
-""")
-````
\ No newline at end of file
diff --git a/tests/_convert/snapshots/unsafe-doc-old.py.txt b/tests/_convert/snapshots/unsafe-doc-old.py.txt
deleted file mode 100644
index 643f17808a..0000000000
--- a/tests/_convert/snapshots/unsafe-doc-old.py.txt
+++ /dev/null
@@ -1,139 +0,0 @@
-import marimo
-
-__generated_with = "0.0.0"
-app = marimo.App(app_title="Casually malicious md")
-
-
[email protected](hide_code=True)
-def _(mo):
- mo.md(r"""
- What happens if I just leave a \"\"\"
- " ' ! @ # $ % ^ & * ( ) + = - _ [ ] { } | \ /
-
- # Notebook
- <!--
- \
- """)
- return
-
-
-def _():
- print("Hello, World!")
- return
-
-
[email protected](hide_code=True)
-def _(mo):
- mo.md(r"""
- -->
-
- ```marimo run convert document.md```
- """)
- return
-
-
-app._unparsable_cell(
- r"""
- it's an unparsable cell
- """,
- name="_"
-)
-
-
[email protected](hide_code=True)
-def _(mo):
- mo.md(r"""
- <!-- Actually markdown -->
- ```{python} `
- print("Hello, World!")
-
- <!-- Disabled code block -->
- """)
- return
-
-
[email protected](disabled=True)
-def _():
- 1 + 1
- return
-
-
[email protected](hide_code=True)
-def _(mo):
- mo.md(r"""
- <!-- Hidden code block -->
- """)
- return
-
-
[email protected](hide_code=True)
-def _():
- 1 + 1
- return
-
-
[email protected](hide_code=True)
-def _(mo):
- mo.md(r"""
- <!-- Empty code block -->
- """)
- return
-
-
-def _():
- return
-
-
[email protected](hide_code=True)
-def _(mo):
- mo.md(r"""
- <!-- Improperly nested code block -->
- """)
- return
-
-
-app._unparsable_cell(
- r"""
- \"\"\"
- ```{python}
- print(\"Hello, World!\")
- """,
- name="_"
-)
-
-
[email protected](hide_code=True)
-def _(mo):
- mo.md(r"""
- \"\"\"
- ```
-
- <!-- Improperly nested code block -->
- ```{python}
- ````{python}
- print("Hello, World!")
- ````
- ```
-
- -->
-
- <!-- from the notebook, should remain unchanged -->
- """)
- return
-
-
-def _(mo):
- mo.md("""
- This is a markdown cell with an execution block in it
- ```{python}
- # To ambiguous to convert
- ```
- """)
- return
-
-
-if __name__ == "__main__":
- app.run()
\ No newline at end of file
diff --git a/tests/_convert/snapshots/unsafe-doc.py.txt b/tests/_convert/snapshots/unsafe-doc.py.txt
index b5e7f8460a..e727c024ed 100644
--- a/tests/_convert/snapshots/unsafe-doc.py.txt
+++ b/tests/_convert/snapshots/unsafe-doc.py.txt
@@ -95,7 +95,7 @@ def _(mo):
app._unparsable_cell(
- r"""
+ """
\"\"\"
```python {.marimo}
print(\"Hello, World!\")
diff --git a/tests/_convert/test_basic_marimo_example.py b/tests/_convert/test_convert_py.py
similarity index 52%
rename from tests/_convert/test_basic_marimo_example.py
rename to tests/_convert/test_convert_py.py
index 8d5ff227c0..747c4763f5 100644
--- a/tests/_convert/test_basic_marimo_example.py
+++ b/tests/_convert/test_convert_py.py
@@ -3,6 +3,7 @@ from __future__ import annotations
from textwrap import dedent
+from marimo import __version__
from marimo._convert.converters import MarimoConvert
from tests.mocks import snapshotter
@@ -60,3 +61,66 @@ def test_basic_marimo_example_jupytext_compatibility():
# Test conversion back from markdown to marimo
converted_back = MarimoConvert.from_md(converted_to_md).to_py()
snapshot("basic_marimo_example_roundtrip.py.txt", converted_back)
+
+
+def test_unparsable_cell_with_escaped_quotes():
+ """Test an unparsable cell with escaped quotes."""
+ marimo_script = dedent(rf'''
+ import marimo
+
+ __generated_with = "{__version__}"
+ app = marimo.App()
+
+
+ app._unparsable_cell(
+ r"""
+ return
+ """,
+ name="_"
+ )
+
+
+ app._unparsable_cell(
+ r"""
+ # Single quote string
+ x = "hello \"world\"
+ x.
+ """,
+ name="_"
+ )
+
+
+ app._unparsable_cell(
+ r"""
+ # Triple quote string
+ x = "hello \"\"\"world\"\"\""
+ x.
+ """,
+ name="_"
+ )
+
+
+ app._unparsable_cell(
+ r"""
+ # Triple quote string with other slashes
+ x = "hello \"\"\"world\"\"\"
+ path = "C:\\Users\\test"
+ x.
+ """,
+ name="_"
+ )
+
+
+ if __name__ == "__main__":
+ app.run()
+ ''')[1:]
+
+ def identity(x: str) -> str:
+ return MarimoConvert.from_py(x).to_py()
+
+ assert identity(marimo_script) == marimo_script
+ assert identity(identity(marimo_script)) == marimo_script
+ assert identity(identity(identity(marimo_script))) == marimo_script
+ snapshot(
+ "unparsable_cell_with_escaped_quotes.py.txt", identity(marimo_script)
+ )
diff --git a/tests/_convert/test_markdown_conversion.py b/tests/_convert/test_markdown_conversion.py
index 619e46a4c6..29d44594a2 100644
--- a/tests/_convert/test_markdown_conversion.py
+++ b/tests/_convert/test_markdown_conversion.py
@@ -364,183 +364,6 @@ def test_python_to_md_code_injection() -> None:
)
-def test_old_md_to_python_code_injection() -> None:
- script = dedent(
- remove_empty_lines(
- """
- ---
- title: "Casually malicious md"
- ---
-
- What happens if I just leave a \"""
- " ' ! @ # $ % ^ & * ( ) + = - _ [ ] { } | \\ /
-
- # Notebook
- <!--
- \\
- ```{.python.marimo}
- print("Hello, World!")
- ```
- -->
-
- ```marimo run convert document.md```
-
- ```{python}
- it's an unparsable cell
- ```
-
- <!-- Actually markdown -->
- ```{python} `
- print("Hello, World!")
-
- <!-- Disabled code block -->
- ```{python disabled="true"}
- 1 + 1
- ```
-
- <!-- Hidden code block -->
- ```{python hide_code="true"}
- 1 + 1
- ```
-
- <!-- Empty code block -->
- ```{python}
- ```
-
- <!-- Improperly nested code block -->
- ```{python}
- \"""
- ```{python}
- print("Hello, World!")
- ```
- \"""
- ```
-
- <!-- Improperly nested code block -->
- ```{python}
- ````{python}
- print("Hello, World!")
- ````
- ```
-
- -->
-
- <!-- from the notebook, should remain unchanged -->
- ````{.python.marimo}
- mo.md(\"""
- This is a markdown cell with an execution block in it
- ```{python}
- # To ambiguous to convert
- ```
- \""")
- ````
-
- """
- )
- )
-
- maybe_unsafe_py = sanitized_version(md_to_py(script).strip())
- assert "Casually malicious md" in maybe_unsafe_py
-
- maybe_unsafe_md = convert_from_py(maybe_unsafe_py)
- assert "Casually malicious md" in maybe_unsafe_md
-
- # Idempotent even under strange conditions.
- assert maybe_unsafe_py == sanitized_version(
- md_to_py(maybe_unsafe_md).strip()
- )
-
- snapshot("unsafe-doc-old.py.txt", maybe_unsafe_py)
- snapshot("unsafe-doc-old.md.txt", maybe_unsafe_md)
-
-
-def test_md_to_python_code_injection() -> None:
- script = dedent(
- remove_empty_lines(
- """
- ---
- title: "Casually malicious md"
- ---
-
- What happens if I just leave a \"""
- " ' ! @ # $ % ^ & * ( ) + = - _ [ ] { } | \\ /
-
- # Notebook
- <!--
- \\
- ```python {.marimo}
- print("Hello, World!")
- ```
- -->
-
- ```marimo run convert document.md```
-
- ```python {.marimo}
- it's an unparsable cell
- ```
-
- <!-- Actually markdown -->
- ```python {.marimo} `
- print("Hello, World!")
-
- <!-- Disabled code block -->
- ```python {.marimo disabled="true"}
- 1 + 1
- ```
-
- <!-- Hidden code block -->
- ```python {.marimo hide_code="true"}
- 1 + 1
- ```
-
- <!-- Empty code block -->
- ```python {.marimo}
- ```
-
- <!-- Improperly nested code block -->
- ```python {.marimo}
- \"""
- ```python {.marimo}
- print("Hello, World!")
- ```
- \"""
- ```
-
- <!-- Improperly nested code block -->
- ```python {.marimo}
- ````python {.marimo}
- print("Hello, World!")
- ````
- ```
-
- -->
-
- <!-- from the notebook, should remain unchanged -->
- ````python {.marimo}
- mo.md(\"""
- This is a markdown cell with an execution block in it
- ```python {.marimo}
- # To ambiguous to convert
- ```
- \""")
- ````
-
- """
- )
- )
-
- maybe_unsafe_py = sanitized_version(md_to_py(script).strip())
- maybe_unsafe_md = convert_from_py(maybe_unsafe_py)
-
- # Idempotent even under strange conditions.
- assert maybe_unsafe_py == sanitized_version(
- md_to_py(maybe_unsafe_md).strip()
- )
-
- snapshot("unsafe-doc.py.txt", maybe_unsafe_py)
- snapshot("unsafe-doc.md.txt", maybe_unsafe_md)
-
-
def remove_empty_lines(s: str) -> str:
"Just remove the first and last lines if they are empty"
lines = s.splitlines()