src/eric7/Plugins/CheckerPlugins/SyntaxChecker/pyCheckSyntax.py

branch
eric7
changeset 10162
e7040c88b39e
parent 10111
049fbbd2253d
child 10188
0f873791d67e
equal deleted inserted replaced
10161:b0ccdb47acde 10162:e7040c88b39e
12 import contextlib 12 import contextlib
13 import multiprocessing 13 import multiprocessing
14 import queue 14 import queue
15 import re 15 import re
16 import traceback 16 import traceback
17 import warnings
17 18
18 with contextlib.suppress(ImportError): 19 with contextlib.suppress(ImportError):
19 from pyflakes.checker import Checker 20 from pyflakes.checker import Checker
20 from pyflakes.messages import ImportStarUsage, ImportStarUsed 21 from pyflakes.messages import ImportStarUsage, ImportStarUsed
21 22
241 (file name, line number, column, codestring (only at syntax 242 (file name, line number, column, codestring (only at syntax
242 errors), the message, a list with arguments for the message) 243 errors), the message, a list with arguments for the message)
243 @rtype dict 244 @rtype dict
244 """ 245 """
245 if codestring: 246 if codestring:
247 warnings.filterwarnings("error")
248 errorDict = {}
246 try: 249 try:
247 # Check for VCS conflict markers 250 # Check for VCS conflict markers
248 for conflictMarkerRe in VcsConflictMarkerRegExpList: 251 for conflictMarkerRe in VcsConflictMarkerRegExpList:
249 conflict = conflictMarkerRe.search(codestring) 252 conflict = conflictMarkerRe.search(codestring)
250 if conflict is not None: 253 if conflict is not None:
296 index = len(seLine.rstrip()) - 4 299 index = len(seLine.rstrip()) - 4
297 else: 300 else:
298 fn = detail.filename 301 fn = detail.filename
299 line = detail.lineno or 1 302 line = detail.lineno or 1
300 error = detail.msg 303 error = detail.msg
301 return [{"error": (fn, int(line), index, code.strip(), error)}] 304 errorDict = {"error": (fn, int(line), index, code.strip(), error)}
302 except ValueError as detail: 305 except ValueError as detail:
303 try: 306 try:
304 fn = detail.filename 307 fn = detail.filename
305 line = detail.lineno 308 line = detail.lineno
306 error = detail.msg 309 error = detail.msg
307 except AttributeError: 310 except AttributeError:
308 fn = filename 311 fn = filename
309 line = 1 312 line = 1
310 error = str(detail) 313 error = str(detail)
311 return [{"error": (fn, line, 0, "", error)}] 314 errorDict = {"error": (fn, line, 0, "", error)}
312 except Exception as detail: 315 except Exception as detail:
313 with contextlib.suppress(AttributeError): 316 with contextlib.suppress(AttributeError):
314 fn = detail.filename 317 fn = detail.filename
315 line = detail.lineno 318 line = detail.lineno
316 error = detail.msg 319 error = detail.msg
317 return [{"error": (fn, line, 0, "", error)}] 320 errorDict = {"error": (fn, line, 0, "", error)}
321 finally:
322 warnings.resetwarnings()
323
324 # return the syntax error or warning, if one was detected
325 if errorDict:
326 return [errorDict]
318 327
319 # pyflakes 328 # pyflakes
320 if not checkFlakes: 329 if not checkFlakes:
321 return [{}] 330 return [{}]
322 331
323 results = [] 332 results = []
324 lines = codestring.splitlines() 333 lines = codestring.splitlines()
325 try: 334 try:
326 warnings = Checker( 335 flakesWarnings = Checker(
327 module, filename, builtins=additionalBuiltins, withDoctest=True 336 module, filename, builtins=additionalBuiltins, withDoctest=True
328 ) 337 )
329 warnings.messages.sort(key=lambda a: a.lineno) 338 flakesWarnings.messages.sort(key=lambda a: a.lineno)
330 for warning in warnings.messages: 339 for flakesWarning in flakesWarnings.messages:
331 if ignoreStarImportWarnings and isinstance( 340 if ignoreStarImportWarnings and isinstance(
332 warning, (ImportStarUsed, ImportStarUsage) 341 flakesWarning, (ImportStarUsed, ImportStarUsage)
333 ): 342 ):
334 continue 343 continue
335 344
336 _fn, lineno, col, message, msg_args = warning.getMessageData() 345 _fn, lineno, col, message, msg_args = flakesWarning.getMessageData()
337 lineFlags = extractLineFlags(lines[lineno - 1].strip()) 346 lineFlags = extractLineFlags(lines[lineno - 1].strip())
338 with contextlib.suppress(IndexError): 347 with contextlib.suppress(IndexError):
339 lineFlags += extractLineFlags(lines[lineno].strip(), flagsLine=True) 348 lineFlags += extractLineFlags(lines[lineno].strip(), flagsLine=True)
340 if ( 349 if (
341 "__IGNORE_WARNING__" not in lineFlags 350 "__IGNORE_WARNING__" not in lineFlags

eric ide

mercurial