Skip to content

Simplify converters #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions MySQLdb/_mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -2737,9 +2737,6 @@ init_mysql(void)
_mysql_NewException(dict, edict, "NotSupportedError")))
goto error;
Py_DECREF(emod);
if (!(_mysql_NULL = PyString_FromString("NULL")))
goto error;
if (PyDict_SetItemString(dict, "NULL", _mysql_NULL)) goto error;
error:
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_ImportError, "_mysql: init failed");
Expand Down
33 changes: 8 additions & 25 deletions MySQLdb/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
(with the copy() method), modify the copies, and then pass them to
MySQL.connect().
"""
from decimal import Decimal

from MySQLdb._mysql import string_literal, escape, NULL
from MySQLdb._mysql import string_literal, escape
from MySQLdb.constants import FIELD_TYPE, FLAG
from MySQLdb.times import *
from MySQLdb.compat import PY2, long
Expand All @@ -46,10 +47,8 @@
ArrayType = array.array


def Bool2Str(s, d): return str(int(s))

def Str2Set(s):
return set([ i for i in s.split(',') if i ])
def Bool2Str(s, d):
return b'1' if s else b'0'

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

def None2NULL(o, d):
"""Convert None to NULL."""
return NULL # duh
return b"NULL"

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

def char_array(s):
return array.array('c', s)

def array2Str(o, d):
return Thing2Literal(o.tostring(), d)

Expand All @@ -109,18 +105,18 @@ def quote_tuple(t, d):
DateTimeDeltaType: DateTimeDelta2literal,
str: Thing2Literal, # default
set: Set2Str,
Decimal: Decimal2Literal,

FIELD_TYPE.TINY: int,
FIELD_TYPE.SHORT: int,
FIELD_TYPE.LONG: long,
FIELD_TYPE.FLOAT: float,
FIELD_TYPE.DOUBLE: float,
FIELD_TYPE.DECIMAL: float,
FIELD_TYPE.NEWDECIMAL: float,
FIELD_TYPE.DECIMAL: Decimal,
FIELD_TYPE.NEWDECIMAL: Decimal,
FIELD_TYPE.LONGLONG: long,
FIELD_TYPE.INT24: int,
FIELD_TYPE.YEAR: int,
FIELD_TYPE.SET: Str2Set,
FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter,
FIELD_TYPE.DATETIME: DateTime_or_None,
FIELD_TYPE.TIME: TimeDelta_or_None,
Expand All @@ -134,16 +130,3 @@ def quote_tuple(t, d):
FIELD_TYPE.VAR_STRING: _bytes_or_str,
FIELD_TYPE.VARCHAR: _bytes_or_str,
}

if PY2:
conversions[unicode] = Unicode2Str
else:
conversions[bytes] = Thing2Literal

try:
from decimal import Decimal
conversions[FIELD_TYPE.DECIMAL] = Decimal
conversions[FIELD_TYPE.NEWDECIMAL] = Decimal
conversions[Decimal] = Decimal2Literal
except ImportError:
pass
4 changes: 0 additions & 4 deletions tests/test_MySQLdb_nonstandard.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def test_set_inequality_membership(self):
class TestCoreModule(unittest.TestCase):
"""Core _mysql module features."""

def test_NULL(self):
"""Should have a NULL constant."""
self.assertEqual(_mysql.NULL, 'NULL')

def test_version(self):
"""Version information sanity."""
self.assertTrue(isinstance(_mysql.__version__, str))
Expand Down