1- # Copyright (c) 2010-2019 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.12 .0"
32+ __version__ = "1.16 .0"
3333
3434
3535# Useful for very coarse version differentiation.
@@ -71,6 +71,11 @@ def __len__(self):
7171MAXSIZE = int ((1 << 63 )- 1 )
7272del X
7373
74+ if PY34 :
75+ from importlib .util import spec_from_loader
76+ else :
77+ spec_from_loader = None
78+
7479
7580def _add_doc (func ,doc ):
7681"""Add documentation to a function."""
@@ -182,6 +187,11 @@ def find_module(self, fullname, path=None):
182187return self
183188return None
184189
190+ def find_spec (self ,fullname ,path ,target = None ):
191+ if fullname in self .known_modules :
192+ return spec_from_loader (fullname ,self )
193+ return None
194+
185195def __get_module (self ,fullname ):
186196try :
187197return self .known_modules [fullname ]
@@ -220,6 +230,12 @@ def get_code(self, fullname):
220230
221231get_source = get_code # same as get_code
222232
233+ def create_module (self ,spec ):
234+ return self .load_module (spec .name )
235+
236+ def exec_module (self ,module ):
237+ pass
238+
223239
224240_importer = _SixMetaPathImporter (__name__ )
225241
@@ -260,9 +276,19 @@ class _MovedItems(_LazyModule):
260276 ),
261277MovedModule ("builtins" ,"__builtin__" ),
262278MovedModule ("configparser" ,"ConfigParser" ),
279+ MovedModule (
280+ "collections_abc" ,
281+ "collections" ,
282+ "collections.abc" if sys .version_info >= (3 ,3 )else "collections" ,
283+ ),
263284MovedModule ("copyreg" ,"copy_reg" ),
264285MovedModule ("dbm_gnu" ,"gdbm" ,"dbm.gnu" ),
265- MovedModule ("_dummy_thread" ,"dummy_thread" ,"_dummy_thread" ),
286+ MovedModule ("dbm_ndbm" ,"dbm" ,"dbm.ndbm" ),
287+ MovedModule (
288+ "_dummy_thread" ,
289+ "dummy_thread" ,
290+ "_dummy_thread" if sys .version_info < (3 ,9 )else "_thread" ,
291+ ),
266292MovedModule ("http_cookiejar" ,"cookielib" ,"http.cookiejar" ),
267293MovedModule ("http_cookies" ,"Cookie" ,"http.cookies" ),
268294MovedModule ("html_entities" ,"htmlentitydefs" ,"html.entities" ),
@@ -307,7 +333,9 @@ class _MovedItems(_LazyModule):
307333]
308334# Add windows specific modules.
309335if sys .platform == "win32" :
310- _moved_attributes += [MovedModule ("winreg" ,"_winreg" )]
336+ _moved_attributes += [
337+ MovedModule ("winreg" ,"_winreg" ),
338+ ]
311339
312340for attr in _moved_attributes :
313341setattr (_MovedItems ,attr .name ,attr )
@@ -476,7 +504,7 @@ class Module_six_moves_urllib_robotparser(_LazyModule):
476504
477505
478506_urllib_robotparser_moved_attributes = [
479- MovedAttribute ("RobotFileParser" ,"robotparser" ,"urllib.robotparser" )
507+ MovedAttribute ("RobotFileParser" ,"robotparser" ,"urllib.robotparser" ),
480508]
481509for attr in _urllib_robotparser_moved_attributes :
482510setattr (Module_six_moves_urllib_robotparser ,attr .name ,attr )
@@ -678,9 +706,11 @@ def u(s):
678706if sys .version_info [1 ]<= 1 :
679707_assertRaisesRegex = "assertRaisesRegexp"
680708_assertRegex = "assertRegexpMatches"
709+ _assertNotRegex = "assertNotRegexpMatches"
681710else :
682711_assertRaisesRegex = "assertRaisesRegex"
683712_assertRegex = "assertRegex"
713+ _assertNotRegex = "assertNotRegex"
684714else :
685715
686716def b (s ):
@@ -707,6 +737,7 @@ def indexbytes(buf, i):
707737_assertCountEqual = "assertItemsEqual"
708738_assertRaisesRegex = "assertRaisesRegexp"
709739_assertRegex = "assertRegexpMatches"
740+ _assertNotRegex = "assertNotRegexpMatches"
710741_add_doc (b ,"""Byte literal""" )
711742_add_doc (u ,"""Text literal""" )
712743
@@ -723,6 +754,10 @@ def assertRegex(self, *args, **kwargs):
723754return getattr (self ,_assertRegex )(* args ,** kwargs )
724755
725756
757+ def assertNotRegex (self ,* args ,** kwargs ):
758+ return getattr (self ,_assertNotRegex )(* args ,** kwargs )
759+
760+
726761if PY3 :
727762exec_ = getattr (moves .builtins ,"exec" )
728763
@@ -762,18 +797,7 @@ def exec_(_code_, _globs_=None, _locs_=None):
762797 )
763798
764799
765- if 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- )
776- elif sys .version_info [:2 ]> (3 ,2 ):
800+ if sys .version_info [:2 ]> (3 ,):
777801exec_ (
778802"""def raise_from(value, from_value):
779803 try:
@@ -863,19 +887,41 @@ def print_(*args, **kwargs):
863887_add_doc (reraise ,"""Reraise an exception.""" )
864888
865889if sys .version_info [0 :2 ]< (3 ,4 ):
890+ # This does exactly the same what the :func:`py3:functools.update_wrapper`
891+ # function does on Python versions after 3.2. It sets the ``__wrapped__``
892+ # attribute on ``wrapper`` object and it doesn't raise an error if any of
893+ # the attributes mentioned in ``assigned`` and ``updated`` are missing on
894+ # ``wrapped`` object.
895+ def _update_wrapper (
896+ wrapper ,
897+ wrapped ,
898+ assigned = functools .WRAPPER_ASSIGNMENTS ,
899+ updated = functools .WRAPPER_UPDATES ,
900+ ):
901+ for attr in assigned :
902+ try :
903+ value = getattr (wrapped ,attr )
904+ except AttributeError :
905+ continue
906+ else :
907+ setattr (wrapper ,attr ,value )
908+ for attr in updated :
909+ getattr (wrapper ,attr ).update (getattr (wrapped ,attr , {}))
910+ wrapper .__wrapped__ = wrapped
911+ return wrapper
912+
913+ _update_wrapper .__doc__ = functools .update_wrapper .__doc__
866914
867915def wraps (
868916wrapped ,
869917assigned = functools .WRAPPER_ASSIGNMENTS ,
870918updated = functools .WRAPPER_UPDATES ,
871919 ):
872- def wrapper (f ):
873- f = functools .wraps (wrapped ,assigned ,updated )(f )
874- f .__wrapped__ = wrapped
875- return f
876-
877- return wrapper
920+ return functools .partial (
921+ _update_wrapper ,wrapped = wrapped ,assigned = assigned ,updated = updated
922+ )
878923
924+ wraps .__doc__ = functools .wraps .__doc__
879925
880926else :
881927wraps = functools .wraps
@@ -888,7 +934,15 @@ def with_metaclass(meta, *bases):
888934# the actual metaclass.
889935class metaclass (type ):
890936def __new__ (cls ,name ,this_bases ,d ):
891- return meta (name ,bases ,d )
937+ if sys .version_info [:2 ]>= (3 ,7 ):
938+ # This version introduced PEP 560 that requires a bit
939+ # of extra care (we mimic what is done by __build_class__).
940+ resolved_bases = types .resolve_bases (bases )
941+ if resolved_bases is not bases :
942+ d ["__orig_bases__" ]= bases
943+ else :
944+ resolved_bases = bases
945+ return meta (name ,resolved_bases ,d )
892946
893947@classmethod
894948def __prepare__ (cls ,name ,this_bases ):
@@ -928,12 +982,11 @@ def ensure_binary(s, encoding="utf-8", errors="strict"):
928982 - `str` -> encoded to `bytes`
929983 - `bytes` -> `bytes`
930984 """
985+ if isinstance (s ,binary_type ):
986+ return s
931987if isinstance (s ,text_type ):
932988return s .encode (encoding ,errors )
933- elif isinstance (s ,binary_type ):
934- return s
935- else :
936- raise TypeError ("not expecting type '%s'" % type (s ))
989+ raise TypeError ("not expecting type '%s'" % type (s ))
937990
938991
939992def ensure_str (s ,encoding = "utf-8" ,errors = "strict" ):
@@ -947,12 +1000,15 @@ def ensure_str(s, encoding="utf-8", errors="strict"):
9471000 - `str` -> `str`
9481001 - `bytes` -> decoded to `str`
9491002 """
950- if not isinstance (s , (text_type ,binary_type )):
951- raise TypeError ("not expecting type '%s'" % type (s ))
1003+ # Optimization: Fast return for the common case.
1004+ if type (s )is str :
1005+ return s
9521006if PY2 and isinstance (s ,text_type ):
953- s = s .encode (encoding ,errors )
1007+ return s .encode (encoding ,errors )
9541008elif PY3 and isinstance (s ,binary_type ):
955- s = s .decode (encoding ,errors )
1009+ return s .decode (encoding ,errors )
1010+ elif not isinstance (s , (text_type ,binary_type )):
1011+ raise TypeError ("not expecting type '%s'" % type (s ))
9561012return s
9571013
9581014
@@ -977,7 +1033,7 @@ def ensure_text(s, encoding="utf-8", errors="strict"):
9771033
9781034def python_2_unicode_compatible (klass ):
9791035"""
980- A decorator that defines __unicode__ and __str__ methods under Python 2.
1036+ Aclass decorator that defines __unicode__ and __str__ methods under Python 2.
9811037 Under Python 3 it does nothing.
9821038
9831039 To support Python 2 and 3 with a single code base, define a __str__ method