Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes warning when inserting binary strings #326

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`;")