Navigation Menu

Skip to content

Commit

Permalink
BUG24948205: Results from JSON_TYPE() are returned as bytearray
Browse files Browse the repository at this point in the history
Result value from running a query with JSON_TYPE() is returned
as a VAR_STRING value which is not converted and is returned as
a bytearray. With this patch the returned value will be converted
to a string type using the current charset, in case the conversion
fail the value will still returned as a bytearray value.

v2
  • Loading branch information
isrgomez committed Feb 21, 2018
1 parent a602f15 commit 413817a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/mysql/connector/conversion.py
Expand Up @@ -564,7 +564,10 @@ def _STRING_to_python(self, value, dsc=None): # pylint: disable=C0103
if dsc[7] & FieldFlag.SET:
return self._SET_to_python(value, dsc)
if dsc[7] & FieldFlag.BINARY:
return value
try:
return value.decode(self.charset)
except LookupError:
return value

This comment has been minimized.

Copy link
@mariusnita

mariusnita Apr 2, 2019

I found an edge case where this code change causes badness (in Python 2 btw). I have uuids stored in a BINARY(16) column. One of my uuids just so happens to have bytes that can be successfully decoded to utf8. Here's the culprit:

In [11]: i=uuid.UUID('56283a26-2d44-11e9-bb8f-06773227025c')

In [12]: i.bytes.decode('utf8')
Out[12]: u"V(:&-D\x11\u9ecf\x06w2'\x02\\"

The code change above will happily decode my uuid, thereby breaking my application code which looks like:

cursor.execute('select my_id from Table')
ids = [uuid.UUID(bytes=bytes(row['my_id'])) for row in cursor.fetchall()]

The code that does bytes(row['my_id']) fails on the above ID which has been decoded to utf8.

This behavior was introduced in 8.0.11 and does not exist in 8.0.6.

Any thoughts?

This comment has been minimized.

Copy link
@xcolwell

xcolwell Apr 3, 2019

Good catch, this is an interesting and strange change in the driver. I'm not aware that BINARY should be encoded with the charset. I don't see a mention at [1] of anything changing recently. This seems like a bad change to the driver. Maybe file an issue with the driver? If the maintainer says there is a reason, then we can learn/adapt to that.

  1. https://dev.mysql.com/doc/refman/8.0/en/binary-varbinary.html

if self.charset == 'binary':
return value
Expand Down
1 change: 1 addition & 0 deletions tests/test_bugs.py
Expand Up @@ -58,6 +58,7 @@
from mysql.connector import (connection, cursor, conversion, protocol,
errors, constants, pooling)
from mysql.connector.optionfiles import read_option_files
from mysql.connector.catch23 import STRING_TYPES
import mysql.connector
import cpy_distutils

Expand Down

0 comments on commit 413817a

Please sign in to comment.