[Python-3000-checkins] r57998 - python/branches/py3k/Lib/idlelib/EditorWindow.py python/branches/py3k/Lib/idlelib/FileList.py

kurt.kaiser python-3000-checkins at python.org
Thu Sep 6 06:03:04 CEST 2007


Author: kurt.kaiser
Date: Thu Sep  6 06:03:04 2007
New Revision: 57998

Modified:
   python/branches/py3k/Lib/idlelib/EditorWindow.py
   python/branches/py3k/Lib/idlelib/FileList.py
Log:
1. Fail gracefully if the file fails to decode when loaded.
2. If the load fails, close the half-built edit window.
3. Don't reopen the file to check the shebang.
4. Clarify that we're setting tabs in Tk.

M    EditorWindow.py
M    FileList.py


Modified: python/branches/py3k/Lib/idlelib/EditorWindow.py
==============================================================================
--- python/branches/py3k/Lib/idlelib/EditorWindow.py	(original)
+++ python/branches/py3k/Lib/idlelib/EditorWindow.py	Thu Sep  6 06:03:04 2007
@@ -224,42 +224,32 @@
         # conceivable file).
         # Making the initial values larger slows things down more often.
         self.num_context_lines = 50, 500, 5000000
-
         self.per = per = self.Percolator(text)
-        if self.ispythonsource(filename):
-            self.color = color = self.ColorDelegator()
-            per.insertfilter(color)
-        else:
-            self.color = None
-
+        self.color = None
         self.undo = undo = self.UndoDelegator()
         per.insertfilter(undo)
         text.undo_block_start = undo.undo_block_start
         text.undo_block_stop = undo.undo_block_stop
         undo.set_saved_change_hook(self.saved_change_hook)
-
         # IOBinding implements file I/O and printing functionality
         self.io = io = self.IOBinding(self)
         io.set_filename_change_hook(self.filename_change_hook)
-
-        # Create the recent files submenu
-        self.recent_files_menu = Menu(self.menubar)
-        self.menudict['file'].insert_cascade(3, label='Recent Files',
-                                             underline=0,
-                                             menu=self.recent_files_menu)
-        self.update_recent_files_list()
-
+        self.good_load = False
+        self.set_indentation_params(False)
         if filename:
             if os.path.exists(filename) and not os.path.isdir(filename):
-                io.loadfile(filename)
+                if io.loadfile(filename):
+                    self.good_load = True
+                    is_py_src = self.ispythonsource(filename)
+                    self.set_indentation_params(is_py_src)
+                    if is_py_src:
+                        self.color = color = self.ColorDelegator()
+                        per.insertfilter(color)
             else:
                 io.set_filename(filename)
         self.saved_change_hook()
-
-        self.set_indentation_params(self.ispythonsource(filename))
-
+        self.update_recent_files_list()
         self.load_extensions()
-
         menu = self.menudict.get('windows')
         if menu:
             end = menu.index("end")
@@ -337,13 +327,15 @@
             underline, label = prepstr(label)
             menudict[name] = menu = Menu(mbar, name=name)
             mbar.add_cascade(label=label, menu=menu, underline=underline)
-
         if sys.platform == 'darwin' and '.framework' in sys.executable:
             # Insert the application menu
             menudict['application'] = menu = Menu(mbar, name='apple')
             mbar.add_cascade(label='IDLE', menu=menu)
-
         self.fill_menus()
+        self.recent_files_menu = Menu(self.menubar)
+        self.menudict['file'].insert_cascade(3, label='Recent Files',
+                                             underline=0,
+                                             menu=self.recent_files_menu)
         self.base_helpmenu_length = self.menudict['help'].index(END)
         self.reset_help_menu_entries()
 
@@ -489,7 +481,7 @@
         text.see("insert")
 
     def open_module(self, event=None):
-        # XXX Shouldn't this be in IOBinding or in FileList?
+        # XXX Shouldn't this be in IOBinding?
         try:
             name = self.text.get("sel.first", "sel.last")
         except TclError:
@@ -552,13 +544,8 @@
         base, ext = os.path.splitext(os.path.basename(filename))
         if os.path.normcase(ext) in (".py", ".pyw"):
             return True
-        try:
-            f = open(filename)
-            line = f.readline()
-            f.close()
-        except IOError:
-            return False
-        return line.startswith('#!') and line.find('python') >= 0
+        line = self.text.get('1.0', '1.0 lineend')
+        return line.startswith('#!') and 'python' in line
 
     def close_hook(self):
         if self.flist:
@@ -1003,15 +990,16 @@
     # Return the text widget's current view of what a tab stop means
     # (equivalent width in spaces).
 
-    def get_tabwidth(self):
+    def get_tk_tabwidth(self):
         current = self.text['tabs'] or TK_TABWIDTH_DEFAULT
         return int(current)
 
     # Set the text widget's current view of what a tab stop means.
 
-    def set_tabwidth(self, newtabwidth):
+    def set_tk_tabwidth(self, newtabwidth):
         text = self.text
-        if self.get_tabwidth() != newtabwidth:
+        if self.get_tk_tabwidth() != newtabwidth:
+            # Set text widget tab width
             pixels = text.tk.call("font", "measure", text["font"],
                                   "-displayof", text.master,
                                   "n" * newtabwidth)
@@ -1019,20 +1007,14 @@
 
 ### begin autoindent code ###  (configuration was moved to beginning of class)
 
-    # If ispythonsource and guess are true, guess a good value for
-    # indentwidth based on file content (if possible), and if
-    # indentwidth != tabwidth set usetabs false.
-    # In any case, adjust the Text widget's view of what a tab
-    # character means.
-
-    def set_indentation_params(self, ispythonsource, guess=True):
-        if guess and ispythonsource:
+    def set_indentation_params(self, is_py_src, guess=True):
+        if is_py_src and guess:
             i = self.guess_indent()
             if 2 <= i <= 8:
                 self.indentwidth = i
             if self.indentwidth != self.tabwidth:
                 self.usetabs = False
-        self.set_tabwidth(self.tabwidth)
+        self.set_tk_tabwidth(self.tabwidth)
 
     def smart_backspace_event(self, event):
         text = self.text

Modified: python/branches/py3k/Lib/idlelib/FileList.py
==============================================================================
--- python/branches/py3k/Lib/idlelib/FileList.py	(original)
+++ python/branches/py3k/Lib/idlelib/FileList.py	Thu Sep  6 06:03:04 2007
@@ -33,7 +33,12 @@
             # Don't create window, perform 'action', e.g. open in same window
             return action(filename)
         else:
-            return self.EditorWindow(self, filename, key)
+            edit = self.EditorWindow(self, filename, key)
+            if edit.good_load:
+                return edit
+            else:
+                edit._close()
+                return None
 
     def gotofileline(self, filename, lineno=None):
         edit = self.open(filename)


More information about the Python-3000-checkins mailing list