diff options
| author | Torsten Marek <tmarek@google.com> | 2013-06-19 11:46:37 +0200 |
|---|---|---|
| committer | Torsten Marek <tmarek@google.com> | 2013-06-19 11:46:37 +0200 |
| commit | 7bcb96848e634e1d6db55995e5286168a5ca02a6 (patch) | |
| tree | ae06597f4c9ab7537496eb983281afa36e573168 /checkers/stdlib.py | |
| parent | 82dc4732c5f2196801cbce040003ed30d217624d (diff) | |
| download | pylint-git-7bcb96848e634e1d6db55995e5286168a5ca02a6.tar.gz | |
Add bad-open-mode warning for invalid open modes specified on open/file calls.
Diffstat (limited to 'checkers/stdlib.py')
| -rw-r--r-- | checkers/stdlib.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/checkers/stdlib.py b/checkers/stdlib.py new file mode 100644 index 000000000..364633b74 --- /dev/null +++ b/checkers/stdlib.py @@ -0,0 +1,62 @@ +# Copyright 2012 Google Inc. +# +# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., +# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +"""Checkers for various standard library functions.""" + +import re + +import astroid + +from pylint.interfaces import ITokenChecker, IAstroidChecker +from pylint.checkers import BaseChecker, BaseTokenChecker +from pylint.checkers import utils + +_VALID_OPEN_MODE_REGEX = r'^(r?U|[rwa]\+?b?)$' + + +class OpenModeChecker(BaseChecker): + __implements__ = (IAstroidChecker,) + name = 'open_mode' + + msgs = { + 'W1501': ('"%s" is not a valid mode for open.', + 'bad-open-mode', + 'Python supports: r, w, a modes with b, +, and U options. ' + 'See http://docs.python.org/2/library/functions.html#open'), + } + + @utils.check_messages('W1501') + def visit_callfunc(self, node): + """Visit a CallFunc node.""" + if (hasattr(node, 'func') and utils.is_builtin_object(utils.safe_infer(node.func))): + if getattr(node.func, 'name', None) in ('open', 'file'): + self._check_open_mode(node) + + def _check_open_mode(self, node): + """Check that the mode argument of an open or file call is valid.""" + try: + mode_arg = utils.get_argument_from_call(node, position=1, keyword='mode') + if mode_arg: + mode_arg = utils.safe_infer(mode_arg) + if (isinstance(mode_arg, astroid.Const) + and not re.match(_VALID_OPEN_MODE_REGEX, mode_arg.value)): + self.add_message('W1501', node=node, args=(mode_arg.value)) + except (utils.NoSuchArgumentError, TypeError): + pass + +def register(linter): + """required method to auto register this checker """ + linter.register_checker(OpenModeChecker(linter)) + |
