summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre-Yves David <pierre-yves.david@ens-lyon.org>2011-02-05 02:58:54 +0100
committerPierre-Yves David <pierre-yves.david@ens-lyon.org>2011-02-05 02:58:54 +0100
commit16a5e7d8ffa8e9a0bcac2515ca53a39bd10ef41f (patch)
tree091382be1b72fe98e3f61129c5dd8b832bbbeb00
parentb61ddb40d5c3bff1f97a6b6e5c113c56d9201212 (diff)
downloaddisutils2-16a5e7d8ffa8e9a0bcac2515ca53a39bd10ef41f.tar.gz
Improve iglob error handling.
iblog now raise ValueError when riche iglob are malformated. Related test are included in this changeset.
-rw-r--r--distutils2/tests/test_util.py28
-rw-r--r--distutils2/util.py18
2 files changed, 44 insertions, 2 deletions
diff --git a/distutils2/tests/test_util.py b/distutils2/tests/test_util.py
index 582e24c..e47bc0f 100644
--- a/distutils2/tests/test_util.py
+++ b/distutils2/tests/test_util.py
@@ -625,6 +625,34 @@ class GlobTestCase(GlobTestCaseBase):
'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)
diff --git a/distutils2/util.py b/distutils2/util.py
index 498e04e..6c51774 100644
--- a/distutils2/util.py
+++ b/distutils2/util.py
@@ -1009,14 +1009,28 @@ class Mixin2to3:
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:
@@ -1032,7 +1046,7 @@ def iglob(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