Skip to content

Commit

Permalink
Add unit test to check invalid ANSI sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Nov 28, 2021
1 parent 87e7b4e commit 47cb7f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 5 additions & 4 deletions colorama/ansitowin32.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,17 @@ def extract_params(self, command, paramstring):
def call_win32(self, command, params):
if command == 'm':
# Ansi sequences started by specific param may need to be ignored, see #217
skip = False
skip, skip_count = False, None
for param in params:
if skip:
if skip is not True:
skip -= 1
if skip_count is not None:
skip_count -= 1
continue
if param in (2, 5):
skip = 1 if param == 5 else 3
skip_count = 1 if param == 5 else 3
continue
skip = False
skip_count = False
if param in self.win32_calls:
func_args = self.win32_calls[param]
func = func_args[0]
Expand Down
16 changes: 16 additions & 0 deletions colorama/tests/ansitowin32_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ def testCallWin32UsesLookup(self):
[a[0][0] for a in listener.call_args_list],
[33, 11, 22] )

def testInvalidSequencesAreSkipped(self):
sequences = [
(38, 5, 46),
(38, 2, 120, 33, 255),
(48, 5, 31),
(48, 2, 166, 226, 46),
(38, 48, 5, 46),
]
for sequence in sequences:
with self.subTest(sequence):
listener = Mock()
stream = AnsiToWin32(listener)
stream.win32_calls = {i: (lambda *_, i=i,**__: listener(i),) for i in [2, 5, 33, 46, 166, 226, 46]}
stream.call_win32('m', sequence)
self.assertFalse(listener.call_args_list)


if __name__ == '__main__':
main()

0 comments on commit 47cb7f7

Please sign in to comment.