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

branch
eric7
changeset 10188
0f873791d67e
parent 10162
e7040c88b39e
child 10341
3fdffd9cc21d
diff -r cd500ea7f787 -r 0f873791d67e src/eric7/Plugins/CheckerPlugins/SyntaxChecker/pyCheckSyntax.py
--- a/src/eric7/Plugins/CheckerPlugins/SyntaxChecker/pyCheckSyntax.py	Sat Sep 02 12:33:58 2023 +0200
+++ b/src/eric7/Plugins/CheckerPlugins/SyntaxChecker/pyCheckSyntax.py	Sat Sep 02 15:50:01 2023 +0200
@@ -107,10 +107,11 @@
     @type bool
     @param additionalBuiltins list of names pyflakes should consider as builtins
     @type list of str
-    @return dictionary with the keys 'error' and 'warnings' which
-            hold a list containing details about the error/warnings
-            (file name, line number, column, codestring (only at syntax
-            errors), the message, a list with arguments for the message)
+    @return dictionary with the keys 'error', 'py_warnings' and 'warnings' which
+            hold a list containing details about the syntax error, Python warnings
+            and PyFlakes warnings (file name, line number, column, codestring (only
+            for syntax errors), the message and an optional list with arguments for
+            the message)
     @rtype dict
     """
     return __pySyntaxAndPyflakesCheck(
@@ -237,15 +238,38 @@
     @type bool
     @param additionalBuiltins list of names pyflakes should consider as builtins
     @type list of str
-    @return dictionary with the keys 'error' and 'warnings' which
-            hold a list containing details about the error/ warnings
-            (file name, line number, column, codestring (only at syntax
-            errors), the message, a list with arguments for the message)
+    @return dictionary with the keys 'error', 'py_warnings' and 'warnings' which
+            hold a list containing details about the syntax error, Python warnings
+            and PyFlakes warnings (file name, line number, column, codestring (only
+            for syntax errors), the message and an optional list with arguments for
+            the message)
     @rtype dict
     """
     if codestring:
-        warnings.filterwarnings("error")
         errorDict = {}
+        pyWarnings = []
+
+        def showwarning(
+            message,
+            category,
+            filename,
+            lineno,
+            file=None,  # noqa: U100
+            line=None,  # noqa: U100
+        ):
+            pyWarnings.append(
+                (
+                    filename,
+                    lineno,
+                    0,
+                    "",
+                    "{0}: {1}".format(category.__name__, message),
+                )
+            )
+
+        warnings.showwarning = showwarning
+        warnings.filterwarnings("always")
+
         try:
             # Check for VCS conflict markers
             for conflictMarkerRe in VcsConflictMarkerRegExpList:
@@ -321,42 +345,42 @@
         finally:
             warnings.resetwarnings()
 
-        # return the syntax error or warning, if one was detected
+        # return the syntax error, if one was detected
         if errorDict:
             return [errorDict]
 
         # pyflakes
-        if not checkFlakes:
-            return [{}]
-
         results = []
-        lines = codestring.splitlines()
-        try:
-            flakesWarnings = Checker(
-                module, filename, builtins=additionalBuiltins, withDoctest=True
-            )
-            flakesWarnings.messages.sort(key=lambda a: a.lineno)
-            for flakesWarning in flakesWarnings.messages:
-                if ignoreStarImportWarnings and isinstance(
-                    flakesWarning, (ImportStarUsed, ImportStarUsage)
-                ):
-                    continue
+        if checkFlakes:
+            lines = codestring.splitlines()
+            try:
+                flakesWarnings = Checker(
+                    module, filename, builtins=additionalBuiltins, withDoctest=True
+                )
+                flakesWarnings.messages.sort(key=lambda a: a.lineno)
+                for flakesWarning in flakesWarnings.messages:
+                    if ignoreStarImportWarnings and isinstance(
+                        flakesWarning, (ImportStarUsed, ImportStarUsage)
+                    ):
+                        continue
 
-                _fn, lineno, col, message, msg_args = flakesWarning.getMessageData()
-                lineFlags = extractLineFlags(lines[lineno - 1].strip())
-                with contextlib.suppress(IndexError):
-                    lineFlags += extractLineFlags(lines[lineno].strip(), flagsLine=True)
-                if (
-                    "__IGNORE_WARNING__" not in lineFlags
-                    and "__IGNORE_FLAKES_WARNING__" not in lineFlags
-                    and "noqa" not in lineFlags
-                ):
-                    results.append((_fn, lineno, col, "", message, msg_args))
-        except SyntaxError as err:
-            msg = err.text.strip() if err.text.strip() else err.msg
-            results.append((filename, err.lineno, 0, "FLAKES_ERROR", msg, []))
+                    _fn, lineno, col, message, msg_args = flakesWarning.getMessageData()
+                    lineFlags = extractLineFlags(lines[lineno - 1].strip())
+                    with contextlib.suppress(IndexError):
+                        lineFlags += extractLineFlags(
+                            lines[lineno].strip(), flagsLine=True
+                        )
+                    if (
+                        "__IGNORE_WARNING__" not in lineFlags
+                        and "__IGNORE_FLAKES_WARNING__" not in lineFlags
+                        and "noqa" not in lineFlags
+                    ):
+                        results.append((_fn, lineno, col, "", message, msg_args))
+            except SyntaxError as err:
+                msg = err.text.strip() if err.text.strip() else err.msg
+                results.append((filename, err.lineno, 0, "FLAKES_ERROR", msg, []))
 
-        return [{"warnings": results}]
+        return [{"py_warnings": pyWarnings, "warnings": results}]
 
     else:
         return [{}]

eric ide

mercurial