summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristophe Simonis <chs@odoo.com>2017-04-26 17:55:16 +0200
committerChristophe Simonis <chs@odoo.com>2017-08-07 15:02:56 +0200
commit0ebe59f5d013c8d082a25cda243219a76508c10e (patch)
tree84a9c73e2347542e8cb4b822f3c15d1b3e576548
parent9882f24313c988b1f8e40f1fbe734a798b0dbf7b (diff)
[IMP] test_pylint: check for badly written try/except
-rw-r--r--odoo/addons/test_pylint/tests/_odoo_checkers.py44
-rw-r--r--odoo/addons/test_pylint/tests/test_pylint.py17
2 files changed, 58 insertions, 3 deletions
diff --git a/odoo/addons/test_pylint/tests/_odoo_checkers.py b/odoo/addons/test_pylint/tests/_odoo_checkers.py
new file mode 100644
index 00000000000..5648ac52219
--- /dev/null
+++ b/odoo/addons/test_pylint/tests/_odoo_checkers.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import tokenize
+
+from pylint import checkers, interfaces
+
+class PEP3110TokenChecker(checkers.BaseTokenChecker):
+ __implements__ = interfaces.ITokenChecker
+ name = 'python3'
+ enabled = False
+
+ msgs = {
+ 'E3110': ('Python 3 uses `as` instead of comma token to catch exceptions',
+ 'no-comma-exception',
+ 'See http://www.python.org/dev/peps/pep-3110/',
+ {'maxversion': (3, 0)}),
+ }
+
+ def process_tokens(self, tokens):
+ comma_found = in_except = False
+ pcount = 0
+ for tok_type, token, start, _, _ in tokens:
+ if tok_type == tokenize.NAME and token == 'except':
+ in_except = True
+ comma_found = False
+ pcount = 0
+
+ elif tok_type == tokenize.OP and in_except:
+ if token == '(':
+ pcount += 1
+ elif token == ')':
+ pcount -= 1
+ elif token == ',' and pcount == 0:
+ comma_found = True
+ if token == ':':
+ if in_except and comma_found:
+ self.add_message('no-comma-exception', line=start[0])
+ comma_found = in_except = False
+ pcount = 0
+
+
+def register(linter):
+ linter.register_checker(PEP3110TokenChecker(linter))
diff --git a/odoo/addons/test_pylint/tests/test_pylint.py b/odoo/addons/test_pylint/tests/test_pylint.py
index 598f2098394..eccc90d3bee 100644
--- a/odoo/addons/test_pylint/tests/test_pylint.py
+++ b/odoo/addons/test_pylint/tests/test_pylint.py
@@ -8,13 +8,14 @@ except ImportError:
pylint = None
import subprocess
from distutils.version import LooseVersion
-from os import devnull
+import os
from os.path import join
from odoo.tests.common import TransactionCase
from odoo import tools
from odoo.modules import get_modules, get_module_path
+HERE = os.path.dirname(os.path.realpath(__file__))
_logger = logging.getLogger(__name__)
@@ -54,6 +55,8 @@ class TestPyLint(TransactionCase):
'old-raise-syntax',
'raising-string',
'unpacking-in-except',
+
+ 'no-comma-exception',
]
BAD_FUNCTIONS = [
@@ -115,13 +118,21 @@ class TestPyLint(TransactionCase):
'--enable=%s' % ','.join(self.ENABLED_CODES),
'--reports=n',
"--msg-template='{msg} ({msg_id}) at {path}:{line}'",
- '--load-plugins=pylint.extensions.bad_builtin',
+ '--load-plugins=pylint.extensions.bad_builtin,_odoo_checkers',
'--bad-functions=%s' % ','.join(self.BAD_FUNCTIONS),
'--deprecated-modules=%s' % ','.join(self.BAD_MODULES)
]
+ pypath = HERE + os.pathsep + os.environ.get('PYTHONPATH', '')
+ env = dict(os.environ, PYTHONPATH=pypath)
try:
- process = subprocess.Popen(['pylint'] + options + paths, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ pylint_bin = tools.which('pylint')
+ process = subprocess.Popen(
+ [pylint_bin] + options + paths,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ env=env,
+ )
except (OSError, IOError):
self._skip_test('pylint executable not found in the path')
else: