pycompat.py
166 lines
| 4.2 KiB
| text/x-python
|
PythonLexer
/ mercurial / pycompat.py
timeless
|
r28818 | # 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 | ||||
Pulkit Goyal
|
r29584 | import sys | ||
if sys.version_info[0] < 3: | ||||
Pulkit Goyal
|
r29324 | import cPickle as pickle | ||
Pulkit Goyal
|
r29584 | import cStringIO as io | ||
Pulkit Goyal
|
r29455 | import httplib | ||
Pulkit Goyal
|
r29584 | import Queue as _queue | ||
Pulkit Goyal
|
r29433 | import SocketServer as socketserver | ||
Pulkit Goyal
|
r29584 | import urlparse | ||
Pulkit Goyal
|
r29432 | import xmlrpclib | ||
Pulkit Goyal
|
r29584 | else: | ||
import http.client as httplib | ||||
import io | ||||
import pickle | ||||
import queue as _queue | ||||
import socketserver | ||||
import urllib.parse as urlparse | ||||
Pulkit Goyal
|
r29432 | import xmlrpc.client as xmlrpclib | ||
Pulkit Goyal
|
r29431 | |||
Yuya Nishihara
|
r29798 | if sys.version_info[0] >= 3: | ||
Yuya Nishihara
|
r29797 | import builtins | ||
Yuya Nishihara
|
r29799 | import functools | ||
Yuya Nishihara
|
r29797 | |||
Yuya Nishihara
|
r29799 | 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 | ||||
Yuya Nishihara
|
r29800 | # these wrappers are automagically imported by hgloader | ||
Yuya Nishihara
|
r29799 | delattr = _wrapattrfunc(builtins.delattr) | ||
getattr = _wrapattrfunc(builtins.getattr) | ||||
hasattr = _wrapattrfunc(builtins.hasattr) | ||||
setattr = _wrapattrfunc(builtins.setattr) | ||||
Yuya Nishihara
|
r29800 | xrange = builtins.range | ||
Yuya Nishihara
|
r29799 | |||
Pulkit Goyal
|
r29584 | stringio = io.StringIO | ||
timeless
|
r28818 | empty = _queue.Empty | ||
queue = _queue.Queue | ||||
timeless
|
r28834 | |||
timeless
|
r28882 | class _pycompatstub(object): | ||
Yuya Nishihara
|
r29801 | def __init__(self): | ||
self._aliases = {} | ||||
timeless
|
r28882 | |||
Yuya Nishihara
|
r29801 | 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) | ||||
timeless
|
r28882 | |||
Yuya Nishihara
|
r29801 | def __getattr__(self, name): | ||
timeless
|
r28882 | try: | ||
Yuya Nishihara
|
r29801 | origin, item = self._aliases[name] | ||
except KeyError: | ||||
raise AttributeError(name) | ||||
self.__dict__[name] = obj = getattr(origin, item) | ||||
return obj | ||||
timeless
|
r28882 | |||
Pulkit Goyal
|
r29566 | httpserver = _pycompatstub() | ||
timeless
|
r28882 | urlreq = _pycompatstub() | ||
urlerr = _pycompatstub() | ||||
Yuya Nishihara
|
r29801 | if sys.version_info[0] < 3: | ||
Pulkit Goyal
|
r29566 | import BaseHTTPServer | ||
import CGIHTTPServer | ||||
import SimpleHTTPServer | ||||
timeless
|
r28882 | import urllib2 | ||
import urllib | ||||
Yuya Nishihara
|
r29801 | urlreq._registeraliases(urllib, ( | ||
timeless
|
r28882 | "addclosehook", | ||
"addinfourl", | ||||
"ftpwrapper", | ||||
"pathname2url", | ||||
"quote", | ||||
"splitattr", | ||||
"splitpasswd", | ||||
"splitport", | ||||
"splituser", | ||||
"unquote", | ||||
"url2pathname", | ||||
"urlencode", | ||||
)) | ||||
Yuya Nishihara
|
r29801 | urlreq._registeraliases(urllib2, ( | ||
timeless
|
r28882 | "AbstractHTTPHandler", | ||
"BaseHandler", | ||||
"build_opener", | ||||
"FileHandler", | ||||
"FTPHandler", | ||||
"HTTPBasicAuthHandler", | ||||
"HTTPDigestAuthHandler", | ||||
"HTTPHandler", | ||||
"HTTPPasswordMgrWithDefaultRealm", | ||||
"HTTPSHandler", | ||||
"install_opener", | ||||
"ProxyHandler", | ||||
"Request", | ||||
"urlopen", | ||||
)) | ||||
Yuya Nishihara
|
r29801 | urlerr._registeraliases(urllib2, ( | ||
timeless
|
r28882 | "HTTPError", | ||
"URLError", | ||||
)) | ||||
Yuya Nishihara
|
r29801 | httpserver._registeraliases(BaseHTTPServer, ( | ||
Pulkit Goyal
|
r29566 | "HTTPServer", | ||
"BaseHTTPRequestHandler", | ||||
)) | ||||
Yuya Nishihara
|
r29801 | httpserver._registeraliases(SimpleHTTPServer, ( | ||
Pulkit Goyal
|
r29566 | "SimpleHTTPRequestHandler", | ||
)) | ||||
Yuya Nishihara
|
r29801 | httpserver._registeraliases(CGIHTTPServer, ( | ||
Pulkit Goyal
|
r29566 | "CGIHTTPRequestHandler", | ||
)) | ||||
timeless
|
r28882 | |||
Yuya Nishihara
|
r29801 | else: | ||
timeless
|
r28882 | import urllib.request | ||
Yuya Nishihara
|
r29801 | urlreq._registeraliases(urllib.request, ( | ||
timeless
|
r28882 | "AbstractHTTPHandler", | ||
"addclosehook", | ||||
"addinfourl", | ||||
"BaseHandler", | ||||
"build_opener", | ||||
"FileHandler", | ||||
"FTPHandler", | ||||
"ftpwrapper", | ||||
"HTTPHandler", | ||||
"HTTPSHandler", | ||||
"install_opener", | ||||
"pathname2url", | ||||
"HTTPBasicAuthHandler", | ||||
"HTTPDigestAuthHandler", | ||||
Gregory Szorc
|
r29414 | "HTTPPasswordMgrWithDefaultRealm", | ||
timeless
|
r28882 | "ProxyHandler", | ||
"quote", | ||||
"Request", | ||||
"splitattr", | ||||
"splitpasswd", | ||||
"splitport", | ||||
"splituser", | ||||
"unquote", | ||||
"url2pathname", | ||||
"urlopen", | ||||
)) | ||||
import urllib.error | ||||
Yuya Nishihara
|
r29801 | urlerr._registeraliases(urllib.error, ( | ||
timeless
|
r28882 | "HTTPError", | ||
"URLError", | ||||
)) | ||||
Pulkit Goyal
|
r29566 | import http.server | ||
Yuya Nishihara
|
r29801 | httpserver._registeraliases(http.server, ( | ||
Pulkit Goyal
|
r29566 | "HTTPServer", | ||
"BaseHTTPRequestHandler", | ||||
"SimpleHTTPRequestHandler", | ||||
"CGIHTTPRequestHandler", | ||||
)) | ||||