[Python-checkins] CVS: python/dist/src/Lib urllib2.py,1.5,1.6

Jeremy Hylton python-dev@python.org
Thu, 12 Oct 2000 11:54:21 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory slayer.i.sourceforge.net:/tmp/cvs-serv2932

Modified Files:
	urllib2.py 
Log Message:
two fixes for redirects:
    - don't close the fp, since that appears to also close the socket
    - join the original url with the redirect reponse to deal with
      relative redirect URL

wrap two socket ops in try/except to turn them into URLErrors, so that
client code need only catch one exception. 

in HTTPError.__del__ only close fp if fp is not None

style changes: 
    - use f(*args) instead of apply(f, args)
    - use __super_init instead of super.__init__(self, ...)



Index: urllib2.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib2.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -r1.5 -r1.6
*** urllib2.py	2000/07/16 12:04:30	1.5
--- urllib2.py	2000/10/12 18:54:18	1.6
***************
*** 156,162 ****
  class HTTPError(URLError, addinfourl):
      """Raised when HTTP error occurs, but also acts like non-error return"""
  
      def __init__(self, url, code, msg, hdrs, fp):
!         addinfourl.__init__(self, fp, hdrs, url)
          self.code = code
          self.msg = msg
--- 156,163 ----
  class HTTPError(URLError, addinfourl):
      """Raised when HTTP error occurs, but also acts like non-error return"""
+     __super_init = addinfourl.__init__
  
      def __init__(self, url, code, msg, hdrs, fp):
!         self.__super_init(fp, hdrs, url)
          self.code = code
          self.msg = msg
***************
*** 172,176 ****
          # XXX is this safe? what if user catches exception, then
          # extracts fp and discards exception?
!         self.fp.close()
  
  class GopherError(URLError):
--- 173,178 ----
          # XXX is this safe? what if user catches exception, then
          # extracts fp and discards exception?
!         if self.fp:
!             self.fp.close()
  
  class GopherError(URLError):
***************
*** 216,219 ****
--- 218,222 ----
          if self.type is None:
              self.type, self.__r_type = splittype(self.__original)
+             assert self.type is not None, self.__original
          return self.type
  
***************
*** 298,302 ****
          for handler in handlers:
              func = getattr(handler, meth_name)
!             result = apply(func, args)
              if result is not None:
                  return result
--- 301,306 ----
          for handler in handlers:
              func = getattr(handler, meth_name)
! 
!             result = func(*args)
              if result is not None:
                  return result
***************
*** 319,323 ****
          type_ = req.get_type()
          result = self._call_chain(self.handle_open, type_, type_ + \
!                                   '_open', req) 
          if result:
              return result
--- 323,327 ----
          type_ = req.get_type()
          result = self._call_chain(self.handle_open, type_, type_ + \
!                                   '_open', req)
          if result:
              return result
***************
*** 339,343 ****
              http_err = 0
          args = (dict, proto, meth_name) + args
!         result = apply(self._call_chain, args)
          if result:
              return result
--- 343,347 ----
              http_err = 0
          args = (dict, proto, meth_name) + args
!         result = self._call_chain(*args)
          if result:
              return result
***************
*** 345,349 ****
          if http_err:
              args = (dict, 'default', 'http_error_default') + orig_args
!             return apply(self._call_chain, args)
  
  def is_callable(obj):
--- 349,353 ----
          if http_err:
              args = (dict, 'default', 'http_error_default') + orig_args
!             return self._call_chain(*args)
  
  def is_callable(obj):
***************
*** 441,444 ****
--- 445,450 ----
          fp.close()
  
+         newurl = urlparse.urljoin(req.get_full_url(), newurl)
+ 
          # XXX Probably want to forget about the state of the current
          # request, although that might interact poorly with other
***************
*** 728,744 ****
              raise URLError('no host given')
  
!         h = httplib.HTTP(host) # will parse host:port
! ##      h.set_debuglevel(1)
!         if req.has_data():
!             data = req.get_data()
!             h.putrequest('POST', req.get_selector())
!             h.putheader('Content-type', 'application/x-www-form-urlencoded')
!             h.putheader('Content-length', '%d' % len(data))
!         else:
!             h.putrequest('GET', req.get_selector())
          # XXX proxies would have different host here
          h.putheader('Host', host)
          for args in self.parent.addheaders:
!             apply(h.putheader, args)
          for k, v in req.headers.items():
              h.putheader(k, v)
--- 734,754 ----
              raise URLError('no host given')
  
!         try:
!             h = httplib.HTTP(host) # will parse host:port
!             if req.has_data():
!                 data = req.get_data()
!                 h.putrequest('POST', req.get_selector())
!                 h.putheader('Content-type',
!                             'application/x-www-form-urlencoded')
!                 h.putheader('Content-length', '%d' % len(data))
!             else:
!                 h.putrequest('GET', req.get_selector())
!         except socket.error, err:
!             raise URLError(err)
!             
          # XXX proxies would have different host here
          h.putheader('Host', host)
          for args in self.parent.addheaders:
!             h.putheader(*args)
          for k, v in req.headers.items():
              h.putheader(k, v)
***************
*** 752,761 ****
              return addinfourl(fp, hdrs, req.get_full_url())
          else:
-             # want to make sure the socket is closed, even if error
-             # handling doesn't return immediately.  the socket won't
-             # actually be closed until fp is also closed.
-             if h.sock:
-                 h.sock.close()
-                 h.sock = None
              return self.parent.error('http', req, fp, code, msg, hdrs)
  
--- 762,765 ----
***************
*** 860,864 ****
              raise IOError, ('ftp error', 'no host given')
          # XXX handle custom username & password
!         host = socket.gethostbyname(host)
          host, port = splitport(host)
          if port is None:
--- 864,871 ----
              raise IOError, ('ftp error', 'no host given')
          # XXX handle custom username & password
!         try:
!             host = socket.gethostbyname(host)
!         except socket.error, msg:
!             raise URLError(msg)
          host, port = splitport(host)
          if port is None:
***************
*** 989,993 ****
      if socket.gethostname() == 'bitdiddle':
          localhost = 'bitdiddle.cnri.reston.va.us'
!     elif socket.gethostname() == 'walden':
          localhost = 'localhost'
      else:
--- 996,1000 ----
      if socket.gethostname() == 'bitdiddle':
          localhost = 'bitdiddle.cnri.reston.va.us'
!     elif socket.gethostname() == 'bitdiddle.concentric.net':
          localhost = 'localhost'
      else: