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