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

Guido van Rossum python-dev@python.org
Fri, 15 Dec 2000 07:01:40 -0800


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

Modified Files:
	urllib.py 
Log Message:
Get rid of string functions, except maketrans() (which is *not*
obsolete!).

Fix a bug in ftpwrapper.retrfile() where somehow ftplib.error_perm was
assumed to be a string.  (The fix applies str().)

Also break some long lines and change the output from test() slightly.


Index: urllib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib.py,v
retrieving revision 1.108
retrieving revision 1.109
diff -C2 -r1.108 -r1.109
*** urllib.py	2000/12/03 18:30:10	1.108
--- urllib.py	2000/12/15 15:01:37	1.109
***************
*** 29,33 ****
  import types
  
! __version__ = '1.14'    # XXX This version is not always updated :-(
  
  MAXFTPCACHE = 10        # Trim the ftp cache beyond this size
--- 29,33 ----
  import types
  
! __version__ = '1.15'    # XXX This version is not always updated :-(
  
  MAXFTPCACHE = 10        # Trim the ftp cache beyond this size
***************
*** 156,160 ****
          if '-' in name:
              # replace - with _
!             name = string.join(string.split(name, '-'), '_')
          if not hasattr(self, name):
              if proxy:
--- 156,160 ----
          if '-' in name:
              # replace - with _
!             name = '_'.join(name.split('-'))
          if not hasattr(self, name):
              if proxy:
***************
*** 250,254 ****
              url = rest
              user_passwd = None
!             if string.lower(urltype) != 'http':
                  realhost = None
              else:
--- 250,254 ----
              url = rest
              user_passwd = None
!             if urltype.lower() != 'http':
                  realhost = None
              else:
***************
*** 262,266 ****
          if user_passwd:
              import base64
!             auth = string.strip(base64.encodestring(user_passwd))
          else:
              auth = None
--- 262,266 ----
          if user_passwd:
              import base64
!             auth = base64.encodestring(user_passwd).strip()
          else:
              auth = None
***************
*** 325,329 ****
                  url = rest
                  user_passwd = None
!                 if string.lower(urltype) != 'https':
                      realhost = None
                  else:
--- 325,329 ----
                  url = rest
                  user_passwd = None
!                 if urltype.lower() != 'https':
                      realhost = None
                  else:
***************
*** 337,341 ****
              if user_passwd:
                  import base64
!                 auth = string.strip(base64.encodestring(user_passwd))
              else:
                  auth = None
--- 337,341 ----
              if user_passwd:
                  import base64
!                 auth = base64.encodestring(user_passwd).strip()
              else:
                  auth = None
***************
*** 364,368 ****
                      return self.http_error(url, fp, errcode, errmsg, headers)
                  else:
!                     return self.http_error(url, fp, errcode, errmsg, headers, data)
  
      def open_gopher(self, url):
--- 364,369 ----
                      return self.http_error(url, fp, errcode, errmsg, headers)
                  else:
!                     return self.http_error(url, fp, errcode, errmsg, headers,
!                                            data)
  
      def open_gopher(self, url):
***************
*** 431,439 ****
          path, attrs = splitattr(path)
          path = unquote(path)
!         dirs = string.splitfields(path, '/')
          dirs, file = dirs[:-1], dirs[-1]
          if dirs and not dirs[0]: dirs = dirs[1:]
          if dirs and not dirs[0]: dirs[0] = '/'
!         key = user, host, port, string.join(dirs, '/')
          # XXX thread unsafe!
          if len(self.ftpcache) > MAXFTPCACHE:
--- 432,440 ----
          path, attrs = splitattr(path)
          path = unquote(path)
!         dirs = path.split('/')
          dirs, file = dirs[:-1], dirs[-1]
          if dirs and not dirs[0]: dirs = dirs[1:]
          if dirs and not dirs[0]: dirs[0] = '/'
!         key = user, host, port, '/'.join(dirs)
          # XXX thread unsafe!
          if len(self.ftpcache) > MAXFTPCACHE:
***************
*** 452,458 ****
              for attr in attrs:
                  attr, value = splitvalue(attr)
!                 if string.lower(attr) == 'type' and \
                     value in ('a', 'A', 'i', 'I', 'd', 'D'):
!                     type = string.upper(value)
              (fp, retrlen) = self.ftpcache[key].retrfile(file, type)
              if retrlen is not None and retrlen >= 0:
--- 453,459 ----
              for attr in attrs:
                  attr, value = splitvalue(attr)
!                 if attr.lower() == 'type' and \
                     value in ('a', 'A', 'i', 'I', 'd', 'D'):
!                     type = value.upper()
              (fp, retrlen) = self.ftpcache[key].retrfile(file, type)
              if retrlen is not None and retrlen >= 0:
***************
*** 477,486 ****
          import StringIO, mimetools, time
          try:
!             [type, data] = string.split(url, ',', 1)
          except ValueError:
              raise IOError, ('data error', 'bad data URL')
          if not type:
              type = 'text/plain;charset=US-ASCII'
