Skip to content

Stop crashing on ANSI-colored numeric strings under maxcolwidths - #444

Open
afonsojanu wants to merge 1 commit into
astanin:masterfrom
afonsojanu:fix/colored-numeric-cell-maxcolwidths-crash
Open

Stop crashing on ANSI-colored numeric strings under maxcolwidths#444
afonsojanu wants to merge 1 commit into
astanin:masterfrom
afonsojanu:fix/colored-numeric-cell-maxcolwidths-crash

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes #359.

When maxcolwidths triggers text wrapping, each cell that isn't already recognized as numeric gets cast through _type(cell, numparse)(cell) before wrapping. _type() strips ANSI escapes internally just to classify the value, then hands back a constructor like int or float, and the caller applies that constructor to the original, still-escaped string. A colored cell like \x1b[43m\x1b[30m3\x1b[0m reads as an int once the codes are stripped for classification, but calling int() on the raw string with the escape sequences still in it raises ValueError.

Since the cell is already a string in this situation, there's no need to cast it at all. The existing branch right above already skips the cast for values that already look like numbers via _isnumber(), so this just extends that same short-circuit to strings carrying ANSI codes (covering both the str and bytes forms).

Added a regression test (test_colored_number_with_maxcolwidths) confirming the crash is gone and the color codes survive. Confirmed it fails with the old code and passes with the fix. Full suite: 366 passing, 1 pre-existing skip, no regressions.

When maxcolwidths triggers text wrapping, each cell gets cast through
_type(cell, numparse)(cell) before wrapping. _type() strips ANSI
escapes internally just to classify the value, then hands back a
constructor like int or float, which the caller applies to the
original, still-escaped string. A colored cell like a yellow-and-black
"3" reads as an int once the codes are stripped, but int() on the raw
string with the escape sequences still in it just raises ValueError.

Since the cell is already a string in this case, there's no need to
cast it at all: the existing branch right above already skips casting
for values that already look like numbers via _isnumber(), so this
just extends that same short-circuit to strings carrying ANSI codes,
covering both the str and bytes forms.

Fixes astaninGH-359.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

type error when using maxcolwidths with colored strings

1 participant