Skip to content

Commit 0fd00ee

Browse files
authored
gh-50720: Copy winerror to URLError on Windows (#161047)
1 parent 529d66c commit 0fd00ee

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎Doc/library/urllib.error.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ The following exceptions are raised by :mod:`!urllib.error` as appropriate:
2929

3030
.. versionchanged:: 3.16
3131
The :attr:`~OSError.errno` and :attr:`~OSError.strerror` attributes are
32-
now set from :attr:`reason` when it is an :exc:`OSError`.
32+
now set from :attr:`reason` when it is an :exc:`OSError`. On Windows,
33+
:attr:`~OSError.winerror` is set as well.
3334

3435

3536
.. exception:: HTTPError(url, code, msg, hdrs, fp)

‎Lib/test/test_urllib2.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ def request(self, *args, **kwargs):
104104
self.assertEqual(err.errno, -2)
105105
self.assertEqual(err.strerror, 'Name or service not known')
106106

107+
@unittest.skipUnless(sys.platform == "win32", "Windows-specific test")
108+
def test_URLError_OSError_attributes_winerror(self):
109+
# gh-50720: OSError(errno, strerror, filename, winerror):
110+
# on Windows the constructor derives errno from winerror.
111+
os_error = OSError(0, "The system cannot find the path specified", None, 3)
112+
113+
err = urllib.error.URLError(os_error)
114+
self.assertEqual(err.winerror, 3)
115+
self.assertEqual(err.strerror, os_error.strerror)
116+
107117

108118
class RequestHdrsTests(unittest.TestCase):
109119

‎Lib/urllib/error.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
response.
1212
"""
1313
import io
14+
import sys
1415
import urllib.response
1516

1617
__all__ = ['URLError', 'HTTPError', 'ContentTooShortError']
@@ -32,6 +33,8 @@ def __init__(self, reason, filename=None):
3233
# it would have set from the wrapped error (gh-50720).
3334
self.errno = reason.errno
3435
self.strerror = reason.strerror
36+
if sys.platform == 'win32':
37+
self.winerror = reason.winerror
3538

3639
def __str__(self):
3740
return '<urlopen error %s>' % self.reason
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
:exc:`urllib.error.URLError` now sets the :attr:`~OSError.errno` and
22
:attr:`~OSError.strerror` attributes from
3-
:attr:`~urllib.error.URLError.reason` when it is an :exc:`OSError`.
3+
:attr:`~urllib.error.URLError.reason` when it is an :exc:`OSError`. On
4+
Windows, :attr:`~OSError.winerror` is set as well.

0 commit comments

Comments
 (0)