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: Review exception handling in urllib
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, martin.panter, orsenthil
Priority: normal Keywords:

Created on 2022-01-25 12:33 by iritkatriel, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg411583 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-25 12:33
Is there a reason for this raising, catching and reraising the exception here:

https://github.com/python/cpython/blob/main/Lib/urllib/parse.py#L934


rather than just:

if len(query) and not isinstance(query[0], tuple):
    raise TypeError("not a valid non-string sequence "
                    "or mapping object")

?
msg411584 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-25 12:37
In urllib.request, there are in a few places things like:

  except OSError as msg:
     raise OSError('socket error', msg).with_traceback(sys.exc_info()[2])


I imagine this predates chaining - is there a reason not to raise..from here instead of wrapping the "msg" exception (I'm guessing from the name that this used to be a string, but now it's actually an exception).
msg411588 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-25 13:27
Note that test.support has special handling for urllib's nested exception structure:

https://github.com/python/cpython/blob/3.10/Lib/test/support/socket_helper.py#L250
msg411707 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2022-01-26 06:00
The linked code is for urllib.parse.urlencode, looking something like

try:
    if len(query) and not isinstance(query[0], tuple):
        raise TypeError
except TypeError:
    ty, va, tb = sys.exc_info()
    raise TypeError("not a valid non-string sequence "
                    "or mapping object").with_traceback(tb)

I guess it raises twice so that the error message is not duplicated in the code. The author probably also wants the TypeError initially raised from the "len(query)" and "query[0]" operations to get the same "not a valid . . ." message.

Regarding the OSError, originally it was catching socket.error and raising IOError. I guess someone only wanted the caller to have catch IOError and not need to import the socket module. Later these exception types became aliases of each other.

Anyway, the URLopener class is documented as deprecated, so is it really worth changing anything in that?
msg411708 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-26 07:07
> The author probably also wants the TypeError initially raised from the "len(query)" and "query[0]" operations to get the same "not a valid . . ." message.

I see.


I didn’t realise it’s deprecated, I guess we’ll leave it alone then. Thanks.
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90675
2022-01-26 07:07:51iritkatrielsetstatus: open -> closed
resolution: wont fix
messages: + msg411708

stage: resolved
2022-01-26 06:00:06martin.pantersetnosy: + martin.panter
messages: + msg411707
2022-01-25 13:27:26iritkatrielsetmessages: + msg411588
2022-01-25 12:38:30iritkatrielsettitle: Review exception handling in urllib.parse -> Review exception handling in urllib
2022-01-25 12:37:21iritkatrielsetmessages: + msg411584
2022-01-25 12:33:04iritkatrielcreate