1- # Copyright (c) 2010-2016 Benjamin Peterson
1+ # Copyright (c) 2010-2020 Benjamin Peterson
22#
33# Permission is hereby granted, free of charge, to any person obtaining a copy
44# of this software and associated documentation files (the "Software"), to deal
2929import types
3030
3131__author__ = "Benjamin Peterson <benjamin@python.org>"
32- __version__ = "1.10 .0"
32+ __version__ = "1.14 .0"
3333
3434
3535# Useful for very coarse version differentiation.
@@ -241,6 +241,7 @@ class _MovedItems(_LazyModule):
241241 MovedAttribute ("map" , "itertools" , "builtins" , "imap" , "map" ),
242242 MovedAttribute ("getcwd" , "os" , "os" , "getcwdu" , "getcwd" ),
243243 MovedAttribute ("getcwdb" , "os" , "os" , "getcwd" , "getcwdb" ),
244+ MovedAttribute ("getoutput" , "commands" , "subprocess" ),
244245 MovedAttribute ("range" , "__builtin__" , "builtins" , "xrange" , "range" ),
245246 MovedAttribute ("reload_module" , "__builtin__" , "importlib" if PY34 else "imp" , "reload" ),
246247 MovedAttribute ("reduce" , "__builtin__" , "functools" ),
@@ -254,9 +255,11 @@ class _MovedItems(_LazyModule):
254255 MovedAttribute ("zip_longest" , "itertools" , "itertools" , "izip_longest" , "zip_longest" ),
255256 MovedModule ("builtins" , "__builtin__" ),
256257 MovedModule ("configparser" , "ConfigParser" ),
258+ MovedModule ("collections_abc" , "collections" , "collections.abc" if sys .version_info >= (3 , 3 ) else "collections" ),
257259 MovedModule ("copyreg" , "copy_reg" ),
258260 MovedModule ("dbm_gnu" , "gdbm" , "dbm.gnu" ),
259- MovedModule ("_dummy_thread" , "dummy_thread" , "_dummy_thread" ),
261+ MovedModule ("dbm_ndbm" , "dbm" , "dbm.ndbm" ),
262+ MovedModule ("_dummy_thread" , "dummy_thread" , "_dummy_thread" if sys .version_info < (3 , 9 ) else "_thread" ),
260263 MovedModule ("http_cookiejar" , "cookielib" , "http.cookiejar" ),
261264 MovedModule ("http_cookies" , "Cookie" , "http.cookies" ),
262265 MovedModule ("html_entities" , "htmlentitydefs" , "html.entities" ),
@@ -338,13 +341,12 @@ class Module_six_moves_urllib_parse(_LazyModule):
338341 MovedAttribute ("quote_plus" , "urllib" , "urllib.parse" ),
339342 MovedAttribute ("unquote" , "urllib" , "urllib.parse" ),
340343 MovedAttribute ("unquote_plus" , "urllib" , "urllib.parse" ),
344+ MovedAttribute ("unquote_to_bytes" , "urllib" , "urllib.parse" , "unquote" , "unquote_to_bytes" ),
341345 MovedAttribute ("urlencode" , "urllib" , "urllib.parse" ),
342346 MovedAttribute ("splitquery" , "urllib" , "urllib.parse" ),
343347 MovedAttribute ("splittag" , "urllib" , "urllib.parse" ),
344348 MovedAttribute ("splituser" , "urllib" , "urllib.parse" ),
345349 MovedAttribute ("splitvalue" , "urllib" , "urllib.parse" ),
346- MovedAttribute ("splittype" , "urllib" , "urllib.parse" ),
347- MovedAttribute ("splithost" , "urllib" , "urllib.parse" ),
348350 MovedAttribute ("uses_fragment" , "urlparse" , "urllib.parse" ),
349351 MovedAttribute ("uses_netloc" , "urlparse" , "urllib.parse" ),
350352 MovedAttribute ("uses_params" , "urlparse" , "urllib.parse" ),
@@ -420,6 +422,8 @@ class Module_six_moves_urllib_request(_LazyModule):
420422 MovedAttribute ("URLopener" , "urllib" , "urllib.request" ),
421423 MovedAttribute ("FancyURLopener" , "urllib" , "urllib.request" ),
422424 MovedAttribute ("proxy_bypass" , "urllib" , "urllib.request" ),
425+ MovedAttribute ("parse_http_list" , "urllib2" , "urllib.request" ),
426+ MovedAttribute ("parse_keqv_list" , "urllib2" , "urllib.request" ),
423427]
424428for attr in _urllib_request_moved_attributes :
425429 setattr (Module_six_moves_urllib_request , attr .name , attr )
@@ -511,7 +515,6 @@ def remove_move(name):
511515 _func_code = "__code__"
512516 _func_defaults = "__defaults__"
513517 _func_globals = "__globals__"
514- _func_name = "__name__"
515518else :
516519 _meth_func = "im_func"
517520 _meth_self = "im_self"
@@ -520,7 +523,6 @@ def remove_move(name):
520523 _func_code = "func_code"
521524 _func_defaults = "func_defaults"
522525 _func_globals = "func_globals"
523- _func_name = "func_name"
524526
525527
526528try :
@@ -574,7 +576,6 @@ def next(self):
574576get_function_code = operator .attrgetter (_func_code )
575577get_function_defaults = operator .attrgetter (_func_defaults )
576578get_function_globals = operator .attrgetter (_func_globals )
577- get_function_name = operator .attrgetter (_func_name )
578579
579580
580581if PY3 :
@@ -638,13 +639,16 @@ def u(s):
638639 import io
639640 StringIO = io .StringIO
640641 BytesIO = io .BytesIO
642+ del io
641643 _assertCountEqual = "assertCountEqual"
642644 if sys .version_info [1 ] <= 1 :
643645 _assertRaisesRegex = "assertRaisesRegexp"
644646 _assertRegex = "assertRegexpMatches"
647+ _assertNotRegex = "assertNotRegexpMatches"
645648 else :
646649 _assertRaisesRegex = "assertRaisesRegex"
647650 _assertRegex = "assertRegex"
651+ _assertNotRegex = "assertNotRegex"
648652else :
649653 def b (s ):
650654 return s
@@ -666,6 +670,7 @@ def indexbytes(buf, i):
666670 _assertCountEqual = "assertItemsEqual"
667671 _assertRaisesRegex = "assertRaisesRegexp"
668672 _assertRegex = "assertRegexpMatches"
673+ _assertNotRegex = "assertNotRegexpMatches"
669674_add_doc (b , """Byte literal""" )
670675_add_doc (u , """Text literal""" )
671676
@@ -682,6 +687,10 @@ def assertRegex(self, *args, **kwargs):
682687 return getattr (self , _assertRegex )(* args , ** kwargs )
683688
684689
690+ def assertNotRegex (self , * args , ** kwargs ):
691+ return getattr (self , _assertNotRegex )(* args , ** kwargs )
692+
693+
685694if PY3 :
686695 exec_ = getattr (moves .builtins , "exec" )
687696
@@ -717,16 +726,7 @@ def exec_(_code_, _globs_=None, _locs_=None):
717726""" )
718727
719728
720- if sys .version_info [:2 ] == (3 , 2 ):
721- exec_ ("""def raise_from(value, from_value):
722- try:
723- if from_value is None:
724- raise value
725- raise value from from_value
726- finally:
727- value = None
728- """ )
729- elif sys .version_info [:2 ] > (3 , 2 ):
729+ if sys .version_info [:2 ] > (3 ,):
730730 exec_ ("""def raise_from(value, from_value):
731731 try:
732732 raise value from from_value
@@ -806,13 +806,33 @@ def print_(*args, **kwargs):
806806_add_doc (reraise , """Reraise an exception.""" )
807807
808808if sys .version_info [0 :2 ] < (3 , 4 ):
809+ # This does exactly the same what the :func:`py3:functools.update_wrapper`
810+ # function does on Python versions after 3.2. It sets the ``__wrapped__``
811+ # attribute on ``wrapper`` object and it doesn't raise an error if any of
812+ # the attributes mentioned in ``assigned`` and ``updated`` are missing on
813+ # ``wrapped`` object.
814+ def _update_wrapper (wrapper , wrapped ,
815+ assigned = functools .WRAPPER_ASSIGNMENTS ,
816+ updated = functools .WRAPPER_UPDATES ):
817+ for attr in assigned :
818+ try :
819+ value = getattr (wrapped , attr )
820+ except AttributeError :
821+ continue
822+ else :
823+ setattr (wrapper , attr , value )
824+ for attr in updated :
825+ getattr (wrapper , attr ).update (getattr (wrapped , attr , {}))
826+ wrapper .__wrapped__ = wrapped
827+ return wrapper
828+ _update_wrapper .__doc__ = functools .update_wrapper .__doc__
829+
809830 def wraps (wrapped , assigned = functools .WRAPPER_ASSIGNMENTS ,
810831 updated = functools .WRAPPER_UPDATES ):
811- def wrapper (f ):
812- f = functools .wraps (wrapped , assigned , updated )(f )
813- f .__wrapped__ = wrapped
814- return f
815- return wrapper
832+ return functools .partial (_update_wrapper , wrapped = wrapped ,
833+ assigned = assigned , updated = updated )
834+ wraps .__doc__ = functools .wraps .__doc__
835+
816836else :
817837 wraps = functools .wraps
818838
@@ -822,10 +842,22 @@ def with_metaclass(meta, *bases):
822842 # This requires a bit of explanation: the basic idea is to make a dummy
823843 # metaclass for one level of class instantiation that replaces itself with
824844 # the actual metaclass.
825- class metaclass (meta ):
845+ class metaclass (type ):
826846
827847 def __new__ (cls , name , this_bases , d ):
828- return meta (name , bases , d )
848+ if sys .version_info [:2 ] >= (3 , 7 ):
849+ # This version introduced PEP 560 that requires a bit
850+ # of extra care (we mimic what is done by __build_class__).
851+ resolved_bases = types .resolve_bases (bases )
852+ if resolved_bases is not bases :
853+ d ['__orig_bases__' ] = bases
854+ else :
855+ resolved_bases = bases
856+ return meta (name , resolved_bases , d )
857+
858+ @classmethod
859+ def __prepare__ (cls , name , this_bases ):
860+ return meta .__prepare__ (name , bases )
829861 return type .__new__ (metaclass , 'temporary_class' , (), {})
830862
831863
@@ -841,13 +873,73 @@ def wrapper(cls):
841873 orig_vars .pop (slots_var )
842874 orig_vars .pop ('__dict__' , None )
843875 orig_vars .pop ('__weakref__' , None )
876+ if hasattr (cls , '__qualname__' ):
877+ orig_vars ['__qualname__' ] = cls .__qualname__
844878 return metaclass (cls .__name__ , cls .__bases__ , orig_vars )
845879 return wrapper
846880
847881
882+ def ensure_binary (s , encoding = 'utf-8' , errors = 'strict' ):
883+ """Coerce **s** to six.binary_type.
884+
885+ For Python 2:
886+ - `unicode` -> encoded to `str`
887+ - `str` -> `str`
888+
889+ For Python 3:
890+ - `str` -> encoded to `bytes`
891+ - `bytes` -> `bytes`
892+ """
893+ if isinstance (s , text_type ):
894+ return s .encode (encoding , errors )
895+ elif isinstance (s , binary_type ):
896+ return s
897+ else :
898+ raise TypeError ("not expecting type '%s'" % type (s ))
899+
900+
901+ def ensure_str (s , encoding = 'utf-8' , errors = 'strict' ):
902+ """Coerce *s* to `str`.
903+
904+ For Python 2:
905+ - `unicode` -> encoded to `str`
906+ - `str` -> `str`
907+
908+ For Python 3:
909+ - `str` -> `str`
910+ - `bytes` -> decoded to `str`
911+ """
912+ if not isinstance (s , (text_type , binary_type )):
913+ raise TypeError ("not expecting type '%s'" % type (s ))
914+ if PY2 and isinstance (s , text_type ):
915+ s = s .encode (encoding , errors )
916+ elif PY3 and isinstance (s , binary_type ):
917+ s = s .decode (encoding , errors )
918+ return s
919+
920+
921+ def ensure_text (s , encoding = 'utf-8' , errors = 'strict' ):
922+ """Coerce *s* to six.text_type.
923+
924+ For Python 2:
925+ - `unicode` -> `unicode`
926+ - `str` -> `unicode`
927+
928+ For Python 3:
929+ - `str` -> `str`
930+ - `bytes` -> decoded to `str`
931+ """
932+ if isinstance (s , binary_type ):
933+ return s .decode (encoding , errors )
934+ elif isinstance (s , text_type ):
935+ return s
936+ else :
937+ raise TypeError ("not expecting type '%s'" % type (s ))
938+
939+
848940def python_2_unicode_compatible (klass ):
849941 """
850- A decorator that defines __unicode__ and __str__ methods under Python 2.
942+ A class decorator that defines __unicode__ and __str__ methods under Python 2.
851943 Under Python 3 it does nothing.
852944
853945 To support Python 2 and 3 with a single code base, define a __str__ method
@@ -885,4 +977,4 @@ def python_2_unicode_compatible(klass):
885977 break
886978 del i , importer
887979# Finally, add the importer to the meta path import hook.
888- sys .meta_path .append (_importer )
980+ sys .meta_path .append (_importer )
0 commit comments