Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c66b43c

Browse filesBrowse files
committed
Connection.literal() always returns str instance.
1 parent 382fb9f commit c66b43c
Copy full SHA for c66b43c

File tree

Expand file treeCollapse file tree

2 files changed

+17
-17
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-17
lines changed

‎MySQLdb/connections.py

Copy file name to clipboardExpand all lines: MySQLdb/connections.py
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
NotSupportedError, ProgrammingError
1414
import _mysql
1515
import re
16+
import sys
17+
18+
PY2 = sys.version_info[0] == 2
1619

1720

1821
def defaulterrorhandler(connection, cursor, errorclass, errorvalue):
@@ -280,7 +283,10 @@ def literal(self, o):
280283
applications.
281284
282285
"""
283-
return self.escape(o, self.encoders)
286+
s = self.escape(o, self.encoders)
287+
if not PY2 and isinstance(s, bytes):
288+
return s.decode('ascii', 'surrogateescape')
289+
return s
284290

285291
def begin(self):
286292
"""Explicitly begin a connection. Non-standard.

‎MySQLdb/cursors.py

Copy file name to clipboardExpand all lines: MySQLdb/cursors.py
+10-16Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from MySQLdb.compat import unicode
1313

14-
restr = br"""
14+
restr = r"""
1515
\s
1616
values
1717
\s*
@@ -179,23 +179,12 @@ def execute(self, query, args=None):
179179
db = self._get_db()
180180
if PY2 and isinstance(query, unicode):
181181
query = query.encode(db.unicode_literal.charset)
182-
else:
183-
def decode(x):
184-
if isinstance(x, bytes):
185-
x = x.decode('ascii', 'surrogateescape')
186-
return x
187182

188183
if args is not None:
189184
if isinstance(args, dict):
190-
if PY2:
191-
args = dict((key, db.literal(item)) for key, item in args.iteritems())
192-
else:
193-
args = dict((key, decode(db.literal(item))) for key, item in args.items())
185+
args = dict((key, db.literal(item)) for key, item in args.iteritems())
194186
else:
195-
if PY2:
196-
args = tuple(map(db.literal, args))
197-
else:
198-
args = tuple([decode(db.literal(x)) for x in args])
187+
args = tuple(map(db.literal, args))
199188
if not PY2 and isinstance(query, bytes):
200189
query = query.decode(db.unicode_literal.charset)
201190
query = query % args
@@ -246,8 +235,10 @@ def executemany(self, query, args):
246235
del self.messages[:]
247236
db = self._get_db()
248237
if not args: return
249-
if isinstance(query, unicode):
238+
if PY2 and isinstance(query, unicode):
250239
query = query.encode(db.unicode_literal.charset)
240+
elif not PY2 and isinstance(query, bytes):
241+
query = query.decode(db.unicode_literal.charset)
251242
m = insert_values.search(query)
252243
if not m:
253244
r = 0
@@ -277,7 +268,10 @@ def executemany(self, query, args):
277268
exc, value, tb = sys.exc_info()
278269
del tb
279270
self.errorhandler(self, exc, value)
280-
r = self._query('\n'.join([query[:p], ',\n'.join(q), query[e:]]))
271+
qs = '\n'.join([query[:p], ',\n'.join(q), query[e:]])
272+
if not PY2:
273+
qs = qs.encode(db.unicode_literal.charset, 'surrogateescape')
274+
r = self._query(qs)
281275
if not self._defer_warnings: self._warning_check()
282276
return r
283277

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.