Hi all,
I'd like to submit to patches to review that have been created in response
to internal bug reports/feature requests:
* argfix.patch
This one is rather trivial. It fixes a crash in preprocess_options when an
option that takes a value does not have one in the command line.
Alternatively, preprocess_options could just call sys.exit, but throwing a
normal exception seems cleaner.
* noclobber.patch
This one is rather large. It introduces a new warning for overwriting names
in (mostly erroneous) exception handlers of the style
try:
...
except KeyError, IOError:
...
In the current code, this creates (rather cryptic) error messages about
IOError being an invalid name for a local variable, and an inexperienced
programmer is more likely to silence it than to know the actual problem.
This warning is also silenced in the current patch.
Please tell me if I should submit the patch somewhere else, so that reviews
& updates are more convenient for you.
best,
// Torsten
--
Site Reliability Engineer ∘ Google ✚ ∘ ⬕
diff -r 43377258b765 lint.py
--- a/lint.py Mon Sep 05 09:43:46 2011 +0200
+++ b/lint.py Mon Sep 19 09:45:49 2011 +0200
@@ -687,6 +687,10 @@
__builtins__['_'] = str
+class ArgumentPreprocessingError(Exception):
+ """Raised if an error occurs during argument preprocessing."""
+
+
def preprocess_options(args, search_for):
"""look for some options (keys of <search_for>) which have to be processed
before others
@@ -706,6 +710,8 @@
cb, takearg = search_for[option]
del args[i]
if takearg and val is None:
+ if i >= len(args) or args[i].startswith('-'):
+ raise ArgumentPreprocessingError(arg)
val = args[i]
del args[i]
cb(option, val)
@@ -728,11 +734,16 @@
def __init__(self, args, reporter=None, exit=True):
self._rcfile = None
self._plugins = []
- preprocess_options(args, {
- # option: (callback, takearg)
- 'rcfile': (self.cb_set_rcfile, True),
- 'load-plugins': (self.cb_add_plugins, True),
- })
+ try:
+ preprocess_options(args, {
+ # option: (callback, takearg)
+ 'rcfile': (self.cb_set_rcfile, True),
+ 'load-plugins': (self.cb_add_plugins, True),
+ })
+ except ArgumentPreprocessingError, e:
+ print >> sys.stderr, 'Argument %s expects a value.' % (e.args[0],)
+ sys.exit(32)
+
self.linter = linter = self.LinterClass((
('rcfile',
{'action' : 'callback', 'callback' : lambda *args: 1,
diff -r 43377258b765 test/unittest_lint.py
--- a/test/unittest_lint.py Mon Sep 05 09:43:46 2011 +0200
+++ b/test/unittest_lint.py Mon Sep 19 09:45:49 2011 +0200
@@ -23,7 +23,8 @@
from logilab.common.compat import reload
from pylint import config
-from pylint.lint import PyLinter, Run, UnknownMessage
+from pylint.lint import PyLinter, Run, UnknownMessage, preprocess_options, \
+ ArgumentPreprocessingError
from pylint.utils import sort_msgs, PyLintASTWalker
from pylint import checkers
@@ -352,5 +353,31 @@
os.chdir(HERE)
rmtree(chroot)
+
+class PreprocessOptionsTC(TestCase):
+ def _callback(self, name, value):
+ self.args.append((name, value))
+
+ def test_preprocess(self):
+ self.args = []
+ preprocess_options(['--foo', '--bar=baz', '--qu=ux'],
+ {'foo' : (self._callback, False),
+ 'qu' : (self._callback, True)})
+ self.assertEqual(
+ [('foo', None), ('qu', 'ux')], self.args)
+
+ def test_preprocessing_error(self):
+ self.assertRaises(
+ ArgumentPreprocessingError,
+ preprocess_options,
+ ['--foo', '--bar', '--qu=ux'],
+ {'bar' : (None, True)})
+ self.assertRaises(
+ ArgumentPreprocessingError,
+ preprocess_options,
+ ['--foo', '--bar'],
+ {'bar' : (None, True)})
+
+
if __name__ == '__main__':
unittest_main()
_______________________________________________
Python-Projects mailing list
[email protected]
http://lists.logilab.org/mailman/listinfo/python-projects