##// 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 b' if sys.version_info[0] >= 3:'
110 except IndexError:
110 except IndexError:
111 return False
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 for i, t in enumerate(tokens):
113 for i, t in enumerate(tokens):
154 # This looks like a function call.
114 # This looks like a function call.
155 if t.type == token.NAME and _isop(i + 1, '('):
115 if t.type == token.NAME and _isop(i + 1, '('):
156 fn = t.string
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 # It changes iteritems/values to items/values as they are not
118 # It changes iteritems/values to items/values as they are not
170 # present in Python 3 world.
119 # present in Python 3 world.
171 elif fn in ('iteritems', 'itervalues') and not (
120 if fn in ('iteritems', 'itervalues') and not (
172 tokens[i - 1].type == token.NAME
121 tokens[i - 1].type == token.NAME
173 and tokens[i - 1].string == 'def'
122 and tokens[i - 1].string == 'def'
174 ):
123 ):
@@ -182,7 +131,7 b' if sys.version_info[0] >= 3:'
182 # ``replacetoken`` or any mechanism that changes semantics of module
131 # ``replacetoken`` or any mechanism that changes semantics of module
183 # loading is changed. Otherwise cached bytecode may get loaded without
132 # loading is changed. Otherwise cached bytecode may get loaded without
184 # the new transformation mechanisms applied.
133 # the new transformation mechanisms applied.
185 BYTECODEHEADER = b'HG\x00\x13'
134 BYTECODEHEADER = b'HG\x00\x14'
186
135
187 class hgloader(importlib.machinery.SourceFileLoader):
136 class hgloader(importlib.machinery.SourceFileLoader):
188 """Custom module loader that transforms source code.
137 """Custom module loader that transforms source code.
@@ -255,7 +255,7 b' class KeepAliveHandler(object):'
255 # If not a persistent connection, don't try to reuse it. Look
255 # If not a persistent connection, don't try to reuse it. Look
256 # for this using getattr() since vcr doesn't define this
256 # for this using getattr() since vcr doesn't define this
257 # attribute, and in that case always close the connection.
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 self._cm.remove(h)
259 self._cm.remove(h)
260
260
261 if DEBUG:
261 if DEBUG:
@@ -70,7 +70,7 b' def _importfrom(pkgname, modname):'
70 except AttributeError:
70 except AttributeError:
71 raise ImportError(r'cannot import name %s' % modname)
71 raise ImportError(r'cannot import name %s' % modname)
72 # force import; fakelocals[modname] may be replaced with the real module
72 # force import; fakelocals[modname] may be replaced with the real module
73 getattr(mod, r'__doc__', None)
73 getattr(mod, '__doc__', None)
74 return fakelocals[modname]
74 return fakelocals[modname]
75
75
76
76
@@ -94,7 +94,7 b' def _importfrom(pkgname, modname):'
94
94
95 def _checkmod(pkgname, modname, mod):
95 def _checkmod(pkgname, modname, mod):
96 expected = _cextversions.get((pkgname, modname))
96 expected = _cextversions.get((pkgname, modname))
97 actual = getattr(mod, r'version', None)
97 actual = getattr(mod, 'version', None)
98 if actual != expected:
98 if actual != expected:
99 raise ImportError(
99 raise ImportError(
100 r'cannot import module %s.%s '
100 r'cannot import module %s.%s '
@@ -270,7 +270,7 b' if ispy3:'
270 def getdoc(obj):
270 def getdoc(obj):
271 """Get docstring as bytes; may be None so gettext() won't confuse it
271 """Get docstring as bytes; may be None so gettext() won't confuse it
272 with _('')"""
272 with _('')"""
273 doc = getattr(obj, u'__doc__', None)
273 doc = getattr(obj, '__doc__', None)
274 if doc is None:
274 if doc is None:
275 return doc
275 return doc
276 return sysbytes(doc)
276 return sysbytes(doc)
@@ -24,7 +24,7 b' from ..utils import storageutil'
24
24
25
25
26 class basetestcase(unittest.TestCase):
26 class basetestcase(unittest.TestCase):
27 if not getattr(unittest.TestCase, r'assertRaisesRegex', False):
27 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
28 assertRaisesRegex = ( # camelcase-required
28 assertRaisesRegex = ( # camelcase-required
29 unittest.TestCase.assertRaisesRegexp
29 unittest.TestCase.assertRaisesRegexp
30 )
30 )
@@ -623,7 +623,7 b' class observedbufferedinputpipe(buffered'
623 def _fillbuffer(self):
623 def _fillbuffer(self):
624 res = super(observedbufferedinputpipe, self)._fillbuffer()
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 if fn:
627 if fn:
628 fn(res, _chunksize)
628 fn(res, _chunksize)
629
629
@@ -634,7 +634,7 b' class observedbufferedinputpipe(buffered'
634 def read(self, size):
634 def read(self, size):
635 res = super(observedbufferedinputpipe, self).read(size)
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 if fn:
638 if fn:
639 fn(res, size)
639 fn(res, size)
640
640
@@ -643,7 +643,7 b' class observedbufferedinputpipe(buffered'
643 def readline(self, *args, **kwargs):
643 def readline(self, *args, **kwargs):
644 res = super(observedbufferedinputpipe, self).readline(*args, **kwargs)
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 if fn:
647 if fn:
648 fn(res)
648 fn(res)
649
649
General Comments 0
You need to be logged in to leave comments. Login now