Skip to content

Commit 3476c09

Browse files
peeyushguptaGeert Vanderkelen
authored and
Geert Vanderkelen
committed
BUG19803702: Fix reporting errors with non-ascii characters
We fix reporting errors containing non ascii characters with Python v2. We fix this by encoding error message with 'utf8' when used with Python v2. A unit test has been added for BUG#19803702. (cherry picked from commit c2a7630)
1 parent 0f68012 commit 3476c09

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/mysql/connector/errors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from . import utils
2828
from .locales import get_client_error
29+
from .catch23 import PY2
2930

3031
# _CUSTOM_ERROR_EXCEPTIONS holds custom exceptions and is ued by the
3132
# function custom_error_exception. _ERROR_EXCEPTIONS (at bottom of module)
@@ -187,7 +188,7 @@ def __init__(self, msg=None, errno=None, values=None, sqlstate=None):
187188
if self.msg and self.errno != -1:
188189
fields = {
189190
'errno': self.errno,
190-
'msg': self.msg
191+
'msg': self.msg.encode('utf8') if PY2 else self.msg
191192
}
192193
if self.sqlstate:
193194
fmt = '{errno} ({state}): {msg}'

tests/test_bugs.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,3 +3027,26 @@ def test_compress(self):
30273027
cnx1.close()
30283028
except:
30293029
self.fail("Reset session with compression test failed.")
3030+
3031+
3032+
class BugOra19803702(tests.MySQLConnectorTests):
3033+
"""BUG#19803702: CAN'T REPORT ERRORS THAT HAVE NON-ASCII CHARACTERS
3034+
"""
3035+
def test_errors(self):
3036+
config = tests.get_mysql_config()
3037+
self.cnx = connection.MySQLConnection(**config)
3038+
self.cur = self.cnx.cursor()
3039+
3040+
self.tbl = 'áááëëëááá'
3041+
self.cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl))
3042+
3043+
create = ("CREATE TABLE {0} (col1 VARCHAR(10), col2 INT) "
3044+
"DEFAULT CHARSET latin1".format(self.tbl))
3045+
3046+
self.cur.execute(create)
3047+
self.assertRaises(errors.DatabaseError, self.cur.execute, create)
3048+
3049+
def tearDown(self):
3050+
self.cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl))
3051+
self.cur.close()
3052+
self.cnx.close()

0 commit comments

Comments
 (0)