Skip to content

Commit 762fbd3

Browse files
committed
The sqlite3 module did cut off data from the SQLite database at the first null
character before sending it to a custom converter. This has been fixed now.
1 parent 6ffe499 commit 762fbd3

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

Lib/sqlite3/test/types.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# misrepresented as being the original software.
2222
# 3. This notice may not be removed or altered from any source distribution.
2323

24-
import datetime
24+
import bz2, datetime
2525
import unittest
2626
import sqlite3 as sqlite
2727

@@ -273,6 +273,23 @@ def CheckCasterIsUsed(self):
273273
val = self.cur.fetchone()[0]
274274
self.failUnlessEqual(type(val), float)
275275

276+
class BinaryConverterTests(unittest.TestCase):
277+
def convert(s):
278+
return bz2.decompress(s)
279+
convert = staticmethod(convert)
280+
281+
def setUp(self):
282+
self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES)
283+
sqlite.register_converter("bin", BinaryConverterTests.convert)
284+
285+
def tearDown(self):
286+
self.con.close()
287+
288+
def CheckBinaryInputForConverter(self):
289+
testdata = "abcdefg" * 10
290+
result = self.con.execute('select ? as "x [bin]"', (buffer(bz2.compress(testdata)),)).fetchone()[0]
291+
self.failUnlessEqual(testdata, result)
292+
276293
class DateTimeTests(unittest.TestCase):
277294
def setUp(self):
278295
self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
@@ -322,8 +339,9 @@ def suite():
322339
decltypes_type_suite = unittest.makeSuite(DeclTypesTests, "Check")
323340
colnames_type_suite = unittest.makeSuite(ColNamesTests, "Check")
324341
adaptation_suite = unittest.makeSuite(ObjectAdaptationTests, "Check")
342+
bin_suite = unittest.makeSuite(BinaryConverterTests, "Check")
325343
date_suite = unittest.makeSuite(DateTimeTests, "Check")
326-
return unittest.TestSuite((sqlite_type_suite, decltypes_type_suite, colnames_type_suite, adaptation_suite, date_suite))
344+
return unittest.TestSuite((sqlite_type_suite, decltypes_type_suite, colnames_type_suite, adaptation_suite, bin_suite, date_suite))
327345

328346
def test():
329347
runner = unittest.TextTestRunner()

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Library
4545
- A bug was fixed in logging.config.fileConfig() which caused a crash on
4646
shutdown when fileConfig() was called multiple times.
4747

48+
- The sqlite3 module did cut off data from the SQLite database at the first
49+
null character before sending it to a custom converter. This has been fixed
50+
now.
51+
4852
Extension Modules
4953
-----------------
5054

Modules/_sqlite/cursor.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,13 @@ PyObject* _fetch_one_row(Cursor* self)
321321
}
322322

323323
if (converter != Py_None) {
324-
val_str = (const char*)sqlite3_column_text(self->statement->st, i);
324+
nbytes = sqlite3_column_bytes(self->statement->st, i);
325+
val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
325326
if (!val_str) {
326327
Py_INCREF(Py_None);
327328
converted = Py_None;
328329
} else {
329-
item = PyString_FromString(val_str);
330+
item = PyString_FromStringAndSize(val_str, nbytes);
330331
if (!item) {
331332
return NULL;
332333
}

Modules/_sqlite/module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define PYSQLITE_MODULE_H
2626
#include "Python.h"
2727

28-
#define PYSQLITE_VERSION "2.3.1"
28+
#define PYSQLITE_VERSION "2.3.2"
2929

3030
extern PyObject* Error;
3131
extern PyObject* Warning;

0 commit comments

Comments
 (0)