Skip to content

Commit 2698537

Browse files
committed
Update vendored six to 1.16.0
1 parent 07bed79 commit 2698537

File tree

1 file changed

+89
-33
lines changed

1 file changed

+89
-33
lines changed

src/urllib3/packages/six.py

Lines changed: 89 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
@@ -29,7 +29,7 @@
2929
import 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):
7171
MAXSIZE = int((1 << 63) - 1)
7272
del X
7373

74+
if PY34:
75+
from importlib.util import spec_from_loader
76+
else:
77+
spec_from_loader = None
78+
7479

7580
def _add_doc(func, doc):
7681
"""Add documentation to a function."""
@@ -182,6 +187,11 @@ def find_module(self, fullname, path=None):
182187
return self
183188
return 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+
185195
def __get_module(self, fullname):
186196
try:
187197
return self.known_modules[fullname]
@@ -220,6 +230,12 @@ def get_code(self, fullname):
220230

221231
get_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
),
261277
MovedModule("builtins", "__builtin__"),
262278
MovedModule("configparser", "ConfigParser"),
279+
MovedModule(
280+
"collections_abc",
281+
"collections",
282+
"collections.abc" if sys.version_info >= (3, 3) else "collections",
283+
),
263284
MovedModule("copyreg", "copy_reg"),
264285
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+
),
266292
MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
267293
MovedModule("http_cookies", "Cookie", "http.cookies"),
268294
MovedModule("html_entities", "htmlentitydefs", "html.entities"),
@@ -307,7 +333,9 @@ class _MovedItems(_LazyModule):
307333
]
308334
# Add windows specific modules.
309335
if sys.platform == "win32":
310-
_moved_attributes += [MovedModule("winreg", "_winreg")]
336+
_moved_attributes += [
337+
MovedModule("winreg", "_winreg"),
338+
]
311339

312340
for attr in _moved_attributes:
313341
setattr(_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
]
481509
for attr in _urllib_robotparser_moved_attributes:
482510
setattr(Module_six_moves_urllib_robotparser, attr.name, attr)
@@ -678,9 +706,11 @@ def u(s):
678706
if sys.version_info[1] <= 1:
679707
_assertRaisesRegex = "assertRaisesRegexp"
680708
_assertRegex = "assertRegexpMatches"
709+
_assertNotRegex = "assertNotRegexpMatches"
681710
else:
682711
_assertRaisesRegex = "assertRaisesRegex"
683712
_assertRegex = "assertRegex"
713+
_assertNotRegex = "assertNotRegex"
684714
else:
685715

686716
def 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):
723754
return getattr(self, _assertRegex)(*args, **kwargs)
724755

725756

757+
def assertNotRegex(self, *args, **kwargs):
758+
return getattr(self, _assertNotRegex)(*args, **kwargs)
759+
760+
726761
if PY3:
727762
exec_ = 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,):
777801
exec_(
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

865889
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__
866914

867915
def wraps(
868916
wrapped,
869917
assigned=functools.WRAPPER_ASSIGNMENTS,
870918
updated=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

880926
else:
881927
wraps = functools.wraps
@@ -888,7 +934,15 @@ def with_metaclass(meta, *bases):
888934
# the actual metaclass.
889935
class metaclass(type):
890936
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)
892946

893947
@classmethod
894948
def __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
931987
if isinstance(s, text_type):
932988
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))
937990

938991

939992
def 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
9521006
if PY2 and isinstance(s, text_type):
953-
s = s.encode(encoding, errors)
1007+
return s.encode(encoding, errors)
9541008
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))
9561012
return s
9571013

9581014

@@ -977,7 +1033,7 @@ def ensure_text(s, encoding="utf-8", errors="strict"):
9771033

9781034
def python_2_unicode_compatible(klass):
9791035
"""
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.
9811037
Under Python 3 it does nothing.
9821038
9831039
To support Python 2 and 3 with a single code base, define a __str__ method

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy