support: upgrade bundled six to 1.10 (dbfbfc818e3d)
This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
"""Utilities for writing code that runs on Python 2 and 3"""
|
# Copyright (c) 2010-2017 Benjamin Peterson
|
||||||
|
|
||||||
# Copyright (c) 2010-2014 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
|
||||||
@@ -20,20 +18,24 @@
|
|||||||
# 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
|
||||||
|
import itertools
|
||||||
import operator
|
import operator
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
|
|
||||||
__author__ = "Benjamin Peterson <benjamin@python.org>"
|
__author__ = "Benjamin Peterson <benjamin@python.org>"
|
||||||
__version__ = "1.8.0"
|
__version__ = "1.10.0"
|
||||||
|
|
||||||
|
|
||||||
# Useful for very coarse version differentiation.
|
# Useful for very coarse version differentiation.
|
||||||
PY2 = sys.version_info[0] == 2
|
PY2 = sys.version_info[0] == 2
|
||||||
PY3 = sys.version_info[0] == 3
|
PY3 = sys.version_info[0] == 3
|
||||||
|
PY34 = sys.version_info[0:2] >= (3, 4)
|
||||||
|
|
||||||
if PY3:
|
if PY3:
|
||||||
string_types = str,
|
string_types = str,
|
||||||
@@ -56,6 +58,7 @@ else:
|
|||||||
else:
|
else:
|
||||||
# It's possible to have sizeof(long) != sizeof(Py_ssize_t).
|
# It's possible to have sizeof(long) != sizeof(Py_ssize_t).
|
||||||
class X(object):
|
class X(object):
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return 1 << 31
|
return 1 << 31
|
||||||
try:
|
try:
|
||||||
@@ -88,8 +91,12 @@ class _LazyDescr(object):
|
|||||||
def __get__(self, obj, tp):
|
def __get__(self, obj, tp):
|
||||||
result = self._resolve()
|
result = self._resolve()
|
||||||
setattr(obj, self.name, result) # Invokes __set__.
|
setattr(obj, self.name, result) # Invokes __set__.
|
||||||
# This is a bit ugly, but it avoids running this again.
|
try:
|
||||||
|
# This is a bit ugly, but it avoids running this again by
|
||||||
|
# removing this descriptor.
|
||||||
delattr(obj.__class__, self.name)
|
delattr(obj.__class__, self.name)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@@ -155,12 +162,14 @@ class MovedAttribute(_LazyDescr):
|
|||||||
|
|
||||||
|
|
||||||
class _SixMetaPathImporter(object):
|
class _SixMetaPathImporter(object):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
A meta path importer to import six.moves and its submodules.
|
A meta path importer to import six.moves and its submodules.
|
||||||
|
|
||||||
This class implements a PEP302 finder and loader. It should be compatible
|
This class implements a PEP302 finder and loader. It should be compatible
|
||||||
with Python 2.5 and all existing versions of Python3
|
with Python 2.5 and all existing versions of Python3
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, six_module_name):
|
def __init__(self, six_module_name):
|
||||||
self.name = six_module_name
|
self.name = six_module_name
|
||||||
self.known_modules = {}
|
self.known_modules = {}
|
||||||
@@ -218,6 +227,7 @@ _importer = _SixMetaPathImporter(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class _MovedItems(_LazyModule):
|
class _MovedItems(_LazyModule):
|
||||||
|
|
||||||
"""Lazy loading of moved objects"""
|
"""Lazy loading of moved objects"""
|
||||||
__path__ = [] # mark as package
|
__path__ = [] # mark as package
|
||||||
|
|
||||||
@@ -229,8 +239,10 @@ _moved_attributes = [
|
|||||||
MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"),
|
MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"),
|
||||||
MovedAttribute("intern", "__builtin__", "sys"),
|
MovedAttribute("intern", "__builtin__", "sys"),
|
||||||
MovedAttribute("map", "itertools", "builtins", "imap", "map"),
|
MovedAttribute("map", "itertools", "builtins", "imap", "map"),
|
||||||
|
MovedAttribute("getcwd", "os", "os", "getcwdu", "getcwd"),
|
||||||
|
MovedAttribute("getcwdb", "os", "os", "getcwd", "getcwdb"),
|
||||||
MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"),
|
MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"),
|
||||||
MovedAttribute("reload_module", "__builtin__", "imp", "reload"),
|
MovedAttribute("reload_module", "__builtin__", "importlib" if PY34 else "imp", "reload"),
|
||||||
MovedAttribute("reduce", "__builtin__", "functools"),
|
MovedAttribute("reduce", "__builtin__", "functools"),
|
||||||
MovedAttribute("shlex_quote", "pipes", "shlex", "quote"),
|
MovedAttribute("shlex_quote", "pipes", "shlex", "quote"),
|
||||||
MovedAttribute("StringIO", "StringIO", "io"),
|
MovedAttribute("StringIO", "StringIO", "io"),
|
||||||
@@ -240,7 +252,6 @@ _moved_attributes = [
|
|||||||
MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"),
|
MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"),
|
||||||
MovedAttribute("zip", "itertools", "builtins", "izip", "zip"),
|
MovedAttribute("zip", "itertools", "builtins", "izip", "zip"),
|
||||||
MovedAttribute("zip_longest", "itertools", "itertools", "izip_longest", "zip_longest"),
|
MovedAttribute("zip_longest", "itertools", "itertools", "izip_longest", "zip_longest"),
|
||||||
|
|
||||||
MovedModule("builtins", "__builtin__"),
|
MovedModule("builtins", "__builtin__"),
|
||||||
MovedModule("configparser", "ConfigParser"),
|
MovedModule("configparser", "ConfigParser"),
|
||||||
MovedModule("copyreg", "copy_reg"),
|
MovedModule("copyreg", "copy_reg"),
|
||||||
@@ -251,10 +262,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"),
|
||||||
@@ -287,8 +299,13 @@ _moved_attributes = [
|
|||||||
MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"),
|
MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"),
|
||||||
MovedModule("xmlrpc_client", "xmlrpclib", "xmlrpc.client"),
|
MovedModule("xmlrpc_client", "xmlrpclib", "xmlrpc.client"),
|
||||||
MovedModule("xmlrpc_server", "SimpleXMLRPCServer", "xmlrpc.server"),
|
MovedModule("xmlrpc_server", "SimpleXMLRPCServer", "xmlrpc.server"),
|
||||||
MovedModule("winreg", "_winreg"),
|
|
||||||
]
|
]
|
||||||
|
# Add windows specific modules.
|
||||||
|
if sys.platform == "win32":
|
||||||
|
_moved_attributes += [
|
||||||
|
MovedModule("winreg", "_winreg"),
|
||||||
|
]
|
||||||
|
|
||||||
for attr in _moved_attributes:
|
for attr in _moved_attributes:
|
||||||
setattr(_MovedItems, attr.name, attr)
|
setattr(_MovedItems, attr.name, attr)
|
||||||
if isinstance(attr, MovedModule):
|
if isinstance(attr, MovedModule):
|
||||||
@@ -302,6 +319,7 @@ _importer._add_module(moves, "moves")
|
|||||||
|
|
||||||
|
|
||||||
class Module_six_moves_urllib_parse(_LazyModule):
|
class Module_six_moves_urllib_parse(_LazyModule):
|
||||||
|
|
||||||
"""Lazy loading of moved objects in six.moves.urllib_parse"""
|
"""Lazy loading of moved objects in six.moves.urllib_parse"""
|
||||||
|
|
||||||
|
|
||||||
@@ -324,6 +342,7 @@ _urllib_parse_moved_attributes = [
|
|||||||
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"),
|
||||||
@@ -341,6 +360,7 @@ _importer._add_module(Module_six_moves_urllib_parse(__name__ + ".moves.urllib_pa
|
|||||||
|
|
||||||
|
|
||||||
class Module_six_moves_urllib_error(_LazyModule):
|
class Module_six_moves_urllib_error(_LazyModule):
|
||||||
|
|
||||||
"""Lazy loading of moved objects in six.moves.urllib_error"""
|
"""Lazy loading of moved objects in six.moves.urllib_error"""
|
||||||
|
|
||||||
|
|
||||||
@@ -360,6 +380,7 @@ _importer._add_module(Module_six_moves_urllib_error(__name__ + ".moves.urllib.er
|
|||||||
|
|
||||||
|
|
||||||
class Module_six_moves_urllib_request(_LazyModule):
|
class Module_six_moves_urllib_request(_LazyModule):
|
||||||
|
|
||||||
"""Lazy loading of moved objects in six.moves.urllib_request"""
|
"""Lazy loading of moved objects in six.moves.urllib_request"""
|
||||||
|
|
||||||
|
|
||||||
@@ -409,6 +430,7 @@ _importer._add_module(Module_six_moves_urllib_request(__name__ + ".moves.urllib.
|
|||||||
|
|
||||||
|
|
||||||
class Module_six_moves_urllib_response(_LazyModule):
|
class Module_six_moves_urllib_response(_LazyModule):
|
||||||
|
|
||||||
"""Lazy loading of moved objects in six.moves.urllib_response"""
|
"""Lazy loading of moved objects in six.moves.urllib_response"""
|
||||||
|
|
||||||
|
|
||||||
@@ -429,6 +451,7 @@ _importer._add_module(Module_six_moves_urllib_response(__name__ + ".moves.urllib
|
|||||||
|
|
||||||
|
|
||||||
class Module_six_moves_urllib_robotparser(_LazyModule):
|
class Module_six_moves_urllib_robotparser(_LazyModule):
|
||||||
|
|
||||||
"""Lazy loading of moved objects in six.moves.urllib_robotparser"""
|
"""Lazy loading of moved objects in six.moves.urllib_robotparser"""
|
||||||
|
|
||||||
|
|
||||||
@@ -446,6 +469,7 @@ _importer._add_module(Module_six_moves_urllib_robotparser(__name__ + ".moves.url
|
|||||||
|
|
||||||
|
|
||||||
class Module_six_moves_urllib(types.ModuleType):
|
class Module_six_moves_urllib(types.ModuleType):
|
||||||
|
|
||||||
"""Create a six.moves.urllib namespace that resembles the Python 3 namespace"""
|
"""Create a six.moves.urllib namespace that resembles the Python 3 namespace"""
|
||||||
__path__ = [] # mark as package
|
__path__ = [] # mark as package
|
||||||
parse = _importer._get_module("moves.urllib_parse")
|
parse = _importer._get_module("moves.urllib_parse")
|
||||||
@@ -516,6 +540,9 @@ if PY3:
|
|||||||
|
|
||||||
create_bound_method = types.MethodType
|
create_bound_method = types.MethodType
|
||||||
|
|
||||||
|
def create_unbound_method(func, cls):
|
||||||
|
return func
|
||||||
|
|
||||||
Iterator = object
|
Iterator = object
|
||||||
else:
|
else:
|
||||||
def get_unbound_function(unbound):
|
def get_unbound_function(unbound):
|
||||||
@@ -524,6 +551,9 @@ else:
|
|||||||
def create_bound_method(func, obj):
|
def create_bound_method(func, obj):
|
||||||
return types.MethodType(func, obj, obj.__class__)
|
return types.MethodType(func, obj, obj.__class__)
|
||||||
|
|
||||||
|
def create_unbound_method(func, cls):
|
||||||
|
return types.MethodType(func, None, cls)
|
||||||
|
|
||||||
class Iterator(object):
|
class Iterator(object):
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
@@ -554,18 +584,30 @@ if PY3:
|
|||||||
|
|
||||||
def iterlists(d, **kw):
|
def iterlists(d, **kw):
|
||||||
return iter(d.lists(**kw))
|
return iter(d.lists(**kw))
|
||||||
|
|
||||||
|
viewkeys = operator.methodcaller("keys")
|
||||||
|
|
||||||
|
viewvalues = operator.methodcaller("values")
|
||||||
|
|
||||||
|
viewitems = operator.methodcaller("items")
|
||||||
else:
|
else:
|
||||||
def iterkeys(d, **kw):
|
def iterkeys(d, **kw):
|
||||||
return iter(d.iterkeys(**kw))
|
return d.iterkeys(**kw)
|
||||||
|
|
||||||
def itervalues(d, **kw):
|
def itervalues(d, **kw):
|
||||||
return iter(d.itervalues(**kw))
|
return d.itervalues(**kw)
|
||||||
|
|
||||||
def iteritems(d, **kw):
|
def iteritems(d, **kw):
|
||||||
return iter(d.iteritems(**kw))
|
return d.iteritems(**kw)
|
||||||
|
|
||||||
def iterlists(d, **kw):
|
def iterlists(d, **kw):
|
||||||
return iter(d.iterlists(**kw))
|
return d.iterlists(**kw)
|
||||||
|
|
||||||
|
viewkeys = operator.methodcaller("viewkeys")
|
||||||
|
|
||||||
|
viewvalues = operator.methodcaller("viewvalues")
|
||||||
|
|
||||||
|
viewitems = operator.methodcaller("viewitems")
|
||||||
|
|
||||||
_add_doc(iterkeys, "Return an iterator over the keys of a dictionary.")
|
_add_doc(iterkeys, "Return an iterator over the keys of a dictionary.")
|
||||||
_add_doc(itervalues, "Return an iterator over the values of a dictionary.")
|
_add_doc(itervalues, "Return an iterator over the values of a dictionary.")
|
||||||
@@ -578,51 +620,76 @@ _add_doc(iterlists,
|
|||||||
if PY3:
|
if PY3:
|
||||||
def b(s):
|
def b(s):
|
||||||
return s.encode("latin-1")
|
return s.encode("latin-1")
|
||||||
|
|
||||||
def u(s):
|
def u(s):
|
||||||
return s
|
return s
|
||||||
unichr = chr
|
unichr = chr
|
||||||
if sys.version_info[1] <= 1:
|
import struct
|
||||||
def int2byte(i):
|
int2byte = struct.Struct(">B").pack
|
||||||
return bytes((i,))
|
del struct
|
||||||
else:
|
|
||||||
# This is about 2x faster than the implementation above on 3.2+
|
|
||||||
int2byte = operator.methodcaller("to_bytes", 1, "big")
|
|
||||||
byte2int = operator.itemgetter(0)
|
byte2int = operator.itemgetter(0)
|
||||||
indexbytes = operator.getitem
|
indexbytes = operator.getitem
|
||||||
iterbytes = iter
|
iterbytes = iter
|
||||||
import io
|
import io
|
||||||
StringIO = io.StringIO
|
StringIO = io.StringIO
|
||||||
BytesIO = io.BytesIO
|
BytesIO = io.BytesIO
|
||||||
|
_assertCountEqual = "assertCountEqual"
|
||||||
|
if sys.version_info[1] <= 1:
|
||||||
|
_assertRaisesRegex = "assertRaisesRegexp"
|
||||||
|
_assertRegex = "assertRegexpMatches"
|
||||||
|
else:
|
||||||
|
_assertRaisesRegex = "assertRaisesRegex"
|
||||||
|
_assertRegex = "assertRegex"
|
||||||
else:
|
else:
|
||||||
def b(s):
|
def b(s):
|
||||||
return s
|
return s
|
||||||
# Workaround for standalone backslash
|
# Workaround for standalone backslash
|
||||||
|
|
||||||
def u(s):
|
def u(s):
|
||||||
return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape")
|
return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape")
|
||||||
unichr = unichr
|
unichr = unichr
|
||||||
int2byte = chr
|
int2byte = chr
|
||||||
|
|
||||||
def byte2int(bs):
|
def byte2int(bs):
|
||||||
return ord(bs[0])
|
return ord(bs[0])
|
||||||
|
|
||||||
def indexbytes(buf, i):
|
def indexbytes(buf, i):
|
||||||
return ord(buf[i])
|
return ord(buf[i])
|
||||||
def iterbytes(buf):
|
iterbytes = functools.partial(itertools.imap, ord)
|
||||||
return (ord(byte) for byte in buf)
|
|
||||||
import StringIO
|
import StringIO
|
||||||
StringIO = BytesIO = StringIO.StringIO
|
StringIO = BytesIO = StringIO.StringIO
|
||||||
|
_assertCountEqual = "assertItemsEqual"
|
||||||
|
_assertRaisesRegex = "assertRaisesRegexp"
|
||||||
|
_assertRegex = "assertRegexpMatches"
|
||||||
_add_doc(b, """Byte literal""")
|
_add_doc(b, """Byte literal""")
|
||||||
_add_doc(u, """Text literal""")
|
_add_doc(u, """Text literal""")
|
||||||
|
|
||||||
|
|
||||||
|
def assertCountEqual(self, *args, **kwargs):
|
||||||
|
return getattr(self, _assertCountEqual)(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def assertRaisesRegex(self, *args, **kwargs):
|
||||||
|
return getattr(self, _assertRaisesRegex)(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def assertRegex(self, *args, **kwargs):
|
||||||
|
return getattr(self, _assertRegex)(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
if PY3:
|
if PY3:
|
||||||
exec_ = getattr(moves.builtins, "exec")
|
exec_ = getattr(moves.builtins, "exec")
|
||||||
|
|
||||||
|
|
||||||
def reraise(tp, value, tb=None):
|
def reraise(tp, value, tb=None):
|
||||||
|
try:
|
||||||
if value is None:
|
if value is None:
|
||||||
value = tp()
|
value = tp()
|
||||||
if value.__traceback__ is not tb:
|
if value.__traceback__ is not tb:
|
||||||
raise value.with_traceback(tb)
|
raise value.with_traceback(tb)
|
||||||
raise value
|
raise value
|
||||||
|
finally:
|
||||||
|
value = None
|
||||||
|
tb = None
|
||||||
|
|
||||||
else:
|
else:
|
||||||
def exec_(_code_, _globs_=None, _locs_=None):
|
def exec_(_code_, _globs_=None, _locs_=None):
|
||||||
@@ -637,12 +704,35 @@ else:
|
|||||||
_locs_ = _globs_
|
_locs_ = _globs_
|
||||||
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):
|
||||||
|
try:
|
||||||
raise tp, value, tb
|
raise tp, value, tb
|
||||||
|
finally:
|
||||||
|
tb = None
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
if sys.version_info[:2] == (3, 2):
|
||||||
|
exec_("""def raise_from(value, from_value):
|
||||||
|
try:
|
||||||
|
if from_value is None:
|
||||||
|
raise value
|
||||||
|
raise value from from_value
|
||||||
|
finally:
|
||||||
|
value = None
|
||||||
|
""")
|
||||||
|
elif sys.version_info[:2] > (3, 2):
|
||||||
|
exec_("""def raise_from(value, from_value):
|
||||||
|
try:
|
||||||
|
raise value from from_value
|
||||||
|
finally:
|
||||||
|
value = None
|
||||||
|
""")
|
||||||
|
else:
|
||||||
|
def raise_from(value, from_value):
|
||||||
|
raise value
|
||||||
|
|
||||||
|
|
||||||
print_ = getattr(moves.builtins, "print", None)
|
print_ = getattr(moves.builtins, "print", None)
|
||||||
if print_ is None:
|
if print_ is None:
|
||||||
def print_(*args, **kwargs):
|
def print_(*args, **kwargs):
|
||||||
@@ -650,6 +740,7 @@ if print_ is None:
|
|||||||
fp = kwargs.pop("file", sys.stdout)
|
fp = kwargs.pop("file", sys.stdout)
|
||||||
if fp is None:
|
if fp is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
def write(data):
|
def write(data):
|
||||||
if not isinstance(data, basestring):
|
if not isinstance(data, basestring):
|
||||||
data = str(data)
|
data = str(data)
|
||||||
@@ -697,6 +788,15 @@ if print_ is None:
|
|||||||
write(sep)
|
write(sep)
|
||||||
write(arg)
|
write(arg)
|
||||||
write(end)
|
write(end)
|
||||||
|
if sys.version_info[:2] < (3, 3):
|
||||||
|
_print = print_
|
||||||
|
|
||||||
|
def print_(*args, **kwargs):
|
||||||
|
fp = kwargs.get("file", sys.stdout)
|
||||||
|
flush = kwargs.pop("flush", False)
|
||||||
|
_print(*args, **kwargs)
|
||||||
|
if flush and fp is not None:
|
||||||
|
fp.flush()
|
||||||
|
|
||||||
_add_doc(reraise, """Reraise an exception.""")
|
_add_doc(reraise, """Reraise an exception.""")
|
||||||
|
|
||||||
@@ -704,19 +804,21 @@ if sys.version_info[0:2] < (3, 4):
|
|||||||
def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS,
|
def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS,
|
||||||
updated=functools.WRAPPER_UPDATES):
|
updated=functools.WRAPPER_UPDATES):
|
||||||
def wrapper(f):
|
def wrapper(f):
|
||||||
f = functools.wraps(wrapped)(f)
|
f = functools.wraps(wrapped, assigned, updated)(f)
|
||||||
f.__wrapped__ = wrapped
|
f.__wrapped__ = wrapped
|
||||||
return f
|
return f
|
||||||
return wrapper
|
return wrapper
|
||||||
else:
|
else:
|
||||||
wraps = functools.wraps
|
wraps = functools.wraps
|
||||||
|
|
||||||
|
|
||||||
def with_metaclass(meta, *bases):
|
def with_metaclass(meta, *bases):
|
||||||
"""Create a base class with a metaclass."""
|
"""Create a base class with a metaclass."""
|
||||||
# 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(meta):
|
||||||
|
|
||||||
def __new__(cls, name, this_bases, d):
|
def __new__(cls, name, this_bases, d):
|
||||||
return meta(name, bases, d)
|
return meta(name, bases, d)
|
||||||
return type.__new__(metaclass, 'temporary_class', (), {})
|
return type.__new__(metaclass, 'temporary_class', (), {})
|
||||||
@@ -737,6 +839,25 @@ def add_metaclass(metaclass):
|
|||||||
return metaclass(cls.__name__, cls.__bases__, orig_vars)
|
return metaclass(cls.__name__, cls.__bases__, orig_vars)
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def python_2_unicode_compatible(klass):
|
||||||
|
"""
|
||||||
|
A decorator that defines __unicode__ and __str__ methods under Python 2.
|
||||||
|
Under Python 3 it does nothing.
|
||||||
|
|
||||||
|
To support Python 2 and 3 with a single code base, define a __str__ method
|
||||||
|
returning text and apply this decorator to the class.
|
||||||
|
"""
|
||||||
|
if PY2:
|
||||||
|
if '__str__' not in klass.__dict__:
|
||||||
|
raise ValueError("@python_2_unicode_compatible cannot be applied "
|
||||||
|
"to %s because it doesn't define __str__()." %
|
||||||
|
klass.__name__)
|
||||||
|
klass.__unicode__ = klass.__str__
|
||||||
|
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
|
||||||
|
return klass
|
||||||
|
|
||||||
|
|
||||||
# Complete the moves implementation.
|
# Complete the moves implementation.
|
||||||
# This code is at the end of this module to speed up module loading.
|
# This code is at the end of this module to speed up module loading.
|
||||||
# Turn this module into a package.
|
# Turn this module into a package.
|
||||||
|
|||||||
Reference in New Issue
Block a user