diff options
author | Senthil Kumaran <senthil@uthcode.com> | 2014-09-19 15:23:30 +0800 |
---|---|---|
committer | Senthil Kumaran <senthil@uthcode.com> | 2014-09-19 15:23:30 +0800 |
commit | 57754af23e9a89b7d8dd0a3728a353b26b06a60a (patch) | |
tree | d69a7016d9531372b58738de2bce8c1a19d34b7c /Lib/urllib/request.py | |
parent | 8f12e04ab39f2885a8dacc318e7fb37722b0f5f0 (diff) | |
download | cpython-57754af23e9a89b7d8dd0a3728a353b26b06a60a.tar.gz |
backport context argument of urlopen (#22366) for pep 476
Diffstat (limited to 'Lib/urllib/request.py')
-rw-r--r-- | Lib/urllib/request.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 67c7566c5b..e0c8116373 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -136,9 +136,14 @@ __version__ = sys.version[:3] _opener = None def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, - *, cafile=None, capath=None, cadefault=False): + *, cafile=None, capath=None, cadefault=False, context=None): global _opener if cafile or capath or cadefault: + if context is not None: + raise ValueError( + "You can't pass both context and any of cafile, capath, and " + "cadefault" + ) if not _have_ssl: raise ValueError('SSL support not available') context = ssl._create_stdlib_context(cert_reqs=ssl.CERT_REQUIRED, @@ -146,6 +151,9 @@ def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, capath=capath) https_handler = HTTPSHandler(context=context, check_hostname=True) opener = build_opener(https_handler) + elif context: + https_handler = HTTPSHandler(context=context) + opener = build_opener(https_handler) elif _opener is None: _opener = opener = build_opener() else: |