From 09b8c589681b1141e7b36a6e487c953c7e6e3c18 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Wed, 25 Apr 2018 20:19:17 +0200 Subject: 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. --- kconfiglib.py | 30 ++++++++++++++++++++++++++---- 1 file 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): -- cgit v1.2.3