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
This commit is contained in:
parent
2b3fc5ed1a
commit
18d24e2420
|
@ -146,7 +146,7 @@ class Reduce:
|
||||||
|
|
||||||
def combinelines(self, filedata):
|
def combinelines(self, filedata):
|
||||||
if len(filedata) < 3:
|
if len(filedata) < 3:
|
||||||
return
|
return filedata
|
||||||
|
|
||||||
lines = []
|
lines = []
|
||||||
|
|
||||||
|
@ -172,18 +172,20 @@ class Reduce:
|
||||||
filedata2[line] = filedata2[line].rstrip() + filedata2[line + 1].lstrip()
|
filedata2[line] = filedata2[line].rstrip() + filedata2[line + 1].lstrip()
|
||||||
filedata2[line + 1] = ''
|
filedata2[line + 1] = ''
|
||||||
|
|
||||||
if self.replaceandrun('combine lines', filedata2, lines[i1] + 1, ''):
|
if self.replaceandrun('combine lines (chunk)', filedata2, lines[i1] + 1, ''):
|
||||||
filedata = filedata2
|
filedata = filedata2
|
||||||
lines[i1:i2] = []
|
lines[i1:i2] = []
|
||||||
i = i1
|
i = i1
|
||||||
|
|
||||||
chunksize = chunksize / 2
|
chunksize = int(chunksize / 2)
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
fd1 = filedata[line].rstrip()
|
fd1 = filedata[line].rstrip()
|
||||||
fd2 = filedata[line + 1].lstrip()
|
fd2 = filedata[line + 1].lstrip()
|
||||||
self.replaceandrun2('combine lines', filedata, line, fd1 + fd2, '')
|
self.replaceandrun2('combine lines', filedata, line, fd1 + fd2, '')
|
||||||
|
|
||||||
|
return filedata
|
||||||
|
|
||||||
def removedirectives(self, filedata):
|
def removedirectives(self, filedata):
|
||||||
for i in range(len(filedata)):
|
for i in range(len(filedata)):
|
||||||
line = filedata[i].lstrip()
|
line = filedata[i].lstrip()
|
||||||
|
@ -334,7 +336,7 @@ def main():
|
||||||
reduce.removecomments(filedata)
|
reduce.removecomments(filedata)
|
||||||
|
|
||||||
print('combine lines..')
|
print('combine lines..')
|
||||||
reduce.combinelines(filedata)
|
filedata = reduce.combinelines(filedata)
|
||||||
|
|
||||||
print('remove line...')
|
print('remove line...')
|
||||||
reduce.removeline(filedata)
|
reduce.removeline(filedata)
|
||||||
|
|
|
@ -62,3 +62,81 @@ def test_removedirectives():
|
||||||
|
|
||||||
reduce.removedirectives(filedata)
|
reduce.removedirectives(filedata)
|
||||||
assert filedata == expected
|
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',
|
||||||
|
'']
|
||||||
|
|
Loading…
Reference in New Issue