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 db6ca15 commit b07eab9
Show file tree
Hide file tree
Showing 2 changed files with 29 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
24 changes: 24 additions & 0 deletions colorama/tests/ansitowin32_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,30 @@ def test_osc_codes(self):
for code in data:
stream.write(code)
self.assertEqual(winterm.set_title.call_count, 2)

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:
listener = Mock()
stream = AnsiToWin32(listener)
stream.win32_calls = {
2: (lambda *_, **__: listener(2),),
5: (lambda *_, **__: listener(5),),
33: (lambda *_, **__: listener(33),),
46: (lambda *_, **__: listener(46),),
166: (lambda *_, **__: listener(166),),
226: (lambda *_, **__: listener(226),),
255: (lambda *_, **__: listener(255),),
}
stream.call_win32('m', sequence)
self.assertFalse(listener.called)


if __name__ == '__main__':
main()

0 comments on commit b07eab9

Please sign in to comment.