|
|
@ -1,10 +1,9 @@
|
|
|
|
"""Utilities for writing code that runs on Python 2 and 3"""
|
|
|
|
|
|
|
|
# This code is strewn with things that are not defined on Python3 (unicode,
|
|
|
|
# This code is strewn with things that are not defined on Python3 (unicode,
|
|
|
|
# long, etc) but they are all shielded by version checks. This is also an
|
|
|
|
# long, etc) but they are all shielded by version checks. This is also an
|
|
|
|
# upstrem vendored file that we're not going to modify on our own
|
|
|
|
# upstream vendored file that we're not going to modify on our own
|
|
|
|
# pylint: disable=undefined-variable
|
|
|
|
# pylint: disable=undefined-variable
|
|
|
|
|
|
|
|
|
|
|
|
# Copyright (c) 2010-2015 Benjamin Peterson
|
|
|
|
# Copyright (c) 2010-2017 Benjamin Peterson
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
@ -24,6 +23,8 @@
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
# SOFTWARE.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""Utilities for writing code that runs on Python 2 and 3"""
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
|
|
|
|
import functools
|
|
|
|
import functools
|
|
|
@ -33,7 +34,7 @@ import sys
|
|
|
|
import types
|
|
|
|
import types
|
|
|
|
|
|
|
|
|
|
|
|
__author__ = "Benjamin Peterson <benjamin@python.org>"
|
|
|
|
__author__ = "Benjamin Peterson <benjamin@python.org>"
|
|
|
|
__version__ = "1.10.0"
|
|
|
|
__version__ = "1.11.0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Useful for very coarse version differentiation.
|
|
|
|
# Useful for very coarse version differentiation.
|
|
|
@ -47,6 +48,7 @@ if PY3:
|
|
|
|
class_types = type,
|
|
|
|
class_types = type,
|
|
|
|
text_type = str
|
|
|
|
text_type = str
|
|
|
|
binary_type = bytes
|
|
|
|
binary_type = bytes
|
|
|
|
|
|
|
|
|
|
|
|
MAXSIZE = sys.maxsize
|
|
|
|
MAXSIZE = sys.maxsize
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
string_types = basestring,
|
|
|
|
string_types = basestring,
|
|
|
@ -244,6 +246,7 @@ _moved_attributes = [
|
|
|
|
MovedAttribute("map", "itertools", "builtins", "imap", "map"),
|
|
|
|
MovedAttribute("map", "itertools", "builtins", "imap", "map"),
|
|
|
|
MovedAttribute("getcwd", "os", "os", "getcwdu", "getcwd"),
|
|
|
|
MovedAttribute("getcwd", "os", "os", "getcwdu", "getcwd"),
|
|
|
|
MovedAttribute("getcwdb", "os", "os", "getcwd", "getcwdb"),
|
|
|
|
MovedAttribute("getcwdb", "os", "os", "getcwd", "getcwdb"),
|
|
|
|
|
|
|
|
MovedAttribute("getoutput", "commands", "subprocess"),
|
|
|
|
MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"),
|
|
|
|
MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"),
|
|
|
|
MovedAttribute("reload_module", "__builtin__", "importlib" if PY34 else "imp", "reload"),
|
|
|
|
MovedAttribute("reload_module", "__builtin__", "importlib" if PY34 else "imp", "reload"),
|
|
|
|
MovedAttribute("reduce", "__builtin__", "functools"),
|
|
|
|
MovedAttribute("reduce", "__builtin__", "functools"),
|
|
|
@ -265,10 +268,11 @@ _moved_attributes = [
|
|
|
|
MovedModule("html_entities", "htmlentitydefs", "html.entities"),
|
|
|
|
MovedModule("html_entities", "htmlentitydefs", "html.entities"),
|
|
|
|
MovedModule("html_parser", "HTMLParser", "html.parser"),
|
|
|
|
MovedModule("html_parser", "HTMLParser", "html.parser"),
|
|
|
|
MovedModule("http_client", "httplib", "http.client"),
|
|
|
|
MovedModule("http_client", "httplib", "http.client"),
|
|
|
|
|
|
|
|
MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"),
|
|
|
|
|
|
|
|
MovedModule("email_mime_image", "email.MIMEImage", "email.mime.image"),
|
|
|
|
MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"),
|
|
|
|
MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"),
|
|
|
|
MovedModule("email_mime_nonmultipart", "email.MIMENonMultipart", "email.mime.nonmultipart"),
|
|
|
|
MovedModule("email_mime_nonmultipart", "email.MIMENonMultipart", "email.mime.nonmultipart"),
|
|
|
|
MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"),
|
|
|
|
MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"),
|
|
|
|
MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"),
|
|
|
|
|
|
|
|
MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"),
|
|
|
|
MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"),
|
|
|
|
MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"),
|
|
|
|
MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"),
|
|
|
|
MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"),
|
|
|
|
MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"),
|
|
|
@ -340,10 +344,12 @@ _urllib_parse_moved_attributes = [
|
|
|
|
MovedAttribute("quote_plus", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("quote_plus", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("unquote", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("unquote", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("unquote_plus", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("unquote_plus", "urllib", "urllib.parse"),
|
|
|
|
|
|
|
|
MovedAttribute("unquote_to_bytes", "urllib", "urllib.parse", "unquote", "unquote_to_bytes"),
|
|
|
|
MovedAttribute("urlencode", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("urlencode", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("splitquery", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("splitquery", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("splittag", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("splittag", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("splituser", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("splituser", "urllib", "urllib.parse"),
|
|
|
|
|
|
|
|
MovedAttribute("splitvalue", "urllib", "urllib.parse"),
|
|
|
|
MovedAttribute("uses_fragment", "urlparse", "urllib.parse"),
|
|
|
|
MovedAttribute("uses_fragment", "urlparse", "urllib.parse"),
|
|
|
|
MovedAttribute("uses_netloc", "urlparse", "urllib.parse"),
|
|
|
|
MovedAttribute("uses_netloc", "urlparse", "urllib.parse"),
|
|
|
|
MovedAttribute("uses_params", "urlparse", "urllib.parse"),
|
|
|
|
MovedAttribute("uses_params", "urlparse", "urllib.parse"),
|
|
|
@ -419,6 +425,8 @@ _urllib_request_moved_attributes = [
|
|
|
|
MovedAttribute("URLopener", "urllib", "urllib.request"),
|
|
|
|
MovedAttribute("URLopener", "urllib", "urllib.request"),
|
|
|
|
MovedAttribute("FancyURLopener", "urllib", "urllib.request"),
|
|
|
|
MovedAttribute("FancyURLopener", "urllib", "urllib.request"),
|
|
|
|
MovedAttribute("proxy_bypass", "urllib", "urllib.request"),
|
|
|
|
MovedAttribute("proxy_bypass", "urllib", "urllib.request"),
|
|
|
|
|
|
|
|
MovedAttribute("parse_http_list", "urllib2", "urllib.request"),
|
|
|
|
|
|
|
|
MovedAttribute("parse_keqv_list", "urllib2", "urllib.request"),
|
|
|
|
]
|
|
|
|
]
|
|
|
|
for attr in _urllib_request_moved_attributes:
|
|
|
|
for attr in _urllib_request_moved_attributes:
|
|
|
|
setattr(Module_six_moves_urllib_request, attr.name, attr)
|
|
|
|
setattr(Module_six_moves_urllib_request, attr.name, attr)
|
|
|
@ -682,11 +690,15 @@ if PY3:
|
|
|
|
exec_ = getattr(moves.builtins, "exec")
|
|
|
|
exec_ = getattr(moves.builtins, "exec")
|
|
|
|
|
|
|
|
|
|
|
|
def reraise(tp, value, tb=None):
|
|
|
|
def reraise(tp, value, tb=None):
|
|
|
|
if value is None:
|
|
|
|
try:
|
|
|
|
value = tp()
|
|
|
|
if value is None:
|
|
|
|
if value.__traceback__ is not tb:
|
|
|
|
value = tp()
|
|
|
|
raise value.with_traceback(tb)
|
|
|
|
if value.__traceback__ is not tb:
|
|
|
|
raise value
|
|
|
|
raise value.with_traceback(tb)
|
|
|
|
|
|
|
|
raise value
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
value = None
|
|
|
|
|
|
|
|
tb = None
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
def exec_(_code_, _globs_=None, _locs_=None):
|
|
|
|
def exec_(_code_, _globs_=None, _locs_=None):
|
|
|
@ -702,19 +714,28 @@ else:
|
|
|
|
exec("""exec _code_ in _globs_, _locs_""")
|
|
|
|
exec("""exec _code_ in _globs_, _locs_""")
|
|
|
|
|
|
|
|
|
|
|
|
exec_("""def reraise(tp, value, tb=None):
|
|
|
|
exec_("""def reraise(tp, value, tb=None):
|
|
|
|
raise tp, value, tb
|
|
|
|
try:
|
|
|
|
|
|
|
|
raise tp, value, tb
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
tb = None
|
|
|
|
""")
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if sys.version_info[:2] == (3, 2):
|
|
|
|
if sys.version_info[:2] == (3, 2):
|
|
|
|
exec_("""def raise_from(value, from_value):
|
|
|
|
exec_("""def raise_from(value, from_value):
|
|
|
|
if from_value is None:
|
|
|
|
try:
|
|
|
|
raise value
|
|
|
|
if from_value is None:
|
|
|
|
raise value from from_value
|
|
|
|
raise value
|
|
|
|
|
|
|
|
raise value from from_value
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
value = None
|
|
|
|
""")
|
|
|
|
""")
|
|
|
|
elif sys.version_info[:2] > (3, 2):
|
|
|
|
elif sys.version_info[:2] > (3, 2):
|
|
|
|
exec_("""def raise_from(value, from_value):
|
|
|
|
exec_("""def raise_from(value, from_value):
|
|
|
|
raise value from from_value
|
|
|
|
try:
|
|
|
|
|
|
|
|
raise value from from_value
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
value = None
|
|
|
|
""")
|
|
|
|
""")
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
def raise_from(value, from_value):
|
|
|
|
def raise_from(value, from_value):
|
|
|
@ -805,10 +826,14 @@ def with_metaclass(meta, *bases):
|
|
|
|
# This requires a bit of explanation: the basic idea is to make a dummy
|
|
|
|
# This requires a bit of explanation: the basic idea is to make a dummy
|
|
|
|
# metaclass for one level of class instantiation that replaces itself with
|
|
|
|
# metaclass for one level of class instantiation that replaces itself with
|
|
|
|
# the actual metaclass.
|
|
|
|
# the actual metaclass.
|
|
|
|
class metaclass(meta):
|
|
|
|
class metaclass(type):
|
|
|
|
|
|
|
|
|
|
|
|
def __new__(cls, name, this_bases, d):
|
|
|
|
def __new__(cls, name, this_bases, d):
|
|
|
|
return meta(name, bases, d)
|
|
|
|
return meta(name, bases, d)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
|
|
def __prepare__(cls, name, this_bases):
|
|
|
|
|
|
|
|
return meta.__prepare__(name, bases)
|
|
|
|
return type.__new__(metaclass, 'temporary_class', (), {})
|
|
|
|
return type.__new__(metaclass, 'temporary_class', (), {})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|