Skip to content

Commit 80b3430

Browse files
committed
Fix TypeError exception found in BUG22529828 patch using Python 2
1 parent def354c commit 80b3430

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/mysql/connector/cursor.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,20 @@ def _bytestr_format_dict(bytestr, value_dict):
9696
b'x=%(y)s y=%(x)s'
9797
"""
9898
def replace(matchobj):
99+
value = None
99100
groups = matchobj.groupdict()
100101
if groups["conversion_type"] == b"%":
101-
return b"%"
102+
value = b"%"
102103
if groups["conversion_type"] == b"s":
103-
return value_dict[groups["mapping_key"]]
104-
raise ValueError("Unsupported conversion_type: {0}"
105-
"".format(groups["conversion_type"]))
106-
return RE_PY_MAPPING_PARAM.sub(replace, bytestr)
107-
104+
key = groups["mapping_key"].encode("utf-8") \
105+
if PY2 else groups["mapping_key"]
106+
value = value_dict[key]
107+
if value is None:
108+
raise ValueError("Unsupported conversion_type: {0}"
109+
"".format(groups["conversion_type"]))
110+
return value.decode("utf-8") if PY2 else value
111+
return RE_PY_MAPPING_PARAM.sub(replace, bytestr.decode("utf-8")
112+
if PY2 else bytestr)
108113

109114
class CursorBase(MySQLCursorAbstract):
110115
"""

tests/test_cursor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ def test__process_params(self):
361361
datetime.time(20, 3, 23),
362362
st_now,
363363
datetime.timedelta(hours=40, minutes=30, seconds=12),
364+
'foo %(t)s',
365+
'foo %(s)s',
364366
)
365367
exp = (
366368
b'NULL',
@@ -382,6 +384,8 @@ def test__process_params(self):
382384
b"'" + time.strftime('%Y-%m-%d %H:%M:%S', st_now).encode('ascii')
383385
+ b"'",
384386
b"'40:30:12'",
387+
b"'foo %(t)s'",
388+
b"'foo %(s)s'",
385389
)
386390

387391
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())

0 commit comments

Comments
 (0)