[Python-checkins] python/dist/src/Lib urllib2.py,1.47,1.48

niemeyer@users.sourceforge.net niemeyer@users.sourceforge.net
Sat, 07 Jun 2003 10:53:11 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv16400/Lib

Modified Files:
	urllib2.py 
Log Message:
- urllib2.py now knows how to order proxy classes, so the user doesn't
  have to insert it in front of other classes, nor do dirty tricks like
  inserting a "dummy" HTTPHandler after a ProxyHandler when building an
  opener with proxy support.


Index: urllib2.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib2.py,v
retrieving revision 1.47
retrieving revision 1.48
diff -C2 -d -r1.47 -r1.48
*** urllib2.py	23 May 2003 08:51:51 -0000	1.47
--- urllib2.py	7 Jun 2003 17:53:07 -0000	1.48
***************
*** 268,271 ****
--- 268,272 ----
                  if protocol in self.handle_open:
                      self.handle_open[protocol].append(handler)
+                     self.handle_open[protocol].sort()
                  else:
                      self.handle_open[protocol] = [handler]
***************
*** 284,287 ****
--- 285,289 ----
                  if kind in dict:
                      dict[kind].append(handler)
+                     dict[kind].sort()
                  else:
                      dict[kind] = [handler]
***************
*** 291,294 ****
--- 293,297 ----
          if added:
              self.handlers.append(handler)
+             self.handlers.sort()
              handler.add_parent(self)
  
***************
*** 356,363 ****
              return self._call_chain(*args)
  
! # XXX probably also want an abstract factory that knows things like
! # the fact that a ProxyHandler needs to get inserted first.
! # would also know when it makes sense to skip a superclass in favor of
! # a subclass and when it might make sense to include both
  
  def build_opener(*handlers):
--- 359,365 ----
              return self._call_chain(*args)
  
! # XXX probably also want an abstract factory that knows when it makes
! # sense to skip a superclass in favor of a subclass and when it might
! # make sense to include both
  
  def build_opener(*handlers):
***************
*** 365,370 ****
  
      The opener will use several default handlers, including support
!     for HTTP and FTP.  If there is a ProxyHandler, it must be at the
!     front of the list of handlers.  (Yuck.)
  
      If any of the handlers passed as arguments are subclasses of the
--- 367,371 ----
  
      The opener will use several default handlers, including support
!     for HTTP and FTP.
  
      If any of the handlers passed as arguments are subclasses of the
***************
*** 399,406 ****
--- 400,417 ----
  
  class BaseHandler:
+     handler_order = 500
+ 
      def add_parent(self, parent):
          self.parent = parent
      def close(self):
          self.parent = None
+     def __lt__(self, other):
+         if not hasattr(other, "handler_order"):
+             # Try to preserve the old behavior of having custom classes
+             # inserted after default ones (works only for custom user
+             # classes which are not aware of handler_order).
+             return True
+         return self.handler_order < other.handler_order
+             
  
  class HTTPDefaultErrorHandler(BaseHandler):
***************
*** 474,477 ****
--- 485,491 ----
  
  class ProxyHandler(BaseHandler):
+     # Proxies must be in front
+     handler_order = 100
+ 
      def __init__(self, proxies=None):
          if proxies is None:
***************
*** 524,527 ****
--- 538,544 ----
  
  class CustomProxyHandler(BaseHandler):
+     # Proxies must be in front
+     handler_order = 100
+ 
      def __init__(self, *proxies):
          self.proxies = {}
***************
*** 1052,1062 ****
                          HTTPDefaultErrorHandler, HTTPRedirectHandler,
                          FTPHandler, FileHandler]
-     proxy_handlers = [ProxyHandler]
      handlers = []
      replacement_handlers = []
  
-     def add_proxy_handler(self, ph):
-         self.proxy_handlers = self.proxy_handlers + [ph]
- 
      def add_handler(self, h):
          self.handlers = self.handlers + [h]
--- 1069,1075 ----
***************
*** 1067,1071 ****
      def build_opener(self):
          opener = OpenerDirector()
!         for ph in self.proxy_handlers:
              if inspect.isclass(ph):
                  ph = ph()
--- 1080,1084 ----
      def build_opener(self):
          opener = OpenerDirector()
!         for ph in self.default_handlers:
              if inspect.isclass(ph):
                  ph = ph()