]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
M Debugger.py
authorKurt B. Kaiser <kbk@shore.net>
Sat, 14 Dec 2002 04:38:51 +0000 (04:38 +0000)
committerKurt B. Kaiser <kbk@shore.net>
Sat, 14 Dec 2002 04:38:51 +0000 (04:38 +0000)
M IOBinding.py
M PyShell.py

* Could not remove last set of saved breakpoints from a file
* Starting with empty edit window, could not load a file
* Multiple adjacent breakpoints were saved as one breakpoint
* Storing breakpoints whenever a file is closed can get them out
  of synch with the saved version of a file.  Only store them when the
  file is saved.
* Add comment on current limitations on file editing in the presence of
  breakpoints.
* Replace get_current_breaks() with update_breakpoints(), add an update to
  PyShellEditorWindow.breakpoints, which is the master breakpoint data
  structure, and which is used to reload the subprocess debugger.
* Revert Debugger.Debugger.load_breakpoints() to use editwin.breakpoints.
  It is easier to debug the debugger if the breakpoint list in IDLE is
  identical to the list in the subprocess debugger and is transferred when
  the subprocess debugger is restarted, because this list can be easily
  queried.
* Cleanup some linespacing and comments in IOBinding.py

Lib/idlelib/Debugger.py
Lib/idlelib/IOBinding.py
Lib/idlelib/PyShell.py

index a82fdbb21b579bb1614d8a628ad7be6d34973f2f..30d0002eaf6dca682a2b6e9226c981804a2b9d00 100644 (file)
@@ -331,7 +331,7 @@ class Debugger:
         for editwin in pyshell_edit_windows:
             filename = editwin.io.filename
             try:
-                for lineno in editwin.get_current_breaks():
+                for lineno in editwin.breakpoints:
                     self.set_breakpoint_here(filename, lineno)
             except AttributeError:
                 continue
index 594ecf64719156d07c9171f9d62ea6f75c915cae..a90157ea0c73881ca97893862181c581627e72cd 100644 (file)
@@ -13,27 +13,8 @@ import tempfile
 import tkFileDialog
 import tkMessageBox
 import re
-from configHandler import idleConf
-
-#$ event <<open-window-from-file>>
-#$ win <Control-o>
-#$ unix <Control-x><Control-f>
-
-#$ event <<save-window>>
-#$ win <Control-s>
-#$ unix <Control-x><Control-s>
-
-#$ event <<save-window-as-file>>
-#$ win <Alt-s>
-#$ unix <Control-x><Control-w>
 
-#$ event <<print-window>>
-#$ win <Control-p>
-#$ unix <Control-x><Control-p>
-
-#$ event <<save-copy-of-window-as-file>>
-#$ win <Alt-Shift-s>
-#$ unix <Control-x><w>
+from configHandler import idleConf
 
 try:
     from codecs import BOM_UTF8
@@ -85,11 +66,12 @@ else:
 encoding = encoding.lower()
 
 coding_re = re.compile("coding[:=]\s*([-\w_.]+)")
-def coding_spec(str):
 
+def coding_spec(str):
     """Return the encoding declaration according to PEP 263.
-    Raise LookupError if the encoding is declared but unknown."""
 
+    Raise LookupError if the encoding is declared but unknown.
+    """
     # Only consider the first two lines
     str = str.split("\n")[:2]
     str = "\n".join(str)
@@ -107,6 +89,7 @@ def coding_spec(str):
         raise LookupError, "Unknown encoding "+name
     return name
 
+
 class IOBinding:
 
     def __init__(self, editwin):
@@ -218,14 +201,14 @@ class IOBinding:
         self.set_filename(filename)
         self.text.mark_set("insert", "1.0")
         self.text.see("insert")
-
         self.updaterecentfileslist(filename)
         return True
 
     def decode(self, chars):
-        # Try to create a Unicode string. If that fails, let Tcl try
-        # its best
+        """Create a Unicode string
 
