OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2009-2010 Google, Inc. |
| 2 # This program is free software; you can redistribute it and/or modify it under |
| 3 # the terms of the GNU General Public License as published by the Free Software |
| 4 # Foundation; either version 2 of the License, or (at your option) any later |
| 5 # version. |
| 6 # |
| 7 # This program is distributed in the hope that it will be useful, but WITHOUT |
| 8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 10 # |
| 11 # You should have received a copy of the GNU General Public License along with |
| 12 # this program; if not, write to the Free Software Foundation, Inc., |
| 13 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 14 """checker for use of Python logging |
| 15 """ |
| 16 |
| 17 from logilab import astng |
| 18 from pylint import checkers |
| 19 from pylint import interfaces |
| 20 from pylint.checkers import utils |
| 21 |
| 22 |
| 23 MSGS = { |
| 24 'W1201': ('Specify string format arguments as logging function parameters', |
| 25 'Used when a logging statement has a call form of ' |
| 26 '"logging.<logging method>(format_string % (format_args...))". ' |
| 27 'Such calls should leave string interpolation to the logging ' |
| 28 'method itself and be written ' |
| 29 '"logging.<logging method>(format_string, format_args...)" ' |
| 30 'so that the program may avoid incurring the cost of the ' |
| 31 'interpolation in those cases in which no message will be ' |
| 32 'logged. For more, see ' |
| 33 'http://www.python.org/dev/peps/pep-0282/.'), |
| 34 'E1200': ('Unsupported logging format character %r (%#02x) at index %d', |
| 35 'Used when an unsupported format character is used in a logging\ |
| 36 statement format string.'), |
| 37 'E1201': ('Logging format string ends in middle of conversion specifier', |
| 38 'Used when a logging statement format string terminates before\ |
| 39 the end of a conversion specifier.'), |
| 40 'E1205': ('Too many arguments for logging format string', |
| 41 'Used when a logging format string is given too few arguments.'), |
| 42 'E1206': ('Not enough arguments for logging format string', |
| 43 'Used when a logging format string is given too many arguments'), |
| 44 } |
| 45 |
| 46 |
| 47 CHECKED_CONVENIENCE_FUNCTIONS = set([ |
| 48 'critical', 'debug', 'error', 'exception', 'fatal', 'info', 'warn', |
| 49 'warning']) |
| 50 |
| 51 |
| 52 class LoggingChecker(checkers.BaseChecker): |
| 53 """Checks use of the logging module.""" |
| 54 |
| 55 __implements__ = interfaces.IASTNGChecker |
| 56 name = 'logging' |
| 57 msgs = MSGS |
| 58 |
| 59 def visit_module(self, unused_node): |
| 60 """Clears any state left in this checker from last module checked.""" |
| 61 # The code being checked can just as easily "import logging as foo", |
| 62 # so it is necessary to process the imports and store in this field |
| 63 # what name the logging module is actually given. |
| 64 self._logging_name = None |
| 65 |
| 66 def visit_import(self, node): |
| 67 """Checks to see if this module uses Python's built-in logging.""" |
| 68 for module, as_name in node.names: |
| 69 if module == 'logging': |
| 70 if as_name: |
| 71 self._logging_name = as_name |
| 72 else: |
| 73 self._logging_name = 'logging' |
| 74 |
| 75 def visit_callfunc(self, node): |
| 76 """Checks calls to (simple forms of) logging methods.""" |
| 77 if (not isinstance(node.func, astng.Getattr) |
| 78 or not isinstance(node.func.expr, astng.Name) |
| 79 or node.func.expr.name != self._logging_name): |
| 80 return |
| 81 self._check_convenience_methods(node) |
| 82 self._check_log_methods(node) |
| 83 |
| 84 def _check_convenience_methods(self, node): |
| 85 """Checks calls to logging convenience methods (like logging.warn).""" |
| 86 if node.func.attrname not in CHECKED_CONVENIENCE_FUNCTIONS: |
| 87 return |
| 88 if node.starargs or node.kwargs or not node.args: |
| 89 # Either no args, star args, or double-star args. Beyond the |
| 90 # scope of this checker. |
| 91 return |
| 92 if isinstance(node.args[0], astng.BinOp) and node.args[0].op == '%': |
| 93 self.add_message('W1201', node=node) |
| 94 elif isinstance(node.args[0], astng.Const): |
| 95 self._check_format_string(node, 0) |
| 96 |
| 97 def _check_log_methods(self, node): |
| 98 """Checks calls to logging.log(level, format, *format_args).""" |
| 99 if node.func.attrname != 'log': |
| 100 return |
| 101 if node.starargs or node.kwargs or len(node.args) < 2: |
| 102 # Either a malformed call, star args, or double-star args. Beyond |
| 103 # the scope of this checker. |
| 104 return |
| 105 if isinstance(node.args[1], astng.BinOp) and node.args[1].op == '%': |
| 106 self.add_message('W1201', node=node) |
| 107 elif isinstance(node.args[1], astng.Const): |
| 108 self._check_format_string(node, 1) |
| 109 |
| 110 def _check_format_string(self, node, format_arg): |
| 111 """Checks that format string tokens match the supplied arguments. |
| 112 |
| 113 Args: |
| 114 node: AST node to be checked. |
| 115 format_arg: Index of the format string in the node arguments. |
| 116 """ |
| 117 num_args = self._count_supplied_tokens(node.args[format_arg + 1:]) |
| 118 if not num_args: |
| 119 # If no args were supplied, then all format strings are valid - |
| 120 # don't check any further. |
| 121 return |
| 122 format_string = node.args[format_arg].value |
| 123 if not isinstance(format_string, basestring): |
| 124 # If the log format is constant non-string (e.g. logging.debug(5)), |
| 125 # ensure there are no arguments. |
| 126 required_num_args = 0 |
| 127 else: |
| 128 try: |
| 129 keyword_args, required_num_args = \ |
| 130 utils.parse_format_string(format_string) |
| 131 if keyword_args: |
| 132 # Keyword checking on logging strings is complicated by |
| 133 # special keywords - out of scope. |
| 134 return |
| 135 except utils.UnsupportedFormatCharacter, e: |
| 136 c = format_string[e.index] |
| 137 self.add_message('E1200', node=node, args=(c, ord(c), e.index)) |
| 138 return |
| 139 except utils.IncompleteFormatString: |
| 140 self.add_message('E1201', node=node) |
| 141 return |
| 142 if num_args > required_num_args: |
| 143 self.add_message('E1205', node=node) |
| 144 elif num_args < required_num_args: |
| 145 self.add_message('E1206', node=node) |
| 146 |
| 147 def _count_supplied_tokens(self, args): |
| 148 """Counts the number of tokens in an args list. |
| 149 |
| 150 The Python log functions allow for special keyword arguments: func, |
| 151 exc_info and extra. To handle these cases correctly, we only count |
| 152 arguments that aren't keywords. |
| 153 |
| 154 Args: |
| 155 args: List of AST nodes that are arguments for a log format string. |
| 156 |
| 157 Returns: |
| 158 Number of AST nodes that aren't keywords. |
| 159 """ |
| 160 return sum(1 for arg in args if not isinstance(arg, astng.Keyword)) |
| 161 |
| 162 |
| 163 def register(linter): |
| 164 """Required method to auto-register this checker.""" |
| 165 linter.register_checker(LoggingChecker(linter)) |
OLD | NEW |