Remove some Python 2 compatibility code

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2022-11-10 19:33:25 +01:00
parent fd7aa13671
commit 0c3f5f1471

View file

@ -220,25 +220,17 @@ class FileWrapper(io.FileIO):
:param file_name: File path to open. :param file_name: File path to open.
""" """
super(FileWrapper, self).__init__(file_name, 'r') super().__init__(file_name, 'r')
self._line_no = 0 self._line_no = 0
def next(self): def __next__(self):
""" """
Python 2 iterator method. This method overrides base class's This method overrides base class's __next__ method and extends it
next method and extends the next method to count the line method to count the line numbers as each line is read.
numbers as each line is read.
It works for both Python 2 and Python 3 by checking iterator
method name in the base iterator object.
:return: Line read from file. :return: Line read from file.
""" """
parent = super(FileWrapper, self) line = super().__next__()
if hasattr(parent, '__next__'):
line = parent.__next__() # Python 3
else:
line = parent.next() # Python 2 # pylint: disable=no-member
if line is not None: if line is not None:
self._line_no += 1 self._line_no += 1
# Convert byte array to string with correct encoding and # Convert byte array to string with correct encoding and
@ -246,9 +238,6 @@ class FileWrapper(io.FileIO):
return line.decode(sys.getdefaultencoding()).rstrip() + '\n' return line.decode(sys.getdefaultencoding()).rstrip() + '\n'
return None return None
# Python 3 iterator method
__next__ = next
def get_line_no(self): def get_line_no(self):
""" """
Gives current line number. Gives current line number.