]> granicus.if.org Git - python/commitdiff
#1664522: in urllib, don't read non-existing directories in ftp mode,
authorGeorg Brandl <georg@python.org>
Sun, 20 Jan 2008 12:18:17 +0000 (12:18 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 20 Jan 2008 12:18:17 +0000 (12:18 +0000)
returning a 0-byte file -- raise an IOError instead.
Original patch from Phil Knirsch.

Lib/urllib.py
Misc/NEWS

index 2e720aca720c30b8ac843722f1846df99d2084ad..6608abc771e1216a7e88772e4c465f55373d6664 100644 (file)
@@ -872,9 +872,19 @@ class ftpwrapper:
         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, reason:
+                        raise IOError, ('ftp error', reason), sys.exc_info()[2]
+                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
index 7e4a12e56c341274d0b24c2320ced8917b03a48b..537535a1f86da7661d4e26186f6dbd4afaf73fc5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -369,6 +369,9 @@ Core and builtins
 Library
 -------
 
+- #1664522: in urllib, don't read non-existing directories in ftp mode,
+  returning a 0-byte file -- raise an IOError instead.
+
 - #856047: respect the ``no_proxy`` environment variable when using the
   ``http_proxy`` etc. environment variables in urllib.