[PATCH] D79419: [clang/clang-tools-extra] Fix BZ44437 - add_new_check.py does not work with Python 3

Konrad Wilhelm Kleine via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 5 08:03:29 PDT 2020


kwk created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
kwk edited the summary of this revision.
kwk added reviewers: djasper, dsanders.
kwk edited the summary of this revision.
kwk edited the summary of this revision.

This fixes https://bugs.llvm.org/show_bug.cgi?id=44437.

Thanks to Arnaud Desitter for providing the patch in the bug report!

A simple example program to reproduce this error is this:

  #!/usr/bin/env python
  
  import sys
  
  with open(sys.argv[0], 'r') as f:
    lines = f.readlines()
  lines = iter(lines)
  line = lines.next()
  print(line)

which will error with this in python python 3:

  Traceback (most recent call last):
    File "./mytest.py", line 8, in <module>
      line = lines.next()
  AttributeError: 'list_iterator' object has no attribute 'next'

Here's the same strategy applied to my test program as applied to the `add_new_check.py` file:

  #!/usr/bin/env python
  
  import sys
  
  with open(sys.argv[0], 'r') as f:
    lines = f.readlines()
  lines = iter(lines)
  line = next(lines)
  print(line)

The built-in function `next()` is new since Python 2.6: https://docs.python.org/2/library/functions.html#next


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79419

Files:
  clang-tools-extra/clang-tidy/add_new_check.py


Index: clang-tools-extra/clang-tidy/add_new_check.py
===================================================================
--- clang-tools-extra/clang-tidy/add_new_check.py
+++ clang-tools-extra/clang-tidy/add_new_check.py
@@ -172,7 +172,7 @@
     lines = iter(lines)
     try:
       while True:
-        line = lines.next()
+        line = next(lines)
         if not header_added:
           match = re.search('#include "(.*)"', line)
           if match:
@@ -197,7 +197,7 @@
                 # If we didn't find the check name on this line, look on the
                 # next one.
                 prev_line = line
-                line = lines.next()
+                line = next(lines)
                 match = re.search(' *"([^"]*)"', line)
                 if match:
                   current_check_name = match.group(1)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79419.262106.patch
Type: text/x-patch
Size: 829 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200505/4f2237fd/attachment.bin>


More information about the cfe-commits mailing list