[ python-Bugs-1163401 ] uncaught AttributeError deep in urllib

SourceForge.net noreply at sourceforge.net
Wed Mar 16 18:07:36 CET 2005


Bugs item #1163401, was opened at 2005-03-14 16:39
Message generated for change (Comment added) made by lohnk
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1163401&group_id=5470

Category: Extension Modules
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: K Lars Lohn (lohnk)
Assigned to: Nobody/Anonymous (nobody)
Summary: uncaught AttributeError deep in urllib

Initial Comment:
Python 2.4 and Python 2.3.4 running under Suse 9.2

We're getting an AttributeError exception "AttributeError: 'NoneType' object has no attribute 'read'" within a very simple call to urllib.urlopen.

This was discovered while working on Sentry 2, the new mirror integrity checker for the Mozilla project.  We try to touch hundreds of URLs to make sure that the files are present on each of the mirrors.  One particular URL kills the call to urllib.urlopen: http://mozilla.mirrors.skynet.be/pub/ftp.mozilla.org/firefox/releases/1.0/win32/en-US/Firefox%20Setup%201.0.exe
This file probably does not exist on the mirror, however, in other cases of bad URLs, we get much more graceful failures when we try to read from the object returned by urllib.urlopen.

>>> import urllib
>>> urlReader = urllib.urlopen("http://mozilla.mirrors.skynet.be/pub/ftp.mozilla.org/firefox/releases/1.0/win32/en-US/Firefox%20Setup%201.0.exe")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.4/urllib.py", line 77, in urlopen
    return opener.open(url)
  File "/usr/local/lib/python2.4/urllib.py", line 180, in open
    return getattr(self, name)(url)
  File "/usr/local/lib/python2.4/urllib.py", line 305, in open_http
    return self.http_error(url, fp, errcode, errmsg, headers)
  File "/usr/local/lib/python2.4/urllib.py", line 322, in http_error
    return self.http_error_default(url, fp, errcode, errmsg, headers)
  File "/usr/local/lib/python2.4/urllib.py", line 550, in http_error_default
    return addinfourl(fp, headers, "http:" + url)
  File "/usr/local/lib/python2.4/urllib.py", line 836, in __init__
    addbase.__init__(self, fp)
  File "/usr/local/lib/python2.4/urllib.py", line 786, in __init__
    self.read = self.fp.read
AttributeError: 'NoneType' object has no attribute 'read'

The  attached file is a three line scipt that demos the problem.



----------------------------------------------------------------------

>Comment By: K Lars Lohn (lohnk)
Date: 2005-03-16 09:07

Message:
Logged In: YES 
user_id=1239273

I've changed over to urllib2. The only complication involved the exception handling model: urllib2's HTTPError exceptions cannot be pickled because they contain an open socket._fileobject.  While mildly inconvenient, the workaround was not difficult.

----------------------------------------------------------------------

Comment By: Skip Montanaro (montanaro)
Date: 2005-03-15 11:09

Message:
Logged In: YES 
user_id=44345


Looking through the code I believe I traced the problem back
to httplib.HTTP which sets self.fp to None when it's closed.
It seems that urllib is trying to access this object after
the connection's been closed.

I realize the problem has passed for the moment, but have you 
considered using urllib2?  The urllib library still uses
httplib.HTTP
which is really only there for backward compatibility.  From
this end it would be nice to leave urllib and httplib.HTTP
alone.
New apps should probably use urllib2 which uses the newer
httplib.HTTPConnection class.


----------------------------------------------------------------------

Comment By: K Lars Lohn (lohnk)
Date: 2005-03-15 08:50

Message:
Logged In: YES 
user_id=1239273

This problem is apparently transient depending on network conditions or, perhaps, the configuration of the server end.  On 3/14 the problem has mysteriously vanished....

----------------------------------------------------------------------

Comment By: Jarek Zgoda (zgoda)
Date: 2005-03-15 01:52

Message:
Logged In: YES 
user_id=92222

No such error on Windows:
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit
(Intel)] on win32

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1163401&group_id=5470


More information about the Python-bugs-list mailing list