From ceaa482d16adfbd1609595a2ed6a241bad71f9de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Sun, 28 Aug 2022 17:08:51 +0200 Subject: [PATCH] fix: Allow printing non-string objects Issue #7: https://github.com/pawamoy/markdown-exec/issues/7 --- src/markdown_exec/formatters/python.py | 4 ++-- tests/test_python.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/markdown_exec/formatters/python.py b/src/markdown_exec/formatters/python.py index bab4ebc..f493308 100644 --- a/src/markdown_exec/formatters/python.py +++ b/src/markdown_exec/formatters/python.py @@ -11,8 +11,8 @@ from markdown_exec.rendering import code_block -def _buffer_print(buffer: StringIO, *text: str, end: str = "\n", **kwargs: Any) -> None: - buffer.write(" ".join(text) + end) +def _buffer_print(buffer: StringIO, *texts: str, end: str = "\n", **kwargs: Any) -> None: + buffer.write(" ".join(str(text) for text in texts) + end) def _run_python(code: str, **extra: str) -> str: diff --git a/tests/test_python.py b/tests/test_python.py index 8c86c22..923f4fe 100644 --- a/tests/test_python.py +++ b/tests/test_python.py @@ -61,3 +61,26 @@ def test_error_raised(md: Markdown, caplog) -> None: assert "ValueError" in html assert "oh no!" in html assert "Execution of python code block exited with non-zero status" in caplog.text + + +def test_can_print_non_string_objects(md: Markdown) -> None: + """Assert we can print non-string objects. + + Parameters: + md: A Markdown instance (fixture). + """ + html = md.convert( + dedent( + """ + ```python exec="yes" + class NonString: + def __str__(self): + return "string" + + nonstring = NonString() + print(nonstring, nonstring) + ``` + """ + ) + ) + assert "Traceback" not in html