2016-04-06 23:00:49 +03:00
|
|
|
# pycompat.py - portability shim for python 3
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2 or any later version.
|
|
|
|
|
|
|
|
"""Mercurial portability shim for python 3.
|
|
|
|
|
|
|
|
This contains aliases to hide python version-specific details from the core.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-12-06 04:06:36 +03:00
|
|
|
import getopt
|
2016-11-06 01:03:22 +03:00
|
|
|
import os
|
2016-12-25 00:36:55 +03:00
|
|
|
import shlex
|
2016-07-17 17:18:04 +03:00
|
|
|
import sys
|
2016-06-04 12:08:00 +03:00
|
|
|
|
2016-09-28 14:01:23 +03:00
|
|
|
ispy3 = (sys.version_info[0] >= 3)
|
2017-08-02 16:51:19 +03:00
|
|
|
ispypy = (r'__pypy__' in sys.builtin_module_names)
|
2016-09-28 14:01:23 +03:00
|
|
|
|
|
|
|
if not ispy3:
|
2017-03-10 08:35:21 +03:00
|
|
|
import cookielib
|
2016-07-17 17:18:04 +03:00
|
|
|
import cPickle as pickle
|
2016-06-28 13:31:53 +03:00
|
|
|
import httplib
|
2016-07-17 17:18:04 +03:00
|
|
|
import Queue as _queue
|
2016-06-27 14:18:54 +03:00
|
|
|
import SocketServer as socketserver
|
2016-07-17 17:18:04 +03:00
|
|
|
import xmlrpclib
|
|
|
|
else:
|
2018-02-10 04:31:41 +03:00
|
|
|
import http.cookiejar as cookielib # noqa: F401
|
|
|
|
import http.client as httplib # noqa: F401
|
|
|
|
import pickle # noqa: F401
|
2016-04-06 23:00:49 +03:00
|
|
|
import queue as _queue
|
2018-02-10 04:31:41 +03:00
|
|
|
import socketserver # noqa: F401
|
|
|
|
import xmlrpc.client as xmlrpclib # noqa: F401
|
2016-07-17 17:18:04 +03:00
|
|
|
|
2017-06-16 00:31:22 +03:00
|
|
|
empty = _queue.Empty
|
|
|
|
queue = _queue.Queue
|
|
|
|
|
2017-03-29 15:13:55 +03:00
|
|
|
def identity(a):
|
|
|
|
return a
|
|
|
|
|
2016-09-28 14:01:23 +03:00
|
|
|
if ispy3:
|
2016-08-14 06:41:54 +03:00
|
|
|
import builtins
|
2016-08-14 06:51:21 +03:00
|
|
|
import functools
|
2017-03-12 22:54:11 +03:00
|
|
|
import io
|
2017-03-15 19:30:50 +03:00
|
|
|
import struct
|
2017-03-12 22:54:11 +03:00
|
|
|
|
2016-10-09 18:44:23 +03:00
|
|
|
fsencode = os.fsencode
|
2016-11-06 00:42:40 +03:00
|
|
|
fsdecode = os.fsdecode
|
2017-03-29 15:23:28 +03:00
|
|
|
oslinesep = os.linesep.encode('ascii')
|
2016-11-06 01:03:22 +03:00
|
|
|
osname = os.name.encode('ascii')
|
2016-11-06 01:14:44 +03:00
|
|
|
ospathsep = os.pathsep.encode('ascii')
|
|
|
|
ossep = os.sep.encode('ascii')
|
2016-12-17 22:14:21 +03:00
|
|
|
osaltsep = os.altsep
|
|
|
|
if osaltsep:
|
|
|
|
osaltsep = osaltsep.encode('ascii')
|
2016-11-22 16:16:50 +03:00
|
|
|
# os.getcwd() on Python 3 returns string, but it has os.getcwdb() which
|
|
|
|
# returns bytes.
|
|
|
|
getcwd = os.getcwdb
|
2016-12-17 22:22:05 +03:00
|
|
|
sysplatform = sys.platform.encode('ascii')
|
2016-12-19 21:32:24 +03:00
|
|
|
sysexecutable = sys.executable
|
|
|
|
if sysexecutable:
|
|
|
|
sysexecutable = os.fsencode(sysexecutable)
|
2017-03-12 22:25:14 +03:00
|
|
|
stringio = io.BytesIO
|
2017-03-19 21:12:38 +03:00
|
|
|
maplist = lambda *args: list(map(*args))
|
2017-12-11 03:26:35 +03:00
|
|
|
ziplist = lambda *args: list(zip(*args))
|
2017-08-16 07:54:24 +03:00
|
|
|
rawinput = input
|
2016-11-09 16:06:09 +03:00
|
|
|
|
2016-10-20 17:40:24 +03:00
|
|
|
# TODO: .buffer might not exist if std streams were replaced; we'll need
|
|
|
|
# a silly wrapper to make a bytes stream backed by a unicode one.
|
|
|
|
stdin = sys.stdin.buffer
|
|
|
|
stdout = sys.stdout.buffer
|
|
|
|
stderr = sys.stderr.buffer
|
|
|
|
|
2016-11-09 16:06:09 +03:00
|
|
|
# Since Python 3 converts argv to wchar_t type by Py_DecodeLocale() on Unix,
|
|
|
|
# we can use os.fsencode() to get back bytes argv.
|
|
|
|
#
|
|
|
|
# https://hg.python.org/cpython/file/v3.5.1/Programs/python.c#l55
|
|
|
|
#
|
|
|
|
# TODO: On Windows, the native argv is wchar_t, so we'll need a different
|
|
|
|
# workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior.
|
2017-03-07 21:24:24 +03:00
|
|
|
if getattr(sys, 'argv', None) is not None:
|
|
|
|
sysargv = list(map(os.fsencode, sys.argv))
|
2016-08-14 06:41:54 +03:00
|
|
|
|
2017-03-15 19:30:50 +03:00
|
|
|
bytechr = struct.Struct('>B').pack
|
2017-03-08 16:30:12 +03:00
|
|
|
|
2017-03-08 16:48:26 +03:00
|
|
|
class bytestr(bytes):
|
|
|
|
"""A bytes which mostly acts as a Python 2 str
|
|
|
|
|
|
|
|
>>> bytestr(), bytestr(bytearray(b'foo')), bytestr(u'ascii'), bytestr(1)
|
|
|
|
(b'', b'foo', b'ascii', b'1')
|
|
|
|
>>> s = bytestr(b'foo')
|
|
|
|
>>> assert s is bytestr(s)
|
|
|
|
|
2017-05-04 05:51:07 +03:00
|
|
|
__bytes__() should be called if provided:
|
|
|
|
|
|
|
|
>>> class bytesable(object):
|
|
|
|
... def __bytes__(self):
|
|
|
|
... return b'bytes'
|
|
|
|
>>> bytestr(bytesable())
|
|
|
|
b'bytes'
|
|
|
|
|
2017-03-08 16:48:26 +03:00
|
|
|
There's no implicit conversion from non-ascii str as its encoding is
|
|
|
|
unknown:
|
|
|
|
|
|
|
|
>>> bytestr(chr(0x80)) # doctest: +ELLIPSIS
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
UnicodeEncodeError: ...
|
|
|
|
|
|
|
|
Comparison between bytestr and bytes should work:
|
|
|
|
|
|
|
|
>>> assert bytestr(b'foo') == b'foo'
|
|
|
|
>>> assert b'foo' == bytestr(b'foo')
|
|
|
|
>>> assert b'f' in bytestr(b'foo')
|
|
|
|
>>> assert bytestr(b'f') in b'foo'
|
|
|
|
|
|
|
|
Sliced elements should be bytes, not integer:
|
|
|
|
|
|
|
|
>>> s[1], s[:2]
|
|
|
|
(b'o', b'fo')
|
|
|
|
>>> list(s), list(reversed(s))
|
|
|
|
([b'f', b'o', b'o'], [b'o', b'o', b'f'])
|
|
|
|
|
|
|
|
As bytestr type isn't propagated across operations, you need to cast
|
|
|
|
bytes to bytestr explicitly:
|
|
|
|
|
|
|
|
>>> s = bytestr(b'foo').upper()
|
|
|
|
>>> t = bytestr(s)
|
|
|
|
>>> s[0], t[0]
|
|
|
|
(70, b'F')
|
|
|
|
|
|
|
|
Be careful to not pass a bytestr object to a function which expects
|
|
|
|
bytearray-like behavior.
|
|
|
|
|
|
|
|
>>> t = bytes(t) # cast to bytes
|
|
|
|
>>> assert type(t) is bytes
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __new__(cls, s=b''):
|
|
|
|
if isinstance(s, bytestr):
|
|
|
|
return s
|
2017-05-04 05:51:07 +03:00
|
|
|
if (not isinstance(s, (bytes, bytearray))
|
|
|
|
and not hasattr(s, u'__bytes__')): # hasattr-py3-only
|
2017-03-08 16:48:26 +03:00
|
|
|
s = str(s).encode(u'ascii')
|
|
|
|
return bytes.__new__(cls, s)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
s = bytes.__getitem__(self, key)
|
|
|
|
if not isinstance(s, bytes):
|
|
|
|
s = bytechr(s)
|
|
|
|
return s
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iterbytestr(bytes.__iter__(self))
|
|
|
|
|
2017-03-13 03:04:45 +03:00
|
|
|
def iterbytestr(s):
|
|
|
|
"""Iterate bytes as if it were a str object of Python 2"""
|
2017-03-15 19:32:18 +03:00
|
|
|
return map(bytechr, s)
|
2017-03-13 03:04:45 +03:00
|
|
|
|
2017-04-04 18:34:58 +03:00
|
|
|
def sysbytes(s):
|
|
|
|
"""Convert an internal str (e.g. keyword, __doc__) back to bytes
|
|
|
|
|
|
|
|
This never raises UnicodeEncodeError, but only ASCII characters
|
|
|
|
can be round-trip by sysstr(sysbytes(s)).
|
|
|
|
"""
|
|
|
|
return s.encode(u'utf-8')
|
|
|
|
|
2016-09-28 16:32:09 +03:00
|
|
|
def sysstr(s):
|
|
|
|
"""Return a keyword str to be passed to Python functions such as
|
|
|
|
getattr() and str.encode()
|
|
|
|
|
|
|
|
This never raises UnicodeDecodeError. Non-ascii characters are
|
|
|
|
considered invalid and mapped to arbitrary but unique code points
|
|
|
|
such that 'sysstr(a) != sysstr(b)' for all 'a != b'.
|
|
|
|
"""
|
|
|
|
if isinstance(s, builtins.str):
|
|
|
|
return s
|
|
|
|
return s.decode(u'latin-1')
|
|
|
|
|
2017-06-15 22:02:52 +03:00
|
|
|
def strurl(url):
|
|
|
|
"""Converts a bytes url back to str"""
|
|
|
|
return url.decode(u'ascii')
|
|
|
|
|
2017-06-15 22:06:17 +03:00
|
|
|
def bytesurl(url):
|
|
|
|
"""Converts a str url to bytes by encoding in ascii"""
|
|
|
|
return url.encode(u'ascii')
|
|
|
|
|
2017-04-20 16:16:12 +03:00
|
|
|
def raisewithtb(exc, tb):
|
|
|
|
"""Raise exception with the given traceback"""
|
|
|
|
raise exc.with_traceback(tb)
|
|
|
|
|
2017-06-01 16:24:15 +03:00
|
|
|
def getdoc(obj):
|
|
|
|
"""Get docstring as bytes; may be None so gettext() won't confuse it
|
|
|
|
with _('')"""
|
|
|
|
doc = getattr(obj, u'__doc__', None)
|
|
|
|
if doc is None:
|
|
|
|
return doc
|
|
|
|
return sysbytes(doc)
|
|
|
|
|
2016-08-14 06:51:21 +03:00
|
|
|
def _wrapattrfunc(f):
|
|
|
|
@functools.wraps(f)
|
|
|
|
def w(object, name, *args):
|
2016-09-28 16:32:09 +03:00
|
|
|
return f(object, sysstr(name), *args)
|
2016-08-14 06:51:21 +03:00
|
|
|
return w
|
|
|
|
|
2016-08-16 06:35:15 +03:00
|
|
|
# these wrappers are automagically imported by hgloader
|
2016-08-14 06:51:21 +03:00
|
|
|
delattr = _wrapattrfunc(builtins.delattr)
|
|
|
|
getattr = _wrapattrfunc(builtins.getattr)
|
|
|
|
hasattr = _wrapattrfunc(builtins.hasattr)
|
|
|
|
setattr = _wrapattrfunc(builtins.setattr)
|
2016-08-16 06:35:15 +03:00
|
|
|
xrange = builtins.range
|
2017-04-07 21:05:51 +03:00
|
|
|
unicode = str
|
2016-08-14 06:51:21 +03:00
|
|
|
|
2017-03-03 10:34:32 +03:00
|
|
|
def open(name, mode='r', buffering=-1):
|
|
|
|
return builtins.open(name, sysstr(mode), buffering)
|
|
|
|
|
2017-11-23 19:09:00 +03:00
|
|
|
def _getoptbwrapper(orig, args, shortlist, namelist):
|
2017-06-16 00:18:17 +03:00
|
|
|
"""
|
|
|
|
Takes bytes arguments, converts them to unicode, pass them to
|
|
|
|
getopt.getopt(), convert the returned values back to bytes and then
|
|
|
|
return them for Python 3 compatibility as getopt.getopt() don't accepts
|
|
|
|
bytes on Python 3.
|
|
|
|
"""
|
2016-12-06 04:06:36 +03:00
|
|
|
args = [a.decode('latin-1') for a in args]
|
|
|
|
shortlist = shortlist.decode('latin-1')
|
|
|
|
namelist = [a.decode('latin-1') for a in namelist]
|
2017-11-23 19:09:00 +03:00
|
|
|
opts, args = orig(args, shortlist, namelist)
|
2016-12-06 04:06:36 +03:00
|
|
|
opts = [(a[0].encode('latin-1'), a[1].encode('latin-1'))
|
|
|
|
for a in opts]
|
|
|
|
args = [a.encode('latin-1') for a in args]
|
|
|
|
return opts, args
|
|
|
|
|
2016-12-07 19:23:03 +03:00
|
|
|
def strkwargs(dic):
|
2017-06-16 00:18:17 +03:00
|
|
|
"""
|
|
|
|
Converts the keys of a python dictonary to str i.e. unicodes so that
|
|
|
|
they can be passed as keyword arguments as dictonaries with bytes keys
|
|
|
|
can't be passed as keyword arguments to functions on Python 3.
|
|
|
|
"""
|
2016-12-07 19:23:03 +03:00
|
|
|
dic = dict((k.decode('latin-1'), v) for k, v in dic.iteritems())
|
|
|
|
return dic
|
|
|
|
|
|
|
|
def byteskwargs(dic):
|
2017-06-16 00:18:17 +03:00
|
|
|
"""
|
|
|
|
Converts keys of python dictonaries to bytes as they were converted to
|
|
|
|
str to pass that dictonary as a keyword argument on Python 3.
|
|
|
|
"""
|
2016-12-07 19:23:03 +03:00
|
|
|
dic = dict((k.encode('latin-1'), v) for k, v in dic.iteritems())
|
|
|
|
return dic
|
|
|
|
|
2016-12-25 00:36:55 +03:00
|
|
|
# TODO: handle shlex.shlex().
|
|
|
|
def shlexsplit(s):
|
2017-06-16 00:18:17 +03:00
|
|
|
"""
|
|
|
|
Takes bytes argument, convert it to str i.e. unicodes, pass that into
|
|
|
|
shlex.split(), convert the returned value to bytes and return that for
|
|
|
|
Python 3 compatibility as shelx.split() don't accept bytes on Python 3.
|
|
|
|
"""
|
2016-12-25 00:36:55 +03:00
|
|
|
ret = shlex.split(s.decode('latin-1'))
|
|
|
|
return [a.encode('latin-1') for a in ret]
|
|
|
|
|
2016-09-28 16:32:09 +03:00
|
|
|
else:
|
2017-03-12 22:54:11 +03:00
|
|
|
import cStringIO
|
|
|
|
|
2017-03-08 16:30:12 +03:00
|
|
|
bytechr = chr
|
2017-03-08 16:48:26 +03:00
|
|
|
bytestr = str
|
2017-03-13 03:04:45 +03:00
|
|
|
iterbytestr = iter
|
2017-04-04 18:34:58 +03:00
|
|
|
sysbytes = identity
|
2017-03-29 15:13:55 +03:00
|
|
|
sysstr = identity
|
2017-06-15 22:02:52 +03:00
|
|
|
strurl = identity
|
2017-06-15 22:06:17 +03:00
|
|
|
bytesurl = identity
|
2016-09-28 16:32:09 +03:00
|
|
|
|
2017-04-20 16:16:12 +03:00
|
|
|
# this can't be parsed on Python 3
|
|
|
|
exec('def raisewithtb(exc, tb):\n'
|
|
|
|
' raise exc, None, tb\n')
|
|
|
|
|
2016-10-11 01:11:15 +03:00
|
|
|
def fsencode(filename):
|
2017-06-16 00:18:17 +03:00
|
|
|
"""
|
|
|
|
Partial backport from os.py in Python 3, which only accepts bytes.
|
|
|
|
In Python 2, our paths should only ever be bytes, a unicode path
|
|
|
|
indicates a bug.
|
|
|
|
"""
|
2016-10-11 01:11:15 +03:00
|
|
|
if isinstance(filename, str):
|
|
|
|
return filename
|
2016-10-09 18:44:23 +03:00
|
|
|
else:
|
2016-10-11 01:11:15 +03:00
|
|
|
raise TypeError(
|
|
|
|
"expect str, not %s" % type(filename).__name__)
|
2016-10-09 18:44:23 +03:00
|
|
|
|
2016-11-06 00:42:40 +03:00
|
|
|
# In Python 2, fsdecode() has a very chance to receive bytes. So it's
|
|
|
|
# better not to touch Python 2 part as it's already working fine.
|
2017-03-29 15:13:55 +03:00
|
|
|
fsdecode = identity
|
2016-11-06 00:42:40 +03:00
|
|
|
|
2017-06-01 16:24:15 +03:00
|
|
|
def getdoc(obj):
|
|
|
|
return getattr(obj, '__doc__', None)
|
|
|
|
|
2017-11-23 19:09:00 +03:00
|
|
|
def _getoptbwrapper(orig, args, shortlist, namelist):
|
|
|
|
return orig(args, shortlist, namelist)
|
2016-12-06 04:06:36 +03:00
|
|
|
|
2017-03-29 15:13:55 +03:00
|
|
|
strkwargs = identity
|
|
|
|
byteskwargs = identity
|
2016-12-07 19:23:03 +03:00
|
|
|
|
2017-03-29 15:23:28 +03:00
|
|
|
oslinesep = os.linesep
|
2016-11-06 01:03:22 +03:00
|
|
|
osname = os.name
|
2016-11-06 01:14:44 +03:00
|
|
|
ospathsep = os.pathsep
|
|
|
|
ossep = os.sep
|
2016-12-17 22:14:21 +03:00
|
|
|
osaltsep = os.altsep
|
2016-10-20 17:40:24 +03:00
|
|
|
stdin = sys.stdin
|
|
|
|
stdout = sys.stdout
|
|
|
|
stderr = sys.stderr
|
2017-03-07 21:24:24 +03:00
|
|
|
if getattr(sys, 'argv', None) is not None:
|
|
|
|
sysargv = sys.argv
|
2016-12-17 22:22:05 +03:00
|
|
|
sysplatform = sys.platform
|
2016-11-22 16:16:50 +03:00
|
|
|
getcwd = os.getcwd
|
2016-12-19 21:32:24 +03:00
|
|
|
sysexecutable = sys.executable
|
2016-12-25 00:36:55 +03:00
|
|
|
shlexsplit = shlex.split
|
2017-03-12 22:54:11 +03:00
|
|
|
stringio = cStringIO.StringIO
|
2017-03-19 21:12:38 +03:00
|
|
|
maplist = map
|
2017-12-11 03:26:35 +03:00
|
|
|
ziplist = zip
|
flake8: enable F821 check
Summary:
This check is useful and detects real errors (ex. fbconduit). Unfortunately
`arc lint` will run it with both py2 and py3 so a lot of py2 builtins will
still be warned.
I didn't find a clean way to disable py3 check. So this diff tries to fix them.
For `xrange`, the change was done by a script:
```
import sys
import redbaron
headertypes = {'comment', 'endl', 'from_import', 'import', 'string',
'assignment', 'atomtrailers'}
xrangefix = '''try:
xrange(0)
except NameError:
xrange = range
'''
def isxrange(x):
try:
return x[0].value == 'xrange'
except Exception:
return False
def main(argv):
for i, path in enumerate(argv):
print('(%d/%d) scanning %s' % (i + 1, len(argv), path))
content = open(path).read()
try:
red = redbaron.RedBaron(content)
except Exception:
print(' warning: failed to parse')
continue
hasxrange = red.find('atomtrailersnode', value=isxrange)
hasxrangefix = 'xrange = range' in content
if hasxrangefix or not hasxrange:
print(' no need to change')
continue
# find a place to insert the compatibility statement
changed = False
for node in red:
if node.type in headertypes:
continue
# node.insert_before is an easier API, but it has bugs changing
# other "finally" and "except" positions. So do the insert
# manually.
# # node.insert_before(xrangefix)
line = node.absolute_bounding_box.top_left.line - 1
lines = content.splitlines(1)
content = ''.join(lines[:line]) + xrangefix + ''.join(lines[line:])
changed = True
break
if changed:
# "content" is faster than "red.dumps()"
open(path, 'w').write(content)
print(' updated')
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
```
For other py2 builtins that do not have a py3 equivalent, some `# noqa`
were added as a workaround for now.
Reviewed By: DurhamG
Differential Revision: D6934535
fbshipit-source-id: 546b62830af144bc8b46788d2e0fd00496838939
2018-02-10 04:31:44 +03:00
|
|
|
rawinput = raw_input # noqa
|
2017-10-12 03:27:21 +03:00
|
|
|
|
|
|
|
isjython = sysplatform.startswith('java')
|
2017-10-13 05:20:04 +03:00
|
|
|
|
|
|
|
isdarwin = sysplatform == 'darwin'
|
|
|
|
isposix = osname == 'posix'
|
|
|
|
iswindows = osname == 'nt'
|
2017-11-23 19:09:00 +03:00
|
|
|
|
|
|
|
def getoptb(args, shortlist, namelist):
|
|
|
|
return _getoptbwrapper(getopt.getopt, args, shortlist, namelist)
|
|
|
|
|
|
|
|
def gnugetoptb(args, shortlist, namelist):
|
|
|
|
return _getoptbwrapper(getopt.gnu_getopt, args, shortlist, namelist)
|