Skip to content

Commit

Permalink
WIP - Use _binary prefix for bytes/bytearray parameters (???)
Browse files Browse the repository at this point in the history
fix falsely prefixing strings with _binary type identifier (PyMySQL#123)
  • Loading branch information
vtermanis committed Jan 7, 2017
1 parent 2feb5ed commit 931a9bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 7 additions & 2 deletions MySQLdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
paramstyle = "format"

from _mysql import *
from MySQLdb.compat import PY2
from MySQLdb.constants import FIELD_TYPE
from MySQLdb.times import Date, Time, Timestamp, \
DateFromTicks, TimeFromTicks, TimestampFromTicks
Expand Down Expand Up @@ -72,8 +73,12 @@ def test_DBAPISet_set_equality_membership():
def test_DBAPISet_set_inequality_membership():
assert FIELD_TYPE.DATE != STRING

def Binary(x):
return bytes(x)
if PY2:
def Binary(x):
return bytearray(x)
else:
def Binary(x):
return bytes(x)

def Connect(*args, **kwargs):
"""Factory function for connections.Connection."""
Expand Down
15 changes: 11 additions & 4 deletions MySQLdb/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class object, used to create cursors (keyword only)

db = proxy(self)
def _get_string_literal():
# Note: string_literal() is called for bytes object on Python 3.
# Note: string_literal() is called for bytes object on Python 3 (via bytes_literal)
def string_literal(obj, dummy=None):
return db.string_literal(obj)
return string_literal
Expand All @@ -206,20 +206,26 @@ def _get_unicode_literal():
if PY2:
# unicode_literal is called for only unicode object.
def unicode_literal(u, dummy=None):
return db.literal(u.encode(unicode_literal.charset))
return db.string_literal(u.encode(unicode_literal.charset))
else:
# unicode_literal() is called for arbitrary object.
def unicode_literal(u, dummy=None):
return db.literal(str(u).encode(unicode_literal.charset))
return db.string_literal(str(u).encode(unicode_literal.charset))
return unicode_literal

def _get_bytes_literal():
def bytes_literal(obj, dummy=None):
return b'_binary' + db.string_literal(obj)
return bytes_literal

def _get_string_decoder():
def string_decoder(s):
return s.decode(string_decoder.charset)
return string_decoder

string_literal = _get_string_literal()
self.unicode_literal = unicode_literal = _get_unicode_literal()
bytes_literal = _get_bytes_literal()
self.string_decoder = string_decoder = _get_string_decoder()
if not charset:
charset = self.character_set_name()
Expand All @@ -234,7 +240,8 @@ def string_decoder(s):
self.converter[FIELD_TYPE.VARCHAR].append((None, string_decoder))
self.converter[FIELD_TYPE.BLOB].append((None, string_decoder))

self.encoders[bytes] = string_literal
self.encoders[bytes] = string_literal if PY2 else bytes_literal
self.encoders[bytearray] = bytes_literal
self.encoders[unicode] = unicode_literal
self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
if self._transactional:
Expand Down

0 comments on commit 931a9bf

Please sign in to comment.