[pywin32-checkins] /hgrepo/p/py/pywin32/pywin32: allow option to save as binary whe...
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: <pyw...@li...> - 2011-04-24 07:43:12
|
changeset 1f726f95d353 in /hgrepo/p/py/pywin32/pywin32 details: http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/hgrepo/p/py/pywin32/pywin32?cmd=changeset;node=1f726f95d353 summary: allow option to save as binary when encoding fails (bug 3139486) diffstat: CHANGES.txt | 5 ++ Pythonwin/pywin/framework/editor/document.py | 21 ++++++++-- Pythonwin/pywin/framework/editor/editor.py | 4 +- Pythonwin/pywin/scintilla/document.py | 37 +++++++++--------- Pythonwin/pywin/scintilla/view.py | 4 +- 5 files changed, 43 insertions(+), 28 deletions(-) diffs (145 lines): diff -r a5a35d18b1de -r 1f726f95d353 CHANGES.txt --- a/CHANGES.txt Sun Apr 24 16:25:15 2011 +1000 +++ b/CHANGES.txt Sun Apr 24 17:41:43 2011 +1000 @@ -7,6 +7,11 @@ Since build 216: ---------------- +* Pythonwin now warns, but allows you to continue, when saving a file with + an invalid encoding line (bug 3139486) + +* Fix win32com.client.WithEvents on py3k (bug bug 3199843) + * When passing integers/unsigned integers to COM objects which did not fit into 32bits, OverflowErrors were silently discarded and -1 was silently passed. This has been fixed for signed integers (an OverflowError is now diff -r a5a35d18b1de -r 1f726f95d353 Pythonwin/pywin/framework/editor/document.py --- a/Pythonwin/pywin/framework/editor/document.py Sun Apr 24 16:25:15 2011 +1000 +++ b/Pythonwin/pywin/framework/editor/document.py Sun Apr 24 17:41:43 2011 +1000 @@ -78,12 +78,23 @@ except IOError, details: win32ui.MessageBox("Error - could not save file\r\n\r\n%s"%details) return 0 - except UnicodeEncodeError, details: - win32ui.MessageBox("Encoding failed: \r\n%s"%details + + except (UnicodeEncodeError, LookupError), details: + rc = win32ui.MessageBox("Encoding failed: \r\n%s"%details + '\r\nPlease add desired source encoding as first line of file, eg \r\n' + - '# -*- coding: mbcs -*-', - "File save failed") - return 0 + '# -*- coding: mbcs -*-\r\n\r\n' + + 'If you continue, the file will be saved as binary and will\r\n' + + 'not be valid in the declared encoding.\r\n\r\n' + + 'Save the file as binary with an invalid encoding?', + "File save failed", + win32con.MB_YESNO | win32con.MB_DEFBUTTON2) + if rc==win32con.IDYES: + try: + self.SaveFile(fileName, encoding="latin-1") + except IOError, details: + win32ui.MessageBox("Error - could not save file\r\n\r\n%s"%details) + return 0 + else: + return 0 self.SetModifiedFlag(0) # No longer dirty self.bDeclinedReload = 0 # They probably want to know if it changes again! win32ui.AddToRecentFileList(fileName) diff -r a5a35d18b1de -r 1f726f95d353 Pythonwin/pywin/framework/editor/editor.py --- a/Pythonwin/pywin/framework/editor/editor.py Sun Apr 24 16:25:15 2011 +1000 +++ b/Pythonwin/pywin/framework/editor/editor.py Sun Apr 24 17:41:43 2011 +1000 @@ -109,10 +109,10 @@ else: return data - def SaveFile(self, fileName): + def SaveFile(self, fileName, encoding=None): if isRichText: view = self.GetFirstView() - view.SaveTextFile(fileName) + view.SaveTextFile(fileName, encoding=encoding) else: # Old style edit view window. self.GetFirstView().SaveFile(fileName) try: diff -r a5a35d18b1de -r 1f726f95d353 Pythonwin/pywin/scintilla/document.py --- a/Pythonwin/pywin/scintilla/document.py Sun Apr 24 16:25:15 2011 +1000 +++ b/Pythonwin/pywin/scintilla/document.py Sun Apr 24 17:41:43 2011 +1000 @@ -59,9 +59,9 @@ rc = win32ui.MessageBox("Cannot create the file %s" % filename) return 1 - def SaveFile(self, fileName): + def SaveFile(self, fileName, encoding=None): view = self.GetFirstView() - ok = view.SaveTextFile(fileName) + ok = view.SaveTextFile(fileName, encoding=encoding) if ok: view.SCISetSavePoint() return ok @@ -126,11 +126,9 @@ dec = text.decode(source_encoding) except UnicodeError: print "WARNING: Failed to decode bytes from '%s' encoding - treating as latin1" % source_encoding - print "WARNING: Do not modify this file - you will not be able to save it." dec = text.decode('latin1') except LookupError: print "WARNING: Invalid encoding '%s' specified - treating as latin1" % source_encoding - print "WARNING: Do not modify this file - you will not be able to save it." dec = text.decode('latin1') # and put it back as utf8 - this shouldn't fail. text = dec.encode(default_scintilla_encoding) @@ -148,23 +146,24 @@ # set EOL mode view.SendScintilla(scintillacon.SCI_SETEOLMODE, eol_mode) - def _SaveTextToFile(self, view, filename): + def _SaveTextToFile(self, view, filename, encoding=None): s = view.GetTextRange() # already decoded from scintilla's encoding - source_encoding = None - if self.bom: - source_encoding = self.source_encoding - else: - # no BOM - look for an encoding. - bits = re.split("[\r\n]*", s, 3) - for look in bits[:-1]: - match = re_encoding_text.search(look) - if match is not None: - source_encoding = match.group(1) - self.source_encoding = source_encoding - break - + source_encoding = encoding if source_encoding is None: - source_encoding = 'latin1' + if self.bom: + source_encoding = self.source_encoding + else: + # no BOM - look for an encoding. + bits = re.split("[\r\n]*", s, 3) + for look in bits[:-1]: + match = re_encoding_text.search(look) + if match is not None: + source_encoding = match.group(1) + self.source_encoding = source_encoding + break + + if source_encoding is None: + source_encoding = 'latin1' ## encode data before opening file so script is not lost if encoding fails file_contents = s.encode(source_encoding) diff -r a5a35d18b1de -r 1f726f95d353 Pythonwin/pywin/scintilla/view.py --- a/Pythonwin/pywin/scintilla/view.py Sun Apr 24 16:25:15 2011 +1000 +++ b/Pythonwin/pywin/scintilla/view.py Sun Apr 24 17:41:43 2011 +1000 @@ -384,9 +384,9 @@ self.SCIGotoLine(lineNo) return 0 - def SaveTextFile(self, filename): + def SaveTextFile(self, filename, encoding=None): doc = self.GetDocument() - doc._SaveTextToFile(self, filename) + doc._SaveTextToFile(self, filename, encoding=encoding) doc.SetModifiedFlag(0) return 1 |