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

branch
eric7
changeset 10188
0f873791d67e
parent 10162
e7040c88b39e
child 10341
3fdffd9cc21d
equal deleted inserted replaced
10187:cd500ea7f787 10188:0f873791d67e
105 @type bool 105 @type bool
106 @param ignoreStarImportWarnings flag indicating to ignore 'star import' warnings 106 @param ignoreStarImportWarnings flag indicating to ignore 'star import' warnings
107 @type bool 107 @type bool
108 @param additionalBuiltins list of names pyflakes should consider as builtins 108 @param additionalBuiltins list of names pyflakes should consider as builtins
109 @type list of str 109 @type list of str
110 @return dictionary with the keys 'error' and 'warnings' which 110 @return dictionary with the keys 'error', 'py_warnings' and 'warnings' which
111 hold a list containing details about the error/warnings 111 hold a list containing details about the syntax error, Python warnings
112 (file name, line number, column, codestring (only at syntax 112 and PyFlakes warnings (file name, line number, column, codestring (only
113 errors), the message, a list with arguments for the message) 113 for syntax errors), the message and an optional list with arguments for
114 the message)
114 @rtype dict 115 @rtype dict
115 """ 116 """
116 return __pySyntaxAndPyflakesCheck( 117 return __pySyntaxAndPyflakesCheck(
117 filename, codestring, checkFlakes, ignoreStarImportWarnings, additionalBuiltins 118 filename, codestring, checkFlakes, ignoreStarImportWarnings, additionalBuiltins
118 ) 119 )
235 @param ignoreStarImportWarnings flag indicating to 236 @param ignoreStarImportWarnings flag indicating to
236 ignore 'star import' warnings 237 ignore 'star import' warnings
237 @type bool 238 @type bool
238 @param additionalBuiltins list of names pyflakes should consider as builtins 239 @param additionalBuiltins list of names pyflakes should consider as builtins
239 @type list of str 240 @type list of str
240 @return dictionary with the keys 'error' and 'warnings' which 241 @return dictionary with the keys 'error', 'py_warnings' and 'warnings' which
241 hold a list containing details about the error/ warnings 242 hold a list containing details about the syntax error, Python warnings
242 (file name, line number, column, codestring (only at syntax 243 and PyFlakes warnings (file name, line number, column, codestring (only
243 errors), the message, a list with arguments for the message) 244 for syntax errors), the message and an optional list with arguments for
245 the message)
244 @rtype dict 246 @rtype dict
245 """ 247 """
246 if codestring: 248 if codestring:
247 warnings.filterwarnings("error")
248 errorDict = {} 249 errorDict = {}
250 pyWarnings = []
251
252 def showwarning(
253 message,
254 category,
255 filename,
256 lineno,
257 file=None, # noqa: U100
258 line=None, # noqa: U100
259 ):
260 pyWarnings.append(
261 (
262 filename,
263 lineno,
264 0,
265 "",
266 "{0}: {1}".format(category.__name__, message),
267 )
268 )
269
270 warnings.showwarning = showwarning
271 warnings.filterwarnings("always")
272
249 try: 273 try:
250 # Check for VCS conflict markers 274 # Check for VCS conflict markers
251 for conflictMarkerRe in VcsConflictMarkerRegExpList: 275 for conflictMarkerRe in VcsConflictMarkerRegExpList:
252 conflict = conflictMarkerRe.search(codestring) 276 conflict = conflictMarkerRe.search(codestring)
253 if conflict is not None: 277 if conflict is not None:
319 error = detail.msg 343 error = detail.msg
320 errorDict = {"error": (fn, line, 0, "", error)} 344 errorDict = {"error": (fn, line, 0, "", error)}
321 finally: 345 finally:
322 warnings.resetwarnings() 346 warnings.resetwarnings()
323 347
324 # return the syntax error or warning, if one was detected 348 # return the syntax error, if one was detected
325 if errorDict: 349 if errorDict:
326 return [errorDict] 350 return [errorDict]
327 351
328 # pyflakes 352 # pyflakes
329 if not checkFlakes:
330 return [{}]
331
332 results = [] 353 results = []
333 lines = codestring.splitlines() 354 if checkFlakes:
334 try: 355 lines = codestring.splitlines()
335 flakesWarnings = Checker( 356 try:
336 module, filename, builtins=additionalBuiltins, withDoctest=True 357 flakesWarnings = Checker(
337 ) 358 module, filename, builtins=additionalBuiltins, withDoctest=True
338 flakesWarnings.messages.sort(key=lambda a: a.lineno) 359 )
339 for flakesWarning in flakesWarnings.messages: 360 flakesWarnings.messages.sort(key=lambda a: a.lineno)
340 if ignoreStarImportWarnings and isinstance( 361 for flakesWarning in flakesWarnings.messages:
341 flakesWarning, (ImportStarUsed, ImportStarUsage) 362 if ignoreStarImportWarnings and isinstance(
342 ): 363 flakesWarning, (ImportStarUsed, ImportStarUsage)
343 continue 364 ):
344 365 continue
345 _fn, lineno, col, message, msg_args = flakesWarning.getMessageData() 366
346 lineFlags = extractLineFlags(lines[lineno - 1].strip()) 367 _fn, lineno, col, message, msg_args = flakesWarning.getMessageData()
347 with contextlib.suppress(IndexError): 368 lineFlags = extractLineFlags(lines[lineno - 1].strip())
348 lineFlags += extractLineFlags(lines[lineno].strip(), flagsLine=True) 369 with contextlib.suppress(IndexError):
349 if ( 370 lineFlags += extractLineFlags(
350 "__IGNORE_WARNING__" not in lineFlags 371 lines[lineno].strip(), flagsLine=True
351 and "__IGNORE_FLAKES_WARNING__" not in lineFlags 372 )
352 and "noqa" not in lineFlags 373 if (
353 ): 374 "__IGNORE_WARNING__" not in lineFlags
354 results.append((_fn, lineno, col, "", message, msg_args)) 375 and "__IGNORE_FLAKES_WARNING__" not in lineFlags
355 except SyntaxError as err: 376 and "noqa" not in lineFlags
356 msg = err.text.strip() if err.text.strip() else err.msg 377 ):
357 results.append((filename, err.lineno, 0, "FLAKES_ERROR", msg, [])) 378 results.append((_fn, lineno, col, "", message, msg_args))
358 379 except SyntaxError as err:
359 return [{"warnings": results}] 380 msg = err.text.strip() if err.text.strip() else err.msg
381 results.append((filename, err.lineno, 0, "FLAKES_ERROR", msg, []))
382
383 return [{"py_warnings": pyWarnings, "warnings": results}]
360 384
361 else: 385 else:
362 return [{}] 386 return [{}]

eric ide

mercurial