[clang-tools-extra] c6bf8b8 - Fix python 2-vs-3 issues in add_new_check.py and rename_check.py

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 11 00:53:29 PDT 2021


Author: Matt Beardsley
Date: 2021-09-11T09:52:50+02:00
New Revision: c6bf8b8592431a4e39aeb20cc185b8fae0d52e43

URL: https://github.com/llvm/llvm-project/commit/c6bf8b8592431a4e39aeb20cc185b8fae0d52e43
DIFF: https://github.com/llvm/llvm-project/commit/c6bf8b8592431a4e39aeb20cc185b8fae0d52e43.diff

LOG: Fix python 2-vs-3 issues in add_new_check.py and rename_check.py

As of this commit:
  https://github.com/llvm/llvm-project/commit/307b1fdd

If either of those scripts are invoked with python 2, neither works due to:
  "TypeError: write() argument 1 must be unicode, not str"

And if rename_check.py is invoked with python 3:
  "ValueError: binary mode doesn't take an encoding argument"
(referring to `with io.open(filename, 'wb', encoding='utf8') as f:`), and

Another issue in rename_check.py in python 2:
  "TypeError: list object is not an iterator"
(referring to `next(filter( ... os.listdir(old_module_path)))`)

(so, rename_check doesn't work with either 2 or 3, and add_new_check
doesn't work with 2, but does work with 3)

I ran these steps to test both python versions:
(manually - appears to be the "status quo" for these files)

  python3 clang-tools-extra/clang-tidy/add_new_check.py readability ggggg
  python3 clang-tools-extra/clang-tidy/rename_check.py readability-ggggg readability-hhhhh

  git checkout HEAD -- clang-tools-extra/clang-tidy/readability/CMakeLists.txt clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/list.rst
  rm -f clang-tools-extra/clang-tidy/readability/GggggCheck.cpp clang-tools-extra/clang-tidy/readability/GggggCheck.h clang-tools-extra/docs/clang-tidy/checks/readability-ggggg.rst clang-tools-extra/test/clang-tidy/checkers/readability-ggggg.cpp clang-tools-extra/clang-tidy/readability/HhhhhCheck.cpp clang-tools-extra/clang-tidy/readability/HhhhhCheck.h clang-tools-extra/docs/clang-tidy/checks/readability-hhhhh.rst

  python2 clang-tools-extra/clang-tidy/add_new_check.py readability ggggg
  python2 clang-tools-extra/clang-tidy/rename_check.py readability-ggggg readability-hhhhh

  git checkout HEAD -- clang-tools-extra/clang-tidy/readability/CMakeLists.txt clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/list.rst
  rm -f clang-tools-extra/clang-tidy/readability/GggggCheck.cpp clang-tools-extra/clang-tidy/readability/GggggCheck.h clang-tools-extra/docs/clang-tidy/checks/readability-ggggg.rst clang-tools-extra/test/clang-tidy/checkers/readability-ggggg.cpp clang-tools-extra/clang-tidy/readability/HhhhhCheck.cpp clang-tools-extra/clang-tidy/readability/HhhhhCheck.h clang-tools-extra/docs/clang-tidy/checks/readability-hhhhh.rst

Reviewed By: kbobyrev

Differential Revision: https://reviews.llvm.org/D109127

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/add_new_check.py
    clang-tools-extra/clang-tidy/rename_check.py

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py
index 9239ca5953cda..0312c04e047c7 100755
--- a/clang-tools-extra/clang-tidy/add_new_check.py
+++ b/clang-tools-extra/clang-tidy/add_new_check.py
@@ -9,6 +9,7 @@
 #===-----------------------------------------------------------------------===#
 
 from __future__ import print_function
+from __future__ import unicode_literals
 
 import argparse
 import io

diff  --git a/clang-tools-extra/clang-tidy/rename_check.py b/clang-tools-extra/clang-tidy/rename_check.py
index 0c48634ac62b6..9c2021751e0e5 100755
--- a/clang-tools-extra/clang-tidy/rename_check.py
+++ b/clang-tools-extra/clang-tidy/rename_check.py
@@ -8,6 +8,8 @@
 #
 #===-----------------------------------------------------------------------===#
 
+from __future__ import unicode_literals
+
 import argparse
 import glob
 import io
@@ -117,7 +119,7 @@ def adapt_cmake(module_path, check_name_camel):
       return False
 
   print('Updating %s...' % filename)
-  with io.open(filename, 'wb', encoding='utf8') as f:
+  with io.open(filename, 'w', encoding='utf8') as f:
     cpp_found = False
     file_added = False
     for line in lines:
@@ -125,21 +127,23 @@ def adapt_cmake(module_path, check_name_camel):
       if (not file_added) and (cpp_line or cpp_found):
         cpp_found = True
         if (line.strip() > cpp_file) or (not cpp_line):
-          f.write(('  ' + cpp_file + '\n').encode())
+          f.write('  ' + cpp_file + '\n')
           file_added = True
-      f.write(line.encode())
+      f.write(line)
 
   return True
 
 # Modifies the module to include the new check.
 def adapt_module(module_path, module, check_name, check_name_camel):
-  modulecpp = next(filter(lambda p: p.lower() == module.lower() + 'tidymodule.cpp', os.listdir(module_path)))
+  modulecpp = next(iter(filter(
+      lambda p: p.lower() == module.lower() + 'tidymodule.cpp',
+      os.listdir(module_path))))
   filename = os.path.join(module_path, modulecpp)
   with io.open(filename, 'r', encoding='utf8') as f:
     lines = f.readlines()
 
   print('Updating %s...' % filename)
-  with io.open(filename, 'wb', encoding='utf8') as f:
+  with io.open(filename, 'w', encoding='utf8') as f:
     header_added = False
     header_found = False
     check_added = False
@@ -153,21 +157,21 @@ def adapt_module(module_path, module, check_name, check_name_camel):
           header_found = True
           if match.group(1) > check_name_camel:
             header_added = True
-            f.write(('#include "' + check_name_camel + '.h"\n').encode())
+            f.write('#include "' + check_name_camel + '.h"\n')
         elif header_found:
           header_added = True
-          f.write(('#include "' + check_name_camel + '.h"\n').encode())
+          f.write('#include "' + check_name_camel + '.h"\n')
 
       if not check_added:
         if line.strip() == '}':
           check_added = True
-          f.write(check_decl.encode())
+          f.write(check_decl)
         else:
           match = re.search('registerCheck<(.*)>', line)
           if match and match.group(1) > check_name_camel:
             check_added = True
-            f.write(check_decl.encode())
-      f.write(line.encode())
+            f.write(check_decl)
+      f.write(line)
 
 
 # Adds a release notes entry.
@@ -182,7 +186,7 @@ def add_release_notes(clang_tidy_path, old_check_name, new_check_name):
   checkMatcher = re.compile('- The \'(.*)')
 
   print('Updating %s...' % filename)
-  with io.open(filename, 'wb', encoding='utf8') as f:
+  with io.open(filename, 'w', encoding='utf8') as f:
     note_added = False
     header_found = False
     add_note_here = False
@@ -202,22 +206,22 @@ def add_release_notes(clang_tidy_path, old_check_name, new_check_name):
 
         if match:
           header_found = True
-          f.write(line.encode())
+          f.write(line)
           continue
 
         if line.startswith('^^^^'):
-          f.write(line.encode())
+          f.write(line)
           continue
 
         if header_found and add_note_here:
           if not line.startswith('^^^^'):
-            f.write(("""- The '%s' check was renamed to :doc:`%s
+            f.write("""- The '%s' check was renamed to :doc:`%s
   <clang-tidy/checks/%s>`
 
-""" % (old_check_name, new_check_name, new_check_name)).encode())
+""" % (old_check_name, new_check_name, new_check_name))
             note_added = True
 
-      f.write(line.encode())
+      f.write(line)
 
 def main():
   parser = argparse.ArgumentParser(description='Rename clang-tidy check.')
@@ -263,9 +267,9 @@ def main():
             (check_name_camel, cmake_lists))
       return 1
 
-    modulecpp = next(filter(
+    modulecpp = next(iter(filter(
         lambda p: p.lower() == old_module.lower() + 'tidymodule.cpp',
-        os.listdir(old_module_path)))
+        os.listdir(old_module_path))))
     deleteMatchingLines(os.path.join(old_module_path, modulecpp),
                       '\\b' + check_name_camel + '|\\b' + args.old_check_name)
 


        


More information about the cfe-commits mailing list