Merged revisions 60124-60142 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r60131 | georg.brandl | 2008-01-20 12:13:29 +0100 (Sun, 20 Jan 2008) | 3 lines

  #1351692: in pprint, always call format() for dict and list items to enable
  custom formatting of contents via subclassing PrettyPrinter.
........
  r60133 | georg.brandl | 2008-01-20 12:43:03 +0100 (Sun, 20 Jan 2008) | 2 lines

  #1178141: add addinfourl.code to get http status code from urllib.
........
  r60134 | georg.brandl | 2008-01-20 13:05:43 +0100 (Sun, 20 Jan 2008) | 4 lines

  #856047: respect the ``no_proxy`` env var when checking for proxies
  in urllib and using the other ``_proxy`` env vars.
  Original patch by Donovan Baarda.
........
  r60135 | georg.brandl | 2008-01-20 13:18:17 +0100 (Sun, 20 Jan 2008) | 4 lines

  #1664522: in urllib, don't read non-existing directories in ftp mode,
  returning a 0-byte file -- raise an IOError instead.
  Original patch from Phil Knirsch.
........
  r60136 | georg.brandl | 2008-01-20 13:57:47 +0100 (Sun, 20 Jan 2008) | 2 lines

  #799369: document possible sys.platform values.
........
  r60137 | georg.brandl | 2008-01-20 14:08:37 +0100 (Sun, 20 Jan 2008) | 2 lines

  #652749: document the constants added to the builtins by site.py.
........
  r60138 | georg.brandl | 2008-01-20 14:59:46 +0100 (Sun, 20 Jan 2008) | 2 lines

  #1648: add sys.gettrace() and sys.getprofile().
........
  r60139 | georg.brandl | 2008-01-20 15:17:42 +0100 (Sun, 20 Jan 2008) | 2 lines

  #1669: don't allow shutil.rmtree() to be called on a symlink.
........
  r60140 | georg.brandl | 2008-01-20 15:20:02 +0100 (Sun, 20 Jan 2008) | 2 lines

  Fix test_pyclbr after urllib change.
........
  r60141 | christian.heimes | 2008-01-20 15:28:28 +0100 (Sun, 20 Jan 2008) | 1 line

  Fixed a wrong assumption in configure.in and Include/pyport.h. The is finite function is not called isfinite() but finite(). Sorry, my fault. :)
........
  r60142 | georg.brandl | 2008-01-20 15:31:27 +0100 (Sun, 20 Jan 2008) | 2 lines

  #1876: fix typos in test_operator.
........
diff --git a/Lib/shutil.py b/Lib/shutil.py
index f3d6fa9..c365ad6 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -156,6 +156,14 @@
     elif onerror is None:
         def onerror(*args):
             raise
+    try:
+        if os.path.islink(path):
+            # symlinks to directories are forbidden, see bug #1669
+            raise OSError("Cannot call rmtree on a symbolic link")
+    except OSError:
+        onerror(os.path.islink, path, sys.exc_info())
+        # can't continue even if onerror hook returns
+        return
     names = []
     try:
         names = os.listdir(path)
diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py
index 57f3846..dbb8f9e 100644
--- a/Lib/test/test_operator.py
+++ b/Lib/test/test_operator.py
@@ -364,9 +364,9 @@
         self.assertRaises(TypeError, operator.attrgetter('x', (), 'y'), record)
 
         class C(object):
-            def __getattr(self, name):
+            def __getattr__(self, name):
                 raise SyntaxError
-        self.failUnlessRaises(AttributeError, operator.attrgetter('foo'), C())
+        self.failUnlessRaises(SyntaxError, operator.attrgetter('foo'), C())
 
     def test_itemgetter(self):
         a = 'ABCDE'
@@ -376,9 +376,9 @@
         self.assertRaises(IndexError, f, a)
 
         class C(object):
-            def __getitem(self, name):
+            def __getitem__(self, name):
                 raise SyntaxError
-        self.failUnlessRaises(TypeError, operator.itemgetter(42), C())
+        self.failUnlessRaises(SyntaxError, operator.itemgetter(42), C())
 
         f = operator.itemgetter('name')
         self.assertRaises(TypeError, f, a)
diff --git a/Lib/test/test_profilehooks.py b/Lib/test/test_profilehooks.py
index 3a17dc7..6d6aa8e 100644
--- a/Lib/test/test_profilehooks.py
+++ b/Lib/test/test_profilehooks.py
@@ -4,6 +4,22 @@
 
 from test import test_support
 
