Skip to content

Commit 006fd86

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43853: Handle sqlite3_value_text() errors (GH-25422)
1 parent 8363ac8 commit 006fd86

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

Lib/sqlite3/test/userfunctions.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,11 @@ def test_func_exception(self):
236236

237237
def test_param_string(self):
238238
cur = self.con.cursor()
239-
cur.execute("select isstring(?)", ("foo",))
240-
val = cur.fetchone()[0]
241-
self.assertEqual(val, 1)
239+
for text in ["foo", str()]:
240+
with self.subTest(text=text):
241+
cur.execute("select isstring(?)", (text,))
242+
val = cur.fetchone()[0]
243+
self.assertEqual(val, 1)
242244

243245
def test_param_int(self):
244246
cur = self.con.cursor()
@@ -391,9 +393,9 @@ def test_aggr_exception_in_finalize(self):
391393

392394
def test_aggr_check_param_str(self):
393395
cur = self.con.cursor()
394-
cur.execute("select checkType('str', ?)", ("foo",))
396+
cur.execute("select checkTypes('str', ?, ?)", ("foo", str()))
395397
val = cur.fetchone()[0]
396-
self.assertEqual(val, 1)
398+
self.assertEqual(val, 2)
397399

398400
def test_aggr_check_param_int(self):
399401
cur = self.con.cursor()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve :mod:`sqlite3` error handling: ``sqlite3_value_text()`` errors that
2+
set ``SQLITE_NOMEM`` now raise :exc:`MemoryError`. Patch by Erlend E.
3+
Aasland.

Modules/_sqlite/connection.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,6 @@ _pysqlite_build_py_params(sqlite3_context *context, int argc,
550550
int i;
551551
sqlite3_value* cur_value;
552552
PyObject* cur_py_value;
553-
const char* val_str;
554553

555554
args = PyTuple_New(argc);
556555
if (!args) {
@@ -566,15 +565,19 @@ _pysqlite_build_py_params(sqlite3_context *context, int argc,
566565
case SQLITE_FLOAT:
567566
cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
568567
break;
569-
case SQLITE_TEXT:
570-
val_str = (const char*)sqlite3_value_text(cur_value);
571-
cur_py_value = PyUnicode_FromString(val_str);
572-
/* TODO: have a way to show errors here */
573-
if (!cur_py_value) {
574-
PyErr_Clear();
575-
cur_py_value = Py_NewRef(Py_None);
568+
case SQLITE_TEXT: {
569+
sqlite3 *db = sqlite3_context_db_handle(context);
570+
const char *text = (const char *)sqlite3_value_text(cur_value);
571+
572+
if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
573+
PyErr_NoMemory();
574+
goto error;
576575
}
576+
577+
Py_ssize_t size = sqlite3_value_bytes(cur_value);
578+
cur_py_value = PyUnicode_FromStringAndSize(text, size);
577579
break;
580+
}
578581
case SQLITE_BLOB: {
579582
sqlite3 *db = sqlite3_context_db_handle(context);
580583
const void *blob = sqlite3_value_blob(cur_value);

0 commit comments

Comments
 (0)