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.
| pulkit |
| hg-reviewers |
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.
| Automatic diff as part of commit; lint not applicable. |
| Automatic diff as part of commit; unit tests not applicable. |
| Path | Packages | |||
|---|---|---|---|---|
| M | contrib/testparseutil.py (2 lines) | |||
| M | hgext/lfs/blobstore.py (2 lines) | |||
| M | mercurial/__init__.py (10 lines) | |||
| M | mercurial/pycompat.py (10 lines) |
| def bytestr(s): | def bytestr(s): | ||||
| # tiny version of pycompat.bytestr | # tiny version of pycompat.bytestr | ||||
| return s.encode('latin1') | return s.encode('latin1') | ||||
| def sysstr(s): | def sysstr(s): | ||||
| if isinstance(s, builtins.str): | if isinstance(s, builtins.str): | ||||
| return s | return s | ||||
| return s.decode(u'latin-1') | return s.decode('latin-1') | ||||
| def opentext(f): | def opentext(f): | ||||
| return open(f, 'r') | return open(f, 'r') | ||||
| else: | else: | ||||
| bytestr = str | bytestr = str | ||||
| sysstr = identity | sysstr = identity | ||||
| separators=(r'', r': '), | separators=(r'', r': '), | ||||
| sort_keys=True, | sort_keys=True, | ||||
| ) | ) | ||||
| ) | ) | ||||
| ) | ) | ||||
| def encodestr(x): | def encodestr(x): | ||||
| if isinstance(x, pycompat.unicode): | if isinstance(x, pycompat.unicode): | ||||
| return x.encode(u'utf-8') | return x.encode('utf-8') | ||||
| return x | return x | ||||
| return pycompat.rapply(encodestr, response) | return pycompat.rapply(encodestr, response) | ||||
| def _checkforservererror(self, pointers, responses, action): | def _checkforservererror(self, pointers, responses, action): | ||||
| """Scans errors from objects | """Scans errors from objects | ||||
| Raises LfsRemoteError if any objects have an error""" | Raises LfsRemoteError if any objects have an error""" | ||||
| 'setattr', | 'setattr', | ||||
| 'hasattr', | 'hasattr', | ||||
| 'safehasattr', | 'safehasattr', | ||||
| ) and not _isop(i - 1, '.'): | ) and not _isop(i - 1, '.'): | ||||
| arg1idx = _findargnofcall(1) | arg1idx = _findargnofcall(1) | ||||
| if arg1idx is not None: | if arg1idx is not None: | ||||
| _ensureunicode(arg1idx) | _ensureunicode(arg1idx) | ||||
| # .encode() and .decode() on str/bytes/unicode don't accept | |||||
| # byte strings on Python 3. | |||||
| elif fn in ('encode', 'decode') and _isop(i - 1, '.'): | |||||
| for argn in range(2): | |||||
| argidx = _findargnofcall(argn) | |||||
| if argidx is not None: | |||||
| _ensureunicode(argidx) | |||||
| # It changes iteritems/values to items/values as they are not | # It changes iteritems/values to items/values as they are not | ||||
| # present in Python 3 world. | # present in Python 3 world. | ||||
| elif fn in ('iteritems', 'itervalues') and not ( | elif fn in ('iteritems', 'itervalues') and not ( | ||||
| tokens[i - 1].type == token.NAME | tokens[i - 1].type == token.NAME | ||||
| and tokens[i - 1].string == 'def' | and tokens[i - 1].string == 'def' | ||||
| ): | ): | ||||
| yield t._replace(string=fn[4:]) | yield t._replace(string=fn[4:]) | ||||
| continue | continue | ||||
| # Emit unmodified token. | # Emit unmodified token. | ||||
| yield t | yield t | ||||
| # Header to add to bytecode files. This MUST be changed when | # Header to add to bytecode files. This MUST be changed when | ||||
| # ``replacetoken`` or any mechanism that changes semantics of module | # ``replacetoken`` or any mechanism that changes semantics of module | ||||
| # loading is changed. Otherwise cached bytecode may get loaded without | # loading is changed. Otherwise cached bytecode may get loaded without | ||||
| # the new transformation mechanisms applied. | # the new transformation mechanisms applied. | ||||
| BYTECODEHEADER = b'HG\x00\x12' | BYTECODEHEADER = b'HG\x00\x13' | ||||
| class hgloader(importlib.machinery.SourceFileLoader): | class hgloader(importlib.machinery.SourceFileLoader): | ||||
| """Custom module loader that transforms source code. | """Custom module loader that transforms source code. | ||||
| When the source code is converted to a code object, we transform | When the source code is converted to a code object, we transform | ||||
| certain patterns to be Python 3 compatible. This allows us to write code | certain patterns to be Python 3 compatible. This allows us to write code | ||||
| that is natively Python 2 and compatible with Python 3 without | that is natively Python 2 and compatible with Python 3 without | ||||
| making the code excessively ugly. | making the code excessively ugly. | ||||
| def __new__(cls, s=b''): | def __new__(cls, s=b''): | ||||
| if isinstance(s, bytestr): | if isinstance(s, bytestr): | ||||
| return s | return s | ||||
| if not isinstance( | if not isinstance( | ||||
| s, (bytes, bytearray) | s, (bytes, bytearray) | ||||
| ) and not hasattr( # hasattr-py3-only | ) and not hasattr( # hasattr-py3-only | ||||
| s, u'__bytes__' | s, u'__bytes__' | ||||
| ): | ): | ||||
| s = str(s).encode(u'ascii') | s = str(s).encode('ascii') | ||||
| return bytes.__new__(cls, s) | return bytes.__new__(cls, s) | ||||
| def __getitem__(self, key): | def __getitem__(self, key): | ||||
| s = bytes.__getitem__(self, key) | s = bytes.__getitem__(self, key) | ||||
| if not isinstance(s, bytes): | if not isinstance(s, bytes): | ||||
| s = bytechr(s) | s = bytechr(s) | ||||
| return s | return s | ||||
| return s | return s | ||||
| def sysbytes(s): | def sysbytes(s): | ||||
| """Convert an internal str (e.g. keyword, __doc__) back to bytes | """Convert an internal str (e.g. keyword, __doc__) back to bytes | ||||
| This never raises UnicodeEncodeError, but only ASCII characters | This never raises UnicodeEncodeError, but only ASCII characters | ||||
| can be round-trip by sysstr(sysbytes(s)). | can be round-trip by sysstr(sysbytes(s)). | ||||
| """ | """ | ||||
| return s.encode(u'utf-8') | return s.encode('utf-8') | ||||
| def sysstr(s): | def sysstr(s): | ||||
| """Return a keyword str to be passed to Python functions such as | """Return a keyword str to be passed to Python functions such as | ||||
| getattr() and str.encode() | getattr() and str.encode() | ||||
| This never raises UnicodeDecodeError. Non-ascii characters are | This never raises UnicodeDecodeError. Non-ascii characters are | ||||
| considered invalid and mapped to arbitrary but unique code points | considered invalid and mapped to arbitrary but unique code points | ||||
| such that 'sysstr(a) != sysstr(b)' for all 'a != b'. | such that 'sysstr(a) != sysstr(b)' for all 'a != b'. | ||||
| """ | """ | ||||
| if isinstance(s, builtins.str): | if isinstance(s, builtins.str): | ||||
| return s | return s | ||||
| return s.decode(u'latin-1') | return s.decode('latin-1') | ||||
| def strurl(url): | def strurl(url): | ||||
| """Converts a bytes url back to str""" | """Converts a bytes url back to str""" | ||||
| if isinstance(url, bytes): | if isinstance(url, bytes): | ||||
| return url.decode(u'ascii') | return url.decode('ascii') | ||||
| return url | return url | ||||
| def bytesurl(url): | def bytesurl(url): | ||||
| """Converts a str url to bytes by encoding in ascii""" | """Converts a str url to bytes by encoding in ascii""" | ||||
| if isinstance(url, str): | if isinstance(url, str): | ||||
| return url.encode(u'ascii') | return url.encode('ascii') | ||||
| return url | return url | ||||
| def raisewithtb(exc, tb): | def raisewithtb(exc, tb): | ||||
| """Raise exception with the given traceback""" | """Raise exception with the given traceback""" | ||||
| raise exc.with_traceback(tb) | raise exc.with_traceback(tb) | ||||
| def getdoc(obj): | def getdoc(obj): | ||||
| """Get docstring as bytes; may be None so gettext() won't confuse it | """Get docstring as bytes; may be None so gettext() won't confuse it | ||||