diff --git a/python/umysql.c b/python/umysql.c index 4cc7d47..1fde43b 100644 --- a/python/umysql.c +++ b/python/umysql.c @@ -621,8 +621,11 @@ int API_resultRowValue(void *result, int column, UMTypeInfo *ti, char *value, si case MFTYPE_MEDIUM_BLOB: case MFTYPE_LONG_BLOB: case MFTYPE_BLOB: - // Fall through for string encoding - valobj = PyString_FromStringAndSize( (const char *) value, cbValue); + if (ti->flags & MFFLAG_BINARY_FLAG) { + valobj = PyString_FromStringAndSize( (const char *) value, cbValue); + } else { + valobj = DecodeString (ti, value, cbValue); + } break; //PyString family diff --git a/tests/tests.py b/tests/tests.py index d1345de..544cdb6 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -659,7 +659,37 @@ def testCharsets(self): self.assertEquals(result, expected) cnn.close() - + + def testTextCharsets(self): + aumlaut_unicode = u"\u00e4" + aumlaut_utf8 = "\xc3\xa4" + aumlaut_latin1 = "\xe4" + + cnn = umysql.Connection() + cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB) + + cnn.query("drop table if exists tblutf") + cnn.query("create table tblutf (test_mode TEXT DEFAULT NULL, test_utf TEXT DEFAULT NULL, test_latin1 TEXT) ENGINE=MyISAM DEFAULT CHARSET=utf8") + + # We insert the same character using two different encodings + cnn.query("set names utf8") + cnn.query("insert into tblutf (test_mode, test_utf, test_latin1) values ('utf8', _utf8'" + aumlaut_utf8 + "', _latin1'" + aumlaut_latin1 + "')") + + cnn.query("set names latin1") + cnn.query("insert into tblutf (test_mode, test_utf, test_latin1) values ('latin1', _utf8'" + aumlaut_utf8 + "', _latin1'" + aumlaut_latin1 + "')") + + # We expect the driver to always give us unicode strings back + expected = [(u"utf8", aumlaut_unicode, aumlaut_unicode), (u"latin1", aumlaut_unicode, aumlaut_unicode)] + + # Fetch and test with different charsets + for charset in ("latin1", "utf8", "cp1250"): + cnn.query("set names " + charset) + rs = cnn.query("select test_mode, test_utf, test_latin1 from tblutf") + result = rs.rows + self.assertEquals(result, expected) + + cnn.close() + if __name__ == '__main__': from guppy import hpy hp = hpy()