[New-bugs-announce] [issue16450] test_missing_localfile masks problems in urlopen

Hans Mulder report at bugs.python.org
Sat Nov 10 15:42:12 CET 2012


New submission from Hans Mulder:

Due to a misconfiguration, urllib.thishost() raises an IOError
on my laptop.  This causes urllib.urlopen to raise an exception.
A flaw in test_missing_localfile causes this exception to not be
reported.  The problem happens at line 230-235:

        try:
            self.assertTrue(os.path.exists(tmp_file))
            fp = urllib.urlopen(tmp_fileurl)
        finally:
            os.close(fd)
            fp.close()

On my laptop, urllib.urlopen raises a socket.gaierror.  This
means that the variable fp is never set, and the fp.close() at
line 235 raises an UnboundLoccalError, masking the socket error.

A quick fix would be:

        try:
            self.assertTrue(os.path.exists(tmp_file))
            fp = urllib.urlopen(tmp_fileurl)
            fp.close()
        finally:
            os.close(fd)

That way, the .close is only attempted if the open succeeds.

----------
components: Tests
messages: 175278
nosy: HansM
priority: normal
severity: normal
status: open
title: test_missing_localfile masks problems in urlopen
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16450>
_______________________________________


More information about the New-bugs-announce mailing list