From e4e3d8eec8aa1d9d7a032a62410094e71f8aa653 Mon Sep 17 00:00:00 2001 From: Kotesh Kumar Yelamati Date: Fri, 3 Jul 2026 11:01:49 -0400 Subject: [PATCH] fix: guard against IndexError when repr() returns empty string formatPair() accessed value[0] unconditionally, crashing with IndexError when an object's __repr__ returns an empty string and the output is formatted across multiple lines (e.g. when the prefix is long). Add a len(value) >= 2 guard before the index access. --- icecream/icecream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/icecream/icecream.py b/icecream/icecream.py index 2808b75..f25c6dc 100644 --- a/icecream/icecream.py +++ b/icecream/icecream.py @@ -242,7 +242,7 @@ def formatPair(prefix: str, arg: Union[str, Sentinel], value: str) -> str: argLines = prefix_first_line_indent_remaining(prefix, arg) valuePrefix = argLines[-1] + ': ' - looksLikeAString = (value[0] + value[-1]) in ["''", '""'] + looksLikeAString = len(value) >= 2 and (value[0] + value[-1]) in ["''", '""'] if looksLikeAString: # Align the start of multiline strings. valueLines = prefix_lines(' ', value, startAtLine=1) value = '\n'.join(valueLines)