Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions src/ansi2html/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,12 @@ def prepare(
) -> Attributes:
"""Load the contents of 'ansi' into this object"""

if not isinstance(ansi, str):
raise TypeError(
"ansi must be a str, not %s; join an iterable of lines first, "
"e.g. ''.join(lines)" % type(ansi).__name__
)

body, styles = self.apply_regex(ansi)

if ensure_trailing_newline and _needs_extra_newline(body):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_ansi2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from typing import List
from unittest.mock import patch

import pytest

from ansi2html import Ansi2HTMLConverter
from ansi2html.converter import (
ANSI_BLINK_FAST,
Expand Down Expand Up @@ -57,6 +59,12 @@ def test_linkify(self) -> None:
html = Ansi2HTMLConverter(linkify=True).convert(ansi)
assert target in html

def test_convert_rejects_non_str_input(self) -> None:
# Passing a list of lines used to surface as an unhelpful
# AttributeError deep inside apply_regex (see #242).
with pytest.raises(TypeError, match="must be a str, not list"):
Ansi2HTMLConverter().convert(["a", "b"]) # type: ignore[arg-type]

def test_not_linkify(self) -> None:
ansi = "http://threebean.org"
target = '<a href="http://threebean.org">http://threebean.org</a>'
Expand Down
Loading