eric7/UI/PythonDisViewer.py

branch
eric7
changeset 8539
24daea9ad41b
parent 8358
144a6b854f70
child 8541
927d57b6aae0
equal deleted inserted replaced
8538:01b7559d3f4e 8539:24daea9ad41b
475 filename = self.__editor.getFileName() 475 filename = self.__editor.getFileName()
476 filename = os.path.basename(filename) if filename else "<dis>" 476 filename = os.path.basename(filename) if filename else "<dis>"
477 477
478 with EricOverrideCursor(): 478 with EricOverrideCursor():
479 try: 479 try:
480 codeObject = self.__tryCompile(source, filename) 480 codeObject = tryCompile(source, filename)
481 except Exception as exc: 481 except Exception as exc:
482 codeObject = None 482 codeObject = None
483 self.__createErrorItem(str(exc)) 483 self.__createErrorItem(str(exc))
484 484
485 if codeObject: 485 if codeObject:
649 endLine = itm.data(0, self.EndLineRole) 649 endLine = itm.data(0, self.EndLineRole)
650 650
651 self.__editor.gotoLine(startLine, firstVisible=True, 651 self.__editor.gotoLine(startLine, firstVisible=True,
652 expand=True) 652 expand=True)
653 self.__editor.setHighlight(startLine - 1, 0, endLine, -1) 653 self.__editor.setHighlight(startLine - 1, 0, endLine, -1)
654
655 def __tryCompile(self, source, name):
656 """
657 Private method to attempt to compile the given source, first as an
658 expression and then as a statement if the first approach fails.
659
660 @param source source code string to be compiled
661 @type str
662 @param name name of the file containing the source
663 @type str
664 @return compiled code
665 @rtype code object
666 """
667 try:
668 c = compile(source, name, 'eval')
669 except SyntaxError:
670 c = compile(source, name, 'exec')
671 return c
672 654
673 def __disassembleObject(self, co, parentItem, parentName="", lasti=-1): 655 def __disassembleObject(self, co, parentItem, parentName="", lasti=-1):
674 """ 656 """
675 Private method to disassemble the given code object recursively. 657 Private method to disassemble the given code object recursively.
676 658
886 """ 868 """
887 Private method to open the configuration dialog. 869 Private method to open the configuration dialog.
888 """ 870 """
889 ericApp().getObject("UserInterface").showPreferences( 871 ericApp().getObject("UserInterface").showPreferences(
890 "pythonPage") 872 "pythonPage")
873
874
875 def tryCompile(source, name):
876 """
877 Function to attempt to compile the given source, first as an
878 expression and then as a statement if the first approach fails.
879
880 @param source source code string to be compiled
881 @type str
882 @param name name of the file containing the source
883 @type str
884 @return compiled code
885 @rtype code object
886 """
887 try:
888 c = compile(source, name, 'eval')
889 except SyntaxError:
890 c = compile(source, name, 'exec')
891 return c
892
893
894 def linestarts(co, filename="", getall=True):
895 """
896 Function to get the line starts for the given code object
897
898 @param co reference to the compiled code object or the source code
899 @type code object or str
900 @param filename name of the source file (optional)
901 @type str
902 @param getall flag indicating to get all line starts recursively
903 @type bool
904 @return list of lines starting some byte code instruction block
905 @rtype list of int
906 """
907 if isinstance(co, str):
908 # try to compile the given source code first
909 try:
910 fn = filename if filename else "<dis>"
911 co = tryCompile(co, fn)
912 except SyntaxError:
913 return []
914
915 starts = [inst[1] for inst in dis.findlinestarts(co)]
916 if getall:
917 for x in co.co_consts:
918 if hasattr(x, 'co_code'):
919 starts.extend(linestarts(x))
920 return sorted(starts)

eric ide

mercurial