+class TestGetProfile(unittest.TestCase):
+    def setUp(self):
+        sys.setprofile(None)
+
+    def tearDown(self):
+        sys.setprofile(None)
+
+    def test_empty(self):
+        assert sys.getprofile() == None
+
+    def test_setget(self):
+        def fn(*args):
+            pass
+
+        sys.setprofile(fn)
+        assert sys.getprofile() == fn
 
 class HookWatcher:
     def __init__(self):
@@ -359,6 +375,7 @@
 
 def test_main():
     test_support.run_unittest(
+        TestGetProfile,
         ProfileHookTestCase,
         ProfileSimulatorTestCase
     )
diff --git a/Lib/test/test_pyclbr.py b/Lib/test/test_pyclbr.py
index 5d46db1..f041823 100644
--- a/Lib/test/test_pyclbr.py
+++ b/Lib/test/test_pyclbr.py
@@ -158,6 +158,7 @@
         cm('cgi', ignore=('log',))      # set with = in module
         cm('mhlib')
         cm('urllib', ignore=('getproxies_registry',
+                             'proxy_bypass_registry',
                              'open_https',
                              '_https_connection',
                              'getproxies_internetconfig',)) # not on all platforms
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index ee15d0e..b63031e 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -149,6 +149,20 @@
                 except OSError:
                     pass
 
+        def test_rmtree_on_symlink(self):
+            # bug 1669.
+            os.mkdir(TESTFN)
+            try:
+                src = os.path.join(TESTFN, 'cheese')
+                dst = os.path.join(TESTFN, 'shop')
+                os.mkdir(src)
+                os.symlink(src, dst)
+                self.assertRaises(OSError, shutil.rmtree, dst)
+            finally:
+                shutil.rmtree(TESTFN, ignore_errors=True)
+
+
+
 def test_main():
     test_support.run_unittest(TestShutil)
 
diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py
index 3ec00d2..a0f76c6 100644
--- a/Lib/test/test_trace.py
+++ b/Lib/test/test_trace.py
@@ -268,6 +268,20 @@
         self.compare_events(func.__code__.co_firstlineno,
                             tracer.events, func.events)
 
+    def set_and_retrieve_none(self):
+        sys.settrace(None)
+        assert sys.gettrace() is None
+
+    def set_and_retrieve_func(self):
+        def fn(*args):
+            pass
+
+        sys.settrace(fn)
+        try:
+            assert sys.gettrace() is fn
+        finally:
+            sys.settrace(None)
+
     def test_01_basic(self):
         self.run_test(basic)
     def test_02_arigo(self):
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index a87ab71..c233e35 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -47,7 +47,7 @@
     def test_interface(self):
         # Make sure object returned by urlopen() has the specified methods
         for attr in ("read", "readline", "readlines", "fileno",
-                     "close", "info", "geturl", "__iter__"):
+                     "close", "info", "geturl", "getcode", "__iter__"):
             self.assert_(hasattr(self.returned_obj, attr),
                          "object returned by urlopen() lacks %s attribute" %
                          attr)
@@ -87,6 +87,9 @@
     def test_geturl(self):
         self.assertEqual(self.returned_obj.geturl(), self.pathname)
 
+    def test_getcode(self):
+        self.assertEqual(self.returned_obj.getcode(), None)
+
     def test_iter(self):
         # Test iterator
         # Don't need to count number of iterations since test would fail the
@@ -123,6 +126,8 @@
             fp = urllib.urlopen("http://python.org/")
             self.assertEqual(fp.readline(), b"Hello!")
             self.assertEqual(fp.readline(), b"")
+            self.assertEqual(fp.geturl(), 'http://python.org/')
+            self.assertEqual(fp.getcode(), 200)
         finally:
             self.unfakehttp()
 
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py
index ebd9d2d..a7d3805 100644
--- a/Lib/test/test_urllibnet.py
+++ b/Lib/test/test_urllibnet.py
@@ -83,6 +83,16 @@
             open_url.close()
         self.assertEqual(gotten_url, URL)
 
+    def test_getcode(self):
+        # test getcode() with the fancy opener to get 404 error codes
+        URL = "http://www.python.org/XXXinvalidXXX"
+        open_url = urllib.FancyURLopener().open(URL)
+        try:
+            code = open_url.getcode()
+        finally:
+            open_url.close()
+        self.assertEqual(code, 404)
+
     def test_fileno(self):
         if (sys.platform in ('win32',) or
                 not hasattr(os, 'fdopen')):
