Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/idlelib/IOBinding.py

    r2 r391  
    88import os
    99import types
     10import pipes
    1011import sys
    1112import codecs
     
    1718from SimpleDialog import SimpleDialog
    1819
    19 from configHandler import idleConf
     20from idlelib.configHandler import idleConf
    2021
    2122try:
     
    7172encoding = encoding.lower()
    7273
    73 coding_re = re.compile("coding[:=]\s*([-\w_.]+)")
     74coding_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)')
    7475
    7576class EncodingMessage(SimpleDialog):
     
    125126    """
    126127    # Only consider the first two lines
    127     str = str.split("\n")[:2]
    128     str = "\n".join(str)
    129 
    130     match = coding_re.search(str)
    131     if not match:
     128    lst = str.split("\n", 2)[:2]
     129    for line in lst:
     130        match = coding_re.match(line)
     131        if match is not None:
     132            break
     133    else:
    132134        return None
    133135    name = match.group(1)
     
    197199
    198200    def open(self, event=None, editFile=None):
    199         if self.editwin.flist:
     201        flist = self.editwin.flist
     202        # Save in case parent window is closed (ie, during askopenfile()).
     203        if flist:
    200204            if not editFile:
    201205                filename = self.askopenfile()
     
    203207                filename=editFile
    204208            if filename:
    205                 # If the current window has no filename and hasn't been
    206                 # modified, we replace its contents (no loss).  Otherwise
    207                 # we open a new window.  But we won't replace the
    208                 # shell window (which has an interp(reter) attribute), which
    209                 # gets set to "not modified" at every new prompt.
    210                 try:
    211                     interp = self.editwin.interp
    212                 except AttributeError:
    213                     interp = None
    214                 if not self.filename and self.get_saved() and not interp:
    215                     self.editwin.flist.open(filename, self.loadfile)
     209                # If editFile is valid and already open, flist.open will
     210                # shift focus to its existing window.
     211                # If the current window exists and is a fresh unnamed,
     212                # unmodified editor window (not an interpreter shell),
     213                # pass self.loadfile to flist.open so it will load the file
     214                # in the current window (if the file is not already open)
     215                # instead of a new window.
     216                if (self.editwin and
     217                        not getattr(self.editwin, 'interp', None) and
     218                        not self.filename and
     219                        self.get_saved()):
     220                    flist.open(filename, self.loadfile)
    216221                else:
    217                     self.editwin.flist.open(filename)
     222                    flist.open(filename)
    218223            else:
    219                 self.text.focus_set()
     224                if self.text:
     225                    self.text.focus_set()
    220226            return "break"
    221         #
     227
    222228        # Code for use outside IDLE:
    223229        if self.get_saved():
     
    244250            # open the file in binary mode so that we can handle
    245251            #   end-of-line convention ourselves.
    246             f = open(filename,'rb')
    247             chars = f.read()
    248             f.close()
    249         except IOError, msg:
     252            with open(filename, 'rb') as f:
     253                chars = f.read()
     254        except IOError as msg:
    250255            tkMessageBox.showerror("I/O Error", str(msg), master=self.text)
    251256            return False
     
    267272        self.set_filename(filename)
    268273        self.text.mark_set("insert", "1.0")
    269         self.text.see("insert")
     274        self.text.yview("insert")
    270275        self.updaterecentfileslist(filename)
    271276        return True
     
    290295        try:
    291296            enc = coding_spec(chars)
    292         except LookupError, name:
     297        except LookupError as name:
    293298            tkMessageBox.showerror(
    294299                title="Error loading the file",
     
    321326        message = "Do you want to save %s before closing?" % (
    322327            self.filename or "this untitled document")
    323         m = tkMessageBox.Message(
    324             title="Save On Close",
    325             message=message,
    326             icon=tkMessageBox.QUESTION,
    327             type=tkMessageBox.YESNOCANCEL,
    328             master=self.text)
    329         reply = m.show()
    330         if reply == "yes":
     328        confirm = tkMessageBox.askyesnocancel(
     329                  title="Save On Close",
     330                  message=message,
     331                  default=tkMessageBox.YES,
     332                  master=self.text)
     333        if confirm:
     334            reply = "yes"
    331335            self.save(None)
    332336            if not self.get_saved():
    333337                reply = "cancel"
     338        elif confirm is None:
     339            reply = "cancel"
     340        else:
     341            reply = "no"
    334342        self.text.focus_set()
    335343        return reply
     
    340348        else:
    341349            if self.writefile(self.filename):
    342                 self.set_saved(1)
     350                self.set_saved(True)
    343351                try:
    344352                    self.editwin.store_file_breaks()
     
    376384            chars = chars.replace("\n", self.eol_convention)
    377385        try:
    378             f = open(filename, "wb")
    379             f.write(chars)
    380             f.flush()
    381             f.close()
     386            with open(filename, "wb") as f:
     387                f.write(chars)
    382388            return True
    383         except IOError, msg:
     389        except IOError as msg:
    384390            tkMessageBox.showerror("I/O Error", str(msg),
    385391                                   master=self.text)
     
    401407            enc = coding_spec(chars)
    402408            failed = None
    403         except LookupError, msg:
     409        except LookupError as msg:
    404410            failed = msg
    405411            enc = None
     
    466472
    467473    def print_window(self, event):
    468         m = tkMessageBox.Message(
    469             title="Print",
    470             message="Print to Default Printer",
    471             icon=tkMessageBox.QUESTION,
    472             type=tkMessageBox.OKCANCEL,
    473             default=tkMessageBox.OK,
    474             master=self.text)
    475         reply = m.show()
    476         if reply != tkMessageBox.OK:
     474        confirm = tkMessageBox.askokcancel(
     475                  title="Print",
     476                  message="Print to Default Printer",
     477                  default=tkMessageBox.OK,
     478                  master=self.text)
     479        if not confirm:
    477480            self.text.focus_set()
    478481            return "break"
     
    489492                os.unlink(tempfilename)
    490493                return "break"
    491         platform=os.name
    492         printPlatform=1
     494        platform = os.name
     495        printPlatform = True
    493496        if platform == 'posix': #posix platform
    494497            command = idleConf.GetOption('main','General',
     
    498501            command = idleConf.GetOption('main','General','print-command-win')
    499502        else: #no printing for this platform
    500             printPlatform=0
     503            printPlatform = False
    501504        if printPlatform:  #we can try to print for this platform
    502             command = command % filename
     505            command = command % pipes.quote(filename)
    503506            pipe = os.popen(command, "r")
    504507            # things can get ugly on NT if there is no printer available.
     
    512515                tkMessageBox.showerror("Print status", output, master=self.text)
    513516        else:  #no printing for this platform
    514             message="Printing is not enabled for this platform: %s" % platform
     517            message = "Printing is not enabled for this platform: %s" % platform
    515518            tkMessageBox.showinfo("Print status", message, master=self.text)
    516519        if tempfilename:
     
    522525
    523526    filetypes = [
    524         ("Python and text files", "*.py *.pyw *.txt", "TEXT"),
    525         ("All text files", "*", "TEXT"),
     527        ("Python files", "*.py *.pyw", "TEXT"),
     528        ("Text files", "*.txt", "TEXT"),
    526529        ("All files", "*"),
    527530        ]
Note: See TracChangeset for help on using the changeset viewer.