Stop crashing on ANSI-colored numeric strings under maxcolwidths - #444
Open
afonsojanu wants to merge 1 commit into
Open
Stop crashing on ANSI-colored numeric strings under maxcolwidths#444afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 likeintorfloat, and the caller applies that constructor to the original, still-escaped string. A colored cell like\x1b[43m\x1b[30m3\x1b[0mreads as an int once the codes are stripped for classification, but callingint()on the raw string with the escape sequences still in it raisesValueError.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 thestrandbytesforms).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.