!         semi = string.rfind(type, ';')
          if semi >= 0 and '=' not in type[semi:]:
              encoding = type[semi+1:]
--- 478,487 ----
          import StringIO, mimetools, time
          try:
!             [type, data] = url.split(',', 1)
          except ValueError:
              raise IOError, ('data error', 'bad data URL')
          if not type:
              type = 'text/plain;charset=US-ASCII'
!         semi = type.rfind(';')
          if semi >= 0 and '=' not in type[semi:]:
              encoding = type[semi+1:]
***************
*** 500,504 ****
          msg.append('')
          msg.append(data)
!         msg = string.join(msg, '\n')
          f = StringIO.StringIO(msg)
          headers = mimetools.Message(f, 0)
--- 501,505 ----
          msg.append('')
          msg.append(data)
!         msg = '\n'.join(msg)
          f = StringIO.StringIO(msg)
          headers = mimetools.Message(f, 0)
***************
*** 550,554 ****
              if match:
                  scheme, realm = match.groups()
!                 if string.lower(scheme) == 'basic':
                     name = 'retry_' + self.type + '_basic_auth'
                     if data is None:
--- 551,555 ----
              if match:
                  scheme, realm = match.groups()
!                 if scheme.lower() == 'basic':
                     name = 'retry_' + self.type + '_basic_auth'
                     if data is None:
***************
*** 559,563 ****
      def retry_http_basic_auth(self, url, realm, data=None):
          host, selector = splithost(url)
!         i = string.find(host, '@') + 1
          host = host[i:]
          user, passwd = self.get_user_passwd(host, realm, i)
--- 560,564 ----
      def retry_http_basic_auth(self, url, realm, data=None):
          host, selector = splithost(url)
!         i = host.find('@') + 1
          host = host[i:]
          user, passwd = self.get_user_passwd(host, realm, i)
***************
*** 572,576 ****
      def retry_https_basic_auth(self, url, realm, data=None):
              host, selector = splithost(url)
!             i = string.find(host, '@') + 1
              host = host[i:]
              user, passwd = self.get_user_passwd(host, realm, i)
--- 573,577 ----
      def retry_https_basic_auth(self, url, realm, data=None):
              host, selector = splithost(url)
!             i = host.find('@') + 1
              host = host[i:]
              user, passwd = self.get_user_passwd(host, realm, i)
***************
*** 581,585 ****
  
      def get_user_passwd(self, host, realm, clear_cache = 0):
!         key = realm + '@' + string.lower(host)
          if self.auth_cache.has_key(key):
              if clear_cache:
--- 582,586 ----
  
      def get_user_passwd(self, host, realm, clear_cache = 0):
!         key = realm + '@' + host.lower()
          if self.auth_cache.has_key(key):
              if clear_cache:
***************
*** 690,694 ****
                  conn = self.ftp.ntransfercmd(cmd)
              except ftplib.error_perm, reason:
!                 if reason[:3] != '550':
                      raise IOError, ('ftp error', reason), sys.exc_info()[2]
          if not conn:
--- 691,695 ----
                  conn = self.ftp.ntransfercmd(cmd)
              except ftplib.error_perm, reason:
!                 if str(reason)[:3] != '550':
                      raise IOError, ('ftp error', reason), sys.exc_info()[2]
          if not conn:
***************
*** 806,810 ****
          else:
              # else replace last component
!             i = string.rfind(basepath, '/')
          if i < 0:
              # basepath not absolute
--- 807,811 ----
          else:
              # else replace last component
!             i = basepath.rfind('/')
          if i < 0:
              # basepath not absolute
***************
*** 821,825 ****
          while basepath and path[:3] == '../':
              path = path[3:]
!             i = string.rfind(basepath[:-1], '/')
              if i > 0:
                  basepath = basepath[:i+1]
--- 822,826 ----
          while basepath and path[:3] == '../':
              path = path[3:]
!             i = basepath[:-1].rfind('/')
              if i > 0:
                  basepath = basepath[:i+1]
***************
*** 861,873 ****
              url = url.encode("ASCII")
          except UnicodeError:
!             raise UnicodeError("URL "+repr(url)+" contains non-ASCII characters")
      return url
  
  def unwrap(url):
      """unwrap('<URL:type://host/path>') --> 'type://host/path'."""
!     url = string.strip(url)
      if url[:1] == '<' and url[-1:] == '>':
!         url = string.strip(url[1:-1])
!     if url[:4] == 'URL:': url = string.strip(url[4:])
      return url
  
--- 862,875 ----
              url = url.encode("ASCII")
          except UnicodeError:
!             raise UnicodeError("URL " + repr(url) +
!                                " contains non-ASCII characters")
      return url
  
  def unwrap(url):
      """unwrap('<URL:type://host/path>') --> 'type://host/path'."""
!     url = url.strip()
      if url[:1] == '<' and url[-1:] == '>':
!         url = url[1:-1].strip()
!     if url[:4] == 'URL:': url = url[4:].strip()
      return url
  
***************
*** 950,956 ****
          host, port = match.group(1, 2)
          try:
!             if not port: raise string.atoi_error, "no digits"
!             nport = string.atoi(port)
!         except string.atoi_error:
              nport = None
          return host, nport
--- 952,958 ----
          host, port = match.group(1, 2)
          try:
!             if not port: raise ValueError, "no digits"
!             nport = int(port)
!         except ValueError:
              nport = None
          return host, nport
***************
*** 984,988 ****
      """splitattr('/path;attr1=value1;attr2=value2;...') ->
          '/path', ['attr1=value1', 'attr2=value2', ...]."""
!     words = string.splitfields(url, ';')
      return words[0], words[1:]
  
--- 986,990 ----
      """splitattr('/path;attr1=value1;attr2=value2;...') ->
          '/path', ['attr1=value1', 'attr2=value2', ...]."""
!     words = url.split(';')
      return words[0], words[1:]
  
***************
*** 1008,1013 ****
      """unquote('abc%20def') -> 'abc def'."""
      mychr = chr
!     myatoi = string.atoi
!     list = string.split(s, '%')
      res = [list[0]]
      myappend = res.append
--- 1010,1015 ----
      """unquote('abc%20def') -> 'abc def'."""
      mychr = chr
!     myatoi = int
!     list = s.split('%')
      res = [list[0]]
      myappend = res.append
***************
*** 1022,1026 ****
          else:
              myappend('%' + item)
!     return string.join(res, "")
  
  def unquote_plus(s):
--- 1024,1028 ----
          else:
              myappend('%' + item)
!     return "".join(res)
  
  def unquote_plus(s):
***************
*** 1028,1032 ****
      if '+' in s:
          # replace '+' with ' '
!         s = string.join(string.split(s, '+'), ' ')
      return unquote(s)
  
--- 1030,1034 ----
      if '+' in s:
          # replace '+' with ' '
!         s = ' '.join(s.split('+'))
      return unquote(s)
  
***************
*** 1049,1053 ****
          if not _fast_safe.has_key(c):
              res[i] = '%%%02x' % ord(c)
!     return string.join(res, '')
  
  def quote(s, safe = '/'):
--- 1051,1055 ----
          if not _fast_safe.has_key(c):
              res[i] = '%%%02x' % ord(c)
!     return ''.join(res)
  
  def quote(s, safe = '/'):
***************
*** 1080,1092 ****
          if c not in safe:
              res[i] = '%%%02x' % ord(c)
!     return string.join(res, '')
  
  def quote_plus(s, safe = ''):
      """Quote the query fragment of a URL; replacing ' ' with '+'"""
      if ' ' in s:
!         l = string.split(s, ' ')
          for i in range(len(l)):
              l[i] = quote(l[i], safe)
!         return string.join(l, '+')
      else:
          return quote(s, safe)
--- 1082,1094 ----
          if c not in safe:
              res[i] = '%%%02x' % ord(c)
!     return ''.join(res)
  
  def quote_plus(s, safe = ''):
      """Quote the query fragment of a URL; replacing ' ' with '+'"""
      if ' ' in s:
!         l = s.split(' ')
          for i in range(len(l)):
              l[i] = quote(l[i], safe)
!         return '+'.join(l)
      else:
          return quote(s, safe)
***************
*** 1099,1103 ****
          v = quote_plus(str(v))
          l.append(k + '=' + v)
!     return string.join(l, '&')
  
  # Proxy handling
--- 1101,1105 ----
          v = quote_plus(str(v))
          l.append(k + '=' + v)
!     return '&'.join(l)
  
  # Proxy handling
***************
*** 1113,1117 ****
      proxies = {}
      for name, value in os.environ.items():
!         name = string.lower(name)
          if value and name[-6:] == '_proxy':
              proxies[name[:-6]] = value
--- 1115,1119 ----
      proxies = {}
      for name, value in os.environ.items():
!         name = name.lower()
          if value and name[-6:] == '_proxy':
              proxies[name[:-6]] = value
***************
*** 1224,1228 ****
  def reporthook(blocknum, blocksize, totalsize):
      # Report during remote transfers
!     print "Block number: %d, Block size: %d, Total size: %d" % (blocknum, blocksize, totalsize)
  
  # Test program
--- 1226,1231 ----
  def reporthook(blocknum, blocksize, totalsize):
      # Report during remote transfers
!     print "Block number: %d, Block size: %d, Total size: %d" % (
!         blocknum, blocksize, totalsize)
  
  # Test program
***************
*** 1243,1247 ****
              print '-'*10, url, '-'*10
              fn, h = urlretrieve(url, None, reporthook)
!             print fn, h
              if h:
                  print '======'
--- 1246,1250 ----
              print '-'*10, url, '-'*10
              fn, h = urlretrieve(url, None, reporthook)
!             print fn
              if h:
                  print '======'
***************
*** 1253,1257 ****
              if '\r' in data:
                  table = string.maketrans("", "")
!                 data = string.translate(data, table, "\r")
              print data
              fn, h = None, None
--- 1256,1260 ----
              if '\r' in data:
                  table = string.maketrans("", "")
!                 data = data.translate(table, "\r")
              print data
              fn, h = None, None