3 # Released to the public domain, by Tim Peters, 28 February 2000.
5 """checkappend.py -- search for multi-argument .append() calls.
7 Usage: specify one or more file or directory paths:
8 checkappend [-v] file_or_dir [file_or_dir] ...
10 Each file_or_dir is checked for multi-argument .append() calls. When
11 a directory, all .py files in the directory, and recursively in its
12 subdirectories, are checked.
14 Use -v for status msgs. Use -vv for more status msgs.
16 In the absence of -v, the only output is pairs of the form
19 line containing the suspicious append
21 Note that this finds multi-argument append calls regardless of whether
22 they're attached to list objects. If a module defines a class with an
23 append method that takes more than one argument, calls to that method
26 Note that this will not find multi-argument list.append calls made via a
27 bound method object. For example, this is not caught:
30 push = somelist.append
45 msg
= string
.join(args
)
47 sys
.stderr
.write("\n")
53 opts
, args
= getopt
.getopt(sys
.argv
[1:], "v")
54 except getopt
.error
, msg
:
55 errprint(str(msg
) + "\n\n" + __doc__
)
57 for opt
, optarg
in opts
:
67 if os
.path
.isdir(file) and not os
.path
.islink(file):
69 print "%s: listing directory" % `
file`
70 names
= os
.listdir(file)
72 fullname
= os
.path
.join(file, name
)
73 if ((os
.path
.isdir(fullname
) and
74 not os
.path
.islink(fullname
))
75 or os
.path
.normcase(name
[-3:]) == ".py"):
82 errprint("%s: I/O Error: %s" % (`
file`
, str(msg
)))
86 print "checking", `
file`
, "..."
88 ok
= AppendChecker(file, f
).run()
90 print "%s: Clean bill of health." % `
file`
99 def __init__(self
, fname
, file):
102 self
.state
= FIND_DOT
107 tokenize
.tokenize(self
.file.readline
, self
.tokeneater
)
108 except tokenize
.TokenError
, msg
:
109 errprint("%s: Token Error: %s" % (`self
.fname`
, str(msg
)))
110 self
.nerrors
= self
.nerrors
+ 1
111 return self
.nerrors
== 0
113 def tokeneater(self
, type, token
, start
, end
, line
,
114 NEWLINE
=tokenize
.NEWLINE
,
115 JUNK
=(tokenize
.COMMENT
, tokenize
.NL
),
124 elif state
is FIND_DOT
:
125 if type is OP
and token
== ".":
128 elif state
is FIND_APPEND
:
129 if type is NAME
and token
== "append":
131 self
.lineno
= start
[0]
136 elif state
is FIND_LPAREN
:
137 if type is OP
and token
== "(":
143 elif state
is FIND_COMMA
:
145 if token
in ("(", "{", "["):
146 self
.level
= self
.level
+ 1
147 elif token
in (")", "}", "]"):
148 self
.level
= self
.level
- 1
151 elif token
== "," and self
.level
== 1:
152 self
.nerrors
= self
.nerrors
+ 1
153 print "%s(%d):\n%s" % (self
.fname
, self
.lineno
,
155 # don't gripe about this stmt again
158 elif state
is FIND_STMT
:
163 raise SystemError("unknown internal state '%s'" % `state`
)
167 if __name__
== '__main__':