##// END OF EJS Templates
py3: stop normalizing 2nd argument of *attr() to unicode...
Gregory Szorc -
r43373:c95b2f40 default
parent child Browse files
Show More
@@ -110,65 +110,14 if sys.version_info[0] >= 3:
110 110 except IndexError:
111 111 return False
112 112
113 def _findargnofcall(n):
114 """Find arg n of a call expression (start at 0)
115
116 Returns index of the first token of that argument, or None if
117 there is not that many arguments.
118
119 Assumes that token[i + 1] is '('.
120
121 """
122 nested = 0
123 for j in range(i + 2, len(tokens)):
124 if _isop(j, ')', ']', '}'):
125 # end of call, tuple, subscription or dict / set
126 nested -= 1
127 if nested < 0:
128 return None
129 elif n == 0:
130 # this is the starting position of arg
131 return j
132 elif _isop(j, '(', '[', '{'):
133 nested += 1
134 elif _isop(j, ',') and nested == 0:
135 n -= 1
136
137 return None
138
139 def _ensureunicode(j):
140 """Make sure the token at j is a unicode string
141
142 This rewrites a string token to include the unicode literal prefix
143 so the string transformer won't add the byte prefix.
144
145 Ignores tokens that are not strings. Assumes bounds checking has
146 already been done.
147
148 """
149 st = tokens[j]
150 if st.type == token.STRING and st.string.startswith(("'", '"')):
151 tokens[j] = st._replace(string='u%s' % st.string)
152
153 113 for i, t in enumerate(tokens):
154 114 # This looks like a function call.
155 115 if t.type == token.NAME and _isop(i + 1, '('):
156 116 fn = t.string
157 117
158 # *attr() builtins don't accept byte strings to 2nd argument.
159 if fn in (
160 'getattr',
161 'setattr',
162 'hasattr',
163 'safehasattr',
164 ) and not _isop(i - 1, '.'):
165 arg1idx = _findargnofcall(1)
166 if arg1idx is not None:
167 _ensureunicode(arg1idx)
168
169 118 # It changes iteritems/values to items/values as they are not
170 119 # present in Python 3 world.
171 elif fn in ('iteritems', 'itervalues') and not (
120 if fn in ('iteritems', 'itervalues') and not (
172 121 tokens[i - 1].type == token.NAME
173 122 and tokens[i - 1].string == 'def'
174 123 ):
@@ -182,7 +131,7 if sys.version_info[0] >= 3:
182 131 # ``replacetoken`` or any mechanism that changes semantics of module
183 132 # loading is changed. Otherwise cached bytecode may get loaded without
184 133 # the new transformation mechanisms applied.
185 BYTECODEHEADER = b'HG\x00\x13'
134 BYTECODEHEADER = b'HG\x00\x14'
186 135
187 136 class hgloader(importlib.machinery.SourceFileLoader):
188 137 """Custom module loader that transforms source code.
@@ -255,7 +255,7 class KeepAliveHandler(object):
255 255 # If not a persistent connection, don't try to reuse it. Look
256 256 # for this using getattr() since vcr doesn't define this
257 257 # attribute, and in that case always close the connection.
258 if getattr(r, r'will_close', True):
258 if getattr(r, 'will_close', True):
259 259 self._cm.remove(h)
260 260
261 261 if DEBUG:
@@ -70,7 +70,7 def _importfrom(pkgname, modname):
70 70 except AttributeError:
71 71 raise ImportError(r'cannot import name %s' % modname)
72 72 # force import; fakelocals[modname] may be replaced with the real module
73 getattr(mod, r'__doc__', None)
73 getattr(mod, '__doc__', None)
74 74 return fakelocals[modname]
75 75
76 76
@@ -94,7 +94,7 def _importfrom(pkgname, modname):
94 94
95 95 def _checkmod(pkgname, modname, mod):
96 96 expected = _cextversions.get((pkgname, modname))
97 actual = getattr(mod, r'version', None)
97 actual = getattr(mod, 'version', None)
98 98 if actual != expected:
99 99 raise ImportError(
100 100 r'cannot import module %s.%s '
@@ -270,7 +270,7 if ispy3:
270 270 def getdoc(obj):
271 271 """Get docstring as bytes; may be None so gettext() won't confuse it
272 272 with _('')"""
273 doc = getattr(obj, u'__doc__', None)
273 doc = getattr(obj, '__doc__', None)
274 274 if doc is None:
275 275 return doc
276 276 return sysbytes(doc)
@@ -24,7 +24,7 from ..utils import storageutil
24 24
25 25
26 26 class basetestcase(unittest.TestCase):
27 if not getattr(unittest.TestCase, r'assertRaisesRegex', False):
27 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
28 28 assertRaisesRegex = ( # camelcase-required
29 29 unittest.TestCase.assertRaisesRegexp
30 30 )
@@ -623,7 +623,7 class observedbufferedinputpipe(buffered
623 623 def _fillbuffer(self):
624 624 res = super(observedbufferedinputpipe, self)._fillbuffer()
625 625
626 fn = getattr(self._input._observer, r'osread', None)
626 fn = getattr(self._input._observer, 'osread', None)
627 627 if fn:
628 628 fn(res, _chunksize)
629 629
@@ -634,7 +634,7 class observedbufferedinputpipe(buffered
634 634 def read(self, size):
635 635 res = super(observedbufferedinputpipe, self).read(size)
636 636
637 fn = getattr(self._input._observer, r'bufferedread', None)
637 fn = getattr(self._input._observer, 'bufferedread', None)
638 638 if fn:
639 639 fn(res, size)
640 640
@@ -643,7 +643,7 class observedbufferedinputpipe(buffered
643 643 def readline(self, *args, **kwargs):
644 644 res = super(observedbufferedinputpipe, self).readline(*args, **kwargs)
645 645
646 fn = getattr(self._input._observer, r'bufferedreadline', None)
646 fn = getattr(self._input._observer, 'bufferedreadline', None)
647 647 if fn:
648 648 fn(res)
649 649
General Comments 0
You need to be logged in to leave comments. Login now