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

Jeremy Hylton jhylton@users.sourceforge.net
Tue, 09 Oct 2001 09:18:47 -0700


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

Modified Files:
	urllib2.py 
Log Message:
Fix [ #465502 ] urllib2: urlopen unicode problem

When checking for strings use,
!         if isinstance(uri, (types.StringType, types.UnicodeType)):

Also get rid of some dodgy code that tried to guess whether attributes
were callable or not.



Index: urllib2.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib2.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** urllib2.py	2001/08/27 20:16:53	1.21
--- urllib2.py	2001/10/09 16:18:45	1.22
***************
*** 90,93 ****
--- 90,94 ----
  import socket
  import httplib
+ import inspect
  import re
  import base64
***************
*** 253,257 ****
      def add_handler(self, handler):
          added = 0
!         for meth in get_methods(handler):
              if meth[-5:] == '_open':
                  protocol = meth[:-5]
--- 254,258 ----
      def add_handler(self, handler):
          added = 0
!         for meth in dir(handler):
              if meth[-5:] == '_open':
                  protocol = meth[:-5]
***************
*** 304,308 ****
      def open(self, fullurl, data=None):
          # accept a URL or a Request object
!         if isinstance(fullurl, types.StringType):
              req = Request(fullurl, data)
          else:
--- 305,309 ----
      def open(self, fullurl, data=None):
          # accept a URL or a Request object
!         if isinstance(fullurl, (types.StringType, types.UnicodeType)):
              req = Request(fullurl, data)
          else:
***************
*** 347,378 ****
              return self._call_chain(*args)
  
- def is_callable(obj):
-     # not quite like builtin callable (which I didn't know existed),
-     # not entirely sure it needs to be different
-     if type(obj) in (types.BuiltinFunctionType,
-                      types.BuiltinMethodType,  types.LambdaType,
-                      types.MethodType):
-         return 1
-     if isinstance(obj, types.InstanceType):
-         return hasattr(obj, '__call__')
-     return 0
- 
- def get_methods(inst):
-     methods = {}
-     classes = []
-     classes.append(inst.__class__)
-     while classes:
-         klass = classes[0]
-         del classes[0]
-         classes = classes + list(klass.__bases__)
-         for name in dir(klass):
-             attr = getattr(klass, name)
-             if isinstance(attr, types.UnboundMethodType):
-                 methods[name] = 1
-     for name in dir(inst):
-         if is_callable(getattr(inst, name)):
-             methods[name] = 1
-     return methods.keys()
- 
  # XXX probably also want an abstract factory that knows things like
   # the fact that a ProxyHandler needs to get inserted first.
--- 348,351 ----
***************
*** 400,409 ****
      for klass in default_classes:
          for check in handlers:
!             if isinstance(check, types.ClassType):
                  if issubclass(check, klass):
-                     skip.append(klass)
-             elif isinstance(check, types.InstanceType):
-                 if isinstance(check, klass):
                      skip.append(klass)
      for klass in skip:
          default_classes.remove(klass)
--- 373,381 ----
      for klass in default_classes:
          for check in handlers:
!             if inspect.isclass(check):
                  if issubclass(check, klass):
                      skip.append(klass)
+             elif isinstance(check, klass):
+                 skip.append(klass)
      for klass in skip:
          default_classes.remove(klass)
***************
*** 413,417 ****
  
      for h in handlers:
!         if isinstance(h, types.ClassType):
              h = h()
          opener.add_handler(h)
--- 385,389 ----
  
      for h in handlers:
!         if inspect.isclass(h):
              h = h()
          opener.add_handler(h)
***************
*** 546,550 ****
      def add_password(self, realm, uri, user, passwd):
          # uri could be a single URI or a sequence
!         if isinstance(uri, types.StringType):
              uri = [uri]
          uri = tuple(map(self.reduce_uri, uri))
--- 518,522 ----
      def add_password(self, realm, uri, user, passwd):
          # uri could be a single URI or a sequence
!         if isinstance(uri, (types.StringType, types.UnicodeType)):
              uri = [uri]
          uri = tuple(map(self.reduce_uri, uri))
***************
*** 1068,1072 ****
          opener = OpenerDirector()
          for ph in self.proxy_handlers:
!             if isinstance(ph, types.ClassType):
                  ph = ph()
              opener.add_handler(ph)
--- 1040,1044 ----
          opener = OpenerDirector()
          for ph in self.proxy_handlers:
!             if inspect.isclass(ph):
                  ph = ph()
              opener.add_handler(ph)
***************
*** 1089,1120 ****
          'file:/etc/passwd',
          'file://nonsensename/etc/passwd',
!         'ftp://www.python.org/pub/tmp/httplib.py',
!         'ftp://www.python.org/pub/tmp/imageop.c',
          'ftp://www.python.org/pub/tmp/blat',
          'http://www.espn.com/', # redirect
          'http://www.python.org/Spanish/Inquistion/',
!         ('http://grail.cnri.reston.va.us/cgi-bin/faqw.py',
           'query=pythonistas&querytype=simple&casefold=yes&req=search'),
          'http://www.python.org/',
!         'ftp://prep.ai.mit.edu/welcome.msg',
!         'ftp://www.python.org/pub/tmp/figure.prn',
!         'ftp://www.python.org/pub/tmp/interp.pl',
!         'http://checkproxy.cnri.reston.va.us/test/test.html',
              ]
  
