[apparmor] [patch] prevent encoding errors when reading a file
Christian Boltz
apparmor at cboltz.de
Fri Oct 10 18:04:51 UTC 2014
Hello,
Am Freitag, 10. Oktober 2014 schrieb Kshitij Gupta:
> I think we can move this redundant version check part to module level
> instead of function level.
There is more redundant code - the only differences are the mode ('r'
vs. 'w') and that ..._write() doesn't allow to specify the charset as
parameter.
This patch 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.
Updated patch:
=== modified file 'utils/apparmor/common.py'
--- utils/apparmor/common.py 2014-02-28 22:31:16 +0000
+++ utils/apparmor/common.py 2014-10-10 17:59:47 +0000
@@ -168,19 +168,21 @@
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():
Regards,
Christian Boltz
--
Wenn es jemand gibt, der Facebook derzeit noch stoppen kann, dann wohl
Google. Regentraufen- und Pestcholera-Vergleich bitte hier einfügen.
[http://praegnanz.de/weblog/ein-tag-vier-interessante-news]
More information about the AppArmor
mailing list