Skip to content

Commit

Permalink
Use _binary prefix for bytes/bytearray parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
vtermanis committed Jul 28, 2016
1 parent fb842fb commit 121af51
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 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 @@ -213,13 +213,19 @@ def unicode_literal(u, dummy=None):
return db.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 121af51

Please sign in to comment.