Description: Use 2to3 to convert from Python2 and Python3
Bug-Debian: https://bugs.debian.org/936328
Author: Andreas Tille <tille@debian.org>
Last-Update: Sat, 07 Dec 2019 12:44:55 +0100

--- a/make/Python.mk
+++ b/make/Python.mk
@@ -8,11 +8,11 @@ endif
 include make/Defs.mk
 
 ifeq (,$(wildcard $(PYTHON_INCLUDE)/Python.h))
-$(error python include not configured properly, cannot build python module!)
+$(error python3 include not configured properly, cannot build python3 module!)
 endif
 
 ifeq (,$(wildcard $(NUMPY_INCLUDE)/numpy/arrayobject.h))
-$(error numpy include not configured properly, cannot build python module!)
+$(error numpy include not configured properly, cannot build python3 module!)
 endif
 
 SWIG_CMD   := SWIG_LIB=$(SWIG_LIB) $(SWIG) -Wall -c++ -python -builtin
@@ -27,6 +27,6 @@ $(PYTHON_DLL): $(SWIG_INTERFACES) $(CXX_
 	$(CXX) $(SHLIB_FLAGS) $(INCLUDES) -I $(PYTHON_INCLUDE) -I $(NUMPY_INCLUDE) $(GEN_CXX) $(CXX_LIB) -o $(PYTHON_DLL)
 
 test-python: $(PYTHON_DLL)
-	@PYTHONPATH=$(PYTHON_BUILD_DIR) python src/Demos/Demo.py && echo "Python build is OK!"
+	@PYTHONPATH=$(PYTHON_BUILD_DIR) python3 src/Demos/Demo.py && echo "Python3 build is OK!"
 
 .PHONY: all test-python $(PYTHON_DLL)
--- a/setup.py
+++ b/setup.py
@@ -1,5 +1,5 @@
-#!/usr/bin/env python
-from __future__ import print_function
+#!/usr/bin/python3
+
 
 from distutils.command.build import build as _build
 from distutils.errors import CompileError
--- a/src/Demos/Demo.py
+++ b/src/Demos/Demo.py
@@ -1,4 +1,4 @@
-from __future__ import print_function
+
 import ConsensusCore as cc
 
 print(cc.Version.VersionString())
--- a/src/Tests/meson.build
+++ b/src/Tests/meson.build
@@ -53,7 +53,7 @@ test(
 
 if get_option('swig')
   test(
-    'quiver python import',
+    'quiver python3 import',
     quiver_python,
     args : [
       files('test_import.py')],
--- a/src/Tests/test_import.py
+++ b/src/Tests/test_import.py
@@ -1,4 +1,4 @@
-from __future__ import print_function
+
 import ConsensusCore as cc
 
 # basic test to verify that SWIG worked
--- a/tools/cpplint.py
+++ b/tools/cpplint.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3
 #
 # Copyright (c) 2009 Google Inc. All rights reserved.
 #
@@ -560,7 +560,7 @@ class _CppLintState(object):
 
   def PrintErrorCounts(self):
     """Print a summary of errors by category, and the total."""
-    for category, count in self.errors_by_category.iteritems():
+    for category, count in list(self.errors_by_category.items()):
       sys.stderr.write('Category \'%s\' errors found: %d\n' %
                        (category, count))
     sys.stderr.write('Total errors found: %d\n' % self.error_count)
@@ -1007,7 +1007,7 @@ def CheckForCopyright(filename, lines, e
 
   # We'll say it should occur by line 10. Don't forget there's a
   # dummy line at the front.
-  for line in xrange(1, min(len(lines), 11)):
+  for line in range(1, min(len(lines), 11)):
     if re.search(r'Copyright', lines[line], re.I): break
   else:                       # means no copyright line was found
     error(filename, 0, 'legal/copyright', 5,
@@ -1112,7 +1112,7 @@ def CheckForUnicodeReplacementCharacters
     error: The function to call with any errors found.
   """
   for linenum, line in enumerate(lines):
-    if u'\ufffd' in line:
+    if '\\ufffd' in line:
       error(filename, linenum, 'readability/utf8', 5,
             'Line contains invalid UTF-8 (or Unicode replacement character).')
 
@@ -1602,7 +1602,7 @@ def CheckForFunctionLengths(filename, cl
 
   if starting_func:
     body_found = False
-    for start_linenum in xrange(linenum, clean_lines.NumLines()):
+    for start_linenum in range(linenum, clean_lines.NumLines()):
       start_line = lines[start_linenum]
       joined_line += ' ' + start_line.lstrip()
       if Search(r'(;|})', start_line):  # Declarations and trivial functions
@@ -2136,7 +2136,7 @@ def GetLineWidth(line):
     The width of the line in column positions, accounting for Unicode
     combining characters and wide characters.
   """
-  if isinstance(line, unicode):
+  if isinstance(line, str):
     width = 0
     for uc in unicodedata.normalize('NFC', line):
       if unicodedata.east_asian_width(uc) in ('W', 'F'):
@@ -2472,7 +2472,7 @@ def _GetTextInside(text, start_pattern):
 
   # Give opening punctuations to get the matching close-punctuations.
   matching_punctuation = {'(': ')', '{': '}', '[': ']'}
-  closing_punctuation = set(matching_punctuation.itervalues())
+  closing_punctuation = set(matching_punctuation.values())
 
   # Find the position to start extracting text.
   match = re.search(start_pattern, text, re.M)
@@ -3022,7 +3022,7 @@ def CheckForIncludeWhatYouUse(filename,
   required = {}  # A map of header name to linenumber and the template entity.
                  # Example of required: { '<functional>': (1219, 'less<>') }
 
-  for linenum in xrange(clean_lines.NumLines()):
+  for linenum in range(clean_lines.NumLines()):
     line = clean_lines.elided[linenum]
     if not line or line[0] == '#':
       continue
@@ -3070,7 +3070,7 @@ def CheckForIncludeWhatYouUse(filename,
 
   # include_state is modified during iteration, so we iterate over a copy of
   # the keys.
-  header_keys = include_state.keys()
+  header_keys = list(include_state.keys())
   for header in header_keys:
     (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
     fullpath = common_path + header
@@ -3186,7 +3186,7 @@ def ProcessFileData(filename, file_exten
 
   RemoveMultiLineComments(filename, lines, error)
   clean_lines = CleansedLines(lines)
-  for line in xrange(clean_lines.NumLines()):
+  for line in range(clean_lines.NumLines()):
     ProcessLine(filename, file_extension, clean_lines, line,
                 include_state, function_state, class_state, error,
                 extra_check_functions)
--- a/tools/find_boost
+++ b/tools/find_boost
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3
 
 import os, re, sys
 from glob import glob
@@ -55,8 +55,8 @@ if __name__ == '__main__':
     else:
         boost = find_boost()
     if boost_version(boost) < BOOST_MINIMUM_VERSION:
-        print >> sys.stderr, "Boost version at least %s required!" \
-            % str_version(BOOST_MINIMUM_VERSION)
-        print >> sys.stderr, "Use --boost=<path> to specify boost location."
+        print("Boost version at least %s required!" \
+            % str_version(BOOST_MINIMUM_VERSION), file=sys.stderr)
+        print("Use --boost=<path> to specify boost location.", file=sys.stderr)
         sys.exit(1)
     print(boost)
--- a/tools/find_swig
+++ b/tools/find_swig
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3
 
 import os, re, sys
 from glob import glob
@@ -42,9 +42,9 @@ if __name__ == '__main__':
         swig = find_swig()
 
     if swig is None or swig_version(swig) < SWIG_MINIMUM_VERSION:
-        print >> sys.stderr, "SWIG version at least %s required!" \
-            % str_version(SWIG_MINIMUM_VERSION)
-        print >> sys.stderr, "Use --swig=<path> to specify SWIG location."
+        print("SWIG version at least %s required!" \
+            % str_version(SWIG_MINIMUM_VERSION), file=sys.stderr)
+        print("Use --swig=<path> to specify SWIG location.", file=sys.stderr)
         sys.exit(1)
     else:
         print(swig)
--- a/tools/git-clang-format
+++ b/tools/git-clang-format
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3
 #
 #===- git-clang-format - ClangFormat Git Integration ---------*- python -*--===#
 #
@@ -128,15 +128,15 @@ def main():
   if opts.verbose >= 1:
     ignored_files.difference_update(changed_lines)
     if ignored_files:
-      print 'Ignoring changes in the following files (wrong extension):'
+      print('Ignoring changes in the following files (wrong extension):')
       for filename in ignored_files:
-        print '   ', filename
+        print('   ', filename)
     if changed_lines:
-      print 'Running clang-format on the following files:'
+      print('Running clang-format on the following files:')
       for filename in changed_lines:
-        print '   ', filename
+        print('   ', filename)
   if not changed_lines:
-    print 'no modified files to format'
+    print('no modified files to format')
     return
   # The computed diff outputs absolute paths, so we must cd before accessing
   # those files.
@@ -146,20 +146,20 @@ def main():
                                                binary=opts.binary,
                                                style=opts.style)
   if opts.verbose >= 1:
-    print 'old tree:', old_tree
-    print 'new tree:', new_tree
+    print('old tree:', old_tree)
+    print('new tree:', new_tree)
   if old_tree == new_tree:
     if opts.verbose >= 0:
-      print 'clang-format did not modify any files'
+      print('clang-format did not modify any files')
   elif opts.diff:
     print_diff(old_tree, new_tree)
   else:
     changed_files = apply_changes(old_tree, new_tree, force=opts.force,
                                   patch_mode=opts.patch)
     if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1:
-      print 'changed files:'
+      print('changed files:')
       for filename in changed_files:
-        print '   ', filename
+        print('   ', filename)
 
 
 def load_git_config(non_string_options=None):
@@ -298,7 +298,7 @@ def filter_by_extension(dictionary, allo
   `allowed_extensions` must be a collection of lowercase file extensions,
   excluding the period."""
   allowed_extensions = frozenset(allowed_extensions)
-  for filename in dictionary.keys():
+  for filename in list(dictionary.keys()):
     base_ext = filename.rsplit('.', 1)
     if len(base_ext) == 1 or base_ext[1].lower() not in allowed_extensions:
       del dictionary[filename]
@@ -323,7 +323,7 @@ def run_clang_format_and_save_to_tree(ch
 
   Returns the object ID (SHA-1) of the created tree."""
   def index_info_generator():
-    for filename, line_ranges in changed_lines.iteritems():
+    for filename, line_ranges in changed_lines.items():
       mode = oct(os.stat(filename).st_mode)
       blob_id = clang_format_to_blob(filename, line_ranges, binary=binary,
                                      style=style)
@@ -431,10 +431,10 @@ def apply_changes(old_tree, new_tree, fo
   if not force:
     unstaged_files = run('git', 'diff-files', '--name-status', *changed_files)
     if unstaged_files:
-      print >>sys.stderr, ('The following files would be modified but '
-                           'have unstaged changes:')
-      print >>sys.stderr, unstaged_files
-      print >>sys.stderr, 'Please commit, stage, or stash them first.'
+      print(('The following files would be modified but '
+                           'have unstaged changes:'), file=sys.stderr)
+      print(unstaged_files, file=sys.stderr)
+      print('Please commit, stage, or stash them first.', file=sys.stderr)
       sys.exit(2)
   if patch_mode:
     # In patch mode, we could just as well create an index from the new tree
@@ -464,20 +464,20 @@ def run(*args, **kwargs):
   if p.returncode == 0:
     if stderr:
       if verbose:
-        print >>sys.stderr, '`%s` printed to stderr:' % ' '.join(args)
-      print >>sys.stderr, stderr.rstrip()
+        print('`%s` printed to stderr:' % ' '.join(args), file=sys.stderr)
+      print(stderr.rstrip(), file=sys.stderr)
     if strip:
       stdout = stdout.rstrip('\r\n')
     return stdout
   if verbose:
-    print >>sys.stderr, '`%s` returned %s' % (' '.join(args), p.returncode)
+    print('`%s` returned %s' % (' '.join(args), p.returncode), file=sys.stderr)
   if stderr:
-    print >>sys.stderr, stderr.rstrip()
+    print(stderr.rstrip(), file=sys.stderr)
   sys.exit(2)
 
 
 def die(message):
-  print >>sys.stderr, 'error:', message
+  print('error:', message, file=sys.stderr)
   sys.exit(2)
 
 
