Commits

Pierre-Yves David committed a9b1912

Improve iglob error handling.

iblog now raise ValueError when riche iglob are malformated.

Related test are included in this changeset.

  • Participants
  • Parent commits 3d3b78d

Comments (0)

Files changed (2)

File distutils2/tests/test_util.py

                  'Donotwant': False}
         self.assertGlobMatch(glob, spec)
 
+    def test_invalid_glob_pattern(self):
+        invalids = [
+            'ppooa**',
+            'azzaeaz4**/',
+            '/**ddsfs',
+            '**##1e"&e',
+            'DSFb**c009',
+            '{'
+            '{aaQSDFa'
+            '}'
+            'aQSDFSaa}'
+            '{**a,'
+            ',**a}'
+            '{a**,'
+            ',b**}'
+            '{a**a,babar}'
+            '{bob,b**z}'
+            ]
+        msg = "%r is not supposed to be a valid pattern"
+        for pattern in invalids:
+            try:
+                iglob(pattern)
+            except ValueError:
+                continue
+            else:
+                self.fail("%r is not a valid iglob pattern" % pattern)
+
+
 
 def test_suite():
     suite = unittest.makeSuite(UtilTestCase)

File distutils2/util.py

                         self.options, self.explicit)
 
 RICH_GLOB = re.compile(r'\{([^}]*)\}')
+_CHECK_RECURSIVE_GLOB = re.compile(r'[^/,{]\*\*|\*\*[^/,}]')
+_CHECK_MISMATCH_SET = re.compile(r'^[^{]*\}|\{[^}]*$')
+
 def iglob(path_glob):
     """Richer glob than the std glob module support ** and {opt1,opt2,opt3}"""
+    if _CHECK_RECURSIVE_GLOB.search(path_glob):
+        msg = """Invalid glob %r: Recursive glob "**" must be used alone"""
+        raise ValueError(msg % path_glob)
+    if _CHECK_MISMATCH_SET.search(path_glob):
+        msg = """Invalid glob %r: Mismatching set marker '{' or '}'"""
+        raise ValueError(msg % path_glob)
+    return _iglob(path_glob)
+
+
+def _iglob(path_glob):
+    """Actual logic of the iglob function"""
     rich_path_glob = RICH_GLOB.split(path_glob, 1)
     if len(rich_path_glob) > 1:
         assert len(rich_path_glob) == 3, rich_path_glob
         prefix, set, suffix = rich_path_glob
         for item in set.split(','):
-            for path in iglob( ''.join((prefix, item, suffix))):
+            for path in _iglob( ''.join((prefix, item, suffix))):
                 yield path
     else:
         if '**' not in path_glob:
                 radical = radical.lstrip('/')
             for (path, dir, files) in os.walk(prefix):
                 path = os.path.normpath(path)
-                for file in iglob(os.path.join(path, radical)):
+                for file in _iglob(os.path.join(path, radical)):
                    yield file