Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/urllib3/packages/six.py : 53%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (c) 2010-2019 Benjamin Peterson
2#
3# Permission is hereby granted, free of charge, to any person obtaining a copy
4# of this software and associated documentation files (the "Software"), to deal
5# in the Software without restriction, including without limitation the rights
6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7# copies of the Software, and to permit persons to whom the Software is
8# furnished to do so, subject to the following conditions:
9#
10# The above copyright notice and this permission notice shall be included in all
11# copies or substantial portions of the Software.
12#
13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19# SOFTWARE.
21"""Utilities for writing code that runs on Python 2 and 3"""
23from __future__ import absolute_import
25import functools
26import itertools
27import operator
28import sys
29import types
31__author__ = "Benjamin Peterson <benjamin@python.org>"
32__version__ = "1.12.0"
35# Useful for very coarse version differentiation.
36PY2 = sys.version_info[0] == 2
37PY3 = sys.version_info[0] == 3
38PY34 = sys.version_info[0:2] >= (3, 4)
40if PY3:
41 string_types = (str,)
42 integer_types = (int,)
43 class_types = (type,)
44 text_type = str
45 binary_type = bytes
47 MAXSIZE = sys.maxsize
48else:
49 string_types = (basestring,)
50 integer_types = (int, long)
51 class_types = (type, types.ClassType)
52 text_type = unicode
53 binary_type = str
55 if sys.platform.startswith("java"):
56 # Jython always uses 32 bits.
57 MAXSIZE = int((1 << 31) - 1)
58 else:
59 # It's possible to have sizeof(long) != sizeof(Py_ssize_t).
60 class X(object):
61 def __len__(self):
62 return 1 << 31
64 try:
65 len(X())
66 except OverflowError:
67 # 32-bit
68 MAXSIZE = int((1 << 31) - 1)
69 else:
70 # 64-bit
71 MAXSIZE = int((1 << 63) - 1)
72 del X
75def _add_doc(func, doc):
76 """Add documentation to a function."""
77 func.__doc__ = doc
80def _import_module(name):
81 """Import module, returning the module after the last dot."""
82 __import__(name)
83 return sys.modules[name]
86class _LazyDescr(object):
87 def __init__(self, name):
88 self.name = name
90 def __get__(self, obj, tp):
91 result = self._resolve()
92 setattr(obj, self.name, result) # Invokes __set__.
93 try:
94 # This is a bit ugly, but it avoids running this again by
95 # removing this descriptor.
96 delattr(obj.__class__, self.name)
97 except AttributeError:
98 pass
99 return result
102class MovedModule(_LazyDescr):
103 def __init__(self, name, old, new=None):
104 super(MovedModule, self).__init__(name)
105 if PY3:
106 if new is None:
107 new = name
108 self.mod = new
109 else:
110 self.mod = old
112 def _resolve(self):
113 return _import_module(self.mod)
115 def __getattr__(self, attr):
116 _module = self._resolve()
117 value = getattr(_module, attr)
118 setattr(self, attr, value)
119 return value
122class _LazyModule(types.ModuleType):
123 def __init__(self, name):
124 super(_LazyModule, self).__init__(name)
125 self.__doc__ = self.__class__.__doc__
127 def __dir__(self):
128 attrs = ["__doc__", "__name__"]
129 attrs += [attr.name for attr in self._moved_attributes]
130 return attrs
132 # Subclasses should override this
133 _moved_attributes = []
136class MovedAttribute(_LazyDescr):
137 def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):
138 super(MovedAttribute, self).__init__(name)
139 if PY3:
140 if new_mod is None:
141 new_mod = name
142 self.mod = new_mod
143 if new_attr is None:
144 if old_attr is None:
145 new_attr = name
146 else:
147 new_attr = old_attr
148 self.attr = new_attr
149 else:
150 self.mod = old_mod
151 if old_attr is None:
152 old_attr = name
153 self.attr = old_attr
155 def _resolve(self):
156 module = _import_module(self.mod)
157 return getattr(module, self.attr)
160class _SixMetaPathImporter(object):
162 """
163 A meta path importer to import six.moves and its submodules.
165 This class implements a PEP302 finder and loader. It should be compatible
166 with Python 2.5 and all existing versions of Python3
167 """
169 def __init__(self, six_module_name):
170 self.name = six_module_name
171 self.known_modules = {}
173 def _add_module(self, mod, *fullnames):
174 for fullname in fullnames:
175 self.known_modules[self.name + "." + fullname] = mod
177 def _get_module(self, fullname):
178 return self.known_modules[self.name + "." + fullname]
180 def find_module(self, fullname, path=None):
181 if fullname in self.known_modules:
182 return self
183 return None
185 def __get_module(self, fullname):
186 try:
187 return self.known_modules[fullname]
188 except KeyError:
189 raise ImportError("This loader does not know module " + fullname)
191 def load_module(self, fullname):
192 try:
193 # in case of a reload
194 return sys.modules[fullname]
195 except KeyError:
196 pass
197 mod = self.__get_module(fullname)
198 if isinstance(mod, MovedModule):
199 mod = mod._resolve()
200 else:
201 mod.__loader__ = self
202 sys.modules[fullname] = mod
203 return mod
205 def is_package(self, fullname):
206 """
207 Return true, if the named module is a package.
209 We need this method to get correct spec objects with
210 Python 3.4 (see PEP451)
211 """
212 return hasattr(self.__get_module(fullname), "__path__")
214 def get_code(self, fullname):
215 """Return None
217 Required, if is_package is implemented"""
218 self.__get_module(fullname) # eventually raises ImportError
219 return None
221 get_source = get_code # same as get_code
224_importer = _SixMetaPathImporter(__name__)
227class _MovedItems(_LazyModule):
229 """Lazy loading of moved objects"""
231 __path__ = [] # mark as package
234_moved_attributes = [
235 MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"),
236 MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"),
237 MovedAttribute(
238 "filterfalse", "itertools", "itertools", "ifilterfalse", "filterfalse"
239 ),
240 MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"),
241 MovedAttribute("intern", "__builtin__", "sys"),
242 MovedAttribute("map", "itertools", "builtins", "imap", "map"),
243 MovedAttribute("getcwd", "os", "os", "getcwdu", "getcwd"),
244 MovedAttribute("getcwdb", "os", "os", "getcwd", "getcwdb"),
245 MovedAttribute("getoutput", "commands", "subprocess"),
246 MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"),
247 MovedAttribute(
248 "reload_module", "__builtin__", "importlib" if PY34 else "imp", "reload"
249 ),
250 MovedAttribute("reduce", "__builtin__", "functools"),
251 MovedAttribute("shlex_quote", "pipes", "shlex", "quote"),
252 MovedAttribute("StringIO", "StringIO", "io"),
253 MovedAttribute("UserDict", "UserDict", "collections"),
254 MovedAttribute("UserList", "UserList", "collections"),
255 MovedAttribute("UserString", "UserString", "collections"),
256 MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"),
257 MovedAttribute("zip", "itertools", "builtins", "izip", "zip"),
258 MovedAttribute(
259 "zip_longest", "itertools", "itertools", "izip_longest", "zip_longest"
260 ),
261 MovedModule("builtins", "__builtin__"),
262 MovedModule("configparser", "ConfigParser"),
263 MovedModule("copyreg", "copy_reg"),
264 MovedModule("dbm_gnu", "gdbm", "dbm.gnu"),
265 MovedModule("_dummy_thread", "dummy_thread", "_dummy_thread"),
266 MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
267 MovedModule("http_cookies", "Cookie", "http.cookies"),
268 MovedModule("html_entities", "htmlentitydefs", "html.entities"),
269 MovedModule("html_parser", "HTMLParser", "html.parser"),
270 MovedModule("http_client", "httplib", "http.client"),
271 MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"),
272 MovedModule("email_mime_image", "email.MIMEImage", "email.mime.image"),
273 MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"),
274 MovedModule(
275 "email_mime_nonmultipart", "email.MIMENonMultipart", "email.mime.nonmultipart"
276 ),
277 MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"),
278 MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"),
279 MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"),
280 MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"),
281 MovedModule("cPickle", "cPickle", "pickle"),
282 MovedModule("queue", "Queue"),
283 MovedModule("reprlib", "repr"),
284 MovedModule("socketserver", "SocketServer"),
285 MovedModule("_thread", "thread", "_thread"),
286 MovedModule("tkinter", "Tkinter"),
287 MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"),
288 MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"),
289 MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"),
290 MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"),
291 MovedModule("tkinter_tix", "Tix", "tkinter.tix"),
292 MovedModule("tkinter_ttk", "ttk", "tkinter.ttk"),
293 MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"),
294 MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"),
295 MovedModule("tkinter_colorchooser", "tkColorChooser", "tkinter.colorchooser"),
296 MovedModule("tkinter_commondialog", "tkCommonDialog", "tkinter.commondialog"),
297 MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"),
298 MovedModule("tkinter_font", "tkFont", "tkinter.font"),
299 MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"),
300 MovedModule("tkinter_tksimpledialog", "tkSimpleDialog", "tkinter.simpledialog"),
301 MovedModule("urllib_parse", __name__ + ".moves.urllib_parse", "urllib.parse"),
302 MovedModule("urllib_error", __name__ + ".moves.urllib_error", "urllib.error"),
303 MovedModule("urllib", __name__ + ".moves.urllib", __name__ + ".moves.urllib"),
304 MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"),
305 MovedModule("xmlrpc_client", "xmlrpclib", "xmlrpc.client"),
306 MovedModule("xmlrpc_server", "SimpleXMLRPCServer", "xmlrpc.server"),
307]
308# Add windows specific modules.
309if sys.platform == "win32":
310 _moved_attributes += [MovedModule("winreg", "_winreg")]
312for attr in _moved_attributes:
313 setattr(_MovedItems, attr.name, attr)
314 if isinstance(attr, MovedModule):
315 _importer._add_module(attr, "moves." + attr.name)
316del attr
318_MovedItems._moved_attributes = _moved_attributes
320moves = _MovedItems(__name__ + ".moves")
321_importer._add_module(moves, "moves")
324class Module_six_moves_urllib_parse(_LazyModule):
326 """Lazy loading of moved objects in six.moves.urllib_parse"""
329_urllib_parse_moved_attributes = [
330 MovedAttribute("ParseResult", "urlparse", "urllib.parse"),
331 MovedAttribute("SplitResult", "urlparse", "urllib.parse"),
332 MovedAttribute("parse_qs", "urlparse", "urllib.parse"),
333 MovedAttribute("parse_qsl", "urlparse", "urllib.parse"),
334 MovedAttribute("urldefrag", "urlparse", "urllib.parse"),
335 MovedAttribute("urljoin", "urlparse", "urllib.parse"),
336 MovedAttribute("urlparse", "urlparse", "urllib.parse"),
337 MovedAttribute("urlsplit", "urlparse", "urllib.parse"),
338 MovedAttribute("urlunparse", "urlparse", "urllib.parse"),
339 MovedAttribute("urlunsplit", "urlparse", "urllib.parse"),
340 MovedAttribute("quote", "urllib", "urllib.parse"),
341 MovedAttribute("quote_plus", "urllib", "urllib.parse"),
342 MovedAttribute("unquote", "urllib", "urllib.parse"),
343 MovedAttribute("unquote_plus", "urllib", "urllib.parse"),
344 MovedAttribute(
345 "unquote_to_bytes", "urllib", "urllib.parse", "unquote", "unquote_to_bytes"
346 ),
347 MovedAttribute("urlencode", "urllib", "urllib.parse"),
348 MovedAttribute("splitquery", "urllib", "urllib.parse"),
349 MovedAttribute("splittag", "urllib", "urllib.parse"),
350 MovedAttribute("splituser", "urllib", "urllib.parse"),
351 MovedAttribute("splitvalue", "urllib", "urllib.parse"),
352 MovedAttribute("uses_fragment", "urlparse", "urllib.parse"),
353 MovedAttribute("uses_netloc", "urlparse", "urllib.parse"),
354 MovedAttribute("uses_params", "urlparse", "urllib.parse"),
355 MovedAttribute("uses_query", "urlparse", "urllib.parse"),
356 MovedAttribute("uses_relative", "urlparse", "urllib.parse"),
357]
358for attr in _urllib_parse_moved_attributes:
359 setattr(Module_six_moves_urllib_parse, attr.name, attr)
360del attr
362Module_six_moves_urllib_parse._moved_attributes = _urllib_parse_moved_attributes
364_importer._add_module(
365 Module_six_moves_urllib_parse(__name__ + ".moves.urllib_parse"),
366 "moves.urllib_parse",
367 "moves.urllib.parse",
368)
371class Module_six_moves_urllib_error(_LazyModule):
373 """Lazy loading of moved objects in six.moves.urllib_error"""
376_urllib_error_moved_attributes = [
377 MovedAttribute("URLError", "urllib2", "urllib.error"),
378 MovedAttribute("HTTPError", "urllib2", "urllib.error"),
379 MovedAttribute("ContentTooShortError", "urllib", "urllib.error"),
380]
381for attr in _urllib_error_moved_attributes:
382 setattr(Module_six_moves_urllib_error, attr.name, attr)
383del attr
385Module_six_moves_urllib_error._moved_attributes = _urllib_error_moved_attributes
387_importer._add_module(
388 Module_six_moves_urllib_error(__name__ + ".moves.urllib.error"),
389 "moves.urllib_error",
390 "moves.urllib.error",
391)
394class Module_six_moves_urllib_request(_LazyModule):
396 """Lazy loading of moved objects in six.moves.urllib_request"""
399_urllib_request_moved_attributes = [
400 MovedAttribute("urlopen", "urllib2", "urllib.request"),
401 MovedAttribute("install_opener", "urllib2", "urllib.request"),
402 MovedAttribute("build_opener", "urllib2", "urllib.request"),
403 MovedAttribute("pathname2url", "urllib", "urllib.request"),
404 MovedAttribute("url2pathname", "urllib", "urllib.request"),
405 MovedAttribute("getproxies", "urllib", "urllib.request"),
406 MovedAttribute("Request", "urllib2", "urllib.request"),
407 MovedAttribute("OpenerDirector", "urllib2", "urllib.request"),
408 MovedAttribute("HTTPDefaultErrorHandler", "urllib2", "urllib.request"),
409 MovedAttribute("HTTPRedirectHandler", "urllib2", "urllib.request"),
410 MovedAttribute("HTTPCookieProcessor", "urllib2", "urllib.request"),
411 MovedAttribute("ProxyHandler", "urllib2", "urllib.request"),
412 MovedAttribute("BaseHandler", "urllib2", "urllib.request"),
413 MovedAttribute("HTTPPasswordMgr", "urllib2", "urllib.request"),
414 MovedAttribute("HTTPPasswordMgrWithDefaultRealm", "urllib2", "urllib.request"),
415 MovedAttribute("AbstractBasicAuthHandler", "urllib2", "urllib.request"),
416 MovedAttribute("HTTPBasicAuthHandler", "urllib2", "urllib.request"),
417 MovedAttribute("ProxyBasicAuthHandler", "urllib2", "urllib.request"),
418 MovedAttribute("AbstractDigestAuthHandler", "urllib2", "urllib.request"),
419 MovedAttribute("HTTPDigestAuthHandler", "urllib2", "urllib.request"),
420 MovedAttribute("ProxyDigestAuthHandler", "urllib2", "urllib.request"),
421 MovedAttribute("HTTPHandler", "urllib2", "urllib.request"),
422 MovedAttribute("HTTPSHandler", "urllib2", "urllib.request"),
423 MovedAttribute("FileHandler", "urllib2", "urllib.request"),
424 MovedAttribute("FTPHandler", "urllib2", "urllib.request"),
425 MovedAttribute("CacheFTPHandler", "urllib2", "urllib.request"),
426 MovedAttribute("UnknownHandler", "urllib2", "urllib.request"),
427 MovedAttribute("HTTPErrorProcessor", "urllib2", "urllib.request"),
428 MovedAttribute("urlretrieve", "urllib", "urllib.request"),
429 MovedAttribute("urlcleanup", "urllib", "urllib.request"),
430 MovedAttribute("URLopener", "urllib", "urllib.request"),
431 MovedAttribute("FancyURLopener", "urllib", "urllib.request"),
432 MovedAttribute("proxy_bypass", "urllib", "urllib.request"),
433 MovedAttribute("parse_http_list", "urllib2", "urllib.request"),
434 MovedAttribute("parse_keqv_list", "urllib2", "urllib.request"),
435]
436for attr in _urllib_request_moved_attributes:
437 setattr(Module_six_moves_urllib_request, attr.name, attr)
438del attr
440Module_six_moves_urllib_request._moved_attributes = _urllib_request_moved_attributes
442_importer._add_module(
443 Module_six_moves_urllib_request(__name__ + ".moves.urllib.request"),
444 "moves.urllib_request",
445 "moves.urllib.request",
446)
449class Module_six_moves_urllib_response(_LazyModule):
451 """Lazy loading of moved objects in six.moves.urllib_response"""
454_urllib_response_moved_attributes = [
455 MovedAttribute("addbase", "urllib", "urllib.response"),
456 MovedAttribute("addclosehook", "urllib", "urllib.response"),
457 MovedAttribute("addinfo", "urllib", "urllib.response"),
458 MovedAttribute("addinfourl", "urllib", "urllib.response"),
459]
460for attr in _urllib_response_moved_attributes:
461 setattr(Module_six_moves_urllib_response, attr.name, attr)
462del attr
464Module_six_moves_urllib_response._moved_attributes = _urllib_response_moved_attributes
466_importer._add_module(
467 Module_six_moves_urllib_response(__name__ + ".moves.urllib.response"),
468 "moves.urllib_response",
469 "moves.urllib.response",
470)
473class Module_six_moves_urllib_robotparser(_LazyModule):
475 """Lazy loading of moved objects in six.moves.urllib_robotparser"""
478_urllib_robotparser_moved_attributes = [
479 MovedAttribute("RobotFileParser", "robotparser", "urllib.robotparser")
480]
481for attr in _urllib_robotparser_moved_attributes:
482 setattr(Module_six_moves_urllib_robotparser, attr.name, attr)
483del attr
485Module_six_moves_urllib_robotparser._moved_attributes = (
486 _urllib_robotparser_moved_attributes
487)
489_importer._add_module(
490 Module_six_moves_urllib_robotparser(__name__ + ".moves.urllib.robotparser"),
491 "moves.urllib_robotparser",
492 "moves.urllib.robotparser",
493)
496class Module_six_moves_urllib(types.ModuleType):
498 """Create a six.moves.urllib namespace that resembles the Python 3 namespace"""
500 __path__ = [] # mark as package
501 parse = _importer._get_module("moves.urllib_parse")
502 error = _importer._get_module("moves.urllib_error")
503 request = _importer._get_module("moves.urllib_request")
504 response = _importer._get_module("moves.urllib_response")
505 robotparser = _importer._get_module("moves.urllib_robotparser")
507 def __dir__(self):
508 return ["parse", "error", "request", "response", "robotparser"]
511_importer._add_module(
512 Module_six_moves_urllib(__name__ + ".moves.urllib"), "moves.urllib"
513)
516def add_move(move):
517 """Add an item to six.moves."""
518 setattr(_MovedItems, move.name, move)
521def remove_move(name):
522 """Remove item from six.moves."""
523 try:
524 delattr(_MovedItems, name)
525 except AttributeError:
526 try:
527 del moves.__dict__[name]
528 except KeyError:
529 raise AttributeError("no such move, %r" % (name,))
532if PY3:
533 _meth_func = "__func__"
534 _meth_self = "__self__"
536 _func_closure = "__closure__"
537 _func_code = "__code__"
538 _func_defaults = "__defaults__"
539 _func_globals = "__globals__"
540else:
541 _meth_func = "im_func"
542 _meth_self = "im_self"
544 _func_closure = "func_closure"
545 _func_code = "func_code"
546 _func_defaults = "func_defaults"
547 _func_globals = "func_globals"
550try:
551 advance_iterator = next
552except NameError:
554 def advance_iterator(it):
555 return it.next()
558next = advance_iterator
561try:
562 callable = callable
563except NameError:
565 def callable(obj):
566 return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
569if PY3:
571 def get_unbound_function(unbound):
572 return unbound
574 create_bound_method = types.MethodType
576 def create_unbound_method(func, cls):
577 return func
579 Iterator = object
580else:
582 def get_unbound_function(unbound):
583 return unbound.im_func
585 def create_bound_method(func, obj):
586 return types.MethodType(func, obj, obj.__class__)
588 def create_unbound_method(func, cls):
589 return types.MethodType(func, None, cls)
591 class Iterator(object):
592 def next(self):
593 return type(self).__next__(self)
595 callable = callable
596_add_doc(
597 get_unbound_function, """Get the function out of a possibly unbound function"""
598)
601get_method_function = operator.attrgetter(_meth_func)
602get_method_self = operator.attrgetter(_meth_self)
603get_function_closure = operator.attrgetter(_func_closure)
604get_function_code = operator.attrgetter(_func_code)
605get_function_defaults = operator.attrgetter(_func_defaults)
606get_function_globals = operator.attrgetter(_func_globals)
609if PY3:
611 def iterkeys(d, **kw):
612 return iter(d.keys(**kw))
614 def itervalues(d, **kw):
615 return iter(d.values(**kw))
617 def iteritems(d, **kw):
618 return iter(d.items(**kw))
620 def iterlists(d, **kw):
621 return iter(d.lists(**kw))
623 viewkeys = operator.methodcaller("keys")
625 viewvalues = operator.methodcaller("values")
627 viewitems = operator.methodcaller("items")
628else:
630 def iterkeys(d, **kw):
631 return d.iterkeys(**kw)
633 def itervalues(d, **kw):
634 return d.itervalues(**kw)
636 def iteritems(d, **kw):
637 return d.iteritems(**kw)
639 def iterlists(d, **kw):
640 return d.iterlists(**kw)
642 viewkeys = operator.methodcaller("viewkeys")
644 viewvalues = operator.methodcaller("viewvalues")
646 viewitems = operator.methodcaller("viewitems")
648_add_doc(iterkeys, "Return an iterator over the keys of a dictionary.")
649_add_doc(itervalues, "Return an iterator over the values of a dictionary.")
650_add_doc(iteritems, "Return an iterator over the (key, value) pairs of a dictionary.")
651_add_doc(
652 iterlists, "Return an iterator over the (key, [values]) pairs of a dictionary."
653)
656if PY3:
658 def b(s):
659 return s.encode("latin-1")
661 def u(s):
662 return s
664 unichr = chr
665 import struct
667 int2byte = struct.Struct(">B").pack
668 del struct
669 byte2int = operator.itemgetter(0)
670 indexbytes = operator.getitem
671 iterbytes = iter
672 import io
674 StringIO = io.StringIO
675 BytesIO = io.BytesIO
676 del io
677 _assertCountEqual = "assertCountEqual"
678 if sys.version_info[1] <= 1:
679 _assertRaisesRegex = "assertRaisesRegexp"
680 _assertRegex = "assertRegexpMatches"
681 else:
682 _assertRaisesRegex = "assertRaisesRegex"
683 _assertRegex = "assertRegex"
684else:
686 def b(s):
687 return s
689 # Workaround for standalone backslash
691 def u(s):
692 return unicode(s.replace(r"\\", r"\\\\"), "unicode_escape")
694 unichr = unichr
695 int2byte = chr
697 def byte2int(bs):
698 return ord(bs[0])
700 def indexbytes(buf, i):
701 return ord(buf[i])
703 iterbytes = functools.partial(itertools.imap, ord)
704 import StringIO
706 StringIO = BytesIO = StringIO.StringIO
707 _assertCountEqual = "assertItemsEqual"
708 _assertRaisesRegex = "assertRaisesRegexp"
709 _assertRegex = "assertRegexpMatches"
710_add_doc(b, """Byte literal""")
711_add_doc(u, """Text literal""")
714def assertCountEqual(self, *args, **kwargs):
715 return getattr(self, _assertCountEqual)(*args, **kwargs)
718def assertRaisesRegex(self, *args, **kwargs):
719 return getattr(self, _assertRaisesRegex)(*args, **kwargs)
722def assertRegex(self, *args, **kwargs):
723 return getattr(self, _assertRegex)(*args, **kwargs)
726if PY3:
727 exec_ = getattr(moves.builtins, "exec")
729 def reraise(tp, value, tb=None):
730 try:
731 if value is None:
732 value = tp()
733 if value.__traceback__ is not tb:
734 raise value.with_traceback(tb)
735 raise value
736 finally:
737 value = None
738 tb = None
741else:
743 def exec_(_code_, _globs_=None, _locs_=None):
744 """Execute code in a namespace."""
745 if _globs_ is None:
746 frame = sys._getframe(1)
747 _globs_ = frame.f_globals
748 if _locs_ is None:
749 _locs_ = frame.f_locals
750 del frame
751 elif _locs_ is None:
752 _locs_ = _globs_
753 exec("""exec _code_ in _globs_, _locs_""")
755 exec_(
756 """def reraise(tp, value, tb=None):
757 try:
758 raise tp, value, tb
759 finally:
760 tb = None
761"""
762 )
765if sys.version_info[:2] == (3, 2):
766 exec_(
767 """def raise_from(value, from_value):
768 try:
769 if from_value is None:
770 raise value
771 raise value from from_value
772 finally:
773 value = None
774"""
775 )
776elif sys.version_info[:2] > (3, 2):
777 exec_(
778 """def raise_from(value, from_value):
779 try:
780 raise value from from_value
781 finally:
782 value = None
783"""
784 )
785else:
787 def raise_from(value, from_value):
788 raise value
791print_ = getattr(moves.builtins, "print", None)
792if print_ is None:
794 def print_(*args, **kwargs):
795 """The new-style print function for Python 2.4 and 2.5."""
796 fp = kwargs.pop("file", sys.stdout)
797 if fp is None:
798 return
800 def write(data):
801 if not isinstance(data, basestring):
802 data = str(data)
803 # If the file has an encoding, encode unicode with it.
804 if (
805 isinstance(fp, file)
806 and isinstance(data, unicode)
807 and fp.encoding is not None
808 ):
809 errors = getattr(fp, "errors", None)
810 if errors is None:
811 errors = "strict"
812 data = data.encode(fp.encoding, errors)
813 fp.write(data)
815 want_unicode = False
816 sep = kwargs.pop("sep", None)
817 if sep is not None:
818 if isinstance(sep, unicode):
819 want_unicode = True
820 elif not isinstance(sep, str):
821 raise TypeError("sep must be None or a string")
822 end = kwargs.pop("end", None)
823 if end is not None:
824 if isinstance(end, unicode):
825 want_unicode = True
826 elif not isinstance(end, str):
827 raise TypeError("end must be None or a string")
828 if kwargs:
829 raise TypeError("invalid keyword arguments to print()")
830 if not want_unicode:
831 for arg in args:
832 if isinstance(arg, unicode):
833 want_unicode = True
834 break
835 if want_unicode:
836 newline = unicode("\n")
837 space = unicode(" ")
838 else:
839 newline = "\n"
840 space = " "
841 if sep is None:
842 sep = space
843 if end is None:
844 end = newline
845 for i, arg in enumerate(args):
846 if i:
847 write(sep)
848 write(arg)
849 write(end)
852if sys.version_info[:2] < (3, 3):
853 _print = print_
855 def print_(*args, **kwargs):
856 fp = kwargs.get("file", sys.stdout)
857 flush = kwargs.pop("flush", False)
858 _print(*args, **kwargs)
859 if flush and fp is not None:
860 fp.flush()
863_add_doc(reraise, """Reraise an exception.""")
865if sys.version_info[0:2] < (3, 4):
867 def wraps(
868 wrapped,
869 assigned=functools.WRAPPER_ASSIGNMENTS,
870 updated=functools.WRAPPER_UPDATES,
871 ):
872 def wrapper(f):
873 f = functools.wraps(wrapped, assigned, updated)(f)
874 f.__wrapped__ = wrapped
875 return f
877 return wrapper
880else:
881 wraps = functools.wraps
884def with_metaclass(meta, *bases):
885 """Create a base class with a metaclass."""
886 # This requires a bit of explanation: the basic idea is to make a dummy
887 # metaclass for one level of class instantiation that replaces itself with
888 # the actual metaclass.
889 class metaclass(type):
890 def __new__(cls, name, this_bases, d):
891 return meta(name, bases, d)
893 @classmethod
894 def __prepare__(cls, name, this_bases):
895 return meta.__prepare__(name, bases)
897 return type.__new__(metaclass, "temporary_class", (), {})
900def add_metaclass(metaclass):
901 """Class decorator for creating a class with a metaclass."""
903 def wrapper(cls):
904 orig_vars = cls.__dict__.copy()
905 slots = orig_vars.get("__slots__")
906 if slots is not None:
907 if isinstance(slots, str):
908 slots = [slots]
909 for slots_var in slots:
910 orig_vars.pop(slots_var)
911 orig_vars.pop("__dict__", None)
912 orig_vars.pop("__weakref__", None)
913 if hasattr(cls, "__qualname__"):
914 orig_vars["__qualname__"] = cls.__qualname__
915 return metaclass(cls.__name__, cls.__bases__, orig_vars)
917 return wrapper
920def ensure_binary(s, encoding="utf-8", errors="strict"):
921 """Coerce **s** to six.binary_type.
923 For Python 2:
924 - `unicode` -> encoded to `str`
925 - `str` -> `str`
927 For Python 3:
928 - `str` -> encoded to `bytes`
929 - `bytes` -> `bytes`
930 """
931 if isinstance(s, text_type):
932 return s.encode(encoding, errors)
933 elif isinstance(s, binary_type):
934 return s
935 else:
936 raise TypeError("not expecting type '%s'" % type(s))
939def ensure_str(s, encoding="utf-8", errors="strict"):
940 """Coerce *s* to `str`.
942 For Python 2:
943 - `unicode` -> encoded to `str`
944 - `str` -> `str`
946 For Python 3:
947 - `str` -> `str`
948 - `bytes` -> decoded to `str`
949 """
950 if not isinstance(s, (text_type, binary_type)):
951 raise TypeError("not expecting type '%s'" % type(s))
952 if PY2 and isinstance(s, text_type):
953 s = s.encode(encoding, errors)
954 elif PY3 and isinstance(s, binary_type):
955 s = s.decode(encoding, errors)
956 return s
959def ensure_text(s, encoding="utf-8", errors="strict"):
960 """Coerce *s* to six.text_type.
962 For Python 2:
963 - `unicode` -> `unicode`
964 - `str` -> `unicode`
966 For Python 3:
967 - `str` -> `str`
968 - `bytes` -> decoded to `str`
969 """
970 if isinstance(s, binary_type):
971 return s.decode(encoding, errors)
972 elif isinstance(s, text_type):
973 return s
974 else:
975 raise TypeError("not expecting type '%s'" % type(s))
978def python_2_unicode_compatible(klass):
979 """
980 A decorator that defines __unicode__ and __str__ methods under Python 2.
981 Under Python 3 it does nothing.
983 To support Python 2 and 3 with a single code base, define a __str__ method
984 returning text and apply this decorator to the class.
985 """
986 if PY2:
987 if "__str__" not in klass.__dict__:
988 raise ValueError(
989 "@python_2_unicode_compatible cannot be applied "
990 "to %s because it doesn't define __str__()." % klass.__name__
991 )
992 klass.__unicode__ = klass.__str__
993 klass.__str__ = lambda self: self.__unicode__().encode("utf-8")
994 return klass
997# Complete the moves implementation.
998# This code is at the end of this module to speed up module loading.
999# Turn this module into a package.
1000__path__ = [] # required for PEP 302 and PEP 451
1001__package__ = __name__ # see PEP 366 @ReservedAssignment
1002if globals().get("__spec__") is not None:
1003 __spec__.submodule_search_locations = [] # PEP 451 @UndefinedVariable
1004# Remove other six meta path importers, since they cause problems. This can
1005# happen if six is removed from sys.modules and then reloaded. (Setuptools does
1006# this for some reason.)
1007if sys.meta_path:
1008 for i, importer in enumerate(sys.meta_path):
1009 # Here's some real nastiness: Another "instance" of the six module might
1010 # be floating around. Therefore, we can't use isinstance() to check for
1011 # the six meta path importer, since the other six instance will have
1012 # inserted an importer with different class.
1013 if (
1014 type(importer).__name__ == "_SixMetaPathImporter"
1015 and importer.name == __name__
1016 ):
1017 del sys.meta_path[i]
1018 break
1019 del i, importer
1020# Finally, add the importer to the meta path import hook.
1021sys.meta_path.append(_importer)