From 9de32b5caa73bea65ff8e5d47dcfe0c3aab2657d Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 8 Sep 2021 12:34:21 +0200 Subject: [PATCH 1/3] bpo-45126: Deprecate sqlite3 Connection and Cursor reinitialization --- Lib/sqlite3/test/dbapi.py | 14 ++++++++++++++ Modules/_sqlite/connection.c | 9 +++++++++ Modules/_sqlite/cursor.c | 9 +++++++++ 3 files changed, 32 insertions(+) diff --git a/Lib/sqlite3/test/dbapi.py b/Lib/sqlite3/test/dbapi.py index 89f773daf24a16..2f316f4691fc2d 100644 --- a/Lib/sqlite3/test/dbapi.py +++ b/Lib/sqlite3/test/dbapi.py @@ -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): @@ -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): diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index bf803370c05719..100a6355a25cdf 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -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 removed 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; diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 06ce385cca3a4b..c011947d8ff1c8 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -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 " + "removed 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); From 6bb98d7666223dc59761844fb091c2aefc3c5399 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 8 Sep 2021 12:38:31 +0200 Subject: [PATCH 2/3] Add NEWS and What's New entries --- Doc/whatsnew/3.11.rst | 4 ++++ .../next/Library/2021-09-08-12-36-38.bpo-45126.6N8Wzi.rst | 2 ++ 2 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-09-08-12-36-38.bpo-45126.6N8Wzi.rst diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 60ef953d2ec165..d3e1a9e3612a74 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -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, scheduled for removal in Python 3.13. + (Contributed by Erlend E. Aasland in :issue:`45126`.) + Removed ======= diff --git a/Misc/NEWS.d/next/Library/2021-09-08-12-36-38.bpo-45126.6N8Wzi.rst b/Misc/NEWS.d/next/Library/2021-09-08-12-36-38.bpo-45126.6N8Wzi.rst new file mode 100644 index 00000000000000..e54c363ebc3925 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-09-08-12-36-38.bpo-45126.6N8Wzi.rst @@ -0,0 +1,2 @@ +:class:`sqlite3.Connection` and :class:`sqlite3.Cursor` reinitialization is +now deprecated, scheduled for removal in Python 3.13. From 4801cae6c339aa8c1a615f748c24866bb17136d9 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 8 Sep 2021 14:04:20 +0200 Subject: [PATCH 3/3] Adjust wording --- Doc/whatsnew/3.11.rst | 2 +- .../next/Library/2021-09-08-12-36-38.bpo-45126.6N8Wzi.rst | 2 +- Modules/_sqlite/connection.c | 2 +- Modules/_sqlite/cursor.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index d3e1a9e3612a74..f7886f6322b943 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -233,7 +233,7 @@ sqlite3 :issue:`16379`.) * :class:`sqlite3.Connection` and :class:`sqlite3.Cursor` reinitialization is - now deprecated, scheduled for removal in Python 3.13. + now deprecated. Reinitialization will be disallowed in Python 3.13. (Contributed by Erlend E. Aasland in :issue:`45126`.) diff --git a/Misc/NEWS.d/next/Library/2021-09-08-12-36-38.bpo-45126.6N8Wzi.rst b/Misc/NEWS.d/next/Library/2021-09-08-12-36-38.bpo-45126.6N8Wzi.rst index e54c363ebc3925..2f07e37ed0557c 100644 --- a/Misc/NEWS.d/next/Library/2021-09-08-12-36-38.bpo-45126.6N8Wzi.rst +++ b/Misc/NEWS.d/next/Library/2021-09-08-12-36-38.bpo-45126.6N8Wzi.rst @@ -1,2 +1,2 @@ :class:`sqlite3.Connection` and :class:`sqlite3.Cursor` reinitialization is -now deprecated, scheduled for removal in Python 3.13. +now deprecated. Reinitialization will be disallowed in Python 3.13. diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 100a6355a25cdf..12294a254e1a90 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -110,7 +110,7 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, if (self->initialized) { const char *msg = "Connection reinitialization is depreacted and will " - "be removed in Python 3.13"; + "be disallowed in Python 3.13"; const int stacklevel = 1; if (PyErr_WarnEx(PyExc_DeprecationWarning, msg, stacklevel) < 0) { return -1; diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index c011947d8ff1c8..ed75b9c8cb5043 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -52,7 +52,7 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self, { if (self->initialized) { const char *msg = "Cursor reinitialization is depreacted and will be " - "removed in Python 3.13"; + "disallowed in Python 3.13"; const int stacklevel = 1; if (PyErr_WarnEx(PyExc_DeprecationWarning, msg, stacklevel) < 0) { return -1;