eric7/UI/PythonDisViewer.py

branch
eric7
changeset 8539
24daea9ad41b
parent 8358
144a6b854f70
child 8541
927d57b6aae0
diff -r 01b7559d3f4e -r 24daea9ad41b eric7/UI/PythonDisViewer.py
--- a/eric7/UI/PythonDisViewer.py	Tue Aug 24 17:20:58 2021 +0200
+++ b/eric7/UI/PythonDisViewer.py	Tue Aug 24 18:04:32 2021 +0200
@@ -477,7 +477,7 @@
         
         with EricOverrideCursor():
             try:
-                codeObject = self.__tryCompile(source, filename)
+                codeObject = tryCompile(source, filename)
             except Exception as exc:
                 codeObject = None
                 self.__createErrorItem(str(exc))
@@ -652,24 +652,6 @@
                                    expand=True)
             self.__editor.setHighlight(startLine - 1, 0, endLine, -1)
     
-    def __tryCompile(self, source, name):
-        """
-        Private method to attempt to compile the given source, first as an
-        expression and then as a statement if the first approach fails.
-        
-        @param source source code string to be compiled
-        @type str
-        @param name name of the file containing the source
-        @type str
-        @return compiled code
-        @rtype code object
-        """
-        try:
-            c = compile(source, name, 'eval')
-        except SyntaxError:
-            c = compile(source, name, 'exec')
-        return c
-    
     def __disassembleObject(self, co, parentItem, parentName="", lasti=-1):
         """
         Private method to disassemble the given code object recursively.
@@ -888,3 +870,51 @@
         """
         ericApp().getObject("UserInterface").showPreferences(
             "pythonPage")
+
+
+def tryCompile(source, name):
+    """
+    Function to attempt to compile the given source, first as an
+    expression and then as a statement if the first approach fails.
+    
+    @param source source code string to be compiled
+    @type str
+    @param name name of the file containing the source
+    @type str
+    @return compiled code
+    @rtype code object
+    """
+    try:
+        c = compile(source, name, 'eval')
+    except SyntaxError:
+        c = compile(source, name, 'exec')
+    return c
+
+
+def linestarts(co, filename="", getall=True):
+    """
+    Function to get the line starts for the given code object
+    
+    @param co reference to the compiled code object or the source code
+    @type code object or str
+    @param filename name of the source file (optional)
+    @type str
+    @param getall flag indicating to get all line starts recursively
+    @type bool
+    @return list of lines starting some byte code instruction block
+    @rtype list of int
+    """
+    if isinstance(co, str):
+        # try to compile the given source code first
+        try:
+            fn = filename if filename else "<dis>"
+            co = tryCompile(co, fn)
+        except SyntaxError:
+            return []
+    
+    starts = [inst[1] for inst in dis.findlinestarts(co)]
+    if getall:
+        for x in co.co_consts:
+            if hasattr(x, 'co_code'):
+                starts.extend(linestarts(x))
+    return sorted(starts)

eric ide

mercurial