comparison mercurial/pycompat.py @ 43091:127cc1f72e70

py3: stop normalizing .encode()/.decode() arguments to unicode Now that we don't byte transform string literals, we no longer need this transform. While we're here, we also drop some superfluous u'' prefix in existing callers. Differential Revision: https://phab.mercurial-scm.org/D7011
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 06 Oct 2019 17:27:51 -0400
parents 1f339b503a40
children 813aa8cc55d4
comparison
equal deleted inserted replaced
43090:1f339b503a40 43091:127cc1f72e70
204 if not isinstance( 204 if not isinstance(
205 s, (bytes, bytearray) 205 s, (bytes, bytearray)
206 ) and not hasattr( # hasattr-py3-only 206 ) and not hasattr( # hasattr-py3-only
207 s, u'__bytes__' 207 s, u'__bytes__'
208 ): 208 ):
209 s = str(s).encode(u'ascii') 209 s = str(s).encode('ascii')
210 return bytes.__new__(cls, s) 210 return bytes.__new__(cls, s)
211 211
212 def __getitem__(self, key): 212 def __getitem__(self, key):
213 s = bytes.__getitem__(self, key) 213 s = bytes.__getitem__(self, key)
214 if not isinstance(s, bytes): 214 if not isinstance(s, bytes):
235 """Convert an internal str (e.g. keyword, __doc__) back to bytes 235 """Convert an internal str (e.g. keyword, __doc__) back to bytes
236 236
237 This never raises UnicodeEncodeError, but only ASCII characters 237 This never raises UnicodeEncodeError, but only ASCII characters
238 can be round-trip by sysstr(sysbytes(s)). 238 can be round-trip by sysstr(sysbytes(s)).
239 """ 239 """
240 return s.encode(u'utf-8') 240 return s.encode('utf-8')
241 241
242 def sysstr(s): 242 def sysstr(s):
243 """Return a keyword str to be passed to Python functions such as 243 """Return a keyword str to be passed to Python functions such as
244 getattr() and str.encode() 244 getattr() and str.encode()
245 245
247 considered invalid and mapped to arbitrary but unique code points 247 considered invalid and mapped to arbitrary but unique code points
248 such that 'sysstr(a) != sysstr(b)' for all 'a != b'. 248 such that 'sysstr(a) != sysstr(b)' for all 'a != b'.
249 """ 249 """
250 if isinstance(s, builtins.str): 250 if isinstance(s, builtins.str):
251 return s 251 return s
252 return s.decode(u'latin-1') 252 return s.decode('latin-1')
253 253
254 def strurl(url): 254 def strurl(url):
255 """Converts a bytes url back to str""" 255 """Converts a bytes url back to str"""
256 if isinstance(url, bytes): 256 if isinstance(url, bytes):
257 return url.decode(u'ascii') 257 return url.decode('ascii')
258 return url 258 return url
259 259
260 def bytesurl(url): 260 def bytesurl(url):
261 """Converts a str url to bytes by encoding in ascii""" 261 """Converts a str url to bytes by encoding in ascii"""
262 if isinstance(url, str): 262 if isinstance(url, str):
263 return url.encode(u'ascii') 263 return url.encode('ascii')
264 return url 264 return url
265 265
266 def raisewithtb(exc, tb): 266 def raisewithtb(exc, tb):
267 """Raise exception with the given traceback""" 267 """Raise exception with the given traceback"""
268 raise exc.with_traceback(tb) 268 raise exc.with_traceback(tb)