Patchwork byteify-strings: prevent "__name__ == '__main__'" from being transformed

login
register
mail settings
Submitter Yuya Nishihara
Date Aug. 12, 2018, 5:10 a.m.
Message ID <6ee5e64e124c87b35687.1534050648@mimosa>
Download mbox | patch
Permalink /patch/33697/
State Accepted
Headers show

Comments

Yuya Nishihara - Aug. 12, 2018, 5:10 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1534050202 -32400
#      Sun Aug 12 14:03:22 2018 +0900
# Node ID 6ee5e64e124c87b356874367dbf6355707d00883
# Parent  1c137e0a33afe4d7e61325513d2518af8b93434a
byteify-strings: prevent "__name__ == '__main__'" from being transformed

This was okay for import-time code transformer, but shouldn't be applied to
source code.
Augie Fackler - Aug. 13, 2018, 3:48 p.m.
> On Aug 12, 2018, at 1:10 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1534050202 -32400
> #      Sun Aug 12 14:03:22 2018 +0900
> # Node ID 6ee5e64e124c87b356874367dbf6355707d00883
> # Parent  1c137e0a33afe4d7e61325513d2518af8b93434a
> byteify-strings: prevent "__name__ == '__main__'" from being transformed

queued, thanks

Patch

diff --git a/contrib/byteify-strings.py b/contrib/byteify-strings.py
--- a/contrib/byteify-strings.py
+++ b/contrib/byteify-strings.py
@@ -169,6 +169,11 @@  def replacetokens(tokens, opts):
                 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)