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

branch
eric7
changeset 10111
049fbbd2253d
parent 9924
b41c9a7bcbbb
child 10162
e7040c88b39e
--- a/src/eric7/Plugins/CheckerPlugins/SyntaxChecker/pyCheckSyntax.py	Sat Jul 08 12:08:27 2023 +0200
+++ b/src/eric7/Plugins/CheckerPlugins/SyntaxChecker/pyCheckSyntax.py	Sat Jul 08 16:20:59 2023 +0200
@@ -242,96 +242,112 @@
             errors), the message, a list with arguments for the message)
     @rtype dict
     """
-    try:
-        # Check for VCS conflict markers
-        for conflictMarkerRe in VcsConflictMarkerRegExpList:
-            conflict = conflictMarkerRe.search(codestring)
-            if conflict is not None:
-                start, i = conflict.span()
-                lineindex = 1 + codestring.count("\n", 0, start)
-                return [
-                    {"error": (filename, lineindex, 0, "", "VCS conflict marker found")}
-                ]
-
-        if filename.endswith(".ptl"):
-            try:
-                import quixote.ptl_compile  # __IGNORE_WARNING_I10__
-            except ImportError:
-                return [{"error": (filename, 0, 0, "", "Quixote plugin not found.")}]
-            template = quixote.ptl_compile.Template(codestring, filename)
-            template.compile()
-        else:
-            module = builtins.compile(codestring, filename, "exec", ast.PyCF_ONLY_AST)
-    except SyntaxError as detail:
-        index = 0
-        code = ""
-        error = ""
-        lines = traceback.format_exception_only(SyntaxError, detail)
-        match = re.match(
-            r'\s*File "(.+)", line (\d+)', lines[0].replace("<string>", filename)
-        )
-        if match is not None:
-            fn, line = match.group(1, 2)
-            if lines[1].startswith("SyntaxError:"):
-                error = re.match("SyntaxError: (.+)", lines[1]).group(1)
-            else:
-                code = re.match("(.+)", lines[1]).group(1)
-                for seLine in lines[2:]:
-                    if seLine.startswith("SyntaxError:"):
-                        error = re.match("SyntaxError: (.+)", seLine).group(1)
-                    elif seLine.rstrip().endswith("^"):
-                        index = len(seLine.rstrip()) - 4
-        else:
-            fn = detail.filename
-            line = detail.lineno or 1
-            error = detail.msg
-        return [{"error": (fn, int(line), index, code.strip(), error)}]
-    except ValueError as detail:
+    if codestring:
         try:
-            fn = detail.filename
-            line = detail.lineno
-            error = detail.msg
-        except AttributeError:
-            fn = filename
-            line = 1
-            error = str(detail)
-        return [{"error": (fn, line, 0, "", error)}]
-    except Exception as detail:
-        with contextlib.suppress(AttributeError):
-            fn = detail.filename
-            line = detail.lineno
-            error = detail.msg
-            return [{"error": (fn, line, 0, "", error)}]
-
-    # pyflakes
-    if not checkFlakes:
-        return [{}]
+            # Check for VCS conflict markers
+            for conflictMarkerRe in VcsConflictMarkerRegExpList:
+                conflict = conflictMarkerRe.search(codestring)
+                if conflict is not None:
+                    start, i = conflict.span()
+                    lineindex = 1 + codestring.count("\n", 0, start)
+                    return [
+                        {
+                            "error": (
+                                filename,
+                                lineindex,
+                                0,
+                                "",
+                                "VCS conflict marker found",
+                            )
+                        }
+                    ]
 
-    results = []
-    lines = codestring.splitlines()
-    try:
-        warnings = Checker(
-            module, filename, builtins=additionalBuiltins, withDoctest=True
-        )
-        warnings.messages.sort(key=lambda a: a.lineno)
-        for warning in warnings.messages:
-            if ignoreStarImportWarnings and isinstance(
-                warning, (ImportStarUsed, ImportStarUsage)
-            ):
-                continue
+            if filename.endswith(".ptl"):
+                try:
+                    import quixote.ptl_compile  # __IGNORE_WARNING_I10__
+                except ImportError:
+                    return [
+                        {"error": (filename, 0, 0, "", "Quixote plugin not found.")}
+                    ]
+                template = quixote.ptl_compile.Template(codestring, filename)
+                template.compile()
+            else:
+                module = builtins.compile(
+                    codestring, filename, "exec", ast.PyCF_ONLY_AST
+                )
+        except SyntaxError as detail:
+            index = 0
+            code = ""
+            error = ""
+            lines = traceback.format_exception_only(SyntaxError, detail)
+            match = re.match(
+                r'\s*File "(.+)", line (\d+)', lines[0].replace("<string>", filename)
+            )
+            if match is not None:
+                fn, line = match.group(1, 2)
+                if lines[1].startswith("SyntaxError:"):
+                    error = re.match("SyntaxError: (.+)", lines[1]).group(1)
+                else:
+                    code = re.match("(.+)", lines[1]).group(1)
+                    for seLine in lines[2:]:
+                        if seLine.startswith("SyntaxError:"):
+                            error = re.match("SyntaxError: (.+)", seLine).group(1)
+                        elif seLine.rstrip().endswith("^"):
+                            index = len(seLine.rstrip()) - 4
+            else:
+                fn = detail.filename
+                line = detail.lineno or 1
+                error = detail.msg
+            return [{"error": (fn, int(line), index, code.strip(), error)}]
+        except ValueError as detail:
+            try:
+                fn = detail.filename
+                line = detail.lineno
+                error = detail.msg
+            except AttributeError:
+                fn = filename
+                line = 1
+                error = str(detail)
+            return [{"error": (fn, line, 0, "", error)}]
+        except Exception as detail:
+            with contextlib.suppress(AttributeError):
+                fn = detail.filename
+                line = detail.lineno
+                error = detail.msg
+                return [{"error": (fn, line, 0, "", error)}]
 
-            _fn, lineno, col, message, msg_args = warning.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, []))
+        # pyflakes
+        if not checkFlakes:
+            return [{}]
+
+        results = []
+        lines = codestring.splitlines()
+        try:
+            warnings = Checker(
+                module, filename, builtins=additionalBuiltins, withDoctest=True
+            )
+            warnings.messages.sort(key=lambda a: a.lineno)
+            for warning in warnings.messages:
+                if ignoreStarImportWarnings and isinstance(
+                    warning, (ImportStarUsed, ImportStarUsage)
+                ):
+                    continue
 
-    return [{"warnings": results}]
+                _fn, lineno, col, message, msg_args = warning.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}]
+
+    else:
+        return [{}]

eric ide

mercurial