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 382fb9f

Browse filesBrowse files
committed
Random fixes.
1 parent 9cdef71 commit 382fb9f
Copy full SHA for 382fb9f

File tree

Expand file treeCollapse file tree

3 files changed

+48
-49
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+48
-49
lines changed

‎MySQLdb/__init__.py

Copy file name to clipboardExpand all lines: MySQLdb/__init__.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_DBAPISet_set_inequality_membership():
7373
assert FIELD_TYPE.DATE != STRING
7474

7575
def Binary(x):
76-
return str(x)
76+
return bytes(x)
7777

7878
def Connect(*args, **kwargs):
7979
"""Factory function for connections.Connection."""

‎MySQLdb/converters.py

Copy file name to clipboardExpand all lines: MySQLdb/converters.py
+14-38Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838

3939
try:
4040
from types import IntType, LongType, FloatType, NoneType, TupleType, ListType, DictType, InstanceType, \
41-
StringType, UnicodeType, ObjectType, BooleanType, ClassType, TypeType
41+
ObjectType, BooleanType
42+
PY2 = True
4243
except ImportError:
4344
# Python 3
4445
long = int
4546
IntType, LongType, FloatType, NoneType = int, long, float, type(None)
4647
TupleType, ListType, DictType, InstanceType = tuple, list, dict, None
47-
StringType, UnicodeType, ObjectType, BooleanType = bytes, str, object, bool
48+
ObjectType, BooleanType = object, bool
49+
PY2 = False
4850

4951
import array
5052

@@ -95,34 +97,6 @@ def Thing2Literal(o, d):
9597
return string_literal(o, d)
9698

9799

98-
def Instance2Str(o, d):
99-
100-
"""
101-
102-
Convert an Instance to a string representation. If the __str__()
103-
method produces acceptable output, then you don't need to add the
104-
class to conversions; it will be handled by the default
105-
converter. If the exact class is not found in d, it will use the
106-
first class it can find for which o is an instance.
107-
108-
"""
109-
110-
if o.__class__ in d:
111-
return d[o.__class__](o, d)
112-
cl = filter(lambda x,o=o:
113-
type(x) is ClassType
114-
and isinstance(o, x), d.keys())
115-
if not cl:
116-
cl = filter(lambda x,o=o:
117-
type(x) is TypeType
118-
and isinstance(o, x)
119-
and d[x] is not Instance2Str,
120-
d.keys())
121-
if not cl:
122-
return d[StringType](o,d)
123-
d[o.__class__] = d[cl[0]]
124-
return d[cl[0]](o, d)
125-
126100
def char_array(s):
127101
return array.array('c', s)
128102

@@ -140,14 +114,12 @@ def quote_tuple(t, d):
140114
TupleType: quote_tuple,
141115
ListType: quote_tuple,
142116
DictType: escape_dict,
143-
InstanceType: Instance2Str,
144117
ArrayType: array2Str,
145-
StringType: Thing2Literal, # default
146-
UnicodeType: Unicode2Str,
147-
ObjectType: Instance2Str,
148118
BooleanType: Bool2Str,
119+
Date: Thing2Literal,
149120
DateTimeType: DateTime2literal,
150121
DateTimeDeltaType: DateTimeDelta2literal,
122+
str: str, # default
151123
set: Set2Str,
152124
FIELD_TYPE.TINY: int,
153125
FIELD_TYPE.SHORT: int,
@@ -165,18 +137,22 @@ def quote_tuple(t, d):
165137
FIELD_TYPE.TIME: TimeDelta_or_None,
166138
FIELD_TYPE.DATE: Date_or_None,
167139
FIELD_TYPE.BLOB: [
168-
(FLAG.BINARY, str),
140+
(FLAG.BINARY, bytes),
169141
],
170142
FIELD_TYPE.STRING: [
171-
(FLAG.BINARY, str),
143+
(FLAG.BINARY, bytes),
172144
],
173145
FIELD_TYPE.VAR_STRING: [
174-
(FLAG.BINARY, str),
146+
(FLAG.BINARY, bytes),
175147
],
176148
FIELD_TYPE.VARCHAR: [
177-
(FLAG.BINARY, str),
149+
(FLAG.BINARY, bytes),
178150
],
179151
}
152+
if PY2:
153+
conversions[unicode] = Unicode2Str
154+
else:
155+
conversions[bytes] = bytes
180156

181157
try:
182158
from decimal import Decimal

‎MySQLdb/cursors.py

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

88
import re
99
import sys
10+
PY2 = sys.version_info[0] == 2
1011

1112
from MySQLdb.compat import unicode
1213

13-
restr = r"""
14+
restr = br"""
1415
\s
1516
values
1617
\s*
@@ -68,9 +69,9 @@ class BaseCursor(object):
6869
_defer_warnings = False
6970

7071
def __init__(self, connection):
71-
from weakref import proxy
72+
from weakref import ref
7273

73-
self.connection = proxy(connection)
74+
self.connection = ref(connection)
7475
self.description = None
7576
self.description_flags = None
7677
self.rowcount = -1
@@ -91,7 +92,8 @@ def __del__(self):
9192

9293
def close(self):
9394
"""Close the cursor. No further queries will be possible."""
94-
if not self.connection: return
95+
if self.connection is None or self.connection() is None:
96+
return
9597
while self.nextset(): pass
9698
self.connection = None
9799

@@ -152,9 +154,12 @@ def setoutputsizes(self, *args):
152154
"""Does nothing, required by DB API."""
153155

154156
def _get_db(self):
155-
if not self.connection:
157+
con = self.connection
158+
if con is not None:
159+
con = con()
160+
if con is None:
156161
self.errorhandler(self, ProgrammingError, "cursor closed")
157-
return self.connection
162+
return con
158163

159164
def execute(self, query, args=None):
160165

@@ -172,14 +177,32 @@ def execute(self, query, args=None):
172177
"""
173178
del self.messages[:]
174179
db = self._get_db()
175-
if isinstance(query, unicode):
180+
if PY2 and isinstance(query, unicode):
176181
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
187+
177188
if args is not None:
178189
if isinstance(args, dict):
179-
query = query % dict((key, db.literal(item))
180-
for key, item in args.iteritems())
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())
181194
else:
182-
query = query % tuple([db.literal(item) for item in args])
195+
if PY2:
196+
args = tuple(map(db.literal, args))
197+
else:
198+
args = tuple([decode(db.literal(x)) for x in args])
199+
if not PY2 and isinstance(query, bytes):
200+
query = query.decode(db.unicode_literal.charset)
201+
query = query % args
202+
203+
if isinstance(query, unicode):
204+
query = query.encode(db.unicode_literal.charset, 'surrogateescape')
205+
183206
try:
184207
r = None
185208
r = self._query(query)

0 commit comments

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