Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b7e161

Browse files
committed
backport context argument of urlopen (#22366) for pep 476
1 parent cc23154 commit 8b7e161

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

Doc/library/urllib.request.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ authentication, redirections, cookies and more.
1616
The :mod:`urllib.request` module defines the following functions:
1717

1818

19-
.. function:: urlopen(url, data=None[, timeout], *, cafile=None, capath=None, cadefault=False)
19+
.. function:: urlopen(url, data=None[, timeout], *, cafile=None, capath=None, cadefault=False, context=None)
2020

2121
Open the URL *url*, which can be either a string or a
2222
:class:`Request` object.
@@ -47,6 +47,10 @@ The :mod:`urllib.request` module defines the following functions:
4747
the global default timeout setting will be used). This actually
4848
only works for HTTP, HTTPS and FTP connections.
4949

50+
If *context* is specified, it must be a :class:`ssl.SSLContext` instance
51+
describing the various SSL options. See
52+
:class:`~http.client.HTTPSConnection` for more details.
53+
5054
The optional *cafile* and *capath* parameters specify a set of trusted
5155
CA certificates for HTTPS requests. *cafile* should point to a single
5256
file containing a bundle of CA certificates, whereas *capath* should
@@ -111,6 +115,9 @@ The :mod:`urllib.request` module defines the following functions:
111115
.. versionchanged:: 3.3
112116
*cadefault* was added.
113117

118+
.. versionchanged:: 3.4.3
119+
*context* was added.
120+
114121
.. function:: install_opener(opener)
115122

116123
Install an :class:`OpenerDirector` instance as the default global opener.

Lib/test/test_urllib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from unittest.mock import patch
1111
from test import support
1212
import os
13+
import ssl
1314
import sys
1415
import tempfile
1516
from nturl2path import url2pathname, pathname2url
@@ -379,6 +380,13 @@ def test_URLopener_deprecation(self):
379380
with support.check_warnings(('',DeprecationWarning)):
380381
urllib.request.URLopener()
381382

383+
def test_cafile_and_context(self):
384+
context = ssl.create_default_context()
385+
with self.assertRaises(ValueError):
386+
urllib.request.urlopen(
387+
"https://localhost", cafile="/nonexistent/path", context=context
388+
)
389+
382390
class urlopen_DataTests(unittest.TestCase):
383391
"""Test urlopen() opening a data URL."""
384392

Lib/urllib/request.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,24 @@
136136

137137
_opener = None
138138
def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
139-
*, cafile=None, capath=None, cadefault=False):
139+
*, cafile=None, capath=None, cadefault=False, context=None):
140140
global _opener
141141
if cafile or capath or cadefault:
142+
if context is not None:
143+
raise ValueError(
144+
"You can't pass both context and any of cafile, capath, and "
145+
"cadefault"
146+
)
142147
if not _have_ssl:
143148
raise ValueError('SSL support not available')
144149
context = ssl._create_stdlib_context(cert_reqs=ssl.CERT_REQUIRED,
145150
cafile=cafile,
146151
capath=capath)
147152
https_handler = HTTPSHandler(context=context, check_hostname=True)
148153
opener = build_opener(https_handler)
154+
elif context:
155+
https_handler = HTTPSHandler(context=context)
156+
opener = build_opener(https_handler)
149157
elif _opener is None:
150158
_opener = opener = build_opener()
151159
else:

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ Core and Builtins
3636
Library
3737
-------
3838

39+
- Issue #22366: urllib.request.urlopen will accept a context object
40+
(SSLContext) as an argument which will then used be for HTTPS connection.
41+
Patch by Alex Gaynor.
42+
3943
- Issue #22776: Brought excluded code into the scope of a try block in
4044
SysLogHandler.emit().
4145

0 commit comments

Comments
 (0)