(self, filename)
| 121 | eol_convention = os.linesep # default |
| 122 | |
| 123 | def loadfile(self, filename): |
| 124 | try: |
| 125 | try: |
| 126 | with tokenize.open(filename) as f: |
| 127 | chars = f.read() |
| 128 | fileencoding = f.encoding |
| 129 | eol_convention = f.newlines |
| 130 | converted = False |
| 131 | except (UnicodeDecodeError, SyntaxError): |
| 132 | # Wait for the editor window to appear |
| 133 | self.editwin.text.update() |
| 134 | enc = askstring( |
| 135 | "Specify file encoding", |
| 136 | "The file's encoding is invalid for Python 3.x.\n" |
| 137 | "IDLE will convert it to UTF-8.\n" |
| 138 | "What is the current encoding of the file?", |
| 139 | initialvalue='utf-8', |
| 140 | parent=self.editwin.text) |
| 141 | with open(filename, encoding=enc) as f: |
| 142 | chars = f.read() |
| 143 | fileencoding = f.encoding |
| 144 | eol_convention = f.newlines |
| 145 | converted = True |
| 146 | except OSError as err: |
| 147 | messagebox.showerror("I/O Error", str(err), parent=self.text) |
| 148 | return False |
| 149 | except UnicodeDecodeError: |
| 150 | messagebox.showerror("Decoding Error", |
| 151 | "File %s\nFailed to Decode" % filename, |
| 152 | parent=self.text) |
| 153 | return False |
| 154 | |
| 155 | if not isinstance(eol_convention, str): |
| 156 | # If the file does not contain line separators, it is None. |
| 157 | # If the file contains mixed line separators, it is a tuple. |
| 158 | if eol_convention is not None: |
| 159 | messagebox.showwarning("Mixed Newlines", |
| 160 | "Mixed newlines detected.\n" |
| 161 | "The file will be changed on save.", |
| 162 | parent=self.text) |
| 163 | converted = True |
| 164 | eol_convention = os.linesep # default |
| 165 | |
| 166 | self.text.delete("1.0", "end") |
| 167 | self.set_filename(None) |
| 168 | self.fileencoding = fileencoding |
| 169 | self.eol_convention = eol_convention |
| 170 | self.text.insert("1.0", chars) |
| 171 | self.reset_undo() |
| 172 | self.set_filename(filename) |
| 173 | if converted: |
| 174 | # We need to save the conversion results first |
| 175 | # before being able to execute the code |
| 176 | self.set_saved(False) |
| 177 | self.text.mark_set("insert", "1.0") |
| 178 | self.text.yview("insert") |
| 179 | self.updaterecentfileslist(filename) |
| 180 | return True |
no test coverage detected