Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Doc/library/urllib.error.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ The following exceptions are raised by :mod:`!urllib.error` as appropriate:

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


.. exception:: HTTPError(url, code, msg, hdrs, fp)
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ def request(self, *args, **kwargs):
self.assertEqual(err.errno, -2)
self.assertEqual(err.strerror, 'Name or service not known')

@unittest.skipUnless(sys.platform == "win32", "Windows-specific test")
def test_URLError_OSError_attributes_winerror(self):
# gh-50720: OSError(errno, strerror, filename, winerror):
# on Windows the constructor derives errno from winerror.
os_error = OSError(0, "The system cannot find the path specified", None, 3)

err = urllib.error.URLError(os_error)
self.assertEqual(err.winerror, 3)
self.assertEqual(err.strerror, os_error.strerror)


class RequestHdrsTests(unittest.TestCase):

Expand Down
3 changes: 3 additions & 0 deletions Lib/urllib/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
response.
"""
import io
import sys
import urllib.response

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

def __str__(self):
return '<urlopen error %s>' % self.reason
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
:exc:`urllib.error.URLError` now sets the :attr:`~OSError.errno` and
:attr:`~OSError.strerror` attributes from
:attr:`~urllib.error.URLError.reason` when it is an :exc:`OSError`.
:attr:`~urllib.error.URLError.reason` when it is an :exc:`OSError`. On
Windows, :attr:`~OSError.winerror` is set as well.
Loading