summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTorsten Marek <shlomme@gmail.com>2011-09-23 17:01:32 +0200
committerTorsten Marek <shlomme@gmail.com>2011-09-23 17:01:32 +0200
commit779e2c4078d5f6ee0a319a9807b215e775411426 (patch)
tree7dc61178a94c01e978185a7d830edd01961b6d80
parente62152e132512e83b301e955494e255f9193b1be (diff)
downloadpylint-git-779e2c4078d5f6ee0a319a9807b215e775411426.tar.gz
closes #76920: don't crash in preprocess_option if some looked option has no value
while on is expected.
-rw-r--r--ChangeLog4
-rw-r--r--lint.py21
-rw-r--r--test/unittest_lint.py29
3 files changed, 47 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index e3627191b..b310c95d1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,7 +14,9 @@ ChangeLog for PyLint
* #74747: crash occurs when lookup up a special attribute in class scope
(patch by google)
-
+ * #76920: crash if on eg "pylint --rcfile"(patch by Torsten Marek)
+
+
2011-07-18 -- 0.24.0
* #69738: add regular expressions support for "generated-members"
diff --git a/lint.py b/lint.py
index b1e64d7ec..d11d71673 100644
--- a/lint.py
+++ b/lint.py
@@ -687,6 +687,10 @@ except AttributeError:
__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 @@ def preprocess_options(args, search_for):
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 @@ group are mutually exclusive.'),
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 --git a/test/unittest_lint.py b/test/unittest_lint.py
index c9f6105b8..d4bb32952 100644
--- a/test/unittest_lint.py
+++ b/test/unittest_lint.py
@@ -23,7 +23,8 @@ from logilab.common.testlib import TestCase, unittest_main, create_files
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 @@ class ConfigTC(TestCase):
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()