[Python-checkins] cpython (2.7): Issue #16626: Fix infinite recursion in glob.glob() on Windows when the pattern

antoine.pitrou python-checkins at python.org
Sun Dec 16 13:57:34 CET 2012


http://hg.python.org/cpython/rev/b0935ef48186
changeset:   80882:b0935ef48186
branch:      2.7
parent:      80874:028ecc3900f5
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sun Dec 16 13:55:47 2012 +0100
summary:
  Issue #16626: Fix infinite recursion in glob.glob() on Windows when the pattern contains a wildcard in the drive or UNC path.
Patch by Serhiy Storchaka.

files:
  Lib/glob.py           |  5 ++++-
  Lib/test/test_glob.py |  9 +++++++++
  Misc/NEWS             |  4 ++++
  3 files changed, 17 insertions(+), 1 deletions(-)


diff --git a/Lib/glob.py b/Lib/glob.py
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -38,7 +38,10 @@
         for name in glob1(os.curdir, basename):
             yield name
         return
-    if has_magic(dirname):
+    # `os.path.split()` returns the argument itself as a dirname if it is a
+    # drive or UNC path.  Prevent an infinite recursion if a drive or UNC path
+    # contains magic characters (i.e. r'\\?\C:').
+    if dirname != pathname and has_magic(dirname):
         dirs = iglob(dirname)
     else:
         dirs = [dirname]
diff --git a/Lib/test/test_glob.py b/Lib/test/test_glob.py
--- a/Lib/test/test_glob.py
+++ b/Lib/test/test_glob.py
@@ -3,6 +3,7 @@
 import glob
 import os
 import shutil
+import sys
 
 
 class GlobTests(unittest.TestCase):
@@ -110,6 +111,14 @@
             eq(self.glob('sym1'), [self.norm('sym1')])
             eq(self.glob('sym2'), [self.norm('sym2')])
 
+    @unittest.skipUnless(sys.platform == "win32", "Win32 specific test")
+    def test_glob_magic_in_drive(self):
+        eq = self.assertSequencesEqual_noorder
+        eq(glob.glob('*:'), [])
+        eq(glob.glob(u'*:'), [])
+        eq(glob.glob('?:'), [])
+        eq(glob.glob(u'?:'), [])
+
 
 def test_main():
     run_unittest(GlobTests)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -160,6 +160,10 @@
 Library
 -------
 
+- Issue #16626: Fix infinite recursion in glob.glob() on Windows when the
+  pattern contains a wildcard in the drive or UNC path.  Patch by Serhiy
+  Storchaka.
+
 - Issue #16298: In HTTPResponse.read(), close the socket when there is no
   Content-Length and the incoming stream is finished.  Patch by Eran
   Rundstein.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list