UnicodeDecodeError when creating IntegrityError on duplicate binary key

Bug #725616 reported by blep on 2011-02-26
This bug affects 1 person
Affects Status Importance Assigned to Milestone
MySQL Connector/Python
In Progress
Geert JM Vanderkelen

Bug Description

* Description:
When mysql connector attemps to generate the error message for an IntegrityError due to a duplicate binary key error, it attempts to convert the binary key to text resulting in an unicode decoding error.

Below is the stack trace produced by the attached sample:

Traceback (most recent call last):
  File "C:\Python32\lib\site-packages\mysql\connector\errors.py", line 78, in raise_error
    errmsg = buf.decode('utf-8')
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 17: invalid start byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "binarypkduplicate.py", line 34, in <module>
  File "binarypkduplicate.py", line 29, in main
    cursor.execute( stmt, (KEY2, 'comment') )
  File "C:\Python32\lib\site-packages\mysql\connector\cursor.py", line 333, in execute
    res = self.db().protocol.cmd_query(stmt)
  File "C:\Python32\lib\site-packages\mysql\connector\protocol.py", line 137, in deco
    return func(*args, **kwargs)
  File "C:\Python32\lib\site-packages\mysql\connector\protocol.py", line 495, in cmd_query
    return self.handle_cmd_result(self.conn.recv())
  File "C:\Python32\lib\site-packages\mysql\connector\connection.py", line 180, in recv_plain
  File "C:\Python32\lib\site-packages\mysql\connector\errors.py", line 82, in raise_error
    % e)
mysql.connector.errors.InterfaceError: -1: Failed getting Error information (UnicodeDecodeError('utf8', b"Duplicate entry '\x80\x80\x80\x80\x80\x80\x80\x80' for key 1", 17, 18, 'invalid start byte'))

* Platform details:

python -c "import mysql.connector as db; print(db.__version__")
(0, 3, 2, 'devel', 292)

Python: 3.2

Mysql version: 5.0.51b-community-nt

O.S.: Windows XP SP2, 32 bits

blep (blep) wrote :

Remember verifying this, but did not update the bug.. thanks for the great report!

Smaller test case:

    cnx = mysql.connector.connect(database='test',charset='utf8')
    cur = cnx.cursor()
    cur.execute('DROP TABLE IF EXISTS t1')
    cur.execute('CREATE TABLE t1 (c1 BINARY(1), UNIQUE KEY (c1))')

    insert = "INSERT INTO t1 VALUES (%s)"
    data = (b'\x80')
    cur.execute(insert, (data,))
    cur.execute(insert, (data,))


This is Python 3 specific, and we'll fix it like this in errors.raise_error()
  errmsg = buf.decode('utf-8','surrogateescape')

Maybe we can do something similar for Python v2, but

.. but we'll leave as is (for Python v2)

Changed in myconnpy:
status: New → In Progress
importance: Undecided → Medium
assignee: nobody → Geert JM Vanderkelen (geertjmvdk)
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers