[Python-Dev] problems building python2.7

Hans Mulder hansmu at xs4all.nl
Fri Nov 9 16:44:00 CET 2012


On 9/11/12 15:11:26, Chris Withers wrote:
> On 09/11/2012 11:54, Hans Mulder wrote:
>> I tried "make test", and I got:
>>
>> test test_urllib failed -- Traceback (most recent call last):
>>    File "/Users/hans/python/cpython/cpython-2.7/Lib/test/test_urllib.py",
>> line 235, in test_missing_localfile
>>      fp.close()
>> UnboundLocalError: local variable 'fp' referenced before assignment
>>
>> Is this a bug in the test suite, or a regression in 2.7.3+ ?
>>
>> This is on MacOS 10.6.5, using a checkout from hg.python.org
>> from earlier today.
> 
> I'm on MacOS 10.7.5 and got:
> 
> 356 tests OK.
> 35 tests skipped:
>     test_al test_bsddb test_bsddb3 test_cd test_cl test_codecmaps_cn
>     test_codecmaps_hk test_codecmaps_jp test_codecmaps_kr
>     test_codecmaps_tw test_curses test_dl test_epoll test_gdb
>     test_gdbm test_gl test_imageop test_imgfile test_largefile
>     test_linuxaudiodev test_msilib test_ossaudiodev test_readline
>     test_smtpnet test_socketserver test_startfile test_sunaudiodev
>     test_timeout test_tk test_ttk_guionly test_urllib2net
>     test_urllibnet test_winreg test_winsound test_zipfile64
> 4 skips unexpected on darwin:
>     test_dl test_readline test_tk test_ttk_guionly

I looked into it, and the problem is this bit of code (line 230-235):

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

Due to a misconfiguration, urllib.thishost() raises an IOError on my
laptop.  This causes urllib.urlopen to raise an exception, and the
name fp is never bound, so that the fp.close() in the finally clause
raises an UnboundLocalError, masking the problem in urlopen.

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.

-- HansM




More information about the Python-Dev mailing list