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 e7545c7

Browse filesBrowse files
committed
refactoring.
1 parent af18cc9 commit e7545c7
Copy full SHA for e7545c7

File tree

Expand file treeCollapse file tree

3 files changed

+25
-30
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+25
-30
lines changed

‎MySQLdb/compat.py

Copy file name to clipboard
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import sys
22

33
if sys.version_info[0] == 3:
4+
PY2 = False
45
unicode = str
56
unichr = chr
7+
long = int
68
else:
9+
PY2 = True
710
unicode = unicode
811
unichr = unichr
12+
long = long

‎MySQLdb/connections.py

Copy file name to clipboardExpand all lines: MySQLdb/connections.py
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
88
"""
99
from MySQLdb import cursors
10-
from MySQLdb.compat import unicode
10+
from MySQLdb.compat import unicode, PY2
1111
from _mysql_exceptions import Warning, Error, InterfaceError, DataError, \
1212
DatabaseError, OperationalError, IntegrityError, InternalError, \
1313
NotSupportedError, ProgrammingError
1414
import _mysql
1515
import re
1616
import sys
1717

18-
PY2 = sys.version_info[0] == 2
19-
2018

2119
def defaulterrorhandler(connection, cursor, errorclass, errorvalue):
2220
"""
@@ -123,6 +121,7 @@ class object, used to create cursors (keyword only)
123121
columns are returned as strings. columns are returned as
124122
normal strings. Unicode objects will always be encoded to
125123
the connection's character set regardless of this setting.
124+
Default to False on Python 2 and True on Python 3.
126125
127126
charset
128127
If supplied, the connection character set will be changed
@@ -207,15 +206,18 @@ class object, used to create cursors (keyword only)
207206

208207
db = proxy(self)
209208
def _get_string_literal():
209+
# Note: string_literal() is called for bytes object on Python 3.
210210
def string_literal(obj, dummy=None):
211211
return db.string_literal(obj)
212212
return string_literal
213213

214214
def _get_unicode_literal():
215215
if PY2:
216+
# unicode_literal is called for only unicode object.
216217
def unicode_literal(u, dummy=None):
217218
return db.literal(u.encode(unicode_literal.charset))
218219
else:
220+
# unicode_literal() is called for arbitrary object.
219221
def unicode_literal(u, dummy=None):
220222
return db.literal(str(u).encode(unicode_literal.charset))
221223
return unicode_literal
@@ -288,6 +290,11 @@ def literal(self, o):
288290
289291
"""
290292
s = self.escape(o, self.encoders)
293+
# Python 3 doesn't support % operation for bytes object.
294+
# We should decode it before using %.
295+
# Decoding with ascii and surrogateescape allows convert arbitrary
296+
# bytes to unicode and back again.
297+
# See http://python.org/dev/peps/pep-0383/
291298
if not PY2 and isinstance(s, bytes):
292299
return s.decode('ascii', 'surrogateescape')
293300
return s

‎MySQLdb/converters.py

Copy file name to clipboardExpand all lines: MySQLdb/converters.py
+11-27Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,9 @@
3535
from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
3636
from MySQLdb.constants import FIELD_TYPE, FLAG
3737
from MySQLdb.times import *
38+
from MySQLdb.compat import PY2, long
3839

39-
try:
40-
from types import IntType, LongType, FloatType, NoneType, TupleType, ListType, DictType, InstanceType, \
41-
ObjectType, BooleanType
42-
PY2 = True
43-
except ImportError:
44-
# Python 3
45-
long = int
46-
IntType, LongType, FloatType, NoneType = int, long, float, type(None)
47-
TupleType, ListType, DictType, InstanceType = tuple, list, dict, None
48-
ObjectType, BooleanType = object, bool
49-
PY2 = False
40+
NoneType = type(None)
5041

5142
import array
5243

@@ -78,8 +69,6 @@ def Unicode2Str(s, d):
7869
is connection-dependent."""
7970
return s.encode()
8071

81-
Long2Int = Thing2Str
82-
8372
def Float2Str(o, d):
8473
return '%.15g' % o
8574

@@ -88,12 +77,10 @@ def None2NULL(o, d):
8877
return NULL # duh
8978

9079
def Thing2Literal(o, d):
91-
9280
"""Convert something into a SQL string literal. If using
9381
MySQL-3.23 or newer, string_literal() is a method of the
9482
_mysql.MYSQL object, and this function will be overridden with
9583
that method when the connection is created."""
96-
9784
return string_literal(o, d)
9885

9986

@@ -107,19 +94,19 @@ def quote_tuple(t, d):
10794
return "(%s)" % (','.join(escape_sequence(t, d)))
10895

10996
conversions = {
110-
IntType: Thing2Str,
111-
LongType: Long2Int,
112-
FloatType: Float2Str,
97+
int: Thing2Str,
98+
long: Thing2Str,
99+
float: Float2Str,
113100
NoneType: None2NULL,
114-
TupleType: quote_tuple,
115-
ListType: quote_tuple,
116-
DictType: escape_dict,
101+
tuple: quote_tuple,
102+
list: quote_tuple,
103+
dict: escape_dict,
117104
ArrayType: array2Str,
118-
BooleanType: Bool2Str,
105+
bool: Bool2Str,
119106
Date: Thing2Literal,
120107
DateTimeType: DateTime2literal,
121108
DateTimeDeltaType: DateTimeDelta2literal,
122-
str: str, # default
109+
str: Thing2Literal, # default
123110
set: Set2Str,
124111
FIELD_TYPE.TINY: int,
125112
FIELD_TYPE.SHORT: int,
@@ -152,14 +139,11 @@ def quote_tuple(t, d):
152139
if PY2:
153140
conversions[unicode] = Unicode2Str
154141
else:
155-
conversions[bytes] = bytes
142+
conversions[bytes] = Thing2Literal
156143

157144
try:
158145
from decimal import Decimal
159146
conversions[FIELD_TYPE.DECIMAL] = Decimal
160147
conversions[FIELD_TYPE.NEWDECIMAL] = Decimal
161148
except ImportError:
162149
pass
163-
164-
165-

0 commit comments

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