[issue35447] Redundant try-except block in urllib

Martin Panter report at bugs.python.org
Sun Dec 9 23:08:09 EST 2018


Martin Panter <vadmium+py at gmail.com> added the comment:

Code in question:

try:
    # non-sequence items should not work with len()
    # non-empty strings will fail this
    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)

It is not redundant if you want a specific error message. I think that is the point of the code, to indicate the problem to the programmer. So the message is meant to be useful. I can think of three cases:

>>> urlencode(None)  # Non-sequence
TypeError: not a valid non-string sequence or mapping object
>>> urlencode({'sized but not indexable'})
TypeError: not a valid non-string sequence or mapping object
>>> urlencode('item [0] not a tuple')
TypeError: not a valid non-string sequence or mapping object

----------
nosy: +martin.panter

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35447>
_______________________________________


More information about the Python-bugs-list mailing list