9
9
checker = __import__ ('pyflakes.checker' ).checker
10
10
11
11
def check (codeString , filename ):
12
+ """
13
+ Check the Python source given by C{codeString} for flakes.
14
+
15
+ @param codeString: The Python source to check.
16
+ @type codeString: C{str}
17
+
18
+ @param filename: The name of the file the source came from, used to report
19
+ errors.
20
+ @type filename: C{str}
21
+
22
+ @return: The number of warnings emitted.
23
+ @rtype: C{int}
24
+ """
12
25
# Since compiler.parse does not reliably report syntax errors, use the
13
26
# built in compiler first to detect those.
14
27
try :
@@ -18,16 +31,23 @@ def check(codeString, filename):
18
31
19
32
(lineno , offset , text ) = value .lineno , value .offset , value .text
20
33
21
- line = text .splitlines ()[- 1 ]
34
+ # If there's an encoding problem with the file, the text is None.
35
+ if text is None :
36
+ # Avoid using msg, since for the only known case, it contains a
37
+ # bogus message that claims the encoding the file declared was
38
+ # unknown.
39
+ print >> sys .stderr , "%s: problem decoding source" % (filename , )
40
+ else :
41
+ line = text .splitlines ()[- 1 ]
22
42
23
- if offset is not None :
24
- offset = offset - (len (text ) - len (line ))
43
+ if offset is not None :
44
+ offset = offset - (len (text ) - len (line ))
25
45
26
- print >> sys .stderr , '%s:%d: %s' % (filename , lineno , msg )
27
- print >> sys .stderr , line
46
+ print >> sys .stderr , '%s:%d: %s' % (filename , lineno , msg )
47
+ print >> sys .stderr , line
28
48
29
- if offset is not None :
30
- print >> sys .stderr , " " * offset , "^"
49
+ if offset is not None :
50
+ print >> sys .stderr , " " * offset , "^"
31
51
32
52
return 1
33
53
else :
0 commit comments