numenta/nupic — pull request #261 by oxtopus (2013-09-13 21:59:14+00:00)
Title: Use patched PyMySQL to handle UnicodeDecodeError exceptions gracefully

This PR is in response to https://github.com/numenta/nupic/pull/258

@subutai if you don't mind, please test to see if this fixes the problem

cc: @vitaly-krugl @rmarianetti @rhyolight

This reverts commit 86abd8a4b5654f247a9420bfe4a721dde2b90aa3.

This applies a patch that was previously applied in an internal Grok project,
but didn't make it into NuPIC. In some circumstances, the error message
contains binary data that cannot be decoded properly as utf-8. This patch
handles such failure modes gracefully.

Complete output of `git format-patch pymysql-0.5 --stdout`, comparing
pymysql-0.5-devNupic to pymysql-0.5:

From e7efaf56cbbb37af2a082a593952ef35269fe708 Mon Sep 17 00:00:00 2001
From: Austin Marshall
Date: Fri, 13 Sep 2013 13:38:28 -0700
Subject: [PATCH 1/2] Graceful fallback in the event of UnicodeDecodeError

---
pymysql/err.py | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/pymysql/err.py b/pymysql/err.py
index b4322c6..56c3392 100644
--- a/pymysql/err.py
+++ b/pymysql/err.py
@@ -122,11 +122,19 @@ def _get_error_info(data):
if is_41:
# version 4.1
sqlstate = data[4:9].decode("utf8")
- errorvalue = data[9:].decode("utf8")
+ try:
+ errorvalue = data[9:].decode("utf8")
+ except UnicodeDecodeError:
+ errorvalue = repr(data[9:])
return (errno, sqlstate, errorvalue)
else:
# version 4.0
- return (errno, None, data[3:].decode("utf8"))
+ try:
+ errorvalue = data[3:].decode("utf8")
+ except UnicodeDecodeError:
+ errorvalue = repr(data[3:])
+ return (errno, None, errorvalue)
+

def _check_mysql_exception(errinfo):
errno, sqlstate, errorvalue = errinfo
--
1.8.2.3

From eb961a7727f6851a8962f4cc2f995e34f5ffebb1 Mon Sep 17 00:00:00 2001
From: Austin Marshall
Date: Fri, 13 Sep 2013 13:43:13 -0700
Subject: [PATCH 2/2] Specify nupic-specific version number

---
pymysql/__init__.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/pymysql/__init__.py b/pymysql/__init__.py
index c1e6d03..654ed07 100644
--- a/pymysql/__init__.py
+++ b/pymysql/__init__.py
@@ -23,7 +23,7 @@ THE SOFTWARE.

'''

-VERSION = (0, 5, None)
+VERSION = (0, 5, 'devNupic')

from constants import FIELD_TYPE
from converters import escape_dict, escape_sequence, escape_string
@@ -110,7 +110,7 @@ def thread_safe():
def install_as_MySQLdb():
"""
After this function is called, any application that imports MySQLdb or
- _mysql will unwittingly actually use
+ _mysql will unwittingly actually use
"""
sys.modules["MySQLdb"] = sys.modules["_mysql"] = sys.modules["pymysql"]

--
1.8.2.3