Skip to content

bpo-45126: Deprecate sqlite3 Connection and Cursor reinitialization #28234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ sqlite3
(Contributed by Aviv Palivoda, Daniel Shahaf, and Erlend E. Aasland in
:issue:`16379`.)

* :class:`sqlite3.Connection` and :class:`sqlite3.Cursor` reinitialization is
now deprecated. Reinitialization will be disallowed in Python 3.13.
(Contributed by Erlend E. Aasland in :issue:`45126`.)


Removed
=======
Expand Down
14 changes: 14 additions & 0 deletions Lib/sqlite3/test/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ def test_drop_unused_refs(self):
cu = self.cx.execute(f"select {n}")
self.assertEqual(cu.fetchone()[0], n)

def test_connection_reinit(self):
db = ":memory:"
cx = sqlite.connect(db)
with self.assertWarns(DeprecationWarning) as cm:
cx.__init__(db)
self.assertIn("dbapi.py", cm.filename)


class UninitialisedConnectionTests(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -728,6 +735,13 @@ def test_same_query_in_multiple_cursors(self):
for cu in cursors:
self.assertEqual(cu.fetchall(), [(1,)])

def test_cursor_reinit(self):
db = ":memory:"
cu = self.cx.cursor()
with self.assertWarns(DeprecationWarning) as cm:
cu.__init__(self.cx)
self.assertIn("dbapi.py", cm.filename)


class ThreadTests(unittest.TestCase):
def setUp(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:class:`sqlite3.Connection` and :class:`sqlite3.Cursor` reinitialization is
now deprecated. Reinitialization will be disallowed in Python 3.13.
9 changes: 9 additions & 0 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
return -1;
}

if (self->initialized) {
const char *msg = "Connection reinitialization is depreacted and will "
"be disallowed in Python 3.13";
const int stacklevel = 1;
if (PyErr_WarnEx(PyExc_DeprecationWarning, msg, stacklevel) < 0) {
return -1;
}
}

pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self));
self->state = state;

Expand Down
9 changes: 9 additions & 0 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self,
pysqlite_Connection *connection)
/*[clinic end generated code: output=ac59dce49a809ca8 input=23d4265b534989fb]*/
{
if (self->initialized) {
const char *msg = "Cursor reinitialization is depreacted and will be "
"disallowed in Python 3.13";
const int stacklevel = 1;
if (PyErr_WarnEx(PyExc_DeprecationWarning, msg, stacklevel) < 0) {
return -1;
}
}

Py_INCREF(connection);
Py_XSETREF(self->connection, connection);
Py_CLEAR(self->statement);
Expand Down