[Python-checkins] distutils2: Improve iglob error handling.
tarek.ziade
python-checkins at python.org
Wed Feb 16 22:23:58 CET 2011
tarek.ziade pushed a9b19122fb32 to distutils2:
http://hg.python.org/distutils2/rev/a9b19122fb32
changeset: 1082:a9b19122fb32
user: Pierre-Yves David <pierre-yves.david at ens-lyon.org>
date: Sat Feb 05 02:58:54 2011 +0100
summary:
Improve iglob error handling.
iblog now raise ValueError when riche iglob are malformated.
Related test are included in this changeset.
files:
distutils2/tests/test_util.py
distutils2/util.py
diff --git a/distutils2/tests/test_util.py b/distutils2/tests/test_util.py
--- a/distutils2/tests/test_util.py
+++ b/distutils2/tests/test_util.py
@@ -625,6 +625,34 @@
'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
--- a/distutils2/util.py
+++ b/distutils2/util.py
@@ -1009,14 +1009,28 @@
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 @@
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
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list