+        If that fails, let Tcl try its best
+        """
         # Check presence of a UTF-8 signature first
         if chars.startswith(BOM_UTF8):
             try:
@@ -237,7 +220,6 @@ class IOBinding:
                 # Indicates that this file originally had a BOM
                 self.fileencoding = BOM_UTF8
                 return chars
-
         # Next look for coding specification
         try:
             enc = coding_spec(chars)
@@ -248,19 +230,16 @@ class IOBinding:
                 "installation. The file may not display correctly" % name,
                 master = self.text)
             enc = None
-
         if enc:
             try:
                 return unicode(chars, enc)
             except UnicodeError:
                 pass
-
         # If it is ASCII, we need not to record anything
         try:
             return unicode(chars, 'ascii')
         except UnicodeError:
             pass
-
         # Finally, try the locale's encoding. This is deprecated;
         # the user should declare a non-ASCII encoding
         try:
@@ -295,8 +274,8 @@ class IOBinding:
         else:
             if self.writefile(self.filename):
                 self.set_saved(1)
+                self.editwin.store_file_breaks()
         self.text.focus_set()
-
         return "break"
 
     def save_as(self, event):
@@ -305,8 +284,8 @@ class IOBinding:
             if self.writefile(filename):
                 self.set_filename(filename)
                 self.set_saved(1)
+                self.editwin.store_file_breaks()
         self.text.focus_set()
-
         self.updaterecentfileslist(filename)
         return "break"
 
@@ -315,7 +294,6 @@ class IOBinding:
         if filename:
             self.writefile(filename)
         self.text.focus_set()
-
         self.updaterecentfileslist(filename)
         return "break"
 
@@ -326,7 +304,6 @@ class IOBinding:
             f = open(filename, "w")
             f.write(chars)
             f.close()
-            ## print "saved to", `filename`
             return True
         except IOError, msg:
             tkMessageBox.showerror("I/O Error", str(msg),
@@ -338,14 +315,12 @@ class IOBinding:
             # This is either plain ASCII, or Tk was returning mixed-encoding
             # text to us. Don't try to guess further.
             return chars
-
         # See whether there is anything non-ASCII in it.
         # If not, no need to figure out the encoding.
         try:
             return chars.encode('ascii')
         except UnicodeError:
             pass
-
         # If there is an encoding declared, try this first.
         try:
             enc = coding_spec(chars)
@@ -358,17 +333,14 @@ class IOBinding:
                 return chars.encode(enc)
             except UnicodeError:
                 failed = "Invalid encoding '%s'" % enc
-
         if failed:
             tkMessageBox.showerror(
                 "I/O Error",
                 "%s. Saving as UTF-8" % failed,
                 master = self.text)
-
         # If there was a UTF-8 signature, use that. This should not fail
         if self.fileencoding == BOM_UTF8 or failed:
             return BOM_UTF8 + chars.encode("utf-8")
-
         # Try the original file encoding next, if any
         if self.fileencoding:
             try:
@@ -380,7 +352,6 @@ class IOBinding:
                     % self.fileencoding,
                     master = self.text)
                 return BOM_UTF8 + chars.encode("utf-8")
-
         # Nothing was declared, and we had not determined an encoding
         # on loading. Recommend an encoding line.
         try:
@@ -469,11 +440,8 @@ class IOBinding:
                                                   filetypes=self.filetypes)
         return self.savedialog.show(initialdir=dir, initialfile=base)
 
-
     def updaterecentfileslist(self,filename):
-        #
-        # Updates recent file list on all editor windows
-        #
+        "Update recent file list on all editor windows"
         self.editwin.UpdateRecentFilesList(filename)
 
 def test():
index cf93613e33f0013c174e0dc986a138d8cb9255c7..f6e85f742870263791b0268ebdace7606e8341f4 100644 (file)
@@ -67,10 +67,6 @@ linecache.checkcache = extended_linecache_checkcache
 class PyShellEditorWindow(EditorWindow):
     "Regular text edit window when a shell is present"
 
-    # XXX KBK 10Dec02 Breakpoints are currently removed if module is modified.
-    # In the future, it may be possible to preserve breakpoints by changing
-    # their line numbers as a module is modified.
-
     def __init__(self, *args):
         self.breakpoints = []
         apply(EditorWindow.__init__, (self,) + args)
@@ -78,11 +74,12 @@ class PyShellEditorWindow(EditorWindow):
         self.text.bind("<<clear-breakpoint-here>>", self.clear_breakpoint_here)
         self.text.bind("<<open-python-shell>>", self.flist.open_shell)
 
-        self.breakpointPath=os.path.join(idleConf.GetUserCfgDir(), 'breakpoints.lst')
-
+        self.breakpointPath = os.path.join(idleConf.GetUserCfgDir(),
+                                           'breakpoints.lst')
         # whenever a file is changed, restore breakpoints
         if self.io.filename: self.restore_file_breaks()
-        def filename_changed_hook(old_hook=self.io.filename_change_hook,self=self):
+        def filename_changed_hook(old_hook=self.io.filename_change_hook,
+                                  self=self):
             self.restore_file_breaks()
             old_hook()
         self.io.set_filename_change_hook(filename_changed_hook)
@@ -148,49 +145,82 @@ class PyShellEditorWindow(EditorWindow):
                 pass
 
     def store_file_breaks(self):
-        if not self.breakpoints:
-            return
-        filename=self.io.filename
+        "Save breakpoints when file is saved"
+        # XXX 13 Dec 2002 KBK Currently the file must be saved before it can
+        #     be run.  The breaks are saved at that time.  If we introduce
+        #     a temporary file save feature the save breaks functionality
+        #     needs to be re-verified, since the breaks at the time the
+        #     temp file is created may differ from the breaks at the last
+        #     permanent save of the file.  A break introduced after a save
+        #     will be effective,  but not persistent.  This is necessary to
+        #     keep the saved breaks synched with the saved file.
+        #
+        #     Breakpoints are set as tagged ranges in the text.  Certain
+        #     kinds of edits cause these ranges to be deleted: Inserting
+        #     or deleting a line just before a breakpoint, and certain
+        #     deletions prior to a breakpoint.  These issues need to be
+        #     investigated and understood.  It's not clear if they are
+        #     Tk issues or IDLE issues, or whether they can actually
+        #     be fixed.  Since a modified file has to be saved before it is
+        #     run, and since self.breakpoints (from which the subprocess
+        #     debugger is loaded) is updated during the save, the visible
+        #     breaks stay synched with the subprocess even if one of these
+        #     unexpected breakpoint deletions occurs.
+        breaks = self.breakpoints
+        filename = self.io.filename
         try:
-            lines=open(self.breakpointPath,"r").readlines()
+            lines = open(self.breakpointPath,"r").readlines()
         except IOError:
-            lines=[]
-        new_file=open(self.breakpointPath,"w")
+            lines = []
+        new_file = open(self.breakpointPath,"w")
         for line in lines:
-            if not line.startswith(filename+"="):
+            if not line.startswith(filename + '='):
                 new_file.write(line)
-        new_file.write(filename+"="+`self.get_current_breaks()`+"\n")
+        self.update_breakpoints()
+        breaks = self.breakpoints
+        if breaks:
+            new_file.write(filename + '=' + str(breaks) + '\n')
         new_file.close()
 
     def restore_file_breaks(self):
         self.text.update()   # this enables setting "BREAK" tags to be visible
-        filename=self.io.filename
+        filename = self.io.filename
+        if filename is None:
+            return
         if os.path.isfile(self.breakpointPath):
-            lines=open(self.breakpointPath,"r").readlines()
+            lines = open(self.breakpointPath,"r").readlines()
             for line in lines:
-                if line.startswith(filename+"="):
-                    breakpoint_linenumbers=eval(line[len(filename)+1:]) 
+                if line.startswith(filename + '='):
+                    breakpoint_linenumbers = eval(line[len(filename)+1:]) 
                     for breakpoint_linenumber in breakpoint_linenumbers:
                         self.set_breakpoint(breakpoint_linenumber)
 
-    def get_current_breaks(self):
-        #
-        # retrieves all the breakpoints in the current window
-        #
+    def update_breakpoints(self):
+        "Retrieves all the breakpoints in the current window"
         text = self.text
-        lines = text.tag_ranges("BREAK")
-        result = [int(float((lines[i]))) for i in range(0,len(lines),2)]
-        return result
-    def saved_change_hook(self):
-        "Extend base method - clear breaks if module is modified"
-        if not self.get_saved():
-            self.clear_file_breaks()
-        EditorWindow.saved_change_hook(self)
+        ranges = text.tag_ranges("BREAK")
+        linenumber_list = self.ranges_to_linenumbers(ranges)
+        self.breakpoints = linenumber_list
+
+    def ranges_to_linenumbers(self, ranges):
+        lines = []
+        for index in range(0, len(ranges), 2):
+            lineno = int(float(ranges[index]))
+            end = int(float(ranges[index+1]))
+            while lineno < end:
+                lines.append(lineno)
+                lineno += 1
+        return lines
+
+# XXX 13 Dec 2020 KBK Not used currently
+#    def saved_change_hook(self):
+#        "Extend base method - clear breaks if module is modified"
+#        if not self.get_saved():
+#            self.clear_file_breaks()
+#        EditorWindow.saved_change_hook(self)
 
     def _close(self):
         "Extend base method - clear breaks when module is closed"
-        self.store_file_breaks()
         self.clear_file_breaks()
         EditorWindow._close(self)