byteify-strings: prevent "__name__ == '__main__'" from being transformed
authorYuya Nishihara <yuya@tcha.org>
Sun, 12 Aug 2018 14:03:22 +0900
changeset 39103 da130c5cef90
parent 39102 08b2ae9fc030
child 39104 2e578eced958
byteify-strings: prevent "__name__ == '__main__'" from being transformed This was okay for import-time code transformer, but shouldn't be applied to source code.
contrib/byteify-strings.py
--- a/contrib/byteify-strings.py	Wed Aug 15 17:06:12 2018 -0400
+++ b/contrib/byteify-strings.py	Sun Aug 12 14:03:22 2018 +0900
@@ -169,6 +169,11 @@
                 yield adjusttokenpos(t._replace(string=fn[4:]), coloffset)
                 continue
 
+        # Looks like "if __name__ == '__main__'".
+        if (t.type == token.NAME and t.string == '__name__'
+            and _isop(i + 1, '==')):
+            _ensuresysstr(i + 2)
+
         # Emit unmodified token.
         yield adjusttokenpos(t, coloffset)