Fix generation of #line directives in Python 2

When using Python 2 (which is done in the Makefile), all #line
directives from the test code were generated with the line number 1.
This traces back to the change in the method name for generators in
Python 2 (next) vs Python 3 (__next__). Override both methods so that
the script remains compatible with both Python 2 and Python 3.
This commit is contained in:
Gilles Peskine 2018-06-18 17:51:56 +02:00 committed by Mohammad Azim Khan
parent 7776141a16
commit 667f7f8369

View File

@ -76,17 +76,24 @@ class FileWrapper(io.FileIO):
super(FileWrapper, self).__init__(file_name, 'r')
self.line_no = 0
# Override the generator function in a way that works in both Python 2
# and Python 3.
def __next__(self):
"""
Iterator return impl.
:return: Line read from file.
"""
line = super(FileWrapper, self).__next__()
parent = super(FileWrapper, self)
if hasattr(parent, '__next__'):
line = parent.__next__() # Python 3
else:
line = parent.next() # Python 2
if line:
self.line_no += 1
# Convert byte array to string with correct encoding
return line.decode(sys.getdefaultencoding())
return None
next = __next__
def split_dep(dep):