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() 

Also, I removed the try/except - it's superfluous because it throws the 
exception without any modifications.
Acked-by: default avatarKshitij Gupta <>
parent a4685beb
......@@ -168,19 +168,21 @@ def get_directory_contents(path):
def open_file_read(path, encoding='UTF-8'):
'''Open specified file read-only'''
orig =, 'r', encoding)
except Exception:
return orig
return open_file_anymode('r', path, encoding)
def open_file_write(path):
'''Open specified file in write/overwrite mode'''
orig =, 'w', 'UTF-8')
except Exception:
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 =, 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