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
Improve argument spec
- Use 'category' for limit category
- Use 'limit' for the new limit
  • Loading branch information
Erlend E. Aasland committed Oct 8, 2021
commit 5e2ae979257f6d4324fb4b39a80a909e154a1205
10 changes: 5 additions & 5 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,9 @@ Connection Objects
.. versionadded:: 3.7


.. method:: getlimit(limit, /)
.. method:: getlimit(category, /)

Get a connection run-time limit. *limit* is the limit category to be
Get a connection run-time limit. *category* is the limit category to be
queried.

Example, query the maximum length of an SQL statement::
Expand All @@ -639,10 +639,10 @@ Connection Objects
.. versionadded:: 3.11


.. method:: setlimit(limit, value, /)
.. method:: setlimit(category, limit, /)

Set a connection run-time limit. *limit* is the limit category to be set.
*value* is the new limit. If the new limit is a negative number, the
Set a connection run-time limit. *category* is the limit category to be
set. *limit* is the new limit. If the new limit is a negative number, the
limit is unchanged.

Attempts to increase a limit above its hard upper bound are silently
Expand Down
18 changes: 9 additions & 9 deletions Lib/sqlite3/test/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,22 +347,22 @@ def test_drop_unused_refs(self):
self.assertEqual(cu.fetchone()[0], n)

def test_connection_limits(self):
param = sqlite.SQLITE_LIMIT_SQL_LENGTH
setval = 10
ret1 = self.cx.getlimit(param)
category = sqlite.SQLITE_LIMIT_SQL_LENGTH
saved_limit = self.cx.getlimit(category)
try:
ret2 = self.cx.setlimit(param, setval)
self.assertEqual(ret1, ret2)
self.assertEqual(self.cx.getlimit(param), setval)
new_limit = 10
prev_limit = self.cx.setlimit(category, new_limit)
self.assertEqual(saved_limit, prev_limit)
self.assertEqual(self.cx.getlimit(category), new_limit)
msg = "string or blob too big"
self.assertRaisesRegex(sqlite.DataError, msg,
self.cx.execute, "select 1 as '16'")
finally: # restore old limit
self.cx.setlimit(param, ret1)
finally: # restore saved limit
self.cx.setlimit(category, saved_limit)

def test_connection_set_bad_limit(self):
self.assertRaisesRegex(
sqlite.ProgrammingError, "'limit' is out of bounds",
sqlite.ProgrammingError, "'category' is out of bounds",
self.cx.setlimit, 1111, 0
)

Expand Down
36 changes: 18 additions & 18 deletions Modules/_sqlite/clinic/connection.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -813,14 +813,14 @@ pysqlite_connection_exit(pysqlite_Connection *self, PyObject *const *args, Py_ss
}

PyDoc_STRVAR(setlimit__doc__,
"setlimit($self, limit, value, /)\n"
"setlimit($self, category, limit, /)\n"
"--\n"
"\n"
"Set connection run-time limits. Non-standard.\n"
"\n"
" limit\n"
" category\n"
" The limit category to be set.\n"
" value\n"
" limit\n"
" The new limit. If the new limit is a negative number, the limit is\n"
" unchanged.\n"
"\n"
Expand All @@ -832,58 +832,58 @@ PyDoc_STRVAR(setlimit__doc__,
{"setlimit", (PyCFunction)(void(*)(void))setlimit, METH_FASTCALL, setlimit__doc__},

static PyObject *
setlimit_impl(pysqlite_Connection *self, int limit, int value);
setlimit_impl(pysqlite_Connection *self, int category, int limit);

static PyObject *
setlimit(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
int category;
int limit;
int value;

if (!_PyArg_CheckPositional("setlimit", nargs, 2, 2)) {
goto exit;
}
limit = _PyLong_AsInt(args[0]);
if (limit == -1 && PyErr_Occurred()) {
category = _PyLong_AsInt(args[0]);
if (category == -1 && PyErr_Occurred()) {
goto exit;
}
value = _PyLong_AsInt(args[1]);
if (value == -1 && PyErr_Occurred()) {
limit = _PyLong_AsInt(args[1]);
if (limit == -1 && PyErr_Occurred()) {
goto exit;
}
return_value = setlimit_impl(self, limit, value);
return_value = setlimit_impl(self, category, limit);

exit:
return return_value;
}

PyDoc_STRVAR(getlimit__doc__,
"getlimit($self, limit, /)\n"
"getlimit($self, category, /)\n"
"--\n"
"\n"
"Get connection run-time limits. Non-standard.\n"
"\n"
" limit\n"
" category\n"
" The limit category to be queried.");

#define GETLIMIT_METHODDEF \
{"getlimit", (PyCFunction)getlimit, METH_O, getlimit__doc__},

static PyObject *
getlimit_impl(pysqlite_Connection *self, int limit);
getlimit_impl(pysqlite_Connection *self, int category);

static PyObject *
getlimit(pysqlite_Connection *self, PyObject *arg)
{
PyObject *return_value = NULL;
int limit;
int category;

limit = _PyLong_AsInt(arg);
if (limit == -1 && PyErr_Occurred()) {
category = _PyLong_AsInt(arg);
if (category == -1 && PyErr_Occurred()) {
goto exit;
}
return_value = getlimit_impl(self, limit);
return_value = getlimit_impl(self, category);

exit:
return return_value;
Expand All @@ -896,4 +896,4 @@ getlimit(pysqlite_Connection *self, PyObject *arg)
#ifndef PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
#define PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
#endif /* !defined(PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF) */
/*[clinic end generated code: output=85f8b7247801658e input=a9049054013a1b77]*/
/*[clinic end generated code: output=ef576e9b40005272 input=a9049054013a1b77]*/
24 changes: 12 additions & 12 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1869,9 +1869,9 @@ pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
/*[clinic input]
_sqlite3.Connection.setlimit as setlimit

limit: int
category: int
The limit category to be set.
value: int
limit: int
The new limit. If the new limit is a negative number, the limit is
Comment thread
erlend-aasland marked this conversation as resolved.
unchanged.
/
Expand All @@ -1884,36 +1884,36 @@ the prior value of the limit is returned.
[clinic start generated code]*/

static PyObject *
setlimit_impl(pysqlite_Connection *self, int limit, int value)
/*[clinic end generated code: output=18d15e4be8a7a4ec input=e0990e0c90e747ee]*/
setlimit_impl(pysqlite_Connection *self, int category, int limit)
/*[clinic end generated code: output=0d208213f8d68ccd input=9d3a8e83f4cc32d7]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}

int previous_value = sqlite3_limit(self->db, limit, value);
if (previous_value < 0) {
PyErr_SetString(self->ProgrammingError, "'limit' is out of bounds");
int old_limit = sqlite3_limit(self->db, category, limit);
if (old_limit < 0) {
PyErr_SetString(self->ProgrammingError, "'category' is out of bounds");
return NULL;
}
return PyLong_FromLong(previous_value);
return PyLong_FromLong(old_limit);
}

/*[clinic input]
_sqlite3.Connection.getlimit as getlimit

limit: int
category: int
The limit category to be queried.
/

Get connection run-time limits. Non-standard.
[clinic start generated code]*/

static PyObject *
getlimit_impl(pysqlite_Connection *self, int limit)
/*[clinic end generated code: output=f90c9c8399170536 input=c339eaae3afe367a]*/
getlimit_impl(pysqlite_Connection *self, int category)
/*[clinic end generated code: output=7c3f5d11f24cecb1 input=788e5fb4e2d2b618]*/
{
return setlimit_impl(self, limit, -1);
return setlimit_impl(self, category, -1);
}


Expand Down