@@ -197,7 +197,7 @@ class object, used to create cursors (keyword only)
197
197
198
198
db = proxy (self )
199
199
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)
201
201
def string_literal (obj , dummy = None ):
202
202
return db .string_literal (obj )
203
203
return string_literal
@@ -213,13 +213,19 @@ def unicode_literal(u, dummy=None):
213
213
return db .literal (str (u ).encode (unicode_literal .charset ))
214
214
return unicode_literal
215
215
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
+
216
221
def _get_string_decoder ():
217
222
def string_decoder (s ):
218
223
return s .decode (string_decoder .charset )
219
224
return string_decoder
220
225
221
226
string_literal = _get_string_literal ()
222
227
self .unicode_literal = unicode_literal = _get_unicode_literal ()
228
+ bytes_literal = _get_bytes_literal ()
223
229
self .string_decoder = string_decoder = _get_string_decoder ()
224
230
if not charset :
225
231
charset = self .character_set_name ()
@@ -234,7 +240,8 @@ def string_decoder(s):
234
240
self .converter [FIELD_TYPE .VARCHAR ].append ((None , string_decoder ))
235
241
self .converter [FIELD_TYPE .BLOB ].append ((None , string_decoder ))
236
242
237
- self .encoders [bytes ] = string_literal
243
+ self .encoders [bytes ] = string_literal if PY2 else bytes_literal
244
+ self .encoders [bytearray ] = bytes_literal
238
245
self .encoders [unicode ] = unicode_literal
239
246
self ._transactional = self .server_capabilities & CLIENT .TRANSACTIONS
240
247
if self ._transactional :
0 commit comments