Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
expand test suite and check for PyFloat_AsDouble errors
  • Loading branch information
Erlend E. Aasland committed Aug 7, 2021
commit 13f24137639baef3c02a377987133606ba27b029
34 changes: 24 additions & 10 deletions Lib/sqlite3/test/userfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ def func_memoryerror():
def func_overflowerror():
raise OverflowError

def func_isblob(v):
return isinstance(v, (bytes, memoryview))

def func(*args):
return len(args)

class AggrNoStep:
def __init__(self):
pass
Expand Down Expand Up @@ -188,8 +182,10 @@ def setUp(self):
self.con.create_function("memoryerror", 0, func_memoryerror)
self.con.create_function("overflowerror", 0, func_overflowerror)

self.con.create_function("isblob", 1, func_isblob)
self.con.create_function("spam", -1, func)
self.con.create_function("isblob", 1,
lambda x: isinstance(x, (bytes, memoryview)))
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
self.con.create_function("spam", -1, lambda *x: len(x))
self.con.create_function("boomerang", 1, lambda x: x)
self.con.execute("create table test(t text)")

def tearDown(self):
Expand Down Expand Up @@ -298,15 +294,33 @@ def test_empty_blob(self):
cur = self.con.execute("select isblob(x'')")
self.assertTrue(cur.fetchone()[0])

def test_nan_float(self):
cur = self.con.execute("select boomerang(?)", (float('nan'),))
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
# SQLite has no concept of nan; it is converted to NULL
self.assertIsNone(cur.fetchone()[0])

def test_too_large_int(self):
err = "Python int too large to convert to SQLite INTEGER"
self.assertRaisesRegex(OverflowError, err, self.con.execute,
"select boomerang(?)", (1 << 65,))
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated

def test_non_contiguous_blob(self):
err = "could not convert BLOB to buffer"
self.assertRaisesRegex(ValueError, err, self.con.execute,
"select boomerang(?)",
(memoryview(b"blob")[::2],))

def test_func_params(self):
self.con.create_function("boomerang", 1, lambda x: x)
dataset = (
(42, int),
(1<<42, int), # long long
(-1, int),
(1234567890123456789, int),
(3.14, float),
Comment thread
erlend-aasland marked this conversation as resolved.
(float('inf'), float),
("text", str),
Comment thread
erlend-aasland marked this conversation as resolved.
("1\x002", str),
(b"blob", bytes),
Comment thread
erlend-aasland marked this conversation as resolved.
(bytearray(range(2)), bytes),
Comment thread
erlend-aasland marked this conversation as resolved.
(None, type(None)),
)
for val, tp in dataset:
Expand Down
6 changes: 5 additions & 1 deletion Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,11 @@ _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
return -1;
sqlite3_result_int64(context, value);
} else if (PyFloat_Check(py_val)) {
sqlite3_result_double(context, PyFloat_AsDouble(py_val));
double value = PyFloat_AsDouble(py_val);
Comment thread
erlend-aasland marked this conversation as resolved.
if (value == -1 && PyErr_Occurred()) {
return -1;
}
sqlite3_result_double(context, value);
} else if (PyUnicode_Check(py_val)) {
Py_ssize_t sz;
const char *str = PyUnicode_AsUTF8AndSize(py_val, &sz);
Expand Down
11 changes: 9 additions & 2 deletions Modules/_sqlite/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,16 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec
rc = sqlite3_bind_int64(self->st, pos, value);
break;
}
case TYPE_FLOAT:
rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
case TYPE_FLOAT: {
double value = PyFloat_AsDouble(parameter);
if (value == -1 && PyErr_Occurred()) {
rc = -1;
}
else {
rc = sqlite3_bind_double(self->st, pos, value);
}
break;
}
case TYPE_UNICODE:
string = PyUnicode_AsUTF8AndSize(parameter, &buflen);
if (string == NULL)
Expand Down