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-07-17 17:18:04 +03:00
|
|
|
import sys
|
2016-06-04 12:08:00 +03:00
|
|
|
|
2016-07-17 17:18:04 +03:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
import cPickle as pickle
|
|
|
|
import cStringIO as io
|
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-06-27 13:46:10 +03:00
|
|
|
import urlparse
|
2016-07-17 17:18:04 +03:00
|
|
|
import xmlrpclib
|
|
|
|
else:
|
|
|
|
import http.client as httplib
|
2016-04-06 23:31:31 +03:00
|
|
|
import io
|
2016-07-17 17:18:04 +03:00
|
|
|
import pickle
|
2016-04-06 23:00:49 +03:00
|
|
|
import queue as _queue
|
2016-07-17 17:18:04 +03:00
|
|
|
import socketserver
|
|
|
|
import urllib.parse as urlparse
|
|
|
|
import xmlrpc.client as xmlrpclib
|
|
|
|
|
2016-08-14 06:44:13 +03:00
|
|
|
if sys.version_info[0] >= 3:
|
2016-08-14 06:41:54 +03:00
|
|
|
import builtins
|
2016-08-14 06:51:21 +03:00
|
|
|
import functools
|
2016-08-14 06:41:54 +03:00
|
|
|
|
2016-08-14 06:51:21 +03:00
|
|
|
def _wrapattrfunc(f):
|
|
|
|
@functools.wraps(f)
|
|
|
|
def w(object, name, *args):
|
|
|
|
if isinstance(name, bytes):
|
|
|
|
name = name.decode(u'utf-8')
|
|
|
|
return f(object, name, *args)
|
|
|
|
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
|
2016-08-14 06:51:21 +03:00
|
|
|
|
2016-07-17 17:18:04 +03:00
|
|
|
stringio = io.StringIO
|
2016-04-06 23:00:49 +03:00
|
|
|
empty = _queue.Empty
|
|
|
|
queue = _queue.Queue
|
2016-04-07 01:35:52 +03:00
|
|
|
|
2016-04-07 03:05:48 +03:00
|
|
|
class _pycompatstub(object):
|
2016-08-14 08:46:24 +03:00
|
|
|
def __init__(self):
|
|
|
|
self._aliases = {}
|
2016-04-07 03:05:48 +03:00
|
|
|
|
2016-08-14 08:46:24 +03:00
|
|
|
def _registeraliases(self, origin, items):
|
|
|
|
"""Add items that will be populated at the first access"""
|
|
|
|
self._aliases.update((item.replace('_', '').lower(), (origin, item))
|
|
|
|
for item in items)
|
2016-04-07 03:05:48 +03:00
|
|
|
|
2016-08-14 08:46:24 +03:00
|
|
|
def __getattr__(self, name):
|
2016-04-07 03:05:48 +03:00
|
|
|
try:
|
2016-08-14 08:46:24 +03:00
|
|
|
origin, item = self._aliases[name]
|
|
|
|
except KeyError:
|
|
|
|
raise AttributeError(name)
|
|
|
|
self.__dict__[name] = obj = getattr(origin, item)
|
|
|
|
return obj
|
2016-04-07 03:05:48 +03:00
|
|
|
|
2016-07-13 21:08:29 +03:00
|
|
|
httpserver = _pycompatstub()
|
2016-04-07 03:05:48 +03:00
|
|
|
urlreq = _pycompatstub()
|
|
|
|
urlerr = _pycompatstub()
|
2016-08-14 08:46:24 +03:00
|
|
|
if sys.version_info[0] < 3:
|
2016-07-13 21:08:29 +03:00
|
|
|
import BaseHTTPServer
|
|
|
|
import CGIHTTPServer
|
|
|
|
import SimpleHTTPServer
|
2016-04-07 03:05:48 +03:00
|
|
|
import urllib2
|
|
|
|
import urllib
|
2016-08-14 08:46:24 +03:00
|
|
|
urlreq._registeraliases(urllib, (
|
2016-04-07 03:05:48 +03:00
|
|
|
"addclosehook",
|
|
|
|
"addinfourl",
|
|
|
|
"ftpwrapper",
|
|
|
|
"pathname2url",
|
|
|
|
"quote",
|
|
|
|
"splitattr",
|
|
|
|
"splitpasswd",
|
|
|
|
"splitport",
|
|
|
|
"splituser",
|
|
|
|
"unquote",
|
|
|
|
"url2pathname",
|
|
|
|
"urlencode",
|
|
|
|
))
|
2016-08-14 08:46:24 +03:00
|
|
|
urlreq._registeraliases(urllib2, (
|
2016-04-07 03:05:48 +03:00
|
|
|
"AbstractHTTPHandler",
|
|
|
|
"BaseHandler",
|
|
|
|
"build_opener",
|
|
|
|
"FileHandler",
|
|
|
|
"FTPHandler",
|
|
|
|
"HTTPBasicAuthHandler",
|
|
|
|
"HTTPDigestAuthHandler",
|
|
|
|
"HTTPHandler",
|
|
|
|
"HTTPPasswordMgrWithDefaultRealm",
|
|
|
|
"HTTPSHandler",
|
|
|
|
"install_opener",
|
|
|
|
"ProxyHandler",
|
|
|
|
"Request",
|
|
|
|
"urlopen",
|
|
|
|
))
|
2016-08-14 08:46:24 +03:00
|
|
|
urlerr._registeraliases(urllib2, (
|
2016-04-07 03:05:48 +03:00
|
|
|
"HTTPError",
|
|
|
|
"URLError",
|
|
|
|
))
|
2016-08-14 08:46:24 +03:00
|
|
|
httpserver._registeraliases(BaseHTTPServer, (
|
2016-07-13 21:08:29 +03:00
|
|
|
"HTTPServer",
|
|
|
|
"BaseHTTPRequestHandler",
|
|
|
|
))
|
2016-08-14 08:46:24 +03:00
|
|
|
httpserver._registeraliases(SimpleHTTPServer, (
|
2016-07-13 21:08:29 +03:00
|
|
|
"SimpleHTTPRequestHandler",
|
|
|
|
))
|
2016-08-14 08:46:24 +03:00
|
|
|
httpserver._registeraliases(CGIHTTPServer, (
|
2016-07-13 21:08:29 +03:00
|
|
|
"CGIHTTPRequestHandler",
|
|
|
|
))
|
2016-04-07 03:05:48 +03:00
|
|
|
|
2016-08-14 08:46:24 +03:00
|
|
|
else:
|
2016-04-07 03:05:48 +03:00
|
|
|
import urllib.request
|
2016-08-14 08:46:24 +03:00
|
|
|
urlreq._registeraliases(urllib.request, (
|
2016-04-07 03:05:48 +03:00
|
|
|
"AbstractHTTPHandler",
|
|
|
|
"addclosehook",
|
|
|
|
"addinfourl",
|
|
|
|
"BaseHandler",
|
|
|
|
"build_opener",
|
|
|
|
"FileHandler",
|
|
|
|
"FTPHandler",
|
|
|
|
"ftpwrapper",
|
|
|
|
"HTTPHandler",
|
|
|
|
"HTTPSHandler",
|
|
|
|
"install_opener",
|
|
|
|
"pathname2url",
|
|
|
|
"HTTPBasicAuthHandler",
|
|
|
|
"HTTPDigestAuthHandler",
|
2016-06-26 03:22:06 +03:00
|
|
|
"HTTPPasswordMgrWithDefaultRealm",
|
2016-04-07 03:05:48 +03:00
|
|
|
"ProxyHandler",
|
|
|
|
"quote",
|
|
|
|
"Request",
|
|
|
|
"splitattr",
|
|
|
|
"splitpasswd",
|
|
|
|
"splitport",
|
|
|
|
"splituser",
|
|
|
|
"unquote",
|
|
|
|
"url2pathname",
|
|
|
|
"urlopen",
|
|
|
|
))
|
|
|
|
import urllib.error
|
2016-08-14 08:46:24 +03:00
|
|
|
urlerr._registeraliases(urllib.error, (
|
2016-04-07 03:05:48 +03:00
|
|
|
"HTTPError",
|
|
|
|
"URLError",
|
|
|
|
))
|
2016-07-13 21:08:29 +03:00
|
|
|
import http.server
|
2016-08-14 08:46:24 +03:00
|
|
|
httpserver._registeraliases(http.server, (
|
2016-07-13 21:08:29 +03:00
|
|
|
"HTTPServer",
|
|
|
|
"BaseHTTPRequestHandler",
|
|
|
|
"SimpleHTTPRequestHandler",
|
|
|
|
"CGIHTTPRequestHandler",
|
|
|
|
))
|