!     if localhost is not None:
!         urls = urls + [
!             'file://%s/etc/passwd' % localhost,
!             'http://%s/simple/' % localhost,
!             'http://%s/digest/' % localhost,
!             'http://%s/not/found.h' % localhost,
!             ]
  
!         bauth = HTTPBasicAuthHandler()
!         bauth.add_password('basic_test_realm', localhost, 'jhylton',
!                            'password')
!         dauth = HTTPDigestAuthHandler()
!         dauth.add_password('digest_test_realm', localhost, 'jhylton',
!                            'password')
  
  
--- 1061,1088 ----
          'file:/etc/passwd',
          'file://nonsensename/etc/passwd',
!         'ftp://www.python.org/pub/python/misc/sousa.au',
          'ftp://www.python.org/pub/tmp/blat',
          'http://www.espn.com/', # redirect
          'http://www.python.org/Spanish/Inquistion/',
!         ('http://www.python.org/cgi-bin/faqw.py',
           'query=pythonistas&querytype=simple&casefold=yes&req=search'),
          'http://www.python.org/',
!         'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC/research-reports/00README-Legal-Rules-Regs',
              ]
  
! ##    if localhost is not None:
! ##        urls = urls + [
! ##            'file://%s/etc/passwd' % localhost,
! ##            'http://%s/simple/' % localhost,
! ##            'http://%s/digest/' % localhost,
! ##            'http://%s/not/found.h' % localhost,
! ##            ]
  
! ##        bauth = HTTPBasicAuthHandler()
! ##        bauth.add_password('basic_test_realm', localhost, 'jhylton',
! ##                           'password')
! ##        dauth = HTTPDigestAuthHandler()
! ##        dauth.add_password('digest_test_realm', localhost, 'jhylton',
! ##                           'password')
  
  
***************
*** 1122,1135 ****
      cfh.setTimeout(1)
  
!     # XXX try out some custom proxy objects too!
!     def at_cnri(req):
!         host = req.get_host()
!         print host
!         if host[-18:] == '.cnri.reston.va.us':
!             return 1
!     p = CustomProxy('http', at_cnri, 'proxy.cnri.reston.va.us')
!     ph = CustomProxyHandler(p)
  
!     #install_opener(build_opener(dauth, bauth, cfh, GopherHandler, ph))
  
      for url in urls:
--- 1090,1104 ----
      cfh.setTimeout(1)
  
! ##    # XXX try out some custom proxy objects too!
! ##    def at_cnri(req):
! ##        host = req.get_host()
! ##        print host
! ##        if host[-18:] == '.cnri.reston.va.us':
! ##            return 1
! ##    p = CustomProxy('http', at_cnri, 'proxy.cnri.reston.va.us')
! ##    ph = CustomProxyHandler(p)
  
! ##    install_opener(build_opener(dauth, bauth, cfh, GopherHandler, ph))
!     install_opener(build_opener(cfh, GopherHandler))
  
      for url in urls: