Incorrect substitution by cursor.execute when tuple args contains '%s'
Affects | Status | Importance | Assigned to | Milestone | ||
---|---|---|---|---|---|---|
MySQL Connector/Python | Status tracked in Trunk | |||||
0.3 |
Fix Released
|
Critical
|
Geert JM Vanderkelen | |||
Trunk |
Triaged
|
Critical
|
Geert JM Vanderkelen |
Bug Description
Description:
cursor.execute substitute %s occurrence found in tuple args value, causing an incorrect query to be produced has is shown below:
c.execute( 'REPLACE INTO TestBug VALUES (%s, %s, %s)', (1, 'Format%s', 3) )
MYSQL QUERY: b"REPLACE INTO TestBug VALUES (1, 'Format3', %s)"
The MYSQL QUERY trace is the content of stmt in MySQLCursor.execute before "self.db(
How to reproduce:
CREATE TABLE TestBug (
id INT UNSIGNED NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
count INT NOT NULL,
PRIMARY KEY (id)
);
db = mysql.connector
c = db.cursor()
c.execute( 'REPLACE INTO TestBug VALUES (%s, %s, %s)', (1, 'Format%s', 3) )
MYSQL QUERY: b"REPLACE INTO TestBug VALUES (1, 'Format3', %s)"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python31\
res = self.db(
File "C:\Python31\
return func(*args, **kwargs)
File "C:\Python31\
return self.handle_
File "C:\Python31\
errors.
File "C:\Python31\
raise get_mysql_
mysql.connector
Environment info:
* MySQL Connector/Python version (and/or revision)
python -c "import mysql.connector as db; print( db.__version__ )"
(0, 3, 0, 'devel', 287)
* Python version
python --version
Python 3.1.1
mysql> SELECT VERSION()
| 5.0.51b-
* Platform/OS: Windows XP SP2 32 bits
tags: | added: py3k |
Attached is a patch I used as a work-around for the issue. It ensures the following test pass:
def test_subtargs():
assert _subst_tuple_args( b'A%s, %sB', (b'1',b'2') ) == b'A1, 2B'
assert _subst_tuple_args( b'A%s, %sB', (b'%s1',b'2') ) == b'A%s1, 2B'
assert _subst_tuple_args( b"A%s, 'B%C'", (b'%s1',) ) == b"A%s1, 'B%C'"
assert _subst_tuple_args( b"AB", () ) == b"AB"
try:
assert _subst_tuple_args( b"AB", (1,) ) == '', 'Too many arg exception expected'
except ValueError:
pass
try:
assert _subst_tuple_args( b'A%s, %sB', (b'1',) ) == '', 'Missing arg exception expected'
except IndexError:
pass