Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Decode string for *TEXT columns #12

Merged
merged 2 commits into from
Apr 20, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions python/umysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down