eric6/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py

changeset 7256
4ef3b78ebb4e
parent 7249
0bf517e60f54
child 7289
6f4761a73f5f
equal deleted inserted replaced
7255:d595f6f9cbf8 7256:4ef3b78ebb4e
359 "MinFilesize", 359 "MinFilesize",
360 self.__defaultArgs["CopyrightChecker"]["MinFilesize"]) 360 self.__defaultArgs["CopyrightChecker"]["MinFilesize"])
361 copyrightAuthor = copyrightArgs.get( 361 copyrightAuthor = copyrightArgs.get(
362 "Author", 362 "Author",
363 self.__defaultArgs["CopyrightChecker"]["Author"]) 363 self.__defaultArgs["CopyrightChecker"]["Author"])
364 copyrightRegexStr = \ 364 copyrightRegexStr = (
365 r"Copyright\s+(\(C\)\s+)?(\d{{4}}\s+-\s+)?\d{{4}}\s+{author}" 365 r"Copyright\s+(\(C\)\s+)?(\d{{4}}\s+-\s+)?\d{{4}}\s+{author}"
366 )
366 367
367 tocheck = max(1024, copyrightMinFileSize) 368 tocheck = max(1024, copyrightMinFileSize)
368 topOfSource = source[:tocheck] 369 topOfSource = source[:tocheck]
369 if len(topOfSource) < copyrightMinFileSize: 370 if len(topOfSource) < copyrightMinFileSize:
370 return 371 return
425 def __checkPrintStatements(self): 426 def __checkPrintStatements(self):
426 """ 427 """
427 Private method to check for print statements. 428 Private method to check for print statements.
428 """ 429 """
429 for node in ast.walk(self.__tree): 430 for node in ast.walk(self.__tree):
430 if (isinstance(node, ast.Call) and 431 if (
431 getattr(node.func, 'id', None) == 'print') or \ 432 (isinstance(node, ast.Call) and
432 (hasattr(ast, 'Print') and isinstance(node, ast.Print)): 433 getattr(node.func, 'id', None) == 'print') or
434 (hasattr(ast, 'Print') and isinstance(node, ast.Print))
435 ):
433 self.__error(node.lineno - 1, node.col_offset, "M801") 436 self.__error(node.lineno - 1, node.col_offset, "M801")
434 437
435 def __checkTuple(self): 438 def __checkTuple(self):
436 """ 439 """
437 Private method to check for one element tuples. 440 Private method to check for one element tuples.
438 """ 441 """
439 for node in ast.walk(self.__tree): 442 for node in ast.walk(self.__tree):
440 if isinstance(node, ast.Tuple) and \ 443 if (
441 len(node.elts) == 1: 444 isinstance(node, ast.Tuple) and
445 len(node.elts) == 1
446 ):
442 self.__error(node.lineno - 1, node.col_offset, "M811") 447 self.__error(node.lineno - 1, node.col_offset, "M811")
443 448
444 def __checkFuture(self): 449 def __checkFuture(self):
445 """ 450 """
446 Private method to check the __future__ imports. 451 Private method to check the __future__ imports.
648 653
649 for node in ast.walk(self.__tree): 654 for node in ast.walk(self.__tree):
650 if isinstance(node, ast.Assign): 655 if isinstance(node, ast.Assign):
651 # assign statement 656 # assign statement
652 for element in node.targets: 657 for element in node.targets:
653 if isinstance(element, ast.Name) and \ 658 if (
654 element.id in self.__builtins: 659 isinstance(element, ast.Name) and
660 element.id in self.__builtins
661 ):
655 value = node.value 662 value = node.value
656 if isinstance(value, ast.Name) and \ 663 if (
657 element.id in ignoreBuiltinAssignments and \ 664 isinstance(value, ast.Name) and
658 value.id in ignoreBuiltinAssignments[element.id]: 665 element.id in ignoreBuiltinAssignments and
666 value.id in ignoreBuiltinAssignments[element.id]
667 ):
659 # ignore compatibility assignments 668 # ignore compatibility assignments
660 continue 669 continue
661 self.__error(element.lineno - 1, element.col_offset, 670 self.__error(element.lineno - 1, element.col_offset,
662 "M131", element.id) 671 "M131", element.id)
663 elif isinstance(element, (ast.Tuple, ast.List)): 672 elif isinstance(element, (ast.Tuple, ast.List)):
664 for tupleElement in element.elts: 673 for tupleElement in element.elts:
665 if isinstance(tupleElement, ast.Name) and \ 674 if (
666 tupleElement.id in self.__builtins: 675 isinstance(tupleElement, ast.Name) and
676 tupleElement.id in self.__builtins
677 ):
667 self.__error(tupleElement.lineno - 1, 678 self.__error(tupleElement.lineno - 1,
668 tupleElement.col_offset, 679 tupleElement.col_offset,
669 "M131", tupleElement.id) 680 "M131", tupleElement.id)
670 elif isinstance(node, ast.For): 681 elif isinstance(node, ast.For):
671 # for loop 682 # for loop
672 target = node.target 683 target = node.target
673 if isinstance(target, ast.Name) and \ 684 if (
674 target.id in self.__builtins: 685 isinstance(target, ast.Name) and
686 target.id in self.__builtins
687 ):
675 self.__error(target.lineno - 1, target.col_offset, 688 self.__error(target.lineno - 1, target.col_offset,
676 "M131", target.id) 689 "M131", target.id)
677 elif isinstance(target, (ast.Tuple, ast.List)): 690 elif isinstance(target, (ast.Tuple, ast.List)):
678 for element in target.elts: 691 for element in target.elts:
679 if isinstance(element, ast.Name) and \ 692 if (
680 element.id in self.__builtins: 693 isinstance(element, ast.Name) and
694 element.id in self.__builtins
695 ):
681 self.__error(element.lineno - 1, 696 self.__error(element.lineno - 1,
682 element.col_offset, 697 element.col_offset,
683 "M131", element.id) 698 "M131", element.id)
684 elif any(isinstance(node, functionDef) 699 elif any(isinstance(node, functionDef)
685 for functionDef in functionDefs): 700 for functionDef in functionDefs):
686 # (asynchronous) function definition 701 # (asynchronous) function definition
687 if sys.version_info >= (3, 0): 702 if sys.version_info >= (3, 0):
688 for arg in node.args.args: 703 for arg in node.args.args:
689 if isinstance(arg, ast.arg) and \ 704 if (
690 arg.arg in self.__builtins: 705 isinstance(arg, ast.arg) and
706 arg.arg in self.__builtins
707 ):
691 self.__error(arg.lineno - 1, arg.col_offset, 708 self.__error(arg.lineno - 1, arg.col_offset,
692 "M132", arg.arg) 709 "M132", arg.arg)
693 else: 710 else:
694 for arg in node.args.args: 711 for arg in node.args.args:
695 if isinstance(arg, ast.Name) and \ 712 if (
696 arg.id in self.__builtins: 713 isinstance(arg, ast.Name) and
714 arg.id in self.__builtins
715 ):
697 self.__error(arg.lineno - 1, arg.col_offset, 716 self.__error(arg.lineno - 1, arg.col_offset,
698 "M132", arg.id) 717 "M132", arg.id)
699 718
700 def __checkComprehensions(self): 719 def __checkComprehensions(self):
701 """ 720 """
861 @rtype bool 880 @rtype bool
862 """ 881 """
863 if not all(isinstance(key, ast.Str) for key in node.keys): 882 if not all(isinstance(key, ast.Str) for key in node.keys):
864 return False 883 return False
865 884
866 if "__IGNORE_WARNING__" in self.__source[node.lineno - 1] or \ 885 if (
867 "__IGNORE_WARNING_M201__" in self.__source[node.lineno - 1]: 886 "__IGNORE_WARNING__" in self.__source[node.lineno - 1] or
887 "__IGNORE_WARNING_M201__" in self.__source[node.lineno - 1]
888 ):
868 return False 889 return False
869 890
870 lineNumbers = [key.lineno for key in node.keys] 891 lineNumbers = [key.lineno for key in node.keys]
871 return len(lineNumbers) == len(set(lineNumbers)) 892 return len(lineNumbers) == len(set(lineNumbers))
872 893
893 def __checkGettext(self): 914 def __checkGettext(self):
894 """ 915 """
895 Private method to check the 'gettext' import statement. 916 Private method to check the 'gettext' import statement.
896 """ 917 """
897 for node in ast.walk(self.__tree): 918 for node in ast.walk(self.__tree):
898 if isinstance(node, ast.ImportFrom) and \ 919 if (
899 any(name.asname == '_' for name in node.names): 920 isinstance(node, ast.ImportFrom) and
921 any(name.asname == '_' for name in node.names)
922 ):
900 self.__error(node.lineno - 1, node.col_offset, "M711", 923 self.__error(node.lineno - 1, node.col_offset, "M711",
901 node.names[0].name) 924 node.names[0].name)
902 925
903 def __checkBugBear(self): 926 def __checkBugBear(self):
904 """ 927 """
1156 @param node reference to the node to be checked 1179 @param node reference to the node to be checked
1157 @type ast.keyword 1180 @type ast.keyword
1158 @return flag indicating we are inside the extra keyword 1181 @return flag indicating we are inside the extra keyword
1159 @rtype bool 1182 @rtype bool
1160 """ 1183 """
1161 return self.__currentExtraKeyword is not None and \ 1184 return (
1185 self.__currentExtraKeyword is not None and
1162 self.__currentExtraKeyword != node 1186 self.__currentExtraKeyword != node
1187 )
1163 1188
1164 def __detectLoggingLevel(self, node): 1189 def __detectLoggingLevel(self, node):
1165 """ 1190 """
1166 Private method to decide whether an AST Call is a logging call. 1191 Private method to decide whether an AST Call is a logging call.
1167 1192
1229 self.violations.append((node, "M655")) 1254 self.violations.append((node, "M655"))
1230 1255
1231 for index, child in enumerate(ast.iter_child_nodes(node)): 1256 for index, child in enumerate(ast.iter_child_nodes(node)):
1232 if index == 1: 1257 if index == 1:
1233 self.__currentLoggingArgument = child 1258 self.__currentLoggingArgument = child
1234 if index > 1 and isinstance(child, ast.keyword) and \ 1259 if (
1235 child.arg == "extra": 1260 index > 1 and
1261 isinstance(child, ast.keyword) and
1262 child.arg == "extra"
1263 ):
1236 self.__currentExtraKeyword = child 1264 self.__currentExtraKeyword = child
1237 1265
1238 super(LoggingVisitor, self).visit(child) 1266 super(LoggingVisitor, self).visit(child)
1239 1267
1240 self.__currentLoggingArgument = None 1268 self.__currentLoggingArgument = None
1306 @param node AST node to be traversed 1334 @param node AST node to be traversed
1307 @type ast.Node 1335 @type ast.Node
1308 """ 1336 """
1309 self.__nodeStack.append(node) 1337 self.__nodeStack.append(node)
1310 self.__nodeWindow.append(node) 1338 self.__nodeWindow.append(node)
1311 self.__nodeWindow = \ 1339 self.__nodeWindow = self.__nodeWindow[-BugBearVisitor.NodeWindowSize:]
1312 self.__nodeWindow[-BugBearVisitor.NodeWindowSize:]
1313 1340
1314 super(BugBearVisitor, self).visit(node) 1341 super(BugBearVisitor, self).visit(node)
1315 1342
1316 self.__nodeStack.pop() 1343 self.__nodeStack.pop()
1317 1344
1403 callPath = list(composeCallPath(node)) 1430 callPath = list(composeCallPath(node))
1404 1431
1405 if '.'.join(callPath) == 'sys.maxint' and sys.version_info >= (3, 0): 1432 if '.'.join(callPath) == 'sys.maxint' and sys.version_info >= (3, 0):
1406 self.violations.append((node, "M504")) 1433 self.violations.append((node, "M504"))
1407 1434
1408 elif len(callPath) == 2 and callPath[1] == 'message' and \ 1435 elif (
1409 sys.version_info >= (2, 6): 1436 len(callPath) == 2 and
1437 callPath[1] == 'message' and
1438 sys.version_info >= (2, 6)
1439 ):
1410 name = callPath[0] 1440 name = callPath[0]
1411 for elem in reversed(self.__nodeStack[:-1]): 1441 for elem in reversed(self.__nodeStack[:-1]):
1412 if isinstance(elem, ast.ExceptHandler) and elem.name == name: 1442 if isinstance(elem, ast.ExceptHandler) and elem.name == name:
1413 self.violations.append((node, "M505")) 1443 self.violations.append((node, "M505"))
1414 break 1444 break
1427 if '__metaclass__' in assignTargets and sys.version_info >= (3, 0): 1457 if '__metaclass__' in assignTargets and sys.version_info >= (3, 0):
1428 self.violations.append((node, "M524")) 1458 self.violations.append((node, "M524"))
1429 1459
1430 elif len(node.targets) == 1: 1460 elif len(node.targets) == 1:
1431 target = node.targets[0] 1461 target = node.targets[0]
1432 if isinstance(target, ast.Attribute) and \ 1462 if (
1433 isinstance(target.value, ast.Name): 1463 isinstance(target, ast.Attribute) and
1464 isinstance(target.value, ast.Name)
1465 ):
1434 if (target.value.id, target.attr) == ('os', 'environ'): 1466 if (target.value.id, target.attr) == ('os', 'environ'):
1435 self.violations.append((node, "M506")) 1467 self.violations.append((node, "M506"))
1436 1468
1437 self.generic_visit(node) 1469 self.generic_visit(node)
1438 1470
1452 Public method to handle 'assert' statements. 1484 Public method to handle 'assert' statements.
1453 1485
1454 @param node reference to the node to be processed 1486 @param node reference to the node to be processed
1455 @type ast.Assert 1487 @type ast.Assert
1456 """ 1488 """
1457 if isinstance(node.test, ast.NameConstant) and \ 1489 if (
1458 node.test.value is False: 1490 isinstance(node.test, ast.NameConstant) and
1491 node.test.value is False
1492 ):
1459 self.violations.append((node, "M503")) 1493 self.violations.append((node, "M503"))
1460 1494
1461 self.generic_visit(node) 1495 self.generic_visit(node)
1462 1496
1463 def visit_JoinedStr(self, node): 1497 def visit_JoinedStr(self, node):

eric ide

mercurial