diff -r 6afc0221140a -r 300487f5f517 src/eric7/Utilities/__init__.py --- a/src/eric7/Utilities/__init__.py Fri Feb 02 14:55:14 2024 +0100 +++ b/src/eric7/Utilities/__init__.py Mon Feb 05 11:15:47 2024 +0100 @@ -338,6 +338,31 @@ return str(text, "utf-8", "ignore"), "utf-8-ignore" +def decodeWithEncoding(text, encoding): + """ + Function to decode some byte text into a string. + + @param text byte text to decode + @type bytes + @param encoding encoding to be used to read the file + @type str + @return tuple of decoded text and encoding + @rtype tuple of (str, str) + """ + if encoding: + with contextlib.suppress(UnicodeError, LookupError): + return str(text, encoding), "{0}-selected".format(encoding) + + # Try default encoding + with contextlib.suppress(UnicodeError, LookupError): + codec = Preferences.getEditor("DefaultEncoding") + return str(text, codec), "{0}-default".format(codec) + + # Assume UTF-8 loosing information + return str(text, "utf-8", "ignore"), "utf-8-ignore" + else: + return decode(text) + def readEncodedFileWithEncoding(filename, encoding): """ Function to read a file and decode its contents into proper text. @@ -351,19 +376,7 @@ """ with open(filename, "rb") as f: text = f.read() - if encoding: - with contextlib.suppress(UnicodeError, LookupError): - return str(text, encoding), "{0}-selected".format(encoding) - - # Try default encoding - with contextlib.suppress(UnicodeError, LookupError): - codec = Preferences.getEditor("DefaultEncoding") - return str(text, codec), "{0}-default".format(codec) - - # Assume UTF-8 loosing information - return str(text, "utf-8", "ignore"), "utf-8-ignore" - else: - return decode(text) + return decodeWithEncoding(text, encoding) def writeEncodedFile(filename, text, origEncoding, forcedEncoding=""):