From 79ed18f91e99a05f5b4c36df2a27ba1045677998 Mon Sep 17 00:00:00 2001 From: Peter Pentchev Date: Sat, 18 Mar 2023 22:32:42 +0200 Subject: [PATCH 1/2] Fix a Python bytes/int mismatch in CLI tests In Python 3.x, a single element of a bytes array is returned as an integer number. Thus, NEWLINE is an int variable, and attempting to add it to the line array will fail with a type mismatch error that may be demonstrated as follows: [roam@straylight ~]$ python3 -c 'b"hello" + b"\n"[0]' Traceback (most recent call last): File "", line 1, in TypeError: can't concat int to bytes [roam@straylight ~]$ --- tests/cli-tests/run.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cli-tests/run.py b/tests/cli-tests/run.py index a2e1c1f86df..108f7101949 100755 --- a/tests/cli-tests/run.py +++ b/tests/cli-tests/run.py @@ -109,7 +109,7 @@ def pop_line(data: bytes) -> typing.Tuple[typing.Optional[bytes], bytes]: the first line always ends in a :\n:, even if it is the last line and :data: doesn't end in :\n:. """ - NEWLINE = b"\n"[0] + NEWLINE = b"\n" if data == b'': return (None, data) @@ -124,7 +124,7 @@ def pop_line(data: bytes) -> typing.Tuple[typing.Optional[bytes], bytes]: data = data[end_idx:] assert len(line) != 0 - if line[-1] != NEWLINE: + if not line.endswith(NEWLINE): line += NEWLINE return (line, data) From b6c79d57232921a953cfd5d7e1ce347459cdde56 Mon Sep 17 00:00:00 2001 From: Peter Pentchev Date: Sat, 18 Mar 2023 22:43:56 +0200 Subject: [PATCH 2/2] Simplify line splitting in the CLI tests --- tests/cli-tests/run.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/cli-tests/run.py b/tests/cli-tests/run.py index 108f7101949..9306f8a094d 100755 --- a/tests/cli-tests/run.py +++ b/tests/cli-tests/run.py @@ -114,20 +114,12 @@ def pop_line(data: bytes) -> typing.Tuple[typing.Optional[bytes], bytes]: if data == b'': return (None, data) - newline_idx = data.find(b"\n") - if newline_idx == -1: - end_idx = len(data) - else: - end_idx = newline_idx + 1 - - line = data[:end_idx] - data = data[end_idx:] - - assert len(line) != 0 - if not line.endswith(NEWLINE): - line += NEWLINE + parts = data.split(NEWLINE, maxsplit=1) + line = parts[0] + NEWLINE + if len(parts) == 1: + return line, b'' - return (line, data) + return line, parts[1] def glob_line_matches(actual: bytes, expect: bytes) -> bool: