*** urllib.py.org	2005-09-08 11:08:59.866913960 -0400
--- urllib.py	2005-09-08 12:27:19.054528856 -0400
***************
*** 27,32 ****
--- 27,33 ----
  import os
  import time
  import sys
+ import re
  
  __all__ = ["urlopen", "URLopener", "FancyURLopener", "urlretrieve",
             "urlcleanup", "quote", "quote_plus", "unquote", "unquote_plus",
***************
*** 1112,1131 ****
                 '0123456789' '_.-')
  
  _fast_safe_test = always_safe + '/'
! _fast_safe = None
  
! def _fast_quote(s):
!     global _fast_safe
!     if _fast_safe is None:
!         _fast_safe = {}
!         for c in _fast_safe_test:
!             _fast_safe[c] = c
!     res = list(s)
!     for i in range(len(res)):
!         c = res[i]
!         if not c in _fast_safe:
!             res[i] = '%%%02X' % ord(c)
!     return ''.join(res)
  
  def quote(s, safe = '/'):
      """quote('abc def') -> 'abc%20def'
--- 1113,1124 ----
                 '0123456789' '_.-')
  
  _fast_safe_test = always_safe + '/'
! _fast_safe = dict(zip(_fast_safe_test, _fast_safe_test))
! _must_quote = re.compile(r'[^%s]' % _fast_safe_test)
  
! for c in [chr(i) for i in range(256)]:
!     if c not in _fast_safe:
!         _fast_safe[c] = '%%%02X' % ord(c)
  
  def quote(s, safe = '/'):
      """quote('abc def') -> 'abc%20def'
***************
*** 1148,1156 ****
      called on a path where the existing slash characters are used as
      reserved characters.
      """
      safe = always_safe + safe
-     if _fast_safe_test == safe:
-         return _fast_quote(s)
      res = list(s)
      for i in range(len(res)):
          c = res[i]
--- 1141,1151 ----
      called on a path where the existing slash characters are used as
      reserved characters.
      """
+     if safe == '/': # optimize usual case
+         return (s and (not _must_quote.search(s)
+                         and s or ''.join(map(_fast_safe.get, s))
+                       ))
      safe = always_safe + safe
      res = list(s)
      for i in range(len(res)):
          c = res[i]
