From 18d24e2420b70f2e0f6642934eddd163de830734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Mon, 21 Mar 2022 17:14:47 +0100 Subject: [PATCH] reduce.py: some fixes (#3914) * reduce.py: fixed potential "TypeError: slice indices must be integers or None or have an __index__ method" in combinelines() * reduce.py: the combinelines() changes were not applied when the chunk mode was used --- tools/reduce.py | 10 +++--- tools/test_reduce.py | 78 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/tools/reduce.py b/tools/reduce.py index 77e4a638e..ae4507e61 100755 --- a/tools/reduce.py +++ b/tools/reduce.py @@ -146,7 +146,7 @@ class Reduce: def combinelines(self, filedata): if len(filedata) < 3: - return + return filedata lines = [] @@ -172,18 +172,20 @@ class Reduce: filedata2[line] = filedata2[line].rstrip() + filedata2[line + 1].lstrip() filedata2[line + 1] = '' - if self.replaceandrun('combine lines', filedata2, lines[i1] + 1, ''): + if self.replaceandrun('combine lines (chunk)', filedata2, lines[i1] + 1, ''): filedata = filedata2 lines[i1:i2] = [] i = i1 - chunksize = chunksize / 2 + chunksize = int(chunksize / 2) for line in lines: fd1 = filedata[line].rstrip() fd2 = filedata[line + 1].lstrip() self.replaceandrun2('combine lines', filedata, line, fd1 + fd2, '') + return filedata + def removedirectives(self, filedata): for i in range(len(filedata)): line = filedata[i].lstrip() @@ -334,7 +336,7 @@ def main(): reduce.removecomments(filedata) print('combine lines..') - reduce.combinelines(filedata) + filedata = reduce.combinelines(filedata) print('remove line...') reduce.removeline(filedata) diff --git a/tools/test_reduce.py b/tools/test_reduce.py index 1e0674c68..c58a6a2d4 100644 --- a/tools/test_reduce.py +++ b/tools/test_reduce.py @@ -62,3 +62,81 @@ def test_removedirectives(): reduce.removedirectives(filedata) assert filedata == expected + + +def test_combinelines_chunk(): + """do not fail with 'TypeError: slice indices must be integers or None or have an __index__ method'""" + + class ReduceTestFail(ReduceTest): + def runtool(self, filedata=None): + print(filedata) + return False + + reduce = ReduceTestFail() + + # need to have at least 11 lines ending with comma to enter chunked mode and twice as much for second interation + filedata = [ + 'int i,\n', + 'j,\n', + 'k,\n', + 'l,\n', + 'm,\n', + 'n,\n', + 'o,\n', + 'p,\n', + 'q,\n', + 'r,\n', + 's,\n', + 't;\n', + 'int i1,\n', + 'j1,\n', + 'k1,\n', + 'l1,\n', + 'm1,\n', + 'n1,\n', + 'o1,\n', + 'p1,\n', + 'q1,\n', + 'r1,\n', + 's1,\n', + 't1;\n' + ] + + reduce.combinelines(filedata) + + +def test_combinelines_chunk_2(): + """'filedata' is not changed by the funtion since the data is assigned to a local variable""" + + reduce = ReduceTest() + + # need to have at least 11 lines ending with comma to enter chunked mode + filedata = [ + 'int i,\n', + 'j,\n', + 'k,\n', + 'l,\n', + 'm,\n', + 'n,\n', + 'o,\n', + 'p,\n', + 'q,\n', + 'r,\n', + 's,\n', + 't;\n' + ] + + filedata2 = reduce.combinelines(filedata) + assert filedata == filedata + assert filedata2 == ['int i,j,\n', + '', + 'l,\n', + 'm,\n', + 'n,\n', + 'o,\n', + 'p,\n', + 'q,\n', + 'r,\n', + 's,\n', + 't;\n', + '']