Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Encode query and args to bytes before `%` #221

Merged
merged 1 commit into from

1 participant

INADA Naoki
INADA Naoki
Owner

No description provided.

INADA Naoki
Owner

conn.escape(bytes_obj) returns bytes on Python 2.
So when I have text and binary column, it is too difficult to avoid UnicodeError.

INADA Naoki methane merged commit 633f9b3 into from
INADA Naoki methane deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 10, 2014
  1. INADA Naoki
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 3 deletions.
  1. +19 −3 pymysql/cursors.py
22 pymysql/cursors.py
View
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, absolute_import
-from ._compat import range_type
+from ._compat import range_type, PY2
from .err import (
Warning, Error, InterfaceError, DataError,
@@ -76,13 +76,29 @@ def nextset(self):
return True
def execute(self, query, args=None):
- ''' Execute a query '''
+ '''Execute a query'''
conn = self._get_db()
while self.nextset():
pass
- # TODO: make sure that conn.escape is correct
+ if PY2: # Use bytes on Python 2 always
+ encoding = conn.encoding
+
+ def ensure_bytes(x):
+ if isinstance(x, unicode):
+ x = x.encode(encoding)
+ return x
+
+ query = ensure_bytes(query)
+
+ if args is not None:
+ if isinstance(args, (tuple, list)):
+ args = tuple(map(ensure_bytes, args))
+ elif isinstance(args, dict):
+ args = dict((ensure_bytes(key), ensure_bytes(val)) for (key, val) in args.items())
+ else:
+ args = ensure_bytes(args)
if args is not None:
if isinstance(args, (tuple, list)):
Something went wrong with that request. Please try again.