Skip to content

Commit

Permalink
Fix for CONPY-229:
Browse files Browse the repository at this point in the history
Pass NULL/None values to converter.
  • Loading branch information
9EOR9 committed Oct 19, 2022
1 parent 5dfeda2 commit 80b642b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
11 changes: 4 additions & 7 deletions mariadb/mariadb_codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ static PyObject *ma_convert_value(MrdbCursor *self,
PyObject *func;
PyObject *new_value= NULL;

if (!self->connection->converter || value == Py_None)
if (!self->connection->converter)
return NULL;

if ((func= PyDict_GetItem(self->connection->converter, key)) &&
Expand All @@ -431,17 +431,14 @@ field_fetch_fromtext(MrdbCursor *self, char *data, unsigned int column)
MYSQL_TIME tm;
unsigned long *length;
enum enum_extended_field_type ext_type= mariadb_extended_field_type(&self->fields[column]);
uint16_t type= self->fields[column].type;

if (!data)
{
Py_INCREF(Py_None);
self->values[column]= Py_None;
return;
}
type= MYSQL_TYPE_NULL;

length= mysql_fetch_lengths(self->result);

switch (self->fields[column].type)
switch (type)
{
case MYSQL_TYPE_NULL:
Py_INCREF(Py_None);
Expand Down
3 changes: 3 additions & 0 deletions mariadb/mariadb_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ void MrdbConnection_process_status_info(void *data, enum enum_mariadb_status_inf
if (!strncmp(key->str, "character_set_client", key->length) &&
strncmp(val->str, "utf8mb4", val->length))
{
/* mariadb_throw_exception (PyUnicode_FormatV)
doesn't support string with length,
so we need a temporary variable */
char charset[128];

memcpy(charset, val->str, val->length);
Expand Down
17 changes: 17 additions & 0 deletions testing/test/integration/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ def long_minus(s):
return s - 1


def none_to_string(s):
if s is None:
return "None"
return s


conversions = {
**{FIELD_TYPE.TIME: timedelta_to_time},
**{FIELD_TYPE.LONG: long_minus},
**{FIELD_TYPE.NULL: none_to_string},
**{FIELD_TYPE.LONGLONG: long_minus},
}

Expand Down Expand Up @@ -52,6 +59,16 @@ def test_convert_long(self):
self.assertEqual(row[0], a - 1)
del cursor

def test_convert_none(self):
cursor = self.connection.cursor()
cursor.execute("SELECT NULL")
row = cursor.fetchone()
self.assertEqual(row[0], "None")
cursor.execute("SELECT ?", (None,))
row = cursor.fetchone()
self.assertEqual(row[0], "None")
del cursor


if __name__ == '__main__':
unittest.main()

0 comments on commit 80b642b

Please sign in to comment.