##// END OF EJS Templates
byteify-strings: do not rewrite system string literals to u''...
Yuya Nishihara -
r38408:1d68fd5f default
parent child Browse files
Show More
@@ -27,6 +27,8 b' if True:'
27 27 The input token list may be mutated as part of processing. However,
28 28 its changes do not necessarily match the output token stream.
29 29 """
30 sysstrtokens = set()
31
30 32 # The following utility functions access the tokens list and i index of
31 33 # the for i, t enumerate(tokens) loop below
32 34 def _isop(j, *o):
@@ -62,11 +64,11 b' if True:'
62 64
63 65 return None
64 66
65 def _ensureunicode(j):
66 """Make sure the token at j is a unicode string
67 def _ensuresysstr(j):
68 """Make sure the token at j is a system string
67 69
68 This rewrites a string token to include the unicode literal prefix
69 so the string transformer won't add the byte prefix.
70 Remember the given token so the string transformer won't add
71 the byte prefix.
70 72
71 73 Ignores tokens that are not strings. Assumes bounds checking has
72 74 already been done.
@@ -74,7 +76,7 b' if True:'
74 76 """
75 77 st = tokens[j]
76 78 if st.type == token.STRING and st.string.startswith(("'", '"')):
77 tokens[j] = st._replace(string='u%s' % st.string)
79 sysstrtokens.add(st)
78 80
79 81 for i, t in enumerate(tokens):
80 82 # Convert most string literals to byte literals. String literals
@@ -83,7 +85,7 b' if True:'
83 85 # Rather than rewrite all string literals to use ``b''`` to indicate
84 86 # byte strings, we apply this token transformer to insert the ``b``
85 87 # prefix nearly everywhere.
86 if t.type == token.STRING:
88 if t.type == token.STRING and t not in sysstrtokens:
87 89 s = t.string
88 90
89 91 # Preserve docstrings as string literals. This is inconsistent
@@ -117,7 +119,7 b' if True:'
117 119 not _isop(i - 1, '.')):
118 120 arg1idx = _findargnofcall(1)
119 121 if arg1idx is not None:
120 _ensureunicode(arg1idx)
122 _ensuresysstr(arg1idx)
121 123
122 124 # .encode() and .decode() on str/bytes/unicode don't accept
123 125 # byte strings on Python 3.
@@ -125,7 +127,7 b' if True:'
125 127 for argn in range(2):
126 128 argidx = _findargnofcall(argn)
127 129 if argidx is not None:
128 _ensureunicode(argidx)
130 _ensuresysstr(argidx)
129 131
130 132 # It changes iteritems/values to items/values as they are not
131 133 # present in Python 3 world.
General Comments 0
You need to be logged in to leave comments. Login now