From d22aa0718a0fedad767a9adec75608c4d95a8ef5 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 7 Dec 2018 12:25:23 +0900 Subject: [PATCH 1/3] Simplify converters --- MySQLdb/_mysql.c | 3 --- MySQLdb/converters.py | 33 ++++++++------------------------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index b45682df..6bbd3a01 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -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"); diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index c13e4265..83f24001 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -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 @@ -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. @@ -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 @@ -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) @@ -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, @@ -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 From 2b660c28939a39e182b76d02556daf32ae029bbf Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 7 Dec 2018 12:29:19 +0900 Subject: [PATCH 2/3] fix --- MySQLdb/converters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 83f24001..d5b59a79 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -105,7 +105,7 @@ def quote_tuple(t, d): DateTimeDeltaType: DateTimeDelta2literal, str: Thing2Literal, # default set: Set2Str, - Decimal = Decimal2Literal + Decimal: Decimal2Literal, FIELD_TYPE.TINY: int, FIELD_TYPE.SHORT: int, From 802e4298fc276d801bbe583375f42ae0e2ea923a Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 7 Dec 2018 13:44:13 +0900 Subject: [PATCH 3/3] Remove test for _mysql.NULL --- tests/test_MySQLdb_nonstandard.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/test_MySQLdb_nonstandard.py b/tests/test_MySQLdb_nonstandard.py index e60a2a77..55cfc630 100644 --- a/tests/test_MySQLdb_nonstandard.py +++ b/tests/test_MySQLdb_nonstandard.py @@ -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))