Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2617620

Browse filesBrowse files
vtermanismethane
authored andcommitted
Use _binary prefix for bytes/bytearray parameters (PyMySQL#106)
1 parent 269d320 commit 2617620
Copy full SHA for 2617620

File tree

Expand file treeCollapse file tree

2 files changed

+16
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+16
-4
lines changed

‎MySQLdb/__init__.py

Copy file name to clipboardExpand all lines: MySQLdb/__init__.py
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
paramstyle = "format"
2828

2929
from _mysql import *
30+
from MySQLdb.compat import PY2
3031
from MySQLdb.constants import FIELD_TYPE
3132
from MySQLdb.times import Date, Time, Timestamp, \
3233
DateFromTicks, TimeFromTicks, TimestampFromTicks
@@ -72,8 +73,12 @@ def test_DBAPISet_set_equality_membership():
7273
def test_DBAPISet_set_inequality_membership():
7374
assert FIELD_TYPE.DATE != STRING
7475

75-
def Binary(x):
76-
return bytes(x)
76+
if PY2:
77+
def Binary(x):
78+
return bytearray(x)
79+
else:
80+
def Binary(x):
81+
return bytes(x)
7782

7883
def Connect(*args, **kwargs):
7984
"""Factory function for connections.Connection."""

‎MySQLdb/connections.py

Copy file name to clipboardExpand all lines: MySQLdb/connections.py
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class object, used to create cursors (keyword only)
197197

198198
db = proxy(self)
199199
def _get_string_literal():
200-
# Note: string_literal() is called for bytes object on Python 3.
200+
# Note: string_literal() is called for bytes object on Python 3 (via bytes_literal)
201201
def string_literal(obj, dummy=None):
202202
return db.string_literal(obj)
203203
return string_literal
@@ -213,13 +213,19 @@ def unicode_literal(u, dummy=None):
213213
return db.literal(str(u).encode(unicode_literal.charset))
214214
return unicode_literal
215215

216+
def _get_bytes_literal():
217+
def bytes_literal(obj, dummy=None):
218+
return b'_binary' + db.string_literal(obj)
219+
return bytes_literal
220+
216221
def _get_string_decoder():
217222
def string_decoder(s):
218223
return s.decode(string_decoder.charset)
219224
return string_decoder
220225

221226
string_literal = _get_string_literal()
222227
self.unicode_literal = unicode_literal = _get_unicode_literal()
228+
bytes_literal = _get_bytes_literal()
223229
self.string_decoder = string_decoder = _get_string_decoder()
224230
if not charset:
225231
charset = self.character_set_name()
@@ -234,7 +240,8 @@ def string_decoder(s):
234240
self.converter[FIELD_TYPE.VARCHAR].append((None, string_decoder))
235241
self.converter[FIELD_TYPE.BLOB].append((None, string_decoder))
236242

237-
self.encoders[bytes] = string_literal
243+
self.encoders[bytes] = string_literal if PY2 else bytes_literal
244+
self.encoders[bytearray] = bytes_literal
238245
self.encoders[unicode] = unicode_literal
239246
self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
240247
if self._transactional:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.