# HG changeset patch
# User Torsten Marek <tmarek@google.com>
# Date 1364577100 -3600
# Fri Mar 29 18:11:40 2013 +0100
# Node ID eb20da14bab6123504ff901eef9d719a979a8d69
# Parent aad82d0ccc1eb0bd10e7a67e14fe596cce911bb6
Warn about suspicious arguments in {bytes,str,unicode}.{l,r,}strip calls. Closes #74013
# User Torsten Marek <tmarek@google.com>
# Date 1364577100 -3600
# Fri Mar 29 18:11:40 2013 +0100
# Node ID eb20da14bab6123504ff901eef9d719a979a8d69
# Parent aad82d0ccc1eb0bd10e7a67e14fe596cce911bb6
Warn about suspicious arguments in {bytes,str,unicode}.{l,r,}strip calls. Closes #74013
@@ -1,9 +1,12 @@
1 ChangeLog for PyLint 2 ==================== 3 4 -- 5 + * #74013: new E1310[bad-str-strip-call] message warning when a call to a 6 + {l,r,}strip method contains duplicate characters (patch by Torsten Marek) 7 + 8 * #124660: internal dependencies should not appear in external dependencies 9 report 10 11 * #124662: fix name error causing crash when symbols are included in output 12 messages
@@ -1,7 +1,9 @@
13 # Copyright (c) 2009-2010 Arista Networks, Inc. - James Lingard 14 -# Copyright (c) 2004-2010 LOGILAB S.A. (Paris, FRANCE). 15 +# Copyright (c) 2004-2013 LOGILAB S.A. (Paris, FRANCE). 16 +# Copyright 2012 Google Inc. 17 +# 18 # http://www.logilab.fr/ -- mailto:contact@logilab.fr 19 # This program is free software; you can redistribute it and/or modify it under 20 # the terms of the GNU General Public License as published by the Free Software 21 # Foundation; either version 2 of the License, or (at your option) any later 22 # version.
@@ -15,10 +17,11 @@
23 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 24 """Checker for string formatting operations. 25 """ 26 27 from logilab import astng 28 + 29 from pylint.interfaces import IASTNGChecker 30 from pylint.checkers import BaseChecker 31 from pylint.checkers import utils 32 33
@@ -73,11 +76,11 @@
34 """Checks string formatting operations to ensure that the format string 35 is valid and the arguments match the format string. 36 """ 37 38 __implements__ = (IASTNGChecker,) 39 - name = 'string_format' 40 + name = 'string' 41 msgs = MSGS 42 43 def visit_binop(self, node): 44 if node.op != '%': 45 return
@@ -156,8 +159,35 @@
46 self.add_message('E1305', node=node) 47 elif num_args < required_num_args: 48 self.add_message('E1306', node=node) 49 50 51 +class StringMethodsChecker(BaseChecker): 52 + __implements__ = (IASTNGChecker,) 53 + name = 'string' 54 + msgs = { 55 + 'E1310': ("Suspicious argument in %s.%s call", 56 + "bad-str-strip-call", 57 + "The argument to a str.{l,r,}strip call contains a" 58 + " duplicate character, "), 59 + } 60 + 61 + def visit_callfunc(self, node): 62 + func = utils.safe_infer(node.func) 63 + if (isinstance(func, astng.BoundMethod) 64 + and isinstance(func.bound, astng.Instance) 65 + and func.bound.name in ('str', 'unicode', 'bytes') 66 + and func.name in ('strip', 'lstrip', 'rstrip') 67 + and node.args): 68 + arg = utils.safe_infer(node.args[0]) 69 + if not isinstance(arg, astng.Const): 70 + return 71 + if len(arg.value) != len(set(arg.value)): 72 + self.add_message('E1310', node=node, 73 + args=(func.bound.name, func.name)) 74 + 75 + 76 + 77 def register(linter): 78 """required method to auto register this checker """ 79 linter.register_checker(StringFormatChecker(linter)) 80 + linter.register_checker(StringMethodsChecker(linter))
@@ -0,0 +1,9 @@
81 +"""Suspicious str.strip calls.""" 82 +__revision__ = 1 83 + 84 +''.strip('yo') 85 +''.strip() 86 + 87 +u''.strip('http://') 88 +u''.lstrip('http://') 89 +b''.rstrip('http://')
@@ -0,0 +1,3 @@
90 +E: 7: Suspicious argument in unicode.strip call 91 +E: 8: Suspicious argument in unicode.lstrip call 92 +E: 9: Suspicious argument in str.rstrip call