Skip to content

Commit 819688b

Browse files
authored
Simplify converters (#304)
1 parent 5e8eeac commit 819688b

File tree

3 files changed

+8
-32
lines changed

3 files changed

+8
-32
lines changed

MySQLdb/_mysql.c

-3
Original file line numberDiff line numberDiff line change
@@ -2730,9 +2730,6 @@ init_mysql(void)
27302730
_mysql_NewException(dict, edict, "NotSupportedError")))
27312731
goto error;
27322732
Py_DECREF(emod);
2733-
if (!(_mysql_NULL = PyString_FromString("NULL")))
2734-
goto error;
2735-
if (PyDict_SetItemString(dict, "NULL", _mysql_NULL)) goto error;
27362733
error:
27372734
if (PyErr_Occurred()) {
27382735
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

tests/test_MySQLdb_nonstandard.py

-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ def test_set_inequality_membership(self):
2525
class TestCoreModule(unittest.TestCase):
2626
"""Core _mysql module features."""
2727

28-
def test_NULL(self):
29-
"""Should have a NULL constant."""
30-
self.assertEqual(_mysql.NULL, 'NULL')
31-
3228
def test_version(self):
3329
"""Version information sanity."""
3430
self.assertTrue(isinstance(_mysql.__version__, str))

0 commit comments

Comments
 (0)