1
- # Copyright (c) 2010-2019 Benjamin Peterson
1
+ # Copyright (c) 2010-2020 Benjamin Peterson
2
2
#
3
3
# Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
# of this software and associated documentation files (the "Software"), to deal
29
29
import types
30
30
31
31
__author__ = "Benjamin Peterson <benjamin@python.org>"
32
- __version__ = "1.12 .0"
32
+ __version__ = "1.16 .0"
33
33
34
34
35
35
# Useful for very coarse version differentiation.
@@ -71,6 +71,11 @@ def __len__(self):
71
71
MAXSIZE = int ((1 << 63 ) - 1 )
72
72
del X
73
73
74
+ if PY34 :
75
+ from importlib .util import spec_from_loader
76
+ else :
77
+ spec_from_loader = None
78
+
74
79
75
80
def _add_doc (func , doc ):
76
81
"""Add documentation to a function."""
@@ -182,6 +187,11 @@ def find_module(self, fullname, path=None):
182
187
return self
183
188
return None
184
189
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
+
185
195
def __get_module (self , fullname ):
186
196
try :
187
197
return self .known_modules [fullname ]
@@ -220,6 +230,12 @@ def get_code(self, fullname):
220
230
221
231
get_source = get_code # same as get_code
222
232
233
+ def create_module (self , spec ):
234
+ return self .load_module (spec .name )
235
+
236
+ def exec_module (self , module ):
237
+ pass
238
+
223
239
224
240
_importer = _SixMetaPathImporter (__name__ )
225
241
@@ -260,9 +276,19 @@ class _MovedItems(_LazyModule):
260
276
),
261
277
MovedModule ("builtins" , "__builtin__" ),
262
278
MovedModule ("configparser" , "ConfigParser" ),
279
+ MovedModule (
280
+ "collections_abc" ,
281
+ "collections" ,
282
+ "collections.abc" if sys .version_info >= (3 , 3 ) else "collections" ,
283
+ ),
263
284
MovedModule ("copyreg" , "copy_reg" ),
264
285
MovedModule ("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
+ ),
266
292
MovedModule ("http_cookiejar" , "cookielib" , "http.cookiejar" ),
267
293
MovedModule ("http_cookies" , "Cookie" , "http.cookies" ),
268
294
MovedModule ("html_entities" , "htmlentitydefs" , "html.entities" ),
@@ -307,7 +333,9 @@ class _MovedItems(_LazyModule):
307
333
]
308
334
# Add windows specific modules.
309
335
if sys .platform == "win32" :
310
- _moved_attributes += [MovedModule ("winreg" , "_winreg" )]
336
+ _moved_attributes += [
337
+ MovedModule ("winreg" , "_winreg" ),
338
+ ]
311
339
312
340
for attr in _moved_attributes :
313
341
setattr (_MovedItems , attr .name , attr )
@@ -476,7 +504,7 @@ class Module_six_moves_urllib_robotparser(_LazyModule):
476
504
477
505
478
506
_urllib_robotparser_moved_attributes = [
479
- MovedAttribute ("RobotFileParser" , "robotparser" , "urllib.robotparser" )
507
+ MovedAttribute ("RobotFileParser" , "robotparser" , "urllib.robotparser" ),
480
508
]
481
509
for attr in _urllib_robotparser_moved_attributes :
482
510
setattr (Module_six_moves_urllib_robotparser , attr .name , attr )
@@ -678,9 +706,11 @@ def u(s):
678
706
if sys .version_info [1 ] <= 1 :
679
707
_assertRaisesRegex = "assertRaisesRegexp"
680
708
_assertRegex = "assertRegexpMatches"
709
+ _assertNotRegex = "assertNotRegexpMatches"
681
710
else :
682
711
_assertRaisesRegex = "assertRaisesRegex"
683
712
_assertRegex = "assertRegex"
713
+ _assertNotRegex = "assertNotRegex"
684
714
else :
685
715
686
716
def b (s ):
@@ -707,6 +737,7 @@ def indexbytes(buf, i):
707
737
_assertCountEqual = "assertItemsEqual"
708
738
_assertRaisesRegex = "assertRaisesRegexp"
709
739
_assertRegex = "assertRegexpMatches"
740
+ _assertNotRegex = "assertNotRegexpMatches"
710
741
_add_doc (b , """Byte literal""" )
711
742
_add_doc (u , """Text literal""" )
712
743
@@ -723,6 +754,10 @@ def assertRegex(self, *args, **kwargs):
723
754
return getattr (self , _assertRegex )(* args , ** kwargs )
724
755
725
756
757
+ def assertNotRegex (self , * args , ** kwargs ):
758
+ return getattr (self , _assertNotRegex )(* args , ** kwargs )
759
+
760
+
726
761
if PY3 :
727
762
exec_ = getattr (moves .builtins , "exec" )
728
763
@@ -762,18 +797,7 @@ def exec_(_code_, _globs_=None, _locs_=None):
762
797
)
763
798
764
799
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 ,):
777
801
exec_ (
778
802
"""def raise_from(value, from_value):
779
803
try:
@@ -863,19 +887,41 @@ def print_(*args, **kwargs):
863
887
_add_doc (reraise , """Reraise an exception.""" )
864
888
865
889
if 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__
866
914
867
915
def wraps (
868
916
wrapped ,
869
917
assigned = functools .WRAPPER_ASSIGNMENTS ,
870
918
updated = functools .WRAPPER_UPDATES ,
871
919
):
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
+ )
878
923
924
+ wraps .__doc__ = functools .wraps .__doc__
879
925
880
926
else :
881
927
wraps = functools .wraps
@@ -888,7 +934,15 @@ def with_metaclass(meta, *bases):
888
934
# the actual metaclass.
889
935
class metaclass (type ):
890
936
def __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 )
892
946
893
947
@classmethod
894
948
def __prepare__ (cls , name , this_bases ):
@@ -928,12 +982,11 @@ def ensure_binary(s, encoding="utf-8", errors="strict"):
928
982
- `str` -> encoded to `bytes`
929
983
- `bytes` -> `bytes`
930
984
"""
985
+ if isinstance (s , binary_type ):
986
+ return s
931
987
if isinstance (s , text_type ):
932
988
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 ))
989
+ raise TypeError ("not expecting type '%s'" % type (s ))
937
990
938
991
939
992
def ensure_str (s , encoding = "utf-8" , errors = "strict" ):
@@ -947,12 +1000,15 @@ def ensure_str(s, encoding="utf-8", errors="strict"):
947
1000
- `str` -> `str`
948
1001
- `bytes` -> decoded to `str`
949
1002
"""
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
952
1006
if PY2 and isinstance (s , text_type ):
953
- s = s .encode (encoding , errors )
1007
+ return s .encode (encoding , errors )
954
1008
elif 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 ))
956
1012
return s
957
1013
958
1014
@@ -977,7 +1033,7 @@ def ensure_text(s, encoding="utf-8", errors="strict"):
977
1033
978
1034
def python_2_unicode_compatible (klass ):
979
1035
"""
980
- A decorator that defines __unicode__ and __str__ methods under Python 2.
1036
+ A class decorator that defines __unicode__ and __str__ methods under Python 2.
981
1037
Under Python 3 it does nothing.
982
1038
983
1039
To support Python 2 and 3 with a single code base, define a __str__ method
0 commit comments