This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: urllib2.urlopen() raises OSError instead of URLError
Type: Stage:
Components: Extension Modules Versions: Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: collinwinter, georg.brandl, jerry.seutter, jjlee
Priority: normal Keywords: patch

Created on 2007-02-24 23:20 by jerry.seutter, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bug_1591774_failing_test_and_bugfix.patch jerry.seutter, 2007-02-24 23:20 patch
bug_1591774_failing_test_and_bugfix_v2.patch jerry.seutter, 2007-03-12 06:22 patch v2 - against trunk, uncommented other unit tests that now pass
Messages (7)
msg51952 - (view) Author: Jerry Seutter (jerry.seutter) * (Python committer) Date: 2007-02-24 23:20
See bug 1591774.  

The base problem is that when passing a file:// url into urllib2.urlopen() for a file that doesn't exist, the os.stat() call fails with an OSError exception which is not caught.  The documentation for urllib2 states that urlopen() raises a URLError upon error.

This bug can also be triggered when fetching a http:// url.  A misconfigured webserver can cause urllib2.urlopen to use the FileHandler (see bug report).

This patch catches OSError in the FileHandler class and raises it as a URLError.  Unit test included.
msg51953 - (view) Author: John J Lee (jjlee) Date: 2007-02-25 17:01
I recall that when I wrote the urllib2 tests there were a fair few cases similar to this -- indeed, this particular one is already marked in the tests with an "XXXX" in test_file().

What do other people think about these -- should they be fixed, or the docs changed?
msg51954 - (view) Author: Jerry Seutter (jerry.seutter) * (Python committer) Date: 2007-02-26 04:12
Sorry, I looked at that commented out section and didn't immediately see what it was for, so ignored it.

I'm not sure what the correct behavior would be in this case.  Getting a OSError from a http:// url is unsettling.

I guess that if I were using the library, I would only want to have to catch one type of exception.  Having to catch both URLError and OSError is inconvenient.  If I were looking for a file on disk I would use file(), so the only time someone probably uses the file:// url syntax is when the end user is entering the URL.  The end user wouldn't distinguish between the two exception types in this case.

msg51955 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2007-03-09 18:18
I think it should be fixed to raise URLError.
msg51956 - (view) Author: Jerry Seutter (jerry.seutter) * (Python committer) Date: 2007-03-12 06:22
File Added: bug_1591774_failing_test_and_bugfix_v2.patch
msg51957 - (view) Author: Jerry Seutter (jerry.seutter) * (Python committer) Date: 2007-03-12 06:23
Added patch v2 which is a patch against trunk, and adds in the tests that were commented out (they previously failed).
msg51958 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-13 08:15
Committed in rev. 54326.

Not backporting since it raises a different exception now.
History
Date User Action Args
2022-04-11 14:56:22adminsetgithub: 44611
2007-02-24 23:20:57jerry.seuttercreate