Skip to content

Commit e8c1ebb

Browse files
committed
Call decoders with unicode when field is not str and bytes.
1 parent b5780d2 commit e8c1ebb

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

_mysql.c

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,11 @@ _mysql_ResultObject_Initialize(
438438
Py_INCREF(Py_None);
439439
}
440440
else if (PySequence_Check(fun)) {
441+
long flags = fields[i].flags;
441442
int j, n2=PySequence_Size(fun);
443+
if (fields[i].charsetnr != 63) { /* maaagic */
444+
flags &= ~BINARY_FLAG;
445+
}
442446
PyObject *fun2=NULL;
443447
for (j=0; j<n2; j++) {
444448
PyObject *t = PySequence_GetItem(fun, j);
@@ -448,17 +452,13 @@ _mysql_ResultObject_Initialize(
448452
return -1;
449453
}
450454
if (PyTuple_Check(t) && PyTuple_GET_SIZE(t) == 2) {
451-
long mask, flags;
455+
long mask;
452456
PyObject *pmask=NULL;
453457
pmask = PyTuple_GET_ITEM(t, 0);
454458
fun2 = PyTuple_GET_ITEM(t, 1);
455459
Py_XINCREF(fun2);
456460
if (PyInt_Check(pmask)) {
457461
mask = PyInt_AS_LONG(pmask);
458-
flags = fields[i].flags;
459-
if (fields[i].charsetnr != 63) { /* maaagic */
460-
flags &= ~BINARY_FLAG;
461-
}
462462
if (mask & flags) {
463463
Py_DECREF(t);
464464
break;
@@ -1343,23 +1343,41 @@ static PyObject *
13431343
_mysql_field_to_python(
13441344
PyObject *converter,
13451345
char *rowitem,
1346-
unsigned long length)
1346+
unsigned long length,
1347+
MYSQL_FIELD *field)
13471348
{
1349+
int field_type = field->type;
13481350
PyObject *v;
1351+
#ifdef IS_PY3K
1352+
// Return bytes for binary and string types.
1353+
int binary = 0;
1354+
if (field_type == FIELD_TYPE_TINY_BLOB ||
1355+
field_type == FIELD_TYPE_MEDIUM_BLOB ||
1356+
field_type == FIELD_TYPE_LONG_BLOB ||
1357+
field_type == FIELD_TYPE_BLOB ||
1358+
field_type == FIELD_TYPE_VAR_STRING ||
1359+
field_type == FIELD_TYPE_STRING) {
1360+
binary = 1;
1361+
}
1362+
#endif
13491363
if (rowitem) {
13501364
if (converter != Py_None) {
1365+
const char *fmt = "s#";
13511366
v = PyObject_CallFunction(converter,
13521367
#ifdef IS_PY3K
1353-
"y#",
1368+
binary ? "y#" : "s#",
13541369
#else
13551370
"s#",
13561371
#endif
13571372
rowitem,
13581373
(int)length);
13591374
} else {
1360-
// TODO: Should we decode here on Python 3?
1361-
v = PyBytes_FromStringAndSize(rowitem,
1362-
(int)length);
1375+
#ifdef IS_PY3K
1376+
if (!binary) {
1377+
v = PyUnicode_FromStringAndSize(rowitem, (int)length);
1378+
} else
1379+
#endif
1380+
v = PyBytes_FromStringAndSize(rowitem, (int)length);
13631381
}
13641382
if (!v)
13651383
return NULL;
@@ -1378,14 +1396,16 @@ _mysql_row_to_tuple(
13781396
unsigned int n, i;
13791397
unsigned long *length;
13801398
PyObject *r, *c;
1399+
MYSQL_FIELD *fields;
13811400

13821401
n = mysql_num_fields(self->result);
13831402
if (!(r = PyTuple_New(n))) return NULL;
13841403
length = mysql_fetch_lengths(self->result);
1404+
fields = mysql_fetch_fields(self->result);
13851405
for (i=0; i<n; i++) {
13861406
PyObject *v;
13871407
c = PyTuple_GET_ITEM(self->converter, i);
1388-
v = _mysql_field_to_python(c, row[i], length[i]);
1408+
v = _mysql_field_to_python(c, row[i], length[i], &fields[i]);
13891409
if (!v) goto error;
13901410
PyTuple_SET_ITEM(r, i, v);
13911411
}
@@ -1403,16 +1423,16 @@ _mysql_row_to_dict(
14031423
unsigned int n, i;
14041424
unsigned long *length;
14051425
PyObject *r, *c;
1406-
MYSQL_FIELD *fields;
1426+
MYSQL_FIELD *fields;
14071427

14081428
n = mysql_num_fields(self->result);
14091429
if (!(r = PyDict_New())) return NULL;
14101430
length = mysql_fetch_lengths(self->result);
1411-
fields = mysql_fetch_fields(self->result);
1431+
fields = mysql_fetch_fields(self->result);
14121432
for (i=0; i<n; i++) {
14131433
PyObject *v;
14141434
c = PyTuple_GET_ITEM(self->converter, i);
1415-
v = _mysql_field_to_python(c, row[i], length[i]);
1435+
v = _mysql_field_to_python(c, row[i], length[i], &fields[i]);
14161436
if (!v) goto error;
14171437
if (!PyMapping_HasKeyString(r, fields[i].name)) {
14181438
PyMapping_SetItemString(r, fields[i].name, v);
@@ -1447,11 +1467,11 @@ _mysql_row_to_dict_old(
14471467
n = mysql_num_fields(self->result);
14481468
if (!(r = PyDict_New())) return NULL;
14491469
length = mysql_fetch_lengths(self->result);
1450-
fields = mysql_fetch_fields(self->result);
1470+
fields = mysql_fetch_fields(self->result);
14511471
for (i=0; i<n; i++) {
14521472
PyObject *v;
14531473
c = PyTuple_GET_ITEM(self->converter, i);
1454-
v = _mysql_field_to_python(c, row[i], length[i]);
1474+
v = _mysql_field_to_python(c, row[i], length[i], &fields[i]);
14551475
if (!v) goto error;
14561476
{
14571477
int len=0;

0 commit comments

Comments
 (0)