Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poor urllib error handling #44218

Closed
gvanrossum opened this issue Nov 9, 2006 · 4 comments
Closed

poor urllib error handling #44218

gvanrossum opened this issue Nov 9, 2006 · 4 comments

Comments

@gvanrossum
Copy link
Member

BPO 1593751
Nosy @gvanrossum, @birkenfeld

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2007-03-14.08:30:39.000>
created_at = <Date 2006-11-09.21:04:07.000>
labels = []
title = 'poor urllib error handling'
updated_at = <Date 2007-03-14.08:30:39.000>
user = 'https://github.com/gvanrossum'

bugs.python.org fields:

activity = <Date 2007-03-14.08:30:39.000>
actor = 'georg.brandl'
assignee = 'none'
closed = True
closed_date = None
closer = None
components = ['None']
creation = <Date 2006-11-09.21:04:07.000>
creator = 'gvanrossum'
dependencies = []
files = []
hgrepos = []
issue_num = 1593751
keywords = []
message_count = 4.0
messages = ['30498', '30499', '30500', '30501']
nosy_count = 4.0
nosy_names = ['gvanrossum', 'georg.brandl', 'racarr', 'robertwinder']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1593751'
versions = []

@gvanrossum
Copy link
Member Author

I set up a simple server that returns an empty response.

>>> from socket import *
>>> s = socket()
>>> s.bind(("", 9999))
>>> while 1: x, c = s.accept(); print c; x.recv(1000);
x.close()
...

Pointing urllib at this gives a traceback:

Python 2.6a0 (trunk:52099M, Oct  3 2006, 09:59:17) 
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> import urllib
>>> urllib.urlopen('http://localhost:9999/')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/guido/p/Lib/urllib.py", line 82, in urlopen
    return opener.open(url)
  File "/home/guido/p/Lib/urllib.py", line 190, in open
    return getattr(self, name)(url)
  File "/home/guido/p/Lib/urllib.py", line 334, in
open_http
    return self.http_error(url, fp, errcode, errmsg,
headers)
  File "/home/guido/p/Lib/urllib.py", line 351, in
http_error
    return self.http_error_default(url, fp, errcode,
errmsg, headers)
  File "/home/guido/p/Lib/urllib.py", line 608, in
http_error_default
    return addinfourl(fp, headers, "http:" + url)
  File "/home/guido/p/Lib/urllib.py", line 951, in __init__
    addbase.__init__(self, fp)
  File "/home/guido/p/Lib/urllib.py", line 898, in __init__
    self.read = self.fp.read
AttributeError: 'NoneType' object has no attribute 'read'
>>> 

I can repeat this with 2.2.3 and 2.4.3 as well (don't
have 2.3 around for testing).

The direct cause of the problem is that h.getfile() on
line 329 of urllib.py (in head of trunk) returns None.

@racarr
Copy link
Mannequin

racarr mannequin commented Dec 5, 2006

Fix?

Index: urllib.py
===================================================================
--- urllib.py (revision 52918)
+++ urllib.py (working copy)
@@ -895,8 +895,10 @@

     def __init__(self, fp):
         self.fp = fp
-        self.read = self.fp.read
-        self.readline = self.fp.readline
+       try:
+               self.read = self.fp.read
+               self.readline = self.fp.readline
+       except: print "File handler is none"
         if hasattr(self.fp, "readlines"): self.readlines = self.fp.readlines
         if hasattr(self.fp, "fileno"):
             self.fileno = self.fp.fileno

@robertwinder
Copy link
Mannequin

robertwinder mannequin commented Dec 15, 2006

Same error handling with 2.3. Suggested fix doesn't work and gives.

AttributeError: addinfourl instance has no attribute 'read'

@birkenfeld
Copy link
Member

Finally fixed in rev. 54376, 54377 (2.5).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants