From fe81f95bfd284aea11377e6cb8dd79a7fe0f61a8 Mon Sep 17 00:00:00 2001 From: Meik Milevczik Date: Thu, 25 Oct 2018 16:09:50 +0200 Subject: [PATCH] Return raw bytes or bytearray if decoding fails Decoding bytearrays can fail if the value is binary data. Add a fallback and return the raw value if the decode function throws a UnicodeDecodeError. --- lib/mysql/connector/conversion.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/mysql/connector/conversion.py b/lib/mysql/connector/conversion.py index 83be979..1e21ac3 100644 --- a/lib/mysql/connector/conversion.py +++ b/lib/mysql/connector/conversion.py @@ -629,7 +629,10 @@ def _STRING_to_python(self, value, dsc=None): # pylint: disable=C0103 if self.charset == 'binary': return value if isinstance(value, (bytes, bytearray)) and self.use_unicode: - return value.decode(self.charset) + try: + return value.decode(self.charset) + except UnicodeDecodeError: + return value return value