UtilitiesPython2/Py2SyntaxChecker.py

changeset 915
c1e052773c08
parent 805
83ca4d1ff648
child 945
8cd4d08fa9f6
equal deleted inserted replaced
914:01ae5102cd3d 915:c1e052773c08
21 """ 21 """
22 Function to compile one Python source file to Python bytecode. 22 Function to compile one Python source file to Python bytecode.
23 23
24 @param file source filename (string) 24 @param file source filename (string)
25 @param codestring source code (string) 25 @param codestring source code (string)
26 @return A tuple indicating status (1 = an error was found), the 26 @return A tuple indicating status (True = an error was found), the
27 filename, the linenumber, the code string and the error message 27 file name, the line number, the index number, the code string
28 (boolean, string, string, string, string). The values are only 28 and the error message (boolean, string, string, string, string,
29 valid, if the status equals 1. 29 string). The values are only valid, if the status equals 1.
30 """ 30 """
31 import __builtin__ 31 import __builtin__
32 32
33 try: 33 try:
34 if type(file) == type(u""): 34 if type(file) == type(u""):
50 if match is not None: 50 if match is not None:
51 fn, line = match.group(1, 2) 51 fn, line = match.group(1, 2)
52 if lines[1].startswith('SyntaxError:'): 52 if lines[1].startswith('SyntaxError:'):
53 code = "" 53 code = ""
54 error = re.match('SyntaxError: (.+)', lines[1]).group(1) 54 error = re.match('SyntaxError: (.+)', lines[1]).group(1)
55 index = "0"
55 else: 56 else:
56 code = re.match('(.+)', lines[1]).group(1) 57 code = re.match('(.+)', lines[1]).group(1)
57 error = "" 58 error = ""
59 index = "0"
58 for seLine in lines[2:]: 60 for seLine in lines[2:]:
59 if seLine.startswith('SyntaxError:'): 61 if seLine.startswith('SyntaxError:'):
60 error = re.match('SyntaxError: (.+)', seLine).group(1) 62 error = re.match('SyntaxError: (.+)', seLine).group(1)
63 elif seLine.rstrip().endswith('^'):
64 index = len(seLine.rstrip()) - 4
61 else: 65 else:
62 fn = detail.filename 66 fn = detail.filename
63 line = detail.lineno and detail.lineno or 1 67 line = detail.lineno and detail.lineno or 1
64 code = "" 68 code = ""
65 error = detail.msg 69 error = detail.msg
66 return (1, fn, line, code, error) 70 return (1, fn, line, index, code, error)
67 except ValueError, detail: 71 except ValueError, detail:
72 index = "0"
68 try: 73 try:
69 fn = detail.filename 74 fn = detail.filename
70 line = detail.lineno 75 line = detail.lineno
71 error = detail.msg 76 error = detail.msg
72 except AttributeError: 77 except AttributeError:
73 fn = file 78 fn = file
74 line = 1 79 line = 1
75 error = unicode(detail) 80 error = unicode(detail)
76 code = "" 81 code = ""
77 return (1, fn, line, code, error) 82 return (1, fn, line, index, code, error)
78 except StandardError, detail: 83 except StandardError, detail:
79 try: 84 try:
80 fn = detail.filename 85 fn = detail.filename
81 line = detail.lineno 86 line = detail.lineno
82 code = "" 87 code = ""
83 error = detail.msg 88 error = detail.msg
84 return (1, fn, line, code, error) 89 index = "0"
90 return (1, fn, line, index, code, error)
85 except: # this catchall is intentional 91 except: # this catchall is intentional
86 pass 92 pass
87 93
88 return (0, None, None, None, None) 94 return (0, None, None, None, None, None)
89 95
90 def flakesCheck(fileName, codestring, ignoreStarImportWarnings): 96 def flakesCheck(fileName, codestring, ignoreStarImportWarnings):
91 """ 97 """
92 Function to perform a pyflakes check. 98 Function to perform a pyflakes check.
93 99
127 (len(sys.argv) == 3 and sys.argv[1] not in ["-fi", "-fs"]): 133 (len(sys.argv) == 3 and sys.argv[1] not in ["-fi", "-fs"]):
128 print "ERROR" 134 print "ERROR"
129 print "" 135 print ""
130 print "" 136 print ""
131 print "" 137 print ""
138 print ""
132 print "No file name given." 139 print "No file name given."
133 else: 140 else:
134 filename = sys.argv[-1] 141 filename = sys.argv[-1]
135 try: 142 try:
136 codestring = readEncodedFile(filename)[0] 143 codestring = readEncodedFile(filename)[0]
137 codestring = normalizeCode(codestring) 144 codestring = normalizeCode(codestring)
138 145
139 syntaxerror, fname, line, code, error = compile(filename, codestring) 146 syntaxerror, fname, line, index, code, error = \
147 compile(filename, codestring)
140 except IOError, msg: 148 except IOError, msg:
141 # fake a syntax error 149 # fake a syntax error
142 syntaxerror, fname, line, code, error = \ 150 syntaxerror, fname, line, index, code, error = \
143 1, filename, "1", "", "I/O Error: %s" % unicode(msg) 151 1, filename, "1", "0", "", "I/O Error: %s" % unicode(msg)
144 152
145 if syntaxerror: 153 if syntaxerror:
146 print "ERROR" 154 print "ERROR"
147 else: 155 else:
148 print "NO_ERROR" 156 print "NO_ERROR"
149 print fname 157 print fname
150 print line 158 print line
159 print index
151 print code 160 print code
152 print error 161 print error
153 162
154 if not syntaxerror and sys.argv[1] in ["-fi", "-fs"]: 163 if not syntaxerror and sys.argv[1] in ["-fi", "-fs"]:
155 # do pyflakes check 164 # do pyflakes check

eric ide

mercurial