Skip to content

Commit

Permalink
Merge pull request #326 from terrycain/issue_323
Browse files Browse the repository at this point in the history
Fixes warning when inserting binary strings
  • Loading branch information
terrycain authored Jul 31, 2018
2 parents e496221 + 9935d19 commit bd61cdb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
4 changes: 3 additions & 1 deletion aiomysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pymysql.constants import FIELD_TYPE
from pymysql.util import byte2int, int2byte
from pymysql.converters import (escape_item, encoders, decoders,
escape_string, through)
escape_string, escape_bytes_prefixed, through)
from pymysql.err import (Warning, Error,
InterfaceError, DataError, DatabaseError,
OperationalError,
Expand Down Expand Up @@ -375,6 +375,8 @@ def escape(self, obj):
""" Escape whatever value you pass to it"""
if isinstance(obj, str):
return "'" + self.escape_string(obj) + "'"
if isinstance(obj, bytes):
return escape_bytes_prefixed(obj)
return escape_item(obj, self._charset)

def literal(self, obj):
Expand Down
40 changes: 40 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import unittest
import aiomysql

import pytest
from pymysql.err import Warning
from tests import base
from tests._testutils import run_until_complete

Expand Down Expand Up @@ -404,3 +406,41 @@ def test_issue_175(self):
assert len(cur.description) == length
finally:
yield from cur.execute('drop table if exists test_field_count')


# MySQL will get you to renegotiate if sent a cleartext password
@pytest.mark.run_loop
async def test_issue_323(mysql_server, loop, recwarn):
async with aiomysql.create_pool(**mysql_server['conn_params'],
loop=loop) as pool:
async with pool.get() as conn:
async with conn.cursor() as cur:
create_db = "CREATE DATABASE IF NOT EXISTS bugtest;"
await cur.execute(create_db)

create_table = """CREATE TABLE IF NOT EXISTS `bugtest`.`testtable` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`bindata` VARBINARY(200) NOT NULL,
PRIMARY KEY (`id`)
);"""

await cur.execute(create_table)

try:
async with conn.cursor() as cur:
await cur.execute("INSERT INTO `bugtest`.`testtable` "
"(bindata) VALUES (%s);",
(b'\xB0\x17',))

warnings = [warn for warn in recwarn.list
if warn.category is Warning]
assert len(warnings) == 0, "Got unexpected MySQL warning"

await cur.execute("SELECT * FROM `bugtest`.`testtable`;")
rows = await cur.fetchall()

assert len(rows) == 1, "Table should have 1 row"

finally:
async with conn.cursor() as cur:
await cur.execute("DELETE FROM `bugtest`.`testtable`;")

0 comments on commit bd61cdb

Please sign in to comment.