eric6/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheck.py

changeset 7637
c878e8255972
parent 7635
0cdead130a81
child 7639
422fd05e9c91
equal deleted inserted replaced
7636:61566f35ab22 7637:c878e8255972
5 5
6 """ 6 """
7 Module implementing the syntax check for Python 2/3. 7 Module implementing the syntax check for Python 2/3.
8 """ 8 """
9 9
10 try: # Only for Py2 10 import queue
11 import Queue as queue
12 except ImportError:
13 import queue
14
15 import ast 11 import ast
16 import re 12 import re
17 import sys
18 import traceback 13 import traceback
19 import multiprocessing 14 import multiprocessing
20 15
21 16
22 try: 17 try:
64 """ 59 """
65 codestring = codestring.replace("\r\n", "\n").replace("\r", "\n") 60 codestring = codestring.replace("\r\n", "\n").replace("\r", "\n")
66 61
67 if codestring and codestring[-1] != '\n': 62 if codestring and codestring[-1] != '\n':
68 codestring = codestring + '\n' 63 codestring = codestring + '\n'
69
70 # Check type for py2: if not str it's unicode
71 if sys.version_info[0] == 2:
72 try:
73 codestring = codestring.encode('utf-8')
74 except UnicodeError:
75 pass
76 64
77 return codestring 65 return codestring
78 66
79 67
80 def extractLineFlags(line, startComment="#", endComment="", flagsLine=False): 68 def extractLineFlags(line, startComment="#", endComment="", flagsLine=False):
233 import builtins 221 import builtins
234 except ImportError: 222 except ImportError:
235 import __builtin__ as builtins # __IGNORE_WARNING__ 223 import __builtin__ as builtins # __IGNORE_WARNING__
236 224
237 try: 225 try:
238 if sys.version_info[0] == 2:
239 file_enc = filename.encode(sys.getfilesystemencoding())
240 else:
241 file_enc = filename
242
243 codestring = normalizeCode(codestring) 226 codestring = normalizeCode(codestring)
244 227
245 # Check for VCS conflict markers 228 # Check for VCS conflict markers
246 for conflictMarkerRe in VcsConflictMarkerRegExpList: 229 for conflictMarkerRe in VcsConflictMarkerRegExpList:
247 conflict = conflictMarkerRe.search(codestring) 230 conflict = conflictMarkerRe.search(codestring)
248 if conflict is not None: 231 if conflict is not None:
249 start, i = conflict.span() 232 start, i = conflict.span()
250 lineindex = 1 + codestring.count("\n", 0, start) 233 lineindex = 1 + codestring.count("\n", 0, start)
251 return [{'error': 234 return [{'error':
252 (file_enc, lineindex, 0, "", 235 (filename, lineindex, 0, "",
253 "VCS conflict marker found") 236 "VCS conflict marker found")
254 }] 237 }]
255 238
256 if filename.endswith('.ptl'): 239 if filename.endswith('.ptl'):
257 try: 240 try:
258 import quixote.ptl_compile 241 import quixote.ptl_compile
259 except ImportError: 242 except ImportError:
260 return [{'error': (filename, 0, 0, '', 243 return [{'error': (filename, 0, 0, '',
261 'Quixote plugin not found.')}] 244 'Quixote plugin not found.')}]
262 template = quixote.ptl_compile.Template(codestring, file_enc) 245 template = quixote.ptl_compile.Template(codestring, filename)
263 template.compile() 246 template.compile()
264 else: 247 else:
265 module = builtins.compile( 248 module = builtins.compile(
266 codestring, file_enc, 'exec', ast.PyCF_ONLY_AST) 249 codestring, filename, 'exec', ast.PyCF_ONLY_AST)
267 except SyntaxError as detail: 250 except SyntaxError as detail:
268 index = 0 251 index = 0
269 code = "" 252 code = ""
270 error = "" 253 error = ""
271 lines = traceback.format_exception_only(SyntaxError, detail) 254 lines = traceback.format_exception_only(SyntaxError, detail)
272 if sys.version_info[0] == 2:
273 lines = [x.decode(sys.getfilesystemencoding()) for x in lines]
274 match = re.match(r'\s*File "(.+)", line (\d+)', 255 match = re.match(r'\s*File "(.+)", line (\d+)',
275 lines[0].replace('<string>', filename)) 256 lines[0].replace('<string>', filename))
276 if match is not None: 257 if match is not None:
277 fn, line = match.group(1, 2) 258 fn, line = match.group(1, 2)
278 if lines[1].startswith('SyntaxError:'): 259 if lines[1].startswith('SyntaxError:'):

eric ide

mercurial