Commit 7ef2ae9b authored by Christian Boltz's avatar Christian Boltz

Prevent encoding errors when reading a file

This patch changes open_file_read() and open_file_write() to use 
errors='surrogateescape' (with fallback to 'replace' for py2).

This avoids a crash when reading a logfile with special characters that 
are not utf8-encoded (for example a latin1 "ö"), and also avoids crashes 
at several other places we don't know yet ;-)

The patch also changes open_file_read() and open_file_write() to wrapper
functions, and moves the "real" code to the new open_file_anymode() 
function.

Also, I removed the try/except - it's superfluous because it throws the 
exception without any modifications.
Acked-by: default avatarKshitij Gupta <kgupta8592@gmail.com>
parent a4685beb
......@@ -168,19 +168,21 @@ def get_directory_contents(path):
def open_file_read(path, encoding='UTF-8'):
'''Open specified file read-only'''
try:
orig = codecs.open(path, 'r', encoding)
except Exception:
raise
return orig
return open_file_anymode('r', path, encoding)
def open_file_write(path):
'''Open specified file in write/overwrite mode'''
try:
orig = codecs.open(path, 'w', 'UTF-8')
except Exception:
raise
return open_file_anymode('w', path, 'UTF-8')
def open_file_anymode(mode, path, encoding='UTF-8'):
'''Open specified file in specified mode'''
errorhandling = 'surrogateescape'
if sys.version_info[0] < 3:
errorhandling = 'replace'
orig = codecs.open(path, mode, encoding, errors=errorhandling)
return orig
def readkey():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment