[Python-checkins] CVS: python/dist/src/Lib urllib.py,1.107,1.108

Martin v. L÷wis python-dev@python.org
Sun, 3 Dec 2000 10:30:13 -0800


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

Modified Files:
	urllib.py 
Log Message:
Convert Unicode strings to byte strings before passing them into specific
protocols. Closes bug #119822.


Index: urllib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib.py,v
retrieving revision 1.107
retrieving revision 1.108
diff -C2 -r1.107 -r1.108
*** urllib.py	2000/10/02 23:04:02	1.107
--- urllib.py	2000/12/03 18:30:10	1.108
***************
*** 27,34 ****
  import os
  import sys
  
  
- __version__ = '1.13'    # XXX This version is not always updated :-(
- 
  MAXFTPCACHE = 10        # Trim the ftp cache beyond this size
  
--- 27,34 ----
  import os
  import sys
+ import types
  
+ __version__ = '1.14'    # XXX This version is not always updated :-(
  
  MAXFTPCACHE = 10        # Trim the ftp cache beyond this size
  
***************
*** 137,157 ****
      def open(self, fullurl, data=None):
          """Use URLopener().open(file) instead of open(file, 'r')."""
!         fullurl = unwrap(fullurl)
          if self.tempcache and self.tempcache.has_key(fullurl):
              filename, headers = self.tempcache[fullurl]
              fp = open(filename, 'rb')
              return addinfourl(fp, headers, fullurl)
!         type, url = splittype(fullurl)
!         if not type:
!             type = 'file'
!         if self.proxies.has_key(type):
!             proxy = self.proxies[type]
!             type, proxyhost = splittype(proxy)
              host, selector = splithost(proxyhost)
              url = (host, fullurl) # Signal special case to open_*()
          else:
              proxy = None
!         name = 'open_' + type
!         self.type = type
          if '-' in name:
              # replace - with _
--- 137,157 ----
      def open(self, fullurl, data=None):
          """Use URLopener().open(file) instead of open(file, 'r')."""
!         fullurl = unwrap(toBytes(fullurl))
          if self.tempcache and self.tempcache.has_key(fullurl):
              filename, headers = self.tempcache[fullurl]
              fp = open(filename, 'rb')
              return addinfourl(fp, headers, fullurl)
!         urltype, url = splittype(fullurl)
!         if not urltype:
!             urltype = 'file'
!         if self.proxies.has_key(urltype):
!             proxy = self.proxies[urltype]
!             urltype, proxyhost = splittype(proxy)
              host, selector = splithost(proxyhost)
              url = (host, fullurl) # Signal special case to open_*()
          else:
              proxy = None
!         name = 'open_' + urltype
!         self.type = urltype
          if '-' in name:
              # replace - with _
***************
*** 184,188 ****
          """retrieve(url) returns (filename, None) for a local object
          or (tempfilename, headers) for a remote object."""
!         url = unwrap(url)
          if self.tempcache and self.tempcache.has_key(url):
              return self.tempcache[url]
--- 184,188 ----
          """retrieve(url) returns (filename, None) for a local object
          or (tempfilename, headers) for a remote object."""
!         url = unwrap(toBytes(url))
          if self.tempcache and self.tempcache.has_key(url):
              return self.tempcache[url]
***************
*** 239,243 ****
          import httplib
          user_passwd = None
!         if type(url) is type(""):
              host, selector = splithost(url)
              if host:
--- 239,243 ----
          import httplib
          user_passwd = None
!         if type(url) is types.StringType:
              host, selector = splithost(url)
              if host:
***************
*** 314,318 ****
              import httplib
              user_passwd = None
!             if type(url) is type(""):
                  host, selector = splithost(url)
                  if host:
--- 314,318 ----
              import httplib
              user_passwd = None
!             if type(url) in types.StringTypes:
                  host, selector = splithost(url)
                  if host:
***************
*** 852,855 ****
--- 852,866 ----
  # unquote('abc%20def') -> 'abc def'
  # quote('abc def') -> 'abc%20def')
+ 
+ def toBytes(url):
+     """toBytes(u"URL") --> 'URL'."""
+     # Most URL schemes require ASCII. If that changes, the conversion
+     # can be relaxed
+     if type(url) is types.UnicodeType:
+         try:
+             url = url.encode("ASCII")
+         except UnicodeError:
+             raise UnicodeError("URL "+repr(url)+" contains non-ASCII characters")
+     return url
  
  def unwrap(url):