LEFT | RIGHT |
1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
(...skipping 28 matching lines...) Expand all Loading... |
39 } | 39 } |
40 | 40 |
41 ESSENTIAL_BUILTINS = set(dir(builtins)) - {'apply', 'buffer', 'coerce', | 41 ESSENTIAL_BUILTINS = set(dir(builtins)) - {'apply', 'buffer', 'coerce', |
42 'intern', 'file'} | 42 'intern', 'file'} |
43 | 43 |
44 LEAVE_BLOCK = (ast.Return, ast.Raise, ast.Continue, ast.Break) | 44 LEAVE_BLOCK = (ast.Return, ast.Raise, ast.Continue, ast.Break) |
45 VOLATILE = object() | 45 VOLATILE = object() |
46 | 46 |
47 | 47 |
48 def evaluate(node): | 48 def evaluate(node): |
49 names = {'__builtins__': {'True': True, 'False': False, 'None': None}} | |
50 try: | 49 try: |
51 return eval(compile(ast.Expression(node), '', 'eval'), names) | 50 return eval(compile(ast.Expression(node), '', 'eval'), {}) |
52 except Exception: | 51 except Exception: |
53 return VOLATILE | 52 return VOLATILE |
54 | 53 |
55 | 54 |
56 def is_const(node): | 55 def is_const(node): |
57 return evaluate(node) is not VOLATILE | 56 return evaluate(node) is not VOLATILE |
58 | 57 |
59 | 58 |
60 def get_identifier(node): | 59 def get_identifier(node): |
61 if isinstance(node, ast.Name): | 60 if isinstance(node, ast.Name): |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 | 391 |
393 if 'u' in prefixes: | 392 if 'u' in prefixes: |
394 yield (start, 'A112 use "from __future__ import ' | 393 yield (start, 'A112 use "from __future__ import ' |
395 'unicode_literals" instead of ' | 394 'unicode_literals" instead of ' |
396 'prefixing literals with "u"') | 395 'prefixing literals with "u"') |
397 | 396 |
398 if first_token and re.search(r'^(?:(?:def|class)\s|$)', | 397 if first_token and re.search(r'^(?:(?:def|class)\s|$)', |
399 previous_logical): | 398 previous_logical): |
400 pass # Ignore docstrings | 399 pass # Ignore docstrings |
401 elif start[0] != end[0]: | 400 elif start[0] != end[0]: |
402 pass # Ignore multiline strings | 401 pass # Ignore multiline strings |
403 elif 'r' in prefixes: | 402 elif 'r' in prefixes: |
404 if quote != "'" and not (quote == '"' and "'" in text): | 403 if quote != "'" and not (quote == '"' and "'" in text): |
405 yield (start, 'A110 use single quotes for raw string') | 404 yield (start, 'A110 use single quotes for raw string') |
406 else: | 405 else: |
407 prefix = '' | 406 prefix = '' |
408 if sys.version_info[0] >= 3: | 407 if sys.version_info[0] >= 3: |
409 if 'b' in prefixes: | 408 if 'b' in prefixes: |
410 prefix = 'b' | 409 prefix = 'b' |
411 else: | 410 else: |
412 u_literals = checker_state.get('has_unicode_literals') | 411 u_literals = checker_state.get('has_unicode_literals') |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 return [(pos, 'A111 redundant parenthesis for {} ' | 468 return [(pos, 'A111 redundant parenthesis for {} ' |
470 'statement'.format(statement))] | 469 'statement'.format(statement))] |
471 | 470 |
472 return [] | 471 return [] |
473 | 472 |
474 | 473 |
475 for checker in [ASTChecker, check_non_default_encoding, | 474 for checker in [ASTChecker, check_non_default_encoding, |
476 check_quotes, check_redundant_parenthesis]: | 475 check_quotes, check_redundant_parenthesis]: |
477 checker.name = 'eyeo' | 476 checker.name = 'eyeo' |
478 checker.version = __version__ | 477 checker.version = __version__ |
LEFT | RIGHT |