--- a/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Logging/LoggingFormatVisitor.py Thu Nov 30 16:39:46 2023 +0100 +++ b/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Logging/LoggingFormatVisitor.py Thu Nov 30 17:48:55 2023 +0100 @@ -10,7 +10,6 @@ import ast import contextlib - _LoggingLevels = { "debug", "critical", @@ -24,10 +23,30 @@ # default LogRecord attributes that shouldn't be overwritten by extra dict _ReservedAttrs = { - "args", "asctime", "created", "exc_info", "exc_text", "filename", - "funcName", "levelname", "levelno", "lineno", "module", - "msecs", "message", "msg", "name", "pathname", "process", - "processName", "relativeCreated", "stack_info", "thread", "threadName"} + "args", + "asctime", + "created", + "exc_info", + "exc_text", + "filename", + "funcName", + "levelname", + "levelno", + "lineno", + "module", + "msecs", + "message", + "msg", + "name", + "pathname", + "process", + "processName", + "relativeCreated", + "stack_info", + "taskName", + "thread", + "threadName", +} ####################################################################### ## LoggingFormatVisitor @@ -37,14 +56,18 @@ ## Original: Copyright (c) 2017 Globality Engineering ####################################################################### + class LoggingFormatVisitor(ast.NodeVisitor): """ Class implementing a node visitor to check logging formatting issues. """ - def __init__(self): + def __init__(self, errorCallback): """ Constructor + + @param errorCallback callback function to register an error + @type func """ super().__init__() @@ -53,7 +76,8 @@ self.__currentLoggingLevel = None self.__currentExtraKeyword = None self.__currentExceptNames = [] - self.violations = [] + + self.__error = errorCallback def __withinLoggingStatement(self): """ @@ -153,7 +177,6 @@ except AttributeError: return False - def __shouldCheckExtraFieldClash(self, node): """ Private method to check, if the extra field clash check should be done. @@ -174,7 +197,8 @@ """ Private method to check, if the check for extra exceptions should be done. -c @type ast.Dict + @param node reference to the node to be processed + @type ast.Dict @return flag indicating to perform the check @rtype bool """ @@ -211,7 +235,7 @@ return ( isinstance(node, ast.Call) and isinstance(node.func, ast.Name) - and node.func.id in ('str', 'unicode') + and node.func.id in ("str", "unicode") and node.args and self.__isBareException(node.args[0]) ) @@ -224,7 +248,7 @@ @type ast.AST """ if self.__isBareException(node) or self.__isStrException(node): - self.violations.append((node, "L130")) + self.__error(node.lineno - 1, node.col_offset, "L130") def __checkExcInfo(self, node): """ @@ -234,16 +258,16 @@ @param node reference to the node to be processed @type ast.AST """ - if self.__currentLoggingLevel not in ('error', 'exception'): + if self.__currentLoggingLevel not in ("error", "exception"): return for kw in node.keywords: - if kw.arg == 'exc_info': - if self.__currentLoggingLevel == 'error': + if kw.arg == "exc_info": + if self.__currentLoggingLevel == "error": violation = "L131" else: violation = "L132" - self.violations.append((node, violation)) + self.__error(node.lineno - 1, node.col_offset, violation) def visit_Call(self, node): """ @@ -261,7 +285,7 @@ and self.__withinLoggingArgument() and self.__isFormatCall(node) ): - self.violations.append((node, "L101")) + self.__error(node.lineno - 1, node.col_offset, "L101") super().generic_visit(node) return @@ -279,7 +303,7 @@ self.__currentLoggingCall = node if loggingLevel == "warn": - self.violations.append((node, "L110")) + self.__error(node.lineno - 1, node.col_offset, "L110") self.__checkExcInfo(node) @@ -310,11 +334,11 @@ if self.__withinLoggingStatement() and self.__withinLoggingArgument(): # handle percent format if isinstance(node.op, ast.Mod): - self.violations.append((node, "L102")) + self.__error(node.lineno - 1, node.col_offset, "L102") # handle string concat if isinstance(node.op, ast.Add): - self.violations.append((node, "L103")) + self.__error(node.lineno - 1, node.col_offset, "L103") super().generic_visit(node) @@ -329,7 +353,7 @@ for key in node.keys: # key can be None if the dict uses double star syntax if key is not None and key.s in _ReservedAttrs: - self.violations.append((node, "L121", key.s)) + self.__error(node.lineno - 1, node.col_offset, "L121", key.s) if self.__shouldCheckExtraException(node): for value in node.values: @@ -349,11 +373,10 @@ and any(isinstance(i, ast.FormattedValue) for i in node.values) and self.__withinLoggingArgument() ): - self.violations.append((node, "L104")) + self.__error(node.lineno - 1, node.col_offset, "L104") super().generic_visit(node) - def visit_ExceptHandler(self, node): """ Public method to handle an exception handler.