Sun, 10 Jan 2010 19:19:52 +0000
Did a lot of encoding related stuff.
--- a/DataViews/CodeMetrics.py Sun Jan 10 13:59:15 2010 +0000 +++ b/DataViews/CodeMetrics.py Sun Jan 10 19:19:52 2010 +0000 @@ -211,11 +211,10 @@ @param total dictionary receiving the overall code statistics @return a statistics object with the collected code statistics (SourceStat) """ - file = open(filename, 'r') try: - text = file.read() - finally: - file.close() + text = Utilities.readEncodedFile(filename)[0] + except (UnicodeError, IOError): + return SourceStat() parser = Parser() parser.parse(text)
--- a/Debugger/VariablesViewer.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Debugger/VariablesViewer.py Sun Jan 10 19:19:52 2010 +0000 @@ -18,7 +18,6 @@ from .Config import ConfigVarTypeDispStrings, ConfigVarTypeStrings from .VariableDetailDialog import VariableDetailDialog -##from Utilities import toUnicode import Preferences @@ -515,23 +514,6 @@ else: return VariableItem(parent, dvar, dvalue, dtype) -## def __unicode(self, s): -## """ -## Private method to convert a string to unicode. -## -## @param s the string to be converted (string) -## @return unicode representation of s (unicode object) -## """ -## if isinstance(s, type("")): -## return s -## try: -## u = str(s, self.loc) -## except TypeError: -## u = str(s) -## except UnicodeError: -## u = toUnicode(s) -## return u -## def __addItem(self, parent, vtype, var, value): """ Private method used to add an item to the list. @@ -567,7 +549,6 @@ sval = eval(value) except: sval = value -## itm = self.__generateItem(parent, dvar, self.__unicode(sval), dvtype) itm = self.__generateItem(parent, dvar, str(sval), dvtype) else:
--- a/DocumentationTools/IndexGenerator.py Sun Jan 10 13:59:15 2010 +0000 +++ b/DocumentationTools/IndexGenerator.py Sun Jan 10 19:19:52 2010 +0000 @@ -174,7 +174,7 @@ } + \ self.footerTemplate - f = open(filename, "w") + f = open(filename, "w", encoding = "utf-8") f.write(doc) f.close()
--- a/E4XML/DebuggerPropertiesHandler.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/DebuggerPropertiesHandler.py Sun Jan 10 19:19:52 2010 +0000 @@ -57,15 +57,13 @@ """ Handler method for the "Interpreter" end tag. """ - self.project.debugProperties["INTERPRETER"] = \ - self.utf8_to_code(self.buffer) + self.project.debugProperties["INTERPRETER"] = self.buffer def endDebugClient(self): """ Handler method for the "DebugClient" end tag. """ - self.project.debugProperties["DEBUGCLIENT"] = \ - self.utf8_to_code(self.buffer) + self.project.debugProperties["DEBUGCLIENT"] = self.buffer def startEnvironment(self, attrs): """ @@ -80,7 +78,7 @@ Handler method for the "Environment" end tag. """ self.project.debugProperties["ENVIRONMENTSTRING"] = \ - self.unescape(self.utf8_to_code(self.buffer)) + self.unescape(self.buffer) def startRemoteDebugger(self, attrs): """ @@ -94,15 +92,14 @@ """ Handler method for the "RemoteHost" end tag. """ - self.project.debugProperties["REMOTEHOST"] = \ - self.utf8_to_code(self.buffer) + self.project.debugProperties["REMOTEHOST"] = self.buffer def endRemoteCommand(self): """ Handler method for the "RemoteCommand" end tag. """ self.project.debugProperties["REMOTECOMMAND"] = \ - self.unescape(self.utf8_to_code(self.buffer)) + self.unescape(self.buffer) def startPathTranslation(self, attrs): """ @@ -116,15 +113,13 @@ """ Handler method for the "RemotePath" end tag. """ - self.project.debugProperties["REMOTEPATH"] = \ - self.utf8_to_code(self.buffer) + self.project.debugProperties["REMOTEPATH"] = self.buffer def endLocalPath(self): """ Handler method for the "LocalPath" end tag. """ - self.project.debugProperties["LOCALPATH"] = \ - self.utf8_to_code(self.buffer) + self.project.debugProperties["LOCALPATH"] = self.buffer def startConsoleDebugger(self, attrs): """ @@ -139,7 +134,7 @@ Handler method for the "ConsoleDebugger" end tag. """ self.project.debugProperties["CONSOLECOMMAND"] = \ - self.unescape(self.utf8_to_code(self.buffer)) + self.unescape(self.buffer) def startRedirect(self, attrs): """
--- a/E4XML/MultiProjectHandler.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/MultiProjectHandler.py Sun Jan 10 19:19:52 2010 +0000 @@ -60,7 +60,7 @@ """ Handler method for the "Description" end tag. """ - self.buffer = self.unescape(self.utf8_to_code(self.buffer)) + self.buffer = self.unescape(self.buffer) self.multiProject.description = self.decodedNewLines(self.buffer) def startProject(self, attrs): @@ -83,20 +83,20 @@ """ Handler method for the "ProjectName" end tag. """ - self.project["name"] = self.unescape(self.utf8_to_code(self.buffer)) + self.project["name"] = self.unescape(self.buffer) def endProjectFile(self): """ Handler method for the "ProjectFile" end tag. """ - filename = self.utf8_to_code(self.buffer) + filename = self.buffer self.project["file"] = Utilities.toNativeSeparators(filename) def endProjectDescription(self): """ Handler method for the "ProjectDescription" end tag. """ - self.buffer = self.unescape(self.utf8_to_code(self.buffer)) + self.buffer = self.unescape(self.buffer) self.project["description"] = self.decodedNewLines(self.buffer) def getVersion(self):
--- a/E4XML/PluginRepositoryHandler.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/PluginRepositoryHandler.py Sun Jan 10 19:19:52 2010 +0000 @@ -88,50 +88,50 @@ """ Handler method for the "Name" end tag. """ - self.info["name"] = self.unescape(self.utf8_to_code(self.buffer)) + self.info["name"] = self.unescape(self.buffer) def endShort(self): """ Handler method for the "Short" end tag. """ - self.info["short"] = self.unescape(self.utf8_to_code(self.buffer)) + self.info["short"] = self.unescape(self.buffer) def endDescription(self): """ Handler method for the "Description" end tag. """ - txt = self.unescape(self.utf8_to_code(self.buffer)) + txt = self.unescape(self.buffer) self.info["description"] = [line.strip() for line in txt.splitlines()] def endUrl(self): """ Handler method for the "Url" end tag. """ - self.info["url"] = self.unescape(self.utf8_to_code(self.buffer)) + self.info["url"] = self.unescape(self.buffer) def endAuthor(self): """ Handler method for the "Author" end tag. """ - self.info["author"] = self.unescape(self.utf8_to_code(self.buffer)) + self.info["author"] = self.unescape(self.buffer) def endVersion(self): """ Handler method for the "Version" end tag. """ - self.info["version"] = self.unescape(self.utf8_to_code(self.buffer)) + self.info["version"] = self.unescape(self.buffer) def endFilename(self): """ Handler method for the "Filename" end tag. """ - self.info["filename"] = self.unescape(self.utf8_to_code(self.buffer)) + self.info["filename"] = self.unescape(self.buffer) def endRepositoryUrl(self): """ Handler method for the "RepositoryUrl" end tag. """ - url = self.unescape(self.utf8_to_code(self.buffer)).strip() + url = self.unescape(self.buffer).strip() Preferences.setUI("PluginRepositoryUrl5", url) def getVersion(self): @@ -140,4 +140,4 @@ @return String containing the version number. """ - return self.version \ No newline at end of file + return self.version
--- a/E4XML/ProjectHandler.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/ProjectHandler.py Sun Jan 10 19:19:52 2010 +0000 @@ -97,21 +97,20 @@ """ Handler method for the "Language" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.project.pdata["SPELLLANGUAGE"] = [self.buffer] def endProjectWordList(self): """ Handler method for the "ProjectWordList" end tag. """ - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) self.project.pdata["SPELLWORDS"] = [path] def endProjectExcludeList(self): """ Handler method for the "ProjectExcludeList" end tag. """ - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) self.project.pdata["SPELLEXCLUDES"] = [path] def startProgLanguage(self, attrs): @@ -128,21 +127,19 @@ """ Handler method for the "ProgLanguage" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.project.pdata["PROGLANGUAGE"] = [self.buffer] def endProjectType(self): """ Handler method for the "ProjectType" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.project.pdata["PROJECTTYPE"] = [self.buffer] def endDescription(self): """ Handler method for the "Description" end tag. """ - self.buffer = self.unescape(self.utf8_to_code(self.buffer)) + self.buffer = self.unescape(self.buffer) if self.version >= '4.3': self.project.pdata["DESCRIPTION"] = [self.decodedNewLines(self.buffer)] else: @@ -152,28 +149,27 @@ """ Handler method for the "Version" end tag. """ - self.buffer = self.unescape(self.utf8_to_code(self.buffer)) + self.buffer = self.unescape(self.buffer) self.project.pdata["VERSION"] = [self.buffer] def endAuthor(self): """ Handler method for the "Author" end tag. """ - self.buffer = self.unescape(self.utf8_to_code(self.buffer)) + self.buffer = self.unescape(self.buffer) self.project.pdata["AUTHOR"] = [self.buffer] def endEmail(self): """ Handler method for the "Email" end tag. """ - self.buffer = self.unescape(self.utf8_to_code(self.buffer)) + self.buffer = self.unescape(self.buffer) self.project.pdata["EMAIL"] = [self.buffer] def endVcsType(self): """ Handler method for the "VcsType" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.project.pdata["VCS"] = [self.buffer] def startVcsOptions(self, attrs): @@ -306,14 +302,12 @@ """ Handler method for the "Dir" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.pathStack.append(self.buffer) def endName(self): """ Handler method for the "Name" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.pathStack.append(self.buffer) def endTranslationPattern(self): @@ -321,7 +315,7 @@ Handler method for the "TranslationPattern" end tag. """ self.project.pdata["TRANSLATIONPATTERN"].append( - Utilities.toNativeSeparators(self.utf8_to_code(self.buffer))) + Utilities.toNativeSeparators(self.buffer)) def startTranslationsBinPath(self, attrs): """ @@ -337,7 +331,7 @@ Handler method for the "TranslationsBinPath" end tag. """ if self.version >= '4.3': - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) else: path = self.__buildPath() self.project.pdata["TRANSLATIONSBINPATH"].append(path) @@ -356,7 +350,7 @@ Handler method for the "Source" end tag. """ if self.version >= '4.3': - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) else: path = self.__buildPath() self.project.pdata["SOURCES"].append(path) @@ -375,7 +369,7 @@ Handler method for the "Form" end tag. """ if self.version >= '4.3': - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) else: path = self.__buildPath() self.project.pdata["FORMS"].append(path) @@ -394,7 +388,7 @@ Handler method for the "Translation" end tag. """ if self.version >= '4.3': - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) else: path = self.__buildPath() self.project.pdata["TRANSLATIONS"].append(path) @@ -413,7 +407,7 @@ Handler method for the "TranslationException" end tag. """ if self.version >= '4.3': - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) else: path = self.__buildPath() self.project.pdata["TRANSLATIONEXCEPTIONS"].append(path) @@ -432,7 +426,7 @@ Handler method for the "Resource" end tag. """ if self.version >= '4.3': - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) else: path = self.__buildPath() self.project.pdata["RESOURCES"].append(path) @@ -451,7 +445,7 @@ Handler method for the "Interface" end tag. """ if self.version >= '4.3': - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) else: path = self.__buildPath() self.project.pdata["INTERFACES"].append(path) @@ -470,7 +464,7 @@ Handler method for the "Other" end tag. """ if self.version >= '4.3': - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) else: path = self.__buildPath() self.project.pdata["OTHERS"].append(path) @@ -489,7 +483,7 @@ Handler method for the "MainScript" end tag. """ if self.version >= '4.3': - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) else: path = self.__buildPath() self.project.pdata["MAINSCRIPT"] = [path] @@ -553,7 +547,6 @@ """ Handler method for the "UIType" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.project.pdata["PROJECTTYPE"] = [self.buffer] def startTranslationPrefix(self, attrs): @@ -570,7 +563,7 @@ Handler method for the "TranslationPrefix" end tag. """ if self.version >= '4.3': - path = Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + path = Utilities.toNativeSeparators(self.buffer) else: path = self.__buildPath() if not path.endswith("_"):
--- a/E4XML/SessionHandler.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/SessionHandler.py Sun Jan 10 19:19:52 2010 +0000 @@ -95,14 +95,12 @@ """ Handler method for the "MultiProject" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.multiProject.openMultiProject(self.buffer, False) def endProject(self): """ Handler method for the "Project" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.project.openProject(self.buffer, False) def startFilename(self, attrs): @@ -125,7 +123,6 @@ """ Handler method for the "Filename" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.vm.openFiles(self.buffer) ed = self.vm.getOpenEditor(self.buffer) if ed is not None: @@ -179,7 +176,7 @@ """ Handler method for the "BFilename" end tag. """ - self.filename = self.utf8_to_code(self.buffer) + self.filename = self.buffer def startLinenumber(self, attrs): """ @@ -193,8 +190,7 @@ """ Handler method for the "Condition" end tag. """ - cond = self.utf8_to_code(self.buffer) - cond = self.unescape(cond) + cond = self.unescape(self.buffer) if cond == 'None': self.bpCond = '' else: @@ -236,13 +232,12 @@ """ Handler method for the "Special" end tag. """ - self.wpSpecialCond = self.utf8_to_code(self.buffer) + self.wpSpecialCond = self.buffer def endCommandLine(self): """ Handler method for the "CommandLine" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.buffer = self.unescape(self.buffer) self.dbg.setArgvHistory(self.buffer) if not self.isGlobal: @@ -252,7 +247,6 @@ """ Handler method for the "WorkinDirectory" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.dbg.setWdHistory(self.buffer) if not self.isGlobal: self.project.dbgWd = self.buffer @@ -261,7 +255,6 @@ """ Handler method for the "Environment" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.dbg.setEnvHistory(self.buffer) if not self.isGlobal: self.project.dbgEnv = self.buffer @@ -304,7 +297,6 @@ """ Handler method for the "Exception" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.dbgExcList.append(self.buffer) def startIgnoredExceptions(self, attrs): @@ -327,7 +319,6 @@ """ Handler method for the "IgnoredException" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.dbgExcIgnoreList.append(self.buffer) def startAutoClearShell(self, attrs):
--- a/E4XML/ShortcutsHandler.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/ShortcutsHandler.py Sun Jan 10 19:19:52 2010 +0000 @@ -45,19 +45,19 @@ """ Handler method for the "Name" end tag. """ - self.name = self.utf8_to_code(self.buffer) + self.name = self.buffer def endAccel(self): """ Handler method for the "Accel" end tag. """ - self.accel = self.unescape(self.utf8_to_code(self.buffer)) + self.accel = self.unescape(self.buffer) def endAltAccel(self): """ Handler method for the "AltAccel" end tag. """ - self.altAccel = self.unescape(self.utf8_to_code(self.buffer)) + self.altAccel = self.unescape(self.buffer) def startShortcut(self, attrs): """
--- a/E4XML/TasksHandler.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/TasksHandler.py Sun Jan 10 19:19:52 2010 +0000 @@ -106,25 +106,24 @@ """ Handler method for the "Summary" end tag. """ - self.task["summary"] = self.unescape(self.utf8_to_code(self.buffer)) + self.task["summary"] = self.unescape(self.buffer) def endDescription(self): """ Handler method for the "Description" end tag. """ if self.version < '4.1': - self.task["summary"] = self.unescape(self.utf8_to_code(self.buffer)) + self.task["summary"] = self.unescape(self.buffer) elif self.version == '4.1': - self.task["description"] = self.unescape(self.utf8_to_code(self.buffer)) + self.task["description"] = self.unescape(self.buffer) else: - self.buffer = self.unescape(self.utf8_to_code(self.buffer)) + self.buffer = self.unescape(self.buffer) self.task["description"] = self.decodedNewLines(self.buffer) def endCreated(self): """ Handler method for the "Created" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.task["created"] = \ time.mktime(time.strptime(self.buffer, "%Y-%m-%d, %H:%M:%S")) @@ -132,14 +131,12 @@ """ Handler method for the "Dir" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.pathStack.append(self.buffer) def endName(self): """ Handler method for the "Name" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.pathStack.append(self.buffer) def endLinenumber(self): @@ -166,7 +163,7 @@ """ if self.version >= '4.2': self.task["filename"] = \ - Utilities.toNativeSeparators(self.utf8_to_code(self.buffer)) + Utilities.toNativeSeparators(self.buffer) else: self.task["filename"] = self.__buildPath()
--- a/E4XML/TemplatesHandler.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/TemplatesHandler.py Sun Jan 10 19:19:52 2010 +0000 @@ -86,13 +86,13 @@ """ Handler method for the "TemplateText" end tag. """ - self.templateText = self.unescape(self.utf8_to_code(self.buffer)) + self.templateText = self.unescape(self.buffer) def endTemplateDescription(self): """ Handler method for the "TemplateDescription" end tag. """ - self.templateDescription = self.unescape(self.utf8_to_code(self.buffer)) + self.templateDescription = self.unescape(self.buffer) def startTemplates(self, attrs): """ @@ -108,4 +108,4 @@ @return String containing the version number. """ - return self.version \ No newline at end of file + return self.version
--- a/E4XML/UserProjectHandler.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/UserProjectHandler.py Sun Jan 10 19:19:52 2010 +0000 @@ -52,7 +52,6 @@ """ Handler method for the "VcsType" end tag. """ - self.buffer = self.utf8_to_code(self.buffer) self.project.pudata["VCSOVERRIDE"] = [self.buffer] def startVcsStatusMonitorInterval(self, attrs):
--- a/E4XML/XMLHandlerBase.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/XMLHandlerBase.py Sun Jan 10 19:19:52 2010 +0000 @@ -8,8 +8,8 @@ """ import sys -##from types import UnicodeType -import pickle as pickle +import pickle +import base64 from xml.sax.handler import ContentHandler @@ -23,7 +23,6 @@ """ self.startDocumentSpecific = None - # TODO: add support for bytes, bytearray, set, frozenset self.elements = { 'none' : (self.defaultStartElement, self.endNone), 'int' : (self.defaultStartElement, self.endInt), @@ -51,17 +50,6 @@ self.NEWPARA = chr(0x2029) self.NEWLINE = chr(0x2028) - def utf8_to_code(self, text): - """ - Public method to convert a string to unicode and encode it for XML. - - @param text the text to encode (string) - """ - # TODO: convert calls to this method to not use it anymore -## if not isinstance(text, UnicodeType): -## text = str(text, "utf-8") - return text - def unescape(self, text, attribute = False): """ Public method used to unescape certain characters. @@ -184,7 +172,7 @@ """ Handler method for the "string" end tag. """ - s = str(self.utf8_to_code(self.unescape(self.buffer))) + s = str(self.unescape(self.buffer)) self.stack.append(s) def endBytes(self): @@ -200,13 +188,7 @@ """ by = bytearray([int(b) for b in self.buffer.strip().split(",")]) self.stack.append(by) -## def endUnicode(self): -## """ -## Handler method for the "unicode" end tag. -## """ -## u = str(self.utf8_to_code(self.unescape(self.buffer))) -## self.stack.append(u) -## + def startList(self, attrs): """ Handler method for the "list" start tag. @@ -316,5 +298,5 @@ """ Handler method for the "pickle" end tag. """ - pic = self.utf8_to_code(self.buffer).decode(self.pickleEnc) + pic = base64.b64decode(self.buffer.encode("ASCII")) self.stack.append(pickle.loads(pic))
--- a/E4XML/XMLWriterBase.py Sun Jan 10 13:59:15 2010 +0000 +++ b/E4XML/XMLWriterBase.py Sun Jan 10 19:19:52 2010 +0000 @@ -9,6 +9,7 @@ import os import pickle +import base64 class XMLWriterBase(object): """ @@ -36,7 +37,6 @@ dict : self._write_dictionary, set : self._write_set, frozenset : self._write_frozenset, - # TODO: add set, frozenset, bytes, bytearray } self.NEWPARA = chr(0x2029) @@ -49,7 +49,6 @@ @param s string to be written to the XML file @param newline flag indicating a linebreak """ -## self.pf.write("%s%s" % (s.encode('utf-8'), self.pf.write("%s%s" % (s, newline and os.linesep or "")) @@ -254,4 +253,4 @@ @param indent indentation level for prettier output (integer) """ self._write('%s<pickle method="pickle" encoding="base64">%s</pickle>' % \ - (" " * indent, pickle.dumps(value).encode('base64'))) + (" " * indent, str(base64.b64encode(pickle.dumps(value)), "ASCII")))
--- a/Helpviewer/Passwords/PasswordManager.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Helpviewer/Passwords/PasswordManager.py Sun Jan 10 19:19:52 2010 +0000 @@ -167,7 +167,7 @@ loginFile = os.path.join(Utilities.getConfigDir(), "browser", "logins") try: - f = open(loginFile, "w") + f = open(loginFile, "w", encoding = "utf-8") for key, login in list(self.__logins.items()): f.write("%s\n" % key) f.write("%s\n" % login[0]) @@ -198,7 +198,7 @@ loginFile = os.path.join(Utilities.getConfigDir(), "browser", "logins") if os.path.exists(loginFile): try: - f = open(loginFile, "r") + f = open(loginFile, "r", encoding = "utf-8") lines = f.read() f.close() except IOError as err:
--- a/MultiProject/MultiProject.py Sun Jan 10 13:59:15 2010 +0000 +++ b/MultiProject/MultiProject.py Sun Jan 10 19:19:52 2010 +0000 @@ -205,7 +205,7 @@ return False f = gzip.open(fn, "r") else: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") line = f.readline() dtdLine = f.readline() f.close() @@ -276,7 +276,7 @@ return False f = gzip.open(fn, "r") else: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: try: parser.parse(f) @@ -353,7 +353,7 @@ return False f = gzip.open(fn, "w") else: - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") MultiProjectWriter(self, f, os.path.splitext(os.path.basename(fn))[0])\ .writeXML()
--- a/PluginManager/PluginInstallDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/PluginManager/PluginInstallDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -287,7 +287,7 @@ False # parse the plugin module's plugin header - pluginSource = zip.read(pluginFileName) + pluginSource = zip.read(pluginFileName).decode("utf-8") packageName = "" internalPackages = [] needsRestart = False @@ -397,7 +397,7 @@ d = os.path.dirname(outname) if not os.path.exists(d): self.__makedirs(d) - f = open(outname, "w") + f = open(outname, "wb") f.write(zip.read(name)) f.close() self.__installedFiles.append(outname) @@ -406,7 +406,7 @@ compileUiFiles(os.path.join(destination, packageName), True) else: outname = os.path.join(destination, pluginFileName) - f = open(outname, "w") + f = open(outname, "w", encoding = "utf-8") f.write(pluginSource) f.close() self.__installedFiles.append(outname)
--- a/PluginManager/PluginManager.py Sun Jan 10 13:59:15 2010 +0000 +++ b/PluginManager/PluginManager.py Sun Jan 10 19:19:52 2010 +0000 @@ -164,7 +164,7 @@ # create the global plugins directory os.mkdir(self.pluginDirs["global"], 0o755) fname = os.path.join(self.pluginDirs["global"], "__init__.py") - f = open() + f = open(fname, "w", encoding = "utf-8") f.write('# -*- coding: utf-8 -*-' + os.linesep) f.write(os.linesep) f.write('"""' + os.linesep)
--- a/PluginManager/PluginRepositoryDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/PluginManager/PluginRepositoryDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -250,7 +250,7 @@ if os.path.exists(self.pluginRepositoryFile): self.__repositoryMissing = False try: - f = open(self.pluginRepositoryFile, "r") + f = open(self.pluginRepositoryFile, "r", encoding = "utf-8") line = f.readline() dtdLine = f.readline() f.close() @@ -274,7 +274,7 @@ parser.setErrorHandler(eh) try: - f = open(self.pluginRepositoryFile, "r") + f = open(self.pluginRepositoryFile, "r", encoding = "utf-8") try: try: parser.parse(f) @@ -644,4 +644,4 @@ ).format(applPath), self.trUtf8('OK')) - self.close() \ No newline at end of file + self.close()
--- a/Plugins/CheckerPlugins/Tabnanny/Tabnanny.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Plugins/CheckerPlugins/Tabnanny/Tabnanny.py Sun Jan 10 19:19:52 2010 +0000 @@ -106,14 +106,9 @@ check_equal = 0 try: - f = open(file, "r") - except IOError as msg: - return (True, file, "1", "I/O Error: %s" % str(msg)) - - try: - text = f.read() - finally: - f.close() + text = Utilities.readEncodedFile(file)[0] + except (UnicodeError, IOError) as msg: + return (True, file, "1", "Error: %s" % str(msg)) # convert eols text = Utilities.convertLineEnds(text, os.linesep) @@ -123,24 +118,19 @@ process_tokens(tokenize.generate_tokens(source.readline)) except tokenize.TokenError as msg: - f.close() return (True, file, "1", "Token Error: %s" % str(msg)) except IndentationError as err: - f.close() return (True, file, err.lineno, "Indentation Error: %s" % str(err.msg)) except NannyNag as nag: badline = nag.get_lineno() line = nag.get_line() - f.close() return (True, file, str(badline), line) except Exception as err: - f.close() return (True, file, "1", "Unspecific Error: %s" % str(err)) - f.close() return (False, None, None, None) class Whitespace(object): @@ -400,4 +390,4 @@ if not indents[-1].equal(thisguy): witness = indents[-1].not_equal_witness(thisguy) msg = "indent not equal e.g. " + format_witnesses(witness) - raise NannyNag(start[0], msg, line) \ No newline at end of file + raise NannyNag(start[0], msg, line)
--- a/Plugins/VcsPlugins/vcsPySvn/SvnDiffDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Plugins/VcsPlugins/vcsPySvn/SvnDiffDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -345,7 +345,7 @@ fname = Utilities.toNativeSeparators(fname) try: - f = open(fname, "w") + f = open(fname, "w", encoding = "utf-8") f.write(self.contents.toPlainText()) f.close() except IOError as why:
--- a/Plugins/VcsPlugins/vcsSubversion/SvnDiffDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Plugins/VcsPlugins/vcsSubversion/SvnDiffDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -307,7 +307,7 @@ fname = Utilities.toNativeSeparators(fname) try: - f = open(fname, "w") + f = open(fname, "w", encoding = "utf-8") f.write(self.contents.toPlainText()) f.close() except IOError as why:
--- a/Plugins/WizardPlugins/PyRegExpWizard/PyRegExpWizardDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Plugins/WizardPlugins/PyRegExpWizard/PyRegExpWizardDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -322,7 +322,7 @@ return try: - f=open(Utilities.toNativeSeparators(fname), "w") + f=open(Utilities.toNativeSeparators(fname), "w", encoding = "utf-8") f.write(self.regexpTextEdit.toPlainText()) f.close() except IOError as err: @@ -343,7 +343,7 @@ self.trUtf8("RegExp Files (*.rx);;All Files (*)")) if fname: try: - f=open(Utilities.toNativeSeparators(fname), "r") + f=open(Utilities.toNativeSeparators(fname), "r", encoding = "utf-8") regexp = f.read() f.close() self.regexpTextEdit.setPlainText(regexp) @@ -678,4 +678,4 @@ self.resize(size) self.connect(self.cw.buttonBox, SIGNAL("accepted()"), self.close) - self.connect(self.cw.buttonBox, SIGNAL("rejected()"), self.close) \ No newline at end of file + self.connect(self.cw.buttonBox, SIGNAL("rejected()"), self.close)
--- a/Plugins/WizardPlugins/QRegExpWizard/QRegExpWizardDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Plugins/WizardPlugins/QRegExpWizard/QRegExpWizardDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -239,7 +239,7 @@ return try: - f=open(Utilities.toNativeSeparators(fname), "w") + f=open(Utilities.toNativeSeparators(fname), "w", encoding = "utf-8") f.write(self.regexpLineEdit.text()) f.close() except IOError as err: @@ -260,7 +260,7 @@ self.trUtf8("RegExp Files (*.rx);;All Files (*)")) if fname: try: - f=open(Utilities.toNativeSeparators(fname), "r") + f=open(Utilities.toNativeSeparators(fname), "r", encoding = "utf-8") regexp = f.read() f.close() self.regexpLineEdit.setText(regexp) @@ -529,4 +529,4 @@ self.resize(size) self.connect(self.cw.buttonBox, SIGNAL("accepted()"), self.close) - self.connect(self.cw.buttonBox, SIGNAL("rejected()"), self.close) \ No newline at end of file + self.connect(self.cw.buttonBox, SIGNAL("rejected()"), self.close)
--- a/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py Sun Jan 10 19:19:52 2010 +0000 @@ -295,7 +295,7 @@ fn += ex try: - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") HighlightingStylesWriter(f, lexers).writeXML() f.close() except IOError as err: @@ -322,7 +322,7 @@ return try: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: line = f.readline() dtdLine = f.readline() @@ -348,7 +348,7 @@ parser.setErrorHandler(eh) try: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: try: parser.parse(f)
--- a/Preferences/Shortcuts.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Preferences/Shortcuts.py Sun Jan 10 19:19:52 2010 +0000 @@ -203,7 +203,7 @@ return 0 f = gzip.open(fn, "w") else: - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") ShortcutsWriter(f).writeXML() @@ -232,7 +232,7 @@ return False f = gzip.open(fn, "r") else: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: line = f.readline() dtdLine = f.readline() @@ -273,7 +273,7 @@ return False f = gzip.open(fn, "r") else: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: try: parser.parse(f)
--- a/Project/CreateDialogCodeDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Project/CreateDialogCodeDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -304,7 +304,7 @@ # new file try: tmplName = os.path.join(getConfig('ericCodeTemplatesDir'), "impl.py.tmpl") - tmplFile = open(tmplName, 'r') + tmplFile = open(tmplName, 'r', encoding = "utf-8") template = tmplFile.read() tmplFile.close() except IOError as why: @@ -337,7 +337,7 @@ else: # extend existing file try: - srcFile = open(self.srcFile, 'r') + srcFile = open(self.srcFile, 'r', encoding = "utf-8") sourceImpl = srcFile.readlines() srcFile.close() if not sourceImpl[-1].endswith(os.linesep): @@ -394,7 +394,7 @@ # write the new code try: - srcFile = open(self.filenameEdit.text(), 'w') + srcFile = open(self.filenameEdit.text(), 'w', encoding = "utf-8") srcFile.write("".join(sourceImpl)) srcFile.close() except IOError as why:
--- a/Project/Project.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Project/Project.py Sun Jan 10 19:19:52 2010 +0000 @@ -558,7 +558,7 @@ return False f = gzip.open(fn, "r") else: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") line = f.readline() dtdLine = f.readline() f.close() @@ -662,7 +662,7 @@ return False f = gzip.open(fn, "r") else: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: try: parser.parse(f) @@ -744,7 +744,7 @@ return False f = gzip.open(fn, "w") else: - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") ProjectWriter(f, os.path.splitext(os.path.basename(fn))[0]).writeXML() @@ -771,7 +771,7 @@ fn = os.path.join(self.getProjectManagementDir(), '%s.e4q' % fn) if os.path.exists(fn): try: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") parser = make_parser(True) handler = UserProjectHandler(self) @@ -814,7 +814,7 @@ fn = os.path.join(self.getProjectManagementDir(), '%s.e4q' % fn) try: - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") UserProjectWriter(f, os.path.splitext(os.path.basename(fn))[0]).writeXML() @@ -859,7 +859,7 @@ else: fn = os.path.join(self.getProjectManagementDir(), '%s%s.e4s' % (fn, indicator)) - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") line = f.readline() dtdLine = f.readline() f.close() @@ -930,7 +930,7 @@ return f = gzip.open(fn, "r") else: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: try: parser.parse(f) @@ -999,7 +999,7 @@ else: fn = os.path.join(self.getProjectManagementDir(), '%s%s.e4s' % (fn, indicator)) - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") SessionWriter(f, os.path.splitext(os.path.basename(fn))[0]).writeXML() @@ -1066,7 +1066,7 @@ fn = os.path.join(self.getProjectManagementDir(), '%s.e4t' % fn) if not os.path.exists(fn): return - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") line = f.readline() dtdLine = f.readline() f.close() @@ -1120,7 +1120,7 @@ return f = gzip.open(fn, "r") else: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: try: parser.parse(f) @@ -1164,7 +1164,7 @@ f = gzip.open(fn, "w") else: fn = os.path.join(self.getProjectManagementDir(), '%s.e4t' % fn) - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") TasksWriter(f, True, os.path.splitext(os.path.basename(fn))[0]).writeXML() @@ -1208,7 +1208,7 @@ f = gzip.open(fn, "r") else: fn = os.path.join(self.getProjectManagementDir(), '%s.e4d' % fn) - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") line = f.readline() dtdLine = f.readline() f.close() @@ -1267,7 +1267,7 @@ return f = gzip.open(fn, "r") else: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: try: parser.parse(f) @@ -1327,7 +1327,7 @@ f = gzip.open(fn, "w") else: fn = os.path.join(self.getProjectManagementDir(), '%s.e4d' % fn) - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") DebuggerPropertiesWriter(f, os.path.splitext(os.path.basename(fn))[0])\ .writeXML() @@ -2241,7 +2241,7 @@ # (only for Python and Python3) if self.pdata["PROGLANGUAGE"][0] in ["Python", "Python3"]: fn = os.path.join(self.ppath, "__init__.py") - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") f.close() self.appendFile(fn, True) tpd = os.path.join(self.ppath, self.translationsRoot) @@ -2288,7 +2288,7 @@ if self.pdata["PROGLANGUAGE"][0] in ["Python", "Python3"]: fn = os.path.join(self.ppath, "__init__.py") if not os.path.exists(fn): - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") f.close() self.appendFile(fn, True) self.saveProject() @@ -4352,7 +4352,7 @@ # write the file try: - pkglistFile = open(pkglist, "w") + pkglistFile = open(pkglist, "w", encoding = "utf-8") pkglistFile.write("\n".join(lst)) pkglistFile.close() except IOError as why: @@ -4394,7 +4394,7 @@ return try: - pkglistFile = open(pkglist, "r") + pkglistFile = open(pkglist, "r", encoding = "utf-8") names = pkglistFile.read() pkglistFile.close() names = sorted(names.splitlines()) @@ -4493,7 +4493,7 @@ @return modified source (string), snapshot version string (string) """ try: - f = open(filename, "r") + f = open(filename, "r", encoding = "utf-8") sourcelines = f.readlines() f.close() except IOError as why: @@ -4532,7 +4532,7 @@ """ version = "0.0.0" try: - f = open(filename, "r") + f = open(filename, "r", encoding = "utf-8") sourcelines = f.readlines() f.close() except IOError as why:
--- a/Project/ProjectFormsBrowser.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Project/ProjectFormsBrowser.py Sun Jan 10 19:19:52 2010 +0000 @@ -612,7 +612,7 @@ if exitStatus == QProcess.NormalExit and exitCode == 0 and self.buf: ofn = os.path.join(self.project.ppath, self.compiledFile) try: - f = open(ofn, "w") + f = open(ofn, "w", encoding = "utf-8") for line in self.buf.splitlines(): f.write(line + os.linesep) f.close()
--- a/Project/ProjectResourcesBrowser.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Project/ProjectResourcesBrowser.py Sun Jan 10 19:19:52 2010 +0000 @@ -421,7 +421,7 @@ return try: - rcfile = open(fname, 'w') + rcfile = open(fname, 'w', encoding = "utf-8") rcfile.write('<!DOCTYPE RCC>\n') rcfile.write('<RCC version="1.0">\n') rcfile.write('<qresource>\n') @@ -510,7 +510,7 @@ if exitStatus == QProcess.NormalExit and exitCode == 0 and self.buf: ofn = os.path.join(self.project.ppath, self.compiledFile) try: - f = open(ofn, "w") + f = open(ofn, "w", encoding = "utf-8") for line in self.buf.splitlines(): f.write(line + os.linesep) f.close() @@ -700,7 +700,7 @@ @return flag indicating some file is newer (boolean) """ try: - f = open(filename, "r") + f = open(filename, "r", encoding = "utf-8") buf = f.read() f.close() except IOError:
--- a/Project/ProjectSourcesBrowser.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Project/ProjectSourcesBrowser.py Sun Jan 10 19:19:52 2010 +0000 @@ -596,7 +596,7 @@ packageFile = os.path.join(packagePath, "__init__.py") if not os.path.exists(packageFile): try: - f = open(packageFile, "w") + f = open(packageFile, "w", encoding = "utf-8") f.close() except IOError as err: QMessageBox.critical(None,
--- a/Project/ProjectTranslationsBrowser.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Project/ProjectTranslationsBrowser.py Sun Jan 10 19:19:52 2010 +0000 @@ -711,7 +711,7 @@ return False try: - pf = open(pfile, "w") + pf = open(pfile, "w", encoding = "utf-8") for key, list in sections: if len(list) > 0: pf.write('%s = ' % key) @@ -1106,4 +1106,4 @@ "generateSelectedWithObsolete" : None, "releaseAll" : None, "releaseSelected" : None, - } \ No newline at end of file + }
--- a/QScintilla/Editor.py Sun Jan 10 13:59:15 2010 +0000 +++ b/QScintilla/Editor.py Sun Jan 10 19:19:52 2010 +0000 @@ -252,10 +252,8 @@ QMessageBox.No) if res == QMessageBox.No or res == QMessageBox.Cancel: raise IOError() - line0 = self.readLine0(self.fileName) - bindName = self.__bindName(line0) - self.__bindLexer(bindName) self.readFile(self.fileName, True) + bindName = self.__bindName(self.text(0)) self.__bindLexer(bindName) self.__bindCompleter(bindName) self.__autoSyntaxCheck() @@ -2171,38 +2169,6 @@ break # Couldn't find the unmodified state - def readLine0(self, fn, createIt = False): - """ - Public slot to read the first line from a file. - - @param fn filename to read from (string) - @param createIt flag indicating the creation of a new file, if the given - one doesn't exist (boolean) - @return first line of the file (string) - """ - try: - if createIt and not os.path.exists(fn): - f = open(fn, "w") - f.close() - f = open(fn, 'r') - except IOError as why: - QMessageBox.critical(self.vm, self.trUtf8('Open File'), - self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>' - '<p>Reason: {1}</p>') - .format(fn, str(why))) - raise - - try: - txt = f.readline() - except UnicodeDecodeError as why: - QMessageBox.critical(self.vm, self.trUtf8('Open File'), - self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>' - '<p>Reason: {1}</p>') - .format(fn, str(why))) - raise - f.close() - return txt - def readFile(self, fn, createIt = False): """ Public slot to read the text from a file. @@ -2211,29 +2177,14 @@ @param createIt flag indicating the creation of a new file, if the given one doesn't exist (boolean) """ + QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) + try: if createIt and not os.path.exists(fn): f = open(fn, "w") f.close() - f = open(fn, 'r') - except IOError as why: - QMessageBox.critical(self.vm, self.trUtf8('Open File'), - self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>' - '<p>Reason: {1}</p>') - .format(fn, str(why))) - raise - - QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) - -## if fn.endswith('.ts') or fn.endswith('.ui'): -## # special treatment for Qt-Linguist and Qt-Designer files -## txt = f.read() -## self.encoding = 'latin-1' -## else: -## txt, self.encoding = f.read(), "utf-8" #Utilities.decode(f.read()) - try: - txt = f.read() - except UnicodeDecodeError as why: + txt, self.encoding = Utilities.readEncodedFile(fn) + except (UnicodeDecodeError, IOError) as why: QApplication.restoreOverrideCursor() QMessageBox.critical(self.vm, self.trUtf8('Open File'), self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>' @@ -2241,8 +2192,6 @@ .format(fn, str(why))) QApplication.restoreOverrideCursor() raise - self.encoding = f.encoding.lower() - f.close() fileEol = self.detectEolString(txt) modified = False @@ -2303,7 +2252,7 @@ Public slot to write the text to a file. @param fn filename to write to (string) - @return flag indicating success + @return flag indicating success (boolean) """ if Preferences.getEditor("StripTrailingWhitespace"): self.__removeTrailingWhitespace() @@ -2318,15 +2267,7 @@ txt += eol else: txt += eol -## try: -## txt, self.encoding = Utilities.encode(txt, self.encoding) -## except Utilities.CodingError as e: -## QMessageBox.critical(self, self.trUtf8('Save File'), -## self.trUtf8('<p>The file <b>{0}</b> could not be saved.<br/>' -## 'Reason: {1}</p>') -## .format(fn, str(e))) -## return False -## + # create a backup file, if the option is set createBackup = Preferences.getEditor("CreateBackupFile") if createBackup: @@ -2352,13 +2293,11 @@ # now write text to the file fn try: - f = open(fn, 'w', encoding = self.encoding) - f.write(txt) - f.close() + self.encoding = Utilities.writeEncodedFile(fn, txt, self.encoding) if createBackup and perms_valid: os.chmod(fn, permissions) return True - except IOError as why: + except (IOError, Utilities.CodingError, UnicodeError) as why: QMessageBox.critical(self, self.trUtf8('Save File'), self.trUtf8('<p>The file <b>{0}</b> could not be saved.<br/>' 'Reason: {1}</p>') @@ -4427,7 +4366,7 @@ return # user aborted try: - f = open(fname, "r") + f = open(fname, "r", encoding = "utf-8") lines = f.readlines() f.close() except IOError: @@ -4487,7 +4426,7 @@ fname = Utilities.toNativeSeparators(fname) try: - f = open(fname, "w") + f = open(fname, "w", encoding = "utf-8") f.write("%s%s" % (name, os.linesep)) f.write(self.macros[name].save()) f.close()
--- a/QScintilla/Exporters/ExporterHTML.py Sun Jan 10 13:59:15 2010 +0000 +++ b/QScintilla/Exporters/ExporterHTML.py Sun Jan 10 19:19:52 2010 +0000 @@ -73,7 +73,7 @@ styleIsUsed[QsciScintilla.STYLE_DEFAULT] = True try: - f = open(filename, "w") + f = open(filename, "w", encoding = "utf-8") f.write( '''<!DOCTYPE html PUBLIC "-//W3C//DTD'''
--- a/QScintilla/Exporters/ExporterRTF.py Sun Jan 10 13:59:15 2010 +0000 +++ b/QScintilla/Exporters/ExporterRTF.py Sun Jan 10 19:19:52 2010 +0000 @@ -145,7 +145,7 @@ bgColour = self.editor.paper() try: - f = open(filename, "w") + f = open(filename, "w", encoding = "utf-8") styles = {} fonts = {}
--- a/QScintilla/Exporters/ExporterTEX.py Sun Jan 10 13:59:15 2010 +0000 +++ b/QScintilla/Exporters/ExporterTEX.py Sun Jan 10 19:19:52 2010 +0000 @@ -145,7 +145,7 @@ styleIsUsed[QsciScintilla.STYLE_DEFAULT] = True try: - f = open(filename, "w") + f = open(filename, "w", encoding = "utf-8") f.write("\\documentclass[a4paper]{article}\n") f.write("\\usepackage[a4paper,margin=1.5cm]{geometry}\n")
--- a/QScintilla/Lexers/LexerPygments.py Sun Jan 10 13:59:15 2010 +0000 +++ b/QScintilla/Lexers/LexerPygments.py Sun Jan 10 19:19:52 2010 +0000 @@ -343,7 +343,6 @@ if self.editor is None: return True -## text = self.editor.text().encode('utf-8') text = self.editor.text() self.__lexer = self.__guessLexer(text) @@ -367,7 +366,6 @@ @param start position of first character to be styled (integer) @param end position of last character to be styled (integer) """ -## text = self.editor.text()[:end + 1].encode('utf-8') text = self.editor.text()[:end + 1] self.__lexer = self.__guessLexer(text)
--- a/QScintilla/MiniEditor.py Sun Jan 10 13:59:15 2010 +0000 +++ b/QScintilla/MiniEditor.py Sun Jan 10 19:19:52 2010 +0000 @@ -27,6 +27,7 @@ from .Printer import Printer +import Utilities import Preferences class MiniScintilla(QsciScintillaCompat): @@ -107,6 +108,7 @@ self.__textEdit.clearSearchIndicators = self.clearSearchIndicators self.__textEdit.setSearchIndicator = self.setSearchIndicator self.__textEdit.setUtf8(True) + self.encoding = Preferences.getEditor("DefaultEncoding") self.srHistory = { "search" : [], @@ -1506,26 +1508,19 @@ @param fileName name of the file to load (string) @param filetype type of the source file (string) """ + QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) + try: - f = open(fileName, 'r') - except IOError as why: - QMessageBox.critical(self, self.trUtf8('Open File'), - self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>' - '<p>Reason: {1}</p>') - .format(fileName, str(why))) - return - - QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) - try: - txt = f.read() - except UnicodeDecodeError as why: + txt, self.encoding = Utilities.readEncodedFile(fileName) + except (UnicodeDecodeError, IOError) as why: QApplication.restoreOverrideCursor() QMessageBox.critical(self, self.trUtf8('Open File'), self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>' '<p>Reason: {1}</p>') .format(fileName, str(why))) + QApplication.restoreOverrideCursor() return - f.close() + self.__textEdit.setText(txt) QApplication.restoreOverrideCursor() @@ -1547,19 +1542,21 @@ @param fileName name of the file to save to (string) @return flag indicating success (boolean) """ - file = QFile(fileName) - if not file.open(QFile.WriteOnly): - QMessageBox.warning(self, self.trUtf8("eric5 Mini Editor"), - self.trUtf8("Cannot write file {0}:\n{1}.")\ - .format(fileName, file.errorString())) + QApplication.setOverrideCursor(Qt.WaitCursor) + txt = self.__textEdit.text() + try: + self.encoding = Utilities.writeEncodedFile(fileName, txt, self.encoding) + except (IOError, Utilities.CodingError, UnicodeError) as why: + QMessageBox.critical(self, self.trUtf8('Save File'), + self.trUtf8('<p>The file <b>{0}</b> could not be saved.<br/>' + 'Reason: {1}</p>') + .format(fileName, str(why))) + QApplication.restoreOverrideCursor() self.__checkActions() return False - out = QTextStream(file) - QApplication.setOverrideCursor(Qt.WaitCursor) - out << self.__textEdit.text() QApplication.restoreOverrideCursor() self.emit(SIGNAL("editorSaved")) @@ -1589,11 +1586,7 @@ self.__textEdit.setModified(False) self.setWindowModified(False) - try: - line0 = self.readLine0(self.__curFile) - except IOError: - line0 = "" - self.setLanguage(self.__bindName(line0)) + self.setLanguage(self.__bindName(self.__textEdit.text(0))) def getFileName(self): """ @@ -2143,41 +2136,6 @@ self.filetype = "D" return bindName - def readLine0(self, fn, createIt = False): - """ - Public slot to read the first line from a file. - - @param fn filename to read from (string) - @param createIt flag indicating the creation of a new file, if the given - one doesn't exist (boolean) - @return first line of the file (string) - """ - if not fn: - return "" - - try: - if createIt and not os.path.exists(fn): - f = open(fn, "w") - f.close() - f = open(fn, 'r') - except IOError as why: - QMessageBox.critical(self, self.trUtf8('Open File'), - self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>' - '<p>Reason: {1}</p>') - .format(fn, str(why))) - raise - - try: - txt = f.readline() - except UnicodeDecodeError as why: - QMessageBox.critical(self, self.trUtf8('Open File'), - self.trUtf8('<p>The file <b>{0}</b> could not be opened.</p>' - '<p>Reason: {1}</p>') - .format(fn, str(why))) - raise - f.close() - return txt - ########################################################## ## Methods needed for the search functionality ##########################################################
--- a/QScintilla/QsciScintillaCompat.py Sun Jan 10 13:59:15 2010 +0000 +++ b/QScintilla/QsciScintillaCompat.py Sun Jan 10 19:19:52 2010 +0000 @@ -681,10 +681,6 @@ else: self.__targetSearchEnd = self.positionFromLineIndex(endline, endindex) -## if self.isUtf8(): -## self.__targetSearchExpr = expr_.encode("utf-8") -## else: -## self.__targetSearchExpr = expr_.encode("latin1") self.__targetSearchExpr = expr_ if self.__targetSearchExpr: @@ -721,10 +717,6 @@ start = self.SendScintilla(QsciScintilla.SCI_GETTARGETSTART) -## if self.isUtf8(): -## r = replaceStr.encode("utf-8") -## else: -## r = replaceStr.encode("latin1") r = replaceStr self.SendScintilla(cmd, len(r), r)
--- a/QScintilla/Shell.py Sun Jan 10 13:59:15 2010 +0000 +++ b/QScintilla/Shell.py Sun Jan 10 19:19:52 2010 +0000 @@ -21,7 +21,6 @@ import Preferences import UI.PixmapCache -##from Utilities import toUnicode from Debugger.DebugClientCapabilities import HasShell, HasCompleter @@ -576,7 +575,6 @@ """ line, col = self.__getEndPos() self.setCursorPosition(line, col) -## self.insert(toUnicode(s)) self.insert(s) self.prline, self.prcol = self.getCursorPosition() self.ensureCursorVisible()
--- a/QScintilla/Terminal.py Sun Jan 10 13:59:15 2010 +0000 +++ b/QScintilla/Terminal.py Sun Jan 10 19:19:52 2010 +0000 @@ -24,7 +24,6 @@ import Utilities import UI.PixmapCache -##from Utilities import toUnicode from .ShellHistoryDialog import ShellHistoryDialog @@ -423,7 +422,6 @@ """ line, col = self.__getEndPos() self.setCursorPosition(line, col) -## self.insert(toUnicode(s)) self.insert(s) self.prline, self.prcol = self.getCursorPosition() self.ensureCursorVisible()
--- a/Tasks/TaskViewer.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Tasks/TaskViewer.py Sun Jan 10 19:19:52 2010 +0000 @@ -771,11 +771,9 @@ fn = os.path.join(self.project.ppath, file) # read the file and split it into textlines try: - f = open(fn, 'r') - text = f.read() + text, encoding = Utilities.readEncodedFile(fn) lines = text.splitlines() - f.close() - except IOError: + except (UnicodeError, IOError): count += 1 self.progress.setValue(count) continue
--- a/Templates/TemplateViewer.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Templates/TemplateViewer.py Sun Jan 10 19:19:52 2010 +0000 @@ -801,7 +801,7 @@ try: if filename is None: filename = os.path.join(Utilities.getConfigDir(), "eric5templates.e4c") - f = open(filename, "w") + f = open(filename, "w", encoding = "utf-8") TemplatesWriter(f, self).writeXML() @@ -823,7 +823,7 @@ filename = os.path.join(Utilities.getConfigDir(), "eric5templates.e4c") if not os.path.exists(filename): return - f = open(filename, "r") + f = open(filename, "r", encoding = "utf-8") line = f.readline() dtdLine = f.readline() f.close() @@ -846,7 +846,7 @@ parser.setErrorHandler(eh) try: - f = open(filename, "r") + f = open(filename, "r", encoding = "utf-8") try: try: parser.parse(f)
--- a/UI/CompareDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/UI/CompareDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -221,7 +221,7 @@ """ filename1 = Utilities.toNativeSeparators(self.file1Edit.text()) try: - f1 = open(filename1, "r") + f1 = open(filename1, "r", encoding = "utf-8") lines1 = f1.readlines() f1.close() except IOError: @@ -233,7 +233,7 @@ filename2 = Utilities.toNativeSeparators(self.file2Edit.text()) try: - f2 = open(filename2, "r") + f2 = open(filename2, "r", encoding = "utf-8") lines2 = f2.readlines() f2.close() except IOError:
--- a/UI/DiffDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/UI/DiffDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -300,7 +300,7 @@ fname = Utilities.toNativeSeparators(fname) try: - f = open(fname, "w") + f = open(fname, "w", encoding = "utf-8") txt = self.contents.toPlainText() try: f.write(txt) @@ -324,7 +324,7 @@ except IOError: filemtime1 = "" try: - f1 = open(self.filename1, "r") + f1 = open(self.filename1, "r", encoding = "utf-8") lines1 = f1.readlines() f1.close() except IOError: @@ -340,7 +340,7 @@ except IOError: filemtime2 = "" try: - f2 = open(self.filename2, "r") + f2 = open(self.filename2, "r", encoding = "utf-8") lines2 = f2.readlines() f2.close() except IOError:
--- a/UI/EmailDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/UI/EmailDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -28,9 +28,12 @@ ############################################################ from base64 import b64encode as _bencode def _encode_base64(msg): - """Encode the message's payload in Base64. + """ + Function to encode the message's payload in Base64. - Also, add an appropriate Content-Transfer-Encoding header. + Note: It adds an appropriate Content-Transfer-Encoding header. + + @param msg reference to the message object (email.Message) """ orig = msg.get_payload() encdata = str(_bencode(orig), "ASCII") @@ -223,7 +226,8 @@ name = os.path.basename(fname) if maintype == 'text': - att = MIMEText(open(fname, 'r').read(), _subtype = subtype) + att = MIMEText( + open(fname, 'r', encoding = "utf-8").read(), _subtype = subtype) elif maintype == 'image': att = MIMEImage(open(fname, 'rb').read(), _subtype = subtype) elif maintype == 'audio':
--- a/UI/FindFileDialog.py Sun Jan 10 13:59:15 2010 +0000 +++ b/UI/FindFileDialog.py Sun Jan 10 19:19:52 2010 +0000 @@ -366,11 +366,9 @@ fn = file # read the file and split it into textlines try: - f = open(fn, 'r') - text = f.read() + text, encoding = Utilities.readEncodedFile(fn) lines = text.splitlines() - f.close() - except (IOError, UnicodeError): + except (UnicodeError, IOError): progress += 1 self.findProgress.setValue(progress) continue @@ -525,11 +523,9 @@ # read the file and split it into textlines try: - f = open(fn, 'r') - text = f.read() + text, encoding = Utilities.readEncodedFile(fn) lines = text.splitlines() - f.close() - except IOError as err: + except (UnicodeError, IOError): QMessageBox.critical(self, self.trUtf8("Replace in Files"), self.trUtf8("""<p>Could not read the file <b>{0}</b>.""" @@ -551,10 +547,8 @@ # write the file txt = Utilities.linesep().join(lines) try: - f = open(fn, 'w') - f.write(txt) - f.close() - except IOError as err: + Utilities.writeEncodedFile(fn, txt, encoding) + except (IOError, Utilities.CodingError, UnicodeError) as err: QMessageBox.critical(self, self.trUtf8("Replace in Files"), self.trUtf8("""<p>Could not save the file <b>{0}</b>."""
--- a/UI/UserInterface.py Sun Jan 10 13:59:15 2010 +0000 +++ b/UI/UserInterface.py Sun Jan 10 19:19:52 2010 +0000 @@ -622,7 +622,7 @@ styleSheetFile = Preferences.getUI("StyleSheet") if styleSheetFile: try: - f = open(styleSheetFile, "r") + f = open(styleSheetFile, "r", encoding = "utf-8") styleSheet = f.read() f.close() except IOError as msg: @@ -2672,7 +2672,7 @@ address = BugAddress subject = "[eric5] " if attachFile is not None: - f = open(attachFile, "r") + f = open(attachFile, "r", encoding = "utf-8") body = f.read() f.close() if deleteAttachFile: @@ -2700,7 +2700,7 @@ self.trUtf8("An error log file was found. " "What should be done with it?")) try: - f = open(logFile, "r") + f = open(logFile, "r", encoding = "utf-8") txt = f.read() f.close() dlg.setDetailedText(txt) @@ -3775,7 +3775,7 @@ if fn is not None and version == 0: # determine version from file, if not specified try: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") found = False while not found: uiLine = f.readline() @@ -4830,7 +4830,7 @@ """ try: fn = os.path.join(Utilities.getConfigDir(), "eric5tasks.e4t") - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") TasksWriter(f, False).writeXML() @@ -4850,7 +4850,7 @@ fn = os.path.join(Utilities.getConfigDir(), "eric5tasks.e4t") if not os.path.exists(fn): return - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") line = f.readline() dtdLine = f.readline() f.close() @@ -4873,7 +4873,7 @@ parser.setErrorHandler(eh) try: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: try: parser.parse(f) @@ -4905,7 +4905,7 @@ """ try: fn = os.path.join(Utilities.getConfigDir(), "eric5session.e4s") - f = open(fn, "w") + f = open(fn, "w", encoding = "utf-8") SessionWriter(f, None).writeXML() @@ -4929,7 +4929,7 @@ self.trUtf8("<p>The session file <b>{0}</b> could not be read.</p>")\ .format(fn)) return - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") line = f.readline() dtdLine = f.readline() f.close() @@ -4952,7 +4952,7 @@ parser.setErrorHandler(eh) try: - f = open(fn, "r") + f = open(fn, "r", encoding = "utf-8") try: try: parser.parse(f)
--- a/Utilities/ClassBrowsers/__init__.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Utilities/ClassBrowsers/__init__.py Sun Jan 10 19:19:52 2010 +0000 @@ -109,8 +109,6 @@ if name.lower().endswith('.py'): name = name[:-3] -## if isinstance(name, type("")): -## name = name.encode('utf-8') try: return imp.find_module(name, path)
--- a/Utilities/ClassBrowsers/idlclbr.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Utilities/ClassBrowsers/idlclbr.py Sun Jan 10 19:19:52 2010 +0000 @@ -189,17 +189,22 @@ f = None fullpath = list(path) f, file, (suff, mode, type) = ClassBrowsers.find_module(module, fullpath) + if f: + f.close() if type not in SUPPORTED_TYPES: # not CORBA IDL source, can't do anything with this module - f.close() _modules[module] = dict return dict _modules[module] = dict classstack = [] # stack of (class, indent) pairs indent = 0 - src = f.read() - f.close() + try: + src = Utilities.readEncodedFile(file)[0] + except (UnicodeError, IOError): + # can't do anything with this module + _modules[module] = dict + return dict lineno, last_lineno_pos = 1, 0 i = 0
--- a/Utilities/ClassBrowsers/pyclbr.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Utilities/ClassBrowsers/pyclbr.py Sun Jan 10 19:19:52 2010 +0000 @@ -236,9 +236,10 @@ path = [file] + path f, file, (suff, mode, type) = \ ClassBrowsers.find_module('__init__', [file]) + if f: + f.close() if type not in SUPPORTED_TYPES: # not Python source, can't do anything with this module - f.close() _modules[module] = dict return dict @@ -248,8 +249,12 @@ deltastack = [] deltaindent = 0 deltaindentcalculated = 0 - src = f.read() - f.close() + try: + src = Utilities.readEncodedFile(file)[0] + except (UnicodeError, IOError): + # can't do anything with this module + _modules[module] = dict + return dict lineno, last_lineno_pos = 1, 0 i = 0
--- a/Utilities/ClassBrowsers/rbclbr.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Utilities/ClassBrowsers/rbclbr.py Sun Jan 10 19:19:52 2010 +0000 @@ -257,9 +257,10 @@ f = None fullpath = list(path) f, file, (suff, mode, type) = ClassBrowsers.find_module(module, fullpath) + if f: + f.close() if type not in SUPPORTED_TYPES: # not Ruby source, can't do anything with this module - f.close() _modules[module] = dict return dict @@ -267,8 +268,12 @@ classstack = [] # stack of (class, indent) pairs acstack = [] # stack of (access control, indent) pairs indent = 0 - src = f.read() - f.close() + try: + src = Utilities.readEncodedFile(file)[0] + except (UnicodeError, IOError): + # can't do anything with this module + _modules[module] = dict + return dict lineno, last_lineno_pos = 1, 0 i = 0
--- a/Utilities/ModuleParser.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Utilities/ModuleParser.py Sun Jan 10 19:19:52 2010 +0000 @@ -1328,19 +1328,19 @@ if f is None: fullpath = list(path) + sys.path f, file, (suff, mode, type) = find_module(module, fullpath, _extensions) + if f: + f.close() if type not in SUPPORTED_TYPES: # not supported source, can't do anything with this module - if f: - f.close() _modules[modname] = Module(modname, None, None) return _modules[modname] mod = Module(modname, file, type) try: - src = f.read() + src = Utilities.readEncodedFile(file)[0] mod.scan(src) - finally: - f.close() + except (UnicodeError, IOError): + pass if caching: _modules[modname] = mod return mod @@ -1389,8 +1389,6 @@ # standard Python module file if name.lower().endswith('.py'): name = name[:-3] -## if isinstance(name, type("")): -## name = name.encode('utf-8') return imp.find_module(name, path)
--- a/Utilities/__init__.py Sun Jan 10 13:59:15 2010 +0000 +++ b/Utilities/__init__.py Sun Jan 10 19:19:52 2010 +0000 @@ -12,7 +12,6 @@ import re import fnmatch import glob -##from types import UnicodeType import random import base64 @@ -36,10 +35,15 @@ configDir = None -##coding_regexps = [ -## (2, re.compile(br'''coding[:=]\s*([-\w_.]+)''')), -## (1, re.compile(br'''<\?xml.*\bencoding\s*=\s*['"]([-\w_.]+)['"]\?>''')), -##] +codingBytes_regexps = [ + (2, re.compile(br'''coding[:=]\s*([-\w_.]+)''')), + (1, re.compile(br'''<\?xml.*\bencoding\s*=\s*['"]([-\w_.]+)['"]\?>''')), +] +coding_regexps = [ + (2, re.compile(r'''coding[:=]\s*([-\w_.]+)''')), + (1, re.compile(r'''<\?xml.*\bencoding\s*=\s*['"]([-\w_.]+)['"]\?>''')), +] + supportedCodecs = ['utf-8', 'iso8859-1', 'iso8859-15', 'iso8859-2', 'iso8859-3', 'iso8859-4', 'iso8859-5', 'iso8859-6', 'iso8859-7', @@ -57,193 +61,191 @@ 'gb2312', 'gb18030', 'ascii'] -##class CodingError(Exception): -## """ -## Class implementing an exception, which is raised, if a given coding is incorrect. -## """ -## def __init__(self, coding): -## """ -## Constructor -## """ -## self.errorMessage = QApplication.translate("CodingError", -## "The coding '{0}' is wrong for the given text.").format(coding) -## -## def __repr__(self): -## """ -## Private method returning a representation of the exception. -## -## @return string representing the error message -## """ -## return str(self.errorMessage) -## -## def __str__(self): -## """ -## Private method returning a string representation of the exception. -## -## @return string representing the error message -## """ -## return str(self.errorMessage) -## -##def get_coding(text): -## """ -## Function to get the coding of a text. -## -## @param text text to inspect (string) -## @return coding string -## """ -## lines = text.splitlines() -## for coding in coding_regexps: -## coding_re = coding[1] -## head = lines[:coding[0]] -## for l in head: -## m = coding_re.search(l) -## if m: -## return m.group(1).lower() -## return None -## -##def decode(text): -## """ -## Function to decode a text. -## -## @param text text to decode (string) -## @return decoded text and encoding -## """ -## try: -## if text.startswith(BOM_UTF8): -## # UTF-8 with BOM -## return str(text[len(BOM_UTF8):], 'utf-8'), 'utf-8-bom' -## elif text.startswith(BOM_UTF16): -## # UTF-16 with BOM -## return str(text[len(BOM_UTF16):], 'utf-16'), 'utf-16' -## elif text.startswith(BOM_UTF32): -## # UTF-32 with BOM -## return str(text[len(BOM_UTF32):], 'utf-32'), 'utf-32' -## coding = get_coding(text) -## if coding: -## coding = coding.decode() -## return text.decode(coding), coding -## except (UnicodeError, LookupError): -## pass -## -## guess = None -## if Preferences.getEditor("AdvancedEncodingDetection"): -## # Try the universal character encoding detector -## try: -## import ThirdParty.CharDet.chardet -## guess = ThirdParty.CharDet.chardet.detect(text) -## if guess and guess['confidence'] > 0.95 and guess['encoding'] is not None: -## codec = guess['encoding'].lower() -## return str(text, codec), '%s-guessed' % codec -## except (UnicodeError, LookupError): -## pass -## except ImportError: -## pass -## -## # Try default encoding -## try: -## codec = Preferences.getEditor("DefaultEncoding") -## return str(text, codec), '%s-default' % codec -## except (UnicodeError, LookupError): -## pass -## -## # Assume UTF-8 -## try: -## return str(text, 'utf-8'), 'utf-8-guessed' -## except (UnicodeError, LookupError): -## pass -## -## if Preferences.getEditor("AdvancedEncodingDetection"): -## # Use the guessed one even if confifence level is low -## if guess and guess['encoding'] is not None: -## try: -## codec = guess['encoding'].lower() -## return str(text, codec), '%s-guessed' % codec -## except (UnicodeError, LookupError): -## pass -## -## # Assume Latin-1 (behaviour before 3.7.1) -## return str(text, "latin-1"), 'latin-1-guessed' -## -##def encode(text, orig_coding): -## """ -## Function to encode a text. -## -## @param text text to encode (string) -## @param orig_coding type of the original coding (string) -## @return encoded text and encoding -## """ -## if orig_coding == 'utf-8-bom': -## return BOM_UTF8 + text.encode("utf-8"), 'utf-8-bom' -## -## # Try declared coding spec -## coding = get_coding(text) -## if coding: -## try: -## return text.encode(coding), coding -## except (UnicodeError, LookupError): -## # Error: Declared encoding is incorrect -## raise CodingError(coding) -## -## if orig_coding and orig_coding.endswith('-selected'): -## coding = orig_coding.replace("-selected", "") -## try: -## return text.encode(coding), coding -## except (UnicodeError, LookupError): -## pass -## if orig_coding and orig_coding.endswith('-default'): -## coding = orig_coding.replace("-default", "") -## try: -## return text.encode(coding), coding -## except (UnicodeError, LookupError): -## pass -## if orig_coding and orig_coding.endswith('-guessed'): -## coding = orig_coding.replace("-guessed", "") -## try: -## return text.encode(coding), coding -## except (UnicodeError, LookupError): -## pass -## -## # Try configured default -## try: -## codec = Preferences.getEditor("DefaultEncoding") -## return text.encode(codec), codec -## except (UnicodeError, LookupError): -## pass -## -## # Try saving as ASCII -## try: -## return text.encode('ascii'), 'ascii' -## except UnicodeError: -## pass -## -## # Save as UTF-8 without BOM -## return text.encode('utf-8'), 'utf-8' -## -##def toUnicode(s): -## """ -## Public method to convert a string to unicode. -## -## Various codes are tried until one converts the string without an error. -## If all codecs fail, the string is returned unaltered. -## -## @param s string to be converted (string) -## @return converted string (unicode) -## """ -## if isinstance(s, str): -## return s -## -## for codec in supportedCodecs: -## try: -## u = str(s, codec) -## return u -## except UnicodeError: -## pass -## except TypeError: -## break -## -## # we didn't succeed -## return s -## +class CodingError(Exception): + """ + Class implementing an exception, which is raised, if a given coding is incorrect. + """ + def __init__(self, coding): + """ + Constructor + """ + self.errorMessage = QApplication.translate("CodingError", + "The coding '{0}' is wrong for the given text.").format(coding) + + def __repr__(self): + """ + Private method returning a representation of the exception. + + @return string representing the error message + """ + return str(self.errorMessage) + + def __str__(self): + """ + Private method returning a string representation of the exception. + + @return string representing the error message + """ + return str(self.errorMessage) + +def get_codingBytes(text): + """ + Function to get the coding of a bytes text. + + @param text bytes text to inspect (bytes) + @return coding string + """ + lines = text.splitlines() + for coding in codingBytes_regexps: + coding_re = coding[1] + head = lines[:coding[0]] + for l in head: + m = coding_re.search(l) + if m: + return str(m.group(1), "ascii").lower() + return None + +def get_coding(text): + """ + Function to get the coding of a text. + + @param text text to inspect (string) + @return coding string + """ + lines = text.splitlines() + for coding in coding_regexps: + coding_re = coding[1] + head = lines[:coding[0]] + for l in head: + m = coding_re.search(l) + if m: + return m.group(1).lower() + return None + +def readEncodedFile(filename): + """ + Function to read a file and decode it's contents into proper text. + + @param filename name of the file to read (string) + @return tuple of decoded text and encoding (string, string) + """ + f = open(filename, "rb") + text = f.read() + f.close() + try: + if text.startswith(BOM_UTF8): + # UTF-8 with BOM + return str(text[len(BOM_UTF8):], 'utf-8'), 'utf-8-bom' + elif text.startswith(BOM_UTF16): + # UTF-16 with BOM + return str(text[len(BOM_UTF16):], 'utf-16'), 'utf-16' + elif text.startswith(BOM_UTF32): + # UTF-32 with BOM + return str(text[len(BOM_UTF32):], 'utf-32'), 'utf-32' + coding = get_codingBytes(text) + if coding: + return str(text, coding), coding + except (UnicodeError, LookupError): + pass + + guess = None + if Preferences.getEditor("AdvancedEncodingDetection"): + # Try the universal character encoding detector + try: + import ThirdParty.CharDet.chardet + guess = ThirdParty.CharDet.chardet.detect(text) + if guess and guess['confidence'] > 0.95 and guess['encoding'] is not None: + codec = guess['encoding'].lower() + return str(text, codec), '%s-guessed' % codec + except (UnicodeError, LookupError): + pass + except ImportError: + pass + + # Try default encoding + try: + codec = Preferences.getEditor("DefaultEncoding") + return str(text, codec), '%s-default' % codec + except (UnicodeError, LookupError): + pass + + # Assume UTF-8 + try: + return str(text, 'utf-8'), 'utf-8-guessed' + except (UnicodeError, LookupError): + pass + + if Preferences.getEditor("AdvancedEncodingDetection"): + # Use the guessed one even if confifence level is low + if guess and guess['encoding'] is not None: + try: + codec = guess['encoding'].lower() + return str(text, codec), '%s-guessed' % codec + except (UnicodeError, LookupError): + pass + + # Assume UTF-8 loosing information + return str(text, "utf-8", "ignore"), 'utf-8-ignore' + +def writeEncodedFile(filename, text, orig_coding): + """ + Function to write a file with properly encoded text. + + @param filename name of the file to read (string) + @param text text to be written (string) + @param orig_coding type of the original encoding (string) + @return encoding used for writing the file (string) + """ + encoding = None + if orig_coding == 'utf-8-bom': + etext, encoding = BOM_UTF8 + text.encode("utf-8"), 'utf-8-bom' + else: + # Try declared coding spec + coding = get_coding(text) + if coding: + try: + etext, encoding = text.encode(coding), coding + except (UnicodeError, LookupError): + # Error: Declared encoding is incorrect + raise CodingError(coding) + else: + if orig_coding and orig_coding.endswith( + ('-selected', '-default', '-guessed', '-ignore')): + coding = orig_coding\ + .replace("-selected", "")\ + .replace("-default", "")\ + .replace("-guessed", "")\ + .replace("-ignore", "") + try: + etext, encoding = text.encode(coding), coding + except (UnicodeError, LookupError): + pass + + if encoding is None: + # Try configured default + try: + codec = Preferences.getEditor("DefaultEncoding") + etext, encoding = text.encode(codec), codec + except (UnicodeError, LookupError): + pass + + if encoding is None: + # Try saving as ASCII + try: + etext, encoding = text.encode('ascii'), 'ascii' + except UnicodeError: + pass + + if encoding is None: + # Save as UTF-8 without BOM + etext, encoding = text.encode('utf-8'), 'utf-8' + + f = open(filename, "wb") + f.write(etext) + f.close() + + return encoding + _escape = re.compile(eval(r'"[&<>\"\u0080-\uffff]"')) _escape_map = { @@ -887,15 +889,10 @@ import builtins if not codestring: try: - f = open(file) -## codestring, encoding = decode(f.read()) - codestring = f.read() - f.close() - except IOError: + codestring = Utilities.readEncodedFile(file)[0] + except (UnicodeDecodeError, IOError): return (False, None, None, None, None) -## if isinstance(codestring, type("")): -## codestring = codestring.encode('utf-8') codestring = codestring.replace("\r\n","\n") codestring = codestring.replace("\r","\n") @@ -903,9 +900,6 @@ codestring = codestring + '\n' try: -## if isinstance(file, type("")): -## file = file.encode('utf-8') -## if file.endswith('.ptl'): try: import quixote.ptl_compile
--- a/eric5-api.py Sun Jan 10 13:59:15 2010 +0000 +++ b/eric5-api.py Sun Jan 10 19:19:52 2010 +0000 @@ -256,7 +256,7 @@ if outdir and not os.path.exists(outdir): os.makedirs(outdir) try: - out = open(outputFile, "w") + out = open(outputFile, "w", encoding = "utf-8") out.write(os.linesep.join(sorted(apis))) out.close() except IOError as v:
--- a/eric5-doc.py Sun Jan 10 13:59:15 2010 +0000 +++ b/eric5-doc.py Sun Jan 10 19:19:52 2010 +0000 @@ -261,7 +261,7 @@ if stylesheetFile: try: - sf = open(stylesheetFile, "r") + sf = open(stylesheetFile, "r", encoding = "utf-8") stylesheet = sf.read() sf.close() except IOError: @@ -370,7 +370,7 @@ # generate output try: - out = open(f, "w") + out = open(f, "w", encoding = "utf-8") out.write(doc) out.close() except IOError as v:
--- a/eric5.py Sun Jan 10 13:59:15 2010 +0000 +++ b/eric5.py Sun Jan 10 19:19:52 2010 +0000 @@ -120,7 +120,7 @@ sections = [separator, timeString, separator, errmsg, separator, tbinfo] msg = '\n'.join(sections) try: - f = open(logFile, "w") + f = open(logFile, "w", encoding = "utf-8") f.write(msg) f.write(versionInfo) f.close()
--- a/install.py Sun Jan 10 13:59:15 2010 +0000 +++ b/install.py Sun Jan 10 19:19:52 2010 +0000 @@ -106,7 +106,7 @@ @param name the name of the file. @param text the contents to copy to the file. """ - f = open(name,"w") + f = open(name,"w", encoding = "utf-8") f.write(text) f.close() @@ -207,7 +207,7 @@ if not os.path.exists(fname): if not os.path.exists(pdir): os.mkdir(pdir, 0o755) - f = open(fname, "w") + f = open(fname, "w", encoding = "utf-8") f.write(\ '''# -*- coding: utf-8 -*-
--- a/patch_modpython.py Sun Jan 10 13:59:15 2010 +0000 +++ b/patch_modpython.py Sun Jan 10 19:19:52 2010 +0000 @@ -76,7 +76,7 @@ try: filename = os.path.join(modDir, "apache.py") - f = open(filename, "r") + f = open(filename, "r", encoding = "utf-8") except EnvironmentError: print("The file %s does not exist. Aborting." % filename) sys.exit(1) @@ -88,7 +88,7 @@ ericFound = False sn = "apache.py" - s = open(sn, "w") + s = open(sn, "w", encoding = "utf-8") for line in lines: if not pdbFound and line.startswith("import pdb"): s.write("import eric5.DebugClients.Python.eric5dbgstub as pdb\n")
--- a/patch_pyxml.py Sun Jan 10 13:59:15 2010 +0000 +++ b/patch_pyxml.py Sun Jan 10 19:19:52 2010 +0000 @@ -62,7 +62,7 @@ try: filename = \ os.path.join(pyxmlModDir, "parsers", "xmlproc", "xmlutils.py") - f = open(filename, "r") + f = open(filename, "r", encoding = "utf-8") except EnvironmentError: print("Could not find the pyXML distribution. Please use the patch_pyxml.py") print("script to apply a patch needed to fix a bug causing it to fail for") @@ -99,7 +99,7 @@ try: filename = \ os.path.join(pyxmlModDir, "parsers", "xmlproc", "xmlutils.py") - f = open(filename, "r") + f = open(filename, "r", encoding = "utf-8") except EnvironmentError: print("The file %s does not exist. Aborting." % filename) sys.exit(1) @@ -111,7 +111,7 @@ patched = False sn = "xmlutils.py" - s = open(sn, "w") + s = open(sn, "w", encoding = "utf-8") for line in lines: if patchPositionFound: if not line.startswith(\ @@ -188,4 +188,4 @@ patchPyXML() if __name__ == "__main__": - main() \ No newline at end of file + main()