Skip to content

Commit

Permalink
fix: Allow printing non-string objects
Browse files Browse the repository at this point in the history
Issue #7: #7
  • Loading branch information
pawamoy committed Aug 28, 2022
1 parent f5b0b28 commit ceaa482
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/markdown_exec/formatters/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ceaa482

Please sign in to comment.