This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by mattbeardsley on Sep 1 2021, 8:36 PM.

Details

Summary

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

Diff Detail

Event Timeline

mattbeardsley requested review of this revision.Sep 1 2021, 8:36 PM
mattbeardsley created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptSep 1 2021, 8:36 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Hey, thanks for noticing!

Ugh, I think that's not good, I was trying to make sure the io.open piece really works with Python 2 and Python 3 as this was the purpose fo the patch :( I need t check that again, I think it's really unfortunate to lose Python 2 support if that's the only reason here. Personally, I think moving to Python 3 completely is a great idea but I would anticipate many buildbots and some older distro versions not having it/being reluctant. As of right now, we still have the >= 2.7 requirement so I think the best option is to keep both somehow :(

Thanks for bringing it up!

mattbeardsley retitled this revision from Use python 3 in add_new_check.py and rename_check.py to Fix python 2-vs-3 issues in add_new_check.py and rename_check.py.
mattbeardsley edited the summary of this revision. (Show Details)

Reverted python-3-only enforcement, fixed both scripts to work with both python 2 and python 3
(generally based on the guidance here: https://python-future.org/compatible_idioms.html#strings-and-bytes)

No worries! Figured I'd start the conversation with the easy fix, but was fully prepared to hear that we can't let go of python 2 just yet, as much as we may want to :).
Let me know if this fix looks suitable. Thanks for your time!

kbobyrev accepted this revision.Sep 8 2021, 11:44 PM

Nice, thank you!

This revision is now accepted and ready to land.Sep 8 2021, 11:44 PM

Thanks! I don't have commit access, I was just reading here what to do:

Prior to obtaining commit access, it is common practice to request that someone with commit access commits on your behalf. When doing so, please provide the name and email address you would like to use in the Author property of the commit.

When you have a chance, would you be able to help with that? Here's my info:
Matt Beardsley
mbeardsl@mathworks.com

Done, thank you again for noticing!