Skip to content

Commit 9935d19

Browse files
committed
Fixed issue by converting binary strings to _binary'blob'
1 parent e496221 commit 9935d19

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

aiomysql/connection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pymysql.constants import FIELD_TYPE
1919
from pymysql.util import byte2int, int2byte
2020
from pymysql.converters import (escape_item, encoders, decoders,
21-
escape_string, through)
21+
escape_string, escape_bytes_prefixed, through)
2222
from pymysql.err import (Warning, Error,
2323
InterfaceError, DataError, DatabaseError,
2424
OperationalError,
@@ -375,6 +375,8 @@ def escape(self, obj):
375375
""" Escape whatever value you pass to it"""
376376
if isinstance(obj, str):
377377
return "'" + self.escape_string(obj) + "'"
378+
if isinstance(obj, bytes):
379+
return escape_bytes_prefixed(obj)
378380
return escape_item(obj, self._charset)
379381

380382
def literal(self, obj):

tests/test_issues.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import unittest
33
import aiomysql
44

5+
import pytest
6+
from pymysql.err import Warning
57
from tests import base
68
from tests._testutils import run_until_complete
79

@@ -404,3 +406,41 @@ def test_issue_175(self):
404406
assert len(cur.description) == length
405407
finally:
406408
yield from cur.execute('drop table if exists test_field_count')
409+
410+
411+
# MySQL will get you to renegotiate if sent a cleartext password
412+
@pytest.mark.run_loop
413+
async def test_issue_323(mysql_server, loop, recwarn):
414+
async with aiomysql.create_pool(**mysql_server['conn_params'],
415+
loop=loop) as pool:
416+
async with pool.get() as conn:
417+
async with conn.cursor() as cur:
418+
create_db = "CREATE DATABASE IF NOT EXISTS bugtest;"
419+
await cur.execute(create_db)
420+
421+
create_table = """CREATE TABLE IF NOT EXISTS `bugtest`.`testtable` (
422+
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
423+
`bindata` VARBINARY(200) NOT NULL,
424+
PRIMARY KEY (`id`)
425+
);"""
426+
427+
await cur.execute(create_table)
428+
429+
try:
430+
async with conn.cursor() as cur:
431+
await cur.execute("INSERT INTO `bugtest`.`testtable` "
432+
"(bindata) VALUES (%s);",
433+
(b'\xB0\x17',))
434+
435+
warnings = [warn for warn in recwarn.list
436+
if warn.category is Warning]
437+
assert len(warnings) == 0, "Got unexpected MySQL warning"
438+
439+
await cur.execute("SELECT * FROM `bugtest`.`testtable`;")
440+
rows = await cur.fetchall()
441+
442+
assert len(rows) == 1, "Table should have 1 row"
443+
444+
finally:
445+
async with conn.cursor() as cur:
446+
await cur.execute("DELETE FROM `bugtest`.`testtable`;")

0 commit comments

Comments
 (0)