summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Magnusson <ulfalizer@gmail.com>2018-04-25 20:19:17 +0200
committerUlf Magnusson <ulfalizer@gmail.com>2018-04-25 20:26:39 +0200
commit09b8c589681b1141e7b36a6e487c953c7e6e3c18 (patch)
tree7326f4654c9b81cad70515722a5f8dc2b6cae873
parentc1c5ef2eb1009bacb6f7278e7d73335ec6223cba (diff)
Give filename and context for UnicodeDecodeError
These errors are a pain to debug otherwise, and might look like Kconfiglib brokenness. Another option would be ignore decoding errors, or do the 'surrogateescape' thing on reading and writing, but keep it simple for now. Pointing out the problem might be more helpful.
-rw-r--r--kconfiglib.py30
1 files changed, 26 insertions, 4 deletions
diff --git a/kconfiglib.py b/kconfiglib.py
index 8169c11..96c7d37 100644
--- a/kconfiglib.py
+++ b/kconfiglib.py
@@ -657,10 +657,14 @@ class Kconfig(object):
self._file = self._open(filename)
- self._parse_block(None, # end_token
- self.top_node, # parent
- self.top_node, # prev
- self.y) # visible_if_deps
+ try:
+ self._parse_block(None, # end_token
+ self.top_node, # parent
+ self.top_node, # prev
+ self.y) # visible_if_deps
+ except UnicodeDecodeError as e:
+ _decoding_error(e, self._filename)
+
self.top_node.list = self.top_node.next
self.top_node.next = None
@@ -738,6 +742,8 @@ class Kconfig(object):
# This stub only exists to make sure _warn_no_prompt gets reenabled
try:
self._load_config(filename, replace)
+ except UnicodeDecodeError as e:
+ _decoding_error(e, filename)
finally:
self._warn_no_prompt = True
@@ -4341,6 +4347,22 @@ def _internal_error(msg):
"email service to tell me about this. Include the message above and "
"the stack trace and describe what you were doing.")
+def _decoding_error(e, filename):
+ # Gives the filename and context for UnicodeDecodeError's, which are a pain
+ # to debug otherwise. 'e' is the UnicodeDecodeError object.
+
+ raise KconfigSyntaxError(
+ "\n"
+ "Malformed {} in {}\n"
+ "Context: {}\n"
+ "Problematic data: {}\n"
+ "Reason: {}".format(
+ e.encoding, filename,
+ e.object[max(e.start - 40, 0):e.end + 40],
+ e.object[e.start:e.end],
+ e.reason))
+
+
# Printing functions
def _sym_choice_str(sc):