diff --git a/Lib/urllib.py b/Lib/urllib.py
index 1b1fc40..f9c8ec0 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -557,7 +557,7 @@
 
     def http_error_default(self, url, fp, errcode, errmsg, headers):
         """Default error handling -- don't raise an exception."""
-        return addinfourl(fp, headers, "http:" + url)
+        return addinfourl(fp, headers, "http:" + url, errcode)
 
     def http_error_302(self, url, fp, errcode, errmsg, headers, data=None):
         """Error 302 -- relocated (temporarily)."""
@@ -815,9 +815,19 @@
         if not conn:
             # Set transfer mode to ASCII!
             self.ftp.voidcmd('TYPE A')
-            # Try a directory listing
-            if file: cmd = 'LIST ' + file
-            else: cmd = 'LIST'
+            # Try a directory listing. Verify that directory exists.
+            if file:
+                pwd = self.ftp.pwd()
+                try:
+                    try:
+                        self.ftp.cwd(file)
+                    except ftplib.error_perm as reason:
+                        raise IOError('ftp error', reason) from reason
+                finally:
+                    self.ftp.cwd(pwd)
+                cmd = 'LIST ' + file
+            else:
+                cmd = 'LIST'
             conn = self.ftp.ntransfercmd(cmd)
         self.busy = 1
         # Pass back both a suitably decorated object and a retrieval length
@@ -898,14 +908,18 @@
 class addinfourl(addbase):
     """class to add info() and geturl() methods to an open file."""
 
-    def __init__(self, fp, headers, url):
+    def __init__(self, fp, headers, url, code=None):
         addbase.__init__(self, fp)
         self.headers = headers
         self.url = url
+        self.code = code
 
     def info(self):
         return self.headers
 
+    def getcode(self):
+        return self.code
+
     def geturl(self):
         return self.url
 
@@ -1228,10 +1242,33 @@
     proxies = {}
     for name, value in os.environ.items():
         name = name.lower()
+        if name == 'no_proxy':
+            # handled in proxy_bypass_environment
+            continue
         if value and name[-6:] == '_proxy':
             proxies[name[:-6]] = value
     return proxies
 
+def proxy_bypass_environment(host):
+    """Test if proxies should not be used for a particular host.
+
+    Checks the environment for a variable named no_proxy, which should
+    be a list of DNS suffixes separated by commas, or '*' for all hosts.
+    """
+    no_proxy = os.environ.get('no_proxy', '') or os.environ.get('NO_PROXY', '')
+    # '*' is special case for always bypass
+    if no_proxy == '*':
+        return 1
+    # strip port off host
+    hostonly, port = splitport(host)
+    # check if the host ends with any of the DNS suffixes
+    for name in no_proxy.split(','):
+        if name and (hostonly.endswith(name) or host.endswith(name)):
+            return 1
+    # otherwise, don't bypass
+    return 0
+
+
 if sys.platform == 'darwin':
     def getproxies_internetconfig():
         """Return a dictionary of scheme -> proxy server URL mappings.
@@ -1259,12 +1296,15 @@
                 pass
             else:
                 proxies['http'] = 'http://%s' % value
-        # FTP: XXXX To be done.
-        # Gopher: XXXX To be done.
+        # FTP: XXX To be done.
+        # Gopher: XXX To be done.
         return proxies
 
-    def proxy_bypass(x):
-        return 0
+    def proxy_bypass(host):
+        if getproxies_environment():
+            return proxy_bypass_environment(host)
+        else:
+            return 0
 
     def getproxies():
         return getproxies_environment() or getproxies_internetconfig()
@@ -1324,7 +1364,7 @@
         """
         return getproxies_environment() or getproxies_registry()
 
-    def proxy_bypass(host):
+    def proxy_bypass_registry(host):
         try:
             import _winreg
             import re
@@ -1383,12 +1423,22 @@
                     return 1
         return 0
 
+    def proxy_bypass(host):
+        """Return a dictionary of scheme -> proxy server URL mappings.
+
+        Returns settings gathered from the environment, if specified,
+        or the registry.
+
+        """
+        if getproxies_environment():
+            return proxy_bypass_environment(host)
+        else:
+            return proxy_bypass_registry(host)
+
 else:
     # By default use environment variables
     getproxies = getproxies_environment
-
-    def proxy_bypass(host):
-        return 0
+    proxy_bypass = proxy_bypass_environment
 
 # Test and time quote() and unquote()
 def test1():