diff options
| author | Cosmin Poieana <cmin@ropython.org> | 2014-11-30 19:35:43 +0200 |
|---|---|---|
| committer | Cosmin Poieana <cmin@ropython.org> | 2014-11-30 19:35:43 +0200 |
| commit | 1931de081093394f4a9523da77b3f924a91b8ce2 (patch) | |
| tree | 7a46aa3371114056dc59001e5013862257aea676 /checkers/stdlib.py | |
| parent | 81c4b6c37a9ee83570d92d6475789b612de7f7a8 (diff) | |
| parent | 8a6e324ffc3f21dcd7cbbb6d7cccbee1d228e251 (diff) | |
| download | pylint-git-1931de081093394f4a9523da77b3f924a91b8ce2.tar.gz | |
Merge; Fix according to review
--HG--
branch : open_mode
Diffstat (limited to 'checkers/stdlib.py')
| -rw-r--r-- | checkers/stdlib.py | 80 |
1 files changed, 39 insertions, 41 deletions
diff --git a/checkers/stdlib.py b/checkers/stdlib.py index 3caaac82b..54f8c1aee 100644 --- a/checkers/stdlib.py +++ b/checkers/stdlib.py @@ -32,6 +32,44 @@ if sys.version_info >= (3, 0): else: OPEN_MODULE = '__builtin__' + +def _check_mode_str(mode): + # check type + if not isinstance(mode, six.string_types): + return False + # check syntax + modes = set(mode) + _mode = "rwatb+U" + if six.PY3: + _mode += "x" + creating = "x" in modes + if modes - set(_mode) or len(mode) > len(modes): + return False + # check logic + creating = False + reading = "r" in modes + writing = "w" in modes + appending = "a" in modes + updating = "+" in modes + text = "t" in modes + binary = "b" in modes + if "U" in modes: + if writing or appending or creating and six.PY3: + return False + reading = True + if text and binary: + return False + total = reading + writing + appending + (creating if six.PY3 else 0) + if total > 1: + return False + if not (reading or writing or appending or creating and six.PY3): + return False + # 2.x constraints + if not six.PY3: + return mode[0] in ("r", "w", "a", "U") + return True + + class StdlibChecker(BaseChecker): __implements__ = (IAstroidChecker,) name = 'stdlib' @@ -89,45 +127,6 @@ class StdlibChecker(BaseChecker): infered.qname() == 'datetime.time'): self.add_message('boolean-datetime', node=node) - @staticmethod - def _check_mode_str(mode): - # check type - cls = str if six.PY3 else basestring - if not isinstance(mode, cls): - return False - # check syntax - modes = set(mode) - _mode = "rwatb+U" - if six.PY3: - _mode += "x" - if modes - set(_mode) or len(mode) > len(modes): - return False - # check logic - creating = False - if six.PY3: - creating = "x" in modes - reading = "r" in modes - writing = "w" in modes - appending = "a" in modes - updating = "+" in modes - text = "t" in modes - binary = "b" in modes - if "U" in modes: - if writing or appending or creating and six.PY3: - return False - reading = True - if text and binary: - return False - total = reading + writing + appending + (creating if six.PY3 else 0) - if total > 1: - return False - if not (reading or writing or appending or creating and six.PY3): - return False - # 2.x constraints - if not six.PY3: - if mode[0] not in ("r", "w", "a", "U"): - return False - return True def _check_open_mode(self, node): """Check that the mode argument of an open or file call is valid.""" @@ -136,7 +135,7 @@ class StdlibChecker(BaseChecker): if mode_arg: mode_arg = utils.safe_infer(mode_arg) if (isinstance(mode_arg, astroid.Const) - and not self._check_mode_str(mode_arg.value)): + and not _check_mode_str(mode_arg.value)): self.add_message('bad-open-mode', node=node, args=(mode_arg.value)) except (utils.NoSuchArgumentError, TypeError): @@ -145,4 +144,3 @@ class StdlibChecker(BaseChecker): def register(linter): """required method to auto register this checker """ linter.register_checker(StdlibChecker(linter)) - |
