Skip to content

Commit d22aa07

Browse files
committed
Simplify converters
1 parent 628bb1b commit d22aa07

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

MySQLdb/_mysql.c

-3
Original file line numberDiff line numberDiff line change
@@ -2737,9 +2737,6 @@ init_mysql(void)
27372737
_mysql_NewException(dict, edict, "NotSupportedError")))
27382738
goto error;
27392739
Py_DECREF(emod);
2740-
if (!(_mysql_NULL = PyString_FromString("NULL")))
2741-
goto error;
2742-
if (PyDict_SetItemString(dict, "NULL", _mysql_NULL)) goto error;
27432740
error:
27442741
if (PyErr_Occurred()) {
27452742
PyErr_SetString(PyExc_ImportError, "_mysql: init failed");

MySQLdb/converters.py

+8-25
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
(with the copy() method), modify the copies, and then pass them to
3131
MySQL.connect().
3232
"""
33+
from decimal import Decimal
3334

34-
from MySQLdb._mysql import string_literal, escape, NULL
35+
from MySQLdb._mysql import string_literal, escape
3536
from MySQLdb.constants import FIELD_TYPE, FLAG
3637
from MySQLdb.times import *
3738
from MySQLdb.compat import PY2, long
@@ -46,10 +47,8 @@
4647
ArrayType = array.array
4748

4849

49-
def Bool2Str(s, d): return str(int(s))
50-
51-
def Str2Set(s):
52-
return set([ i for i in s.split(',') if i ])
50+
def Bool2Str(s, d):
51+
return b'1' if s else b'0'
5352

5453
def Set2Str(s, d):
5554
# Only support ascii string. Not tested.
@@ -73,7 +72,7 @@ def Float2Str(o, d):
7372

7473
def None2NULL(o, d):
7574
"""Convert None to NULL."""
76-
return NULL # duh
75+
return b"NULL"
7776

7877
def Thing2Literal(o, d):
7978
"""Convert something into a SQL string literal. If using
@@ -85,9 +84,6 @@ def Thing2Literal(o, d):
8584
def Decimal2Literal(o, d):
8685
return format(o, 'f')
8786

88-
def char_array(s):
89-
return array.array('c', s)
90-
9187
def array2Str(o, d):
9288
return Thing2Literal(o.tostring(), d)
9389

@@ -109,18 +105,18 @@ def quote_tuple(t, d):
109105
DateTimeDeltaType: DateTimeDelta2literal,
110106
str: Thing2Literal, # default
111107
set: Set2Str,
108+
Decimal = Decimal2Literal
112109

113110
FIELD_TYPE.TINY: int,
114111
FIELD_TYPE.SHORT: int,
115112
FIELD_TYPE.LONG: long,
116113
FIELD_TYPE.FLOAT: float,
117114
FIELD_TYPE.DOUBLE: float,
118-
FIELD_TYPE.DECIMAL: float,
119-
FIELD_TYPE.NEWDECIMAL: float,
115+
FIELD_TYPE.DECIMAL: Decimal,
116+
FIELD_TYPE.NEWDECIMAL: Decimal,
120117
FIELD_TYPE.LONGLONG: long,
121118
FIELD_TYPE.INT24: int,
122119
FIELD_TYPE.YEAR: int,
123-
FIELD_TYPE.SET: Str2Set,
124120
FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter,
125121
FIELD_TYPE.DATETIME: DateTime_or_None,
126122
FIELD_TYPE.TIME: TimeDelta_or_None,
@@ -134,16 +130,3 @@ def quote_tuple(t, d):
134130
FIELD_TYPE.VAR_STRING: _bytes_or_str,
135131
FIELD_TYPE.VARCHAR: _bytes_or_str,
136132
}
137-
138-
if PY2:
139-
conversions[unicode] = Unicode2Str
140-
else:
141-
conversions[bytes] = Thing2Literal
142-
143-
try:
144-
from decimal import Decimal
145-
conversions[FIELD_TYPE.DECIMAL] = Decimal
146-
conversions[FIELD_TYPE.NEWDECIMAL] = Decimal
147-
conversions[Decimal] = Decimal2Literal
148-
except ImportError:
149-
pass

0 commit comments

Comments
 (0)