Code

Ticket #12649: 12649-csrf-migration-helper-file-path-in-unicode-exception.diff

File 12649-csrf-migration-helper-file-path-in-unicode-exception.diff, 804 bytes (added by akaihola, 4 years ago)

the path of the bad HTML file is appended to the exception error message

Line 
1diff --git a/extras/csrf_migration_helper.py b/extras/csrf_migration_helper.py
2index bc352a1..2934ca8 100644
3--- a/extras/csrf_migration_helper.py
4+++ b/extras/csrf_migration_helper.py
5@@ -176,7 +176,12 @@ class Template(object):
6             return self._content
7         except AttributeError:
8             fd = open(self.absolute_filename)
9-            content = fd.read().decode(TEMPLATE_ENCODING)
10+            try:
11+                content = fd.read().decode(TEMPLATE_ENCODING)
12+            except UnicodeDecodeError, e:
13+                message = '%s in %s' % (
14+                    e[4], self.absolute_filename.encode('UTF-8', 'ignore'))
15+                raise UnicodeDecodeError(*(e.args[:4] + (message,)))
16             fd.close()
17             self._content = content
18             return content