From 667f7f8369fc29aef4c14c132cdcfc1667d72e67 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 18 Jun 2018 17:51:56 +0200 Subject: [PATCH] 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. --- tests/scripts/generate_test_code.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/scripts/generate_test_code.py b/tests/scripts/generate_test_code.py index 45fb1f5749..78bbaa3999 100755 --- a/tests/scripts/generate_test_code.py +++ b/tests/scripts/generate_test_code.py @@ -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):