Skip to content

Commit 3fb4eb1

Browse files
friedmethane
authored andcommitted
Use binary type by default for converter input. (#351)
Before *Most* string fields were passed to custom converter functions as "bytes", everything else was assumed to be valid encoded utf-8 and passed as str. This could cause issues where some unknown field was not valid utf-8 and a decode error is triggered where it can't be fixed or avoided by the end user. Instead we should short list those fields that we know for sure have to be str type because the existing converters expect it to be that way. So all the date conversions, and (NEW)DECIMAL since the python decimal type will not accept bytes object. Everything else stays bytes, which is the least suprising and safest thing to do.
1 parent 4a48401 commit 3fb4eb1

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

MySQLdb/_mysql.c

+9-18
Original file line numberDiff line numberDiff line change
@@ -1132,25 +1132,16 @@ _mysql_field_to_python(
11321132
//fprintf(stderr, "\n");
11331133
int binary;
11341134
switch (field->type) {
1135-
case FIELD_TYPE_TINY_BLOB:
1136-
case FIELD_TYPE_MEDIUM_BLOB:
1137-
case FIELD_TYPE_LONG_BLOB:
1138-
case FIELD_TYPE_BLOB:
1139-
case FIELD_TYPE_VAR_STRING:
1140-
case FIELD_TYPE_STRING:
1141-
case FIELD_TYPE_GEOMETRY:
1142-
case FIELD_TYPE_BIT:
1143-
#ifdef FIELD_TYPE_JSON
1144-
case FIELD_TYPE_JSON:
1145-
#else
1146-
case 245: // JSON
1147-
#endif
1148-
// Call converter with bytes
1149-
binary = 1;
1135+
case FIELD_TYPE_DECIMAL:
1136+
case FIELD_TYPE_NEWDECIMAL:
1137+
case FIELD_TYPE_TIMESTAMP:
1138+
case FIELD_TYPE_DATETIME:
1139+
case FIELD_TYPE_TIME:
1140+
case FIELD_TYPE_DATE:
1141+
binary = 0; // pass str, because these converters expect it
11501142
break;
1151-
default: // e.g. FIELD_TYPE_DATETIME, etc.
1152-
// Call converter with unicode string
1153-
binary = 0;
1143+
default: // Default to just passing bytes
1144+
binary = 1;
11541145
}
11551146
return PyObject_CallFunction(converter,
11561147
binary ? "y#" : "s#",

0 commit comments

Comments
 (0)