From a9a75bdfa9380b1d201fe4e0896c19079e78e54b Mon Sep 17 00:00:00 2001 From: csm10495 <5749838+csm10495@users.noreply.github.com> Date: Thu, 3 Dec 2026 06:44:08 +0000 Subject: [PATCH 1/5] gh-163106: Add sqlite3.Connection.changes Expose sqlite3_changes64() (falling back to sqlite3_changes() on SQLite older than 3.37) as a read-only Connection.changes attribute, returning the number of rows modified, inserted, or deleted by the most recently completed INSERT, UPDATE, or DELETE statement. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- Doc/library/sqlite3.rst | 13 +++++++++++++ Doc/whatsnew/3.16.rst | 5 +++++ Lib/test/test_sqlite3/test_dbapi.py | 14 ++++++++++++++ ...26-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst | 3 +++ Modules/_sqlite/connection.c | 17 +++++++++++++++++ 5 files changed, 52 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 787660c893e4e0..bee83ac79f1c3e 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -1463,6 +1463,19 @@ Connection objects Return the total number of database rows that have been modified, inserted, or deleted since the database connection was opened. + .. attribute:: changes + + Return the number of database rows that were modified, inserted, or + deleted by the most recently completed ``INSERT``, ``UPDATE``, or + ``DELETE`` statement on this connection. + Statements that do not modify rows, such as ``SELECT``, + leave the value unchanged. + See the `SQLite changes() documentation`_ for details. + + .. versionadded:: next + + .. _SQLite changes() documentation: https://www.sqlite.org/c3ref/changes.html + .. _sqlite3-cursor-objects: diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index 7a15ae5bcd2dbf..87e2f99967a744 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -808,6 +808,11 @@ sqlite3 :const:`~sqlite3.sqlite_version_info` is now a named tuple. (Contributed by Serhiy Storchaka in :gh:`157470`.) +* Add the :attr:`sqlite3.Connection.changes` attribute, which returns the + number of rows modified, inserted, or deleted by the most recently completed + ``INSERT``, ``UPDATE``, or ``DELETE`` statement. + (Contributed by Charles Machalow in :gh:`163106`.) + ssl --- diff --git a/Lib/test/test_sqlite3/test_dbapi.py b/Lib/test/test_sqlite3/test_dbapi.py index bbb7c1f8b61593..9703984120b3c9 100644 --- a/Lib/test/test_sqlite3/test_dbapi.py +++ b/Lib/test/test_sqlite3/test_dbapi.py @@ -609,6 +609,7 @@ def test_uninit_operations(self): funcs = ( lambda: self.cx.isolation_level, lambda: self.cx.total_changes, + lambda: self.cx.changes, lambda: self.cx.in_transaction, lambda: self.cx.iterdump(), lambda: self.cx.cursor(), @@ -1022,6 +1023,19 @@ def test_total_changes(self): self.cu.execute("insert into test(name) values ('foo')") self.assertLess(2, self.cx.total_changes, msg='total changes reported wrong value') + def test_changes(self): + self.cu.execute("delete from test") + self.cu.executemany("insert into test(name) values (?)", + [("a",), ("b",), ("c",)]) + self.assertEqual(self.cx.changes, 1) + self.cu.execute("update test set name = 'x' where name in ('a', 'b')") + self.assertEqual(self.cx.changes, 2) + # SELECT statements do not reset the count. + self.cu.execute("select * from test").fetchall() + self.assertEqual(self.cx.changes, 2) + self.cu.execute("delete from test") + self.assertEqual(self.cx.changes, 3) + # Checks for executemany: # Sequences are required by the DB-API, iterators # enhancements in pysqlite. diff --git a/Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst b/Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst new file mode 100644 index 00000000000000..07a766107fb9bd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst @@ -0,0 +1,3 @@ +Add :attr:`sqlite3.Connection.changes`, which returns the number of rows +modified, inserted, or deleted by the most recently completed statement. +Patch by Charles Machalow. diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index f8bd3a8df35b66..ae7f7d84bd2154 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -1815,6 +1815,20 @@ pysqlite_connection_get_total_changes(PyObject *op, void *Py_UNUSED(closure)) return PyLong_FromLong(sqlite3_total_changes(self->db)); } +static PyObject * +pysqlite_connection_get_changes(PyObject *op, void *Py_UNUSED(closure)) +{ + pysqlite_Connection *self = _pysqlite_Connection_CAST(op); + if (!pysqlite_check_connection(self)) { + return NULL; + } +#if SQLITE_VERSION_NUMBER >= 3037000 + return PyLong_FromLongLong(sqlite3_changes64(self->db)); +#else + return PyLong_FromLong(sqlite3_changes(self->db)); +#endif +} + static PyObject * pysqlite_connection_get_in_transaction(PyObject *op, void *Py_UNUSED(closure)) { @@ -2675,6 +2689,9 @@ static PyGetSetDef connection_getset[] = { {"isolation_level", pysqlite_connection_get_isolation_level, pysqlite_connection_set_isolation_level}, {"total_changes", pysqlite_connection_get_total_changes, NULL}, + {"changes", pysqlite_connection_get_changes, NULL, + PyDoc_STR("Number of rows modified, inserted, or deleted by the most " + "recently completed INSERT, UPDATE, or DELETE statement.")}, {"in_transaction", pysqlite_connection_get_in_transaction, NULL}, {"autocommit", get_autocommit, set_autocommit}, {"__text_signature__", get_sig, NULL}, From 1cc5ae7d2e79ce0ed7df6ec42e23d608fff50937 Mon Sep 17 00:00:00 2001 From: Charles Machalow Date: Thu, 3 Dec 2026 13:07:25 -0800 Subject: [PATCH 2/5] Wording fix --- Doc/library/sqlite3.rst | 5 +++-- .../Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index bee83ac79f1c3e..db88889082d840 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -1468,8 +1468,9 @@ Connection objects Return the number of database rows that were modified, inserted, or deleted by the most recently completed ``INSERT``, ``UPDATE``, or ``DELETE`` statement on this connection. - Statements that do not modify rows, such as ``SELECT``, - leave the value unchanged. + Statements other than ``INSERT``, ``UPDATE``, or ``DELETE``, + such as ``SELECT``, leave the value unchanged. + An ``UPDATE`` or ``DELETE`` that matches no rows sets the value to zero. See the `SQLite changes() documentation`_ for details. .. versionadded:: next diff --git a/Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst b/Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst index 07a766107fb9bd..83418f24c5555d 100644 --- a/Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst +++ b/Misc/NEWS.d/next/Library/2026-12-03-06-41-07.gh-issue-163106.Xk3pQz.rst @@ -1,3 +1,4 @@ Add :attr:`sqlite3.Connection.changes`, which returns the number of rows -modified, inserted, or deleted by the most recently completed statement. +modified, inserted, or deleted by the most recently completed ``INSERT``, +``UPDATE``, or ``DELETE`` statement. Patch by Charles Machalow. From 4f08560b71350485d29544961a560781a515cb13 Mon Sep 17 00:00:00 2001 From: Charles Machalow Date: Thu, 3 Dec 2026 13:14:18 -0800 Subject: [PATCH 3/5] Fix Mac build via __builtin_available check matching sqlite3.h's SQLITE_AVAILABLE check --- Modules/_sqlite/connection.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index ae7f7d84bd2154..e352acfa72ea40 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -45,6 +45,16 @@ #define HAVE_WINDOW_FUNCTIONS #endif +#if defined(__APPLE__) && defined(__has_builtin) +# if __has_builtin(__builtin_available) +# define HAVE_SQLITE3_CHANGES64_RUNTIME \ + __builtin_available(macOS 12.3, iOS 15.4, tvOS 15.4, watchOS 8.5, *) +# endif +#endif +#ifndef HAVE_SQLITE3_CHANGES64_RUNTIME +# define HAVE_SQLITE3_CHANGES64_RUNTIME 1 +#endif + static const char * get_isolation_level(const char *level) { @@ -1823,10 +1833,11 @@ pysqlite_connection_get_changes(PyObject *op, void *Py_UNUSED(closure)) return NULL; } #if SQLITE_VERSION_NUMBER >= 3037000 - return PyLong_FromLongLong(sqlite3_changes64(self->db)); -#else - return PyLong_FromLong(sqlite3_changes(self->db)); + if (HAVE_SQLITE3_CHANGES64_RUNTIME) { + return PyLong_FromLongLong(sqlite3_changes64(self->db)); + } #endif + return PyLong_FromLong(sqlite3_changes(self->db)); } static PyObject * From 11e6c8557c7a76f8c4b314b4c6e394c1c5dd5fb1 Mon Sep 17 00:00:00 2001 From: Charles Machalow Date: Thu, 3 Dec 2026 19:49:34 -0800 Subject: [PATCH 4/5] Try swapping to configure-based --- Modules/_sqlite/connection.c | 19 ++++----------- PC/pyconfig.h | 3 +++ configure | 45 ++++++++++++++++++++++++++++++++++++ configure.ac | 15 ++++++++++++ pyconfig.h.in | 3 +++ 5 files changed, 70 insertions(+), 15 deletions(-) diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index e352acfa72ea40..39edd68fa70a88 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -45,16 +45,6 @@ #define HAVE_WINDOW_FUNCTIONS #endif -#if defined(__APPLE__) && defined(__has_builtin) -# if __has_builtin(__builtin_available) -# define HAVE_SQLITE3_CHANGES64_RUNTIME \ - __builtin_available(macOS 12.3, iOS 15.4, tvOS 15.4, watchOS 8.5, *) -# endif -#endif -#ifndef HAVE_SQLITE3_CHANGES64_RUNTIME -# define HAVE_SQLITE3_CHANGES64_RUNTIME 1 -#endif - static const char * get_isolation_level(const char *level) { @@ -1832,12 +1822,11 @@ pysqlite_connection_get_changes(PyObject *op, void *Py_UNUSED(closure)) if (!pysqlite_check_connection(self)) { return NULL; } -#if SQLITE_VERSION_NUMBER >= 3037000 - if (HAVE_SQLITE3_CHANGES64_RUNTIME) { - return PyLong_FromLongLong(sqlite3_changes64(self->db)); - } -#endif +#ifdef HAVE_SQLITE3_CHANGES64 + return PyLong_FromLongLong(sqlite3_changes64(self->db)); +#else return PyLong_FromLong(sqlite3_changes(self->db)); +#endif } static PyObject * diff --git a/PC/pyconfig.h b/PC/pyconfig.h index 9de3786a0c769b..5603cd32c7b6d1 100644 --- a/PC/pyconfig.h +++ b/PC/pyconfig.h @@ -730,6 +730,9 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */ #define HAVE_SETSOCKOPT 1 #define HAVE_SOCKET 1 +/* Define to 1 if you have a usable 'sqlite3_changes64' function. */ +#define HAVE_SQLITE3_CHANGES64 1 + /* Define to 1 if you have the `dup' function. */ #define HAVE_DUP 1 diff --git a/configure b/configure index d8d860b84a370a..17160c470cac20 100755 --- a/configure +++ b/configure @@ -18354,6 +18354,51 @@ printf "%s\n" "#define PY_SQLITE_HAVE_SERIALIZE 1" >>confdefs.h fi + CFLAGS="$CFLAGS $CFLAGS_NODIST" + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for usable sqlite3_changes64" >&5 +printf %s "checking for usable sqlite3_changes64... " >&6; } +if test ${ac_cv_lib_sqlite3_sqlite3_changes64+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + + #include +int +main (void) +{ + + (void)sqlite3_changes64(0); + + ; + return 0; +} + +_ACEOF +if ac_fn_c_try_link "$LINENO" +then : + ac_cv_lib_sqlite3_sqlite3_changes64=yes +else case e in #( + e) ac_cv_lib_sqlite3_sqlite3_changes64=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam \ + conftest$ac_exeext conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_sqlite3_sqlite3_changes64" >&5 +printf "%s\n" "$ac_cv_lib_sqlite3_sqlite3_changes64" >&6; } + if test "x$ac_cv_lib_sqlite3_sqlite3_changes64" = xyes +then : + + +printf "%s\n" "#define HAVE_SQLITE3_CHANGES64 1" >>confdefs.h + + +fi else case e in #( e) diff --git a/configure.ac b/configure.ac index 11fd97ac5b765f..a426314d09c56f 100644 --- a/configure.ac +++ b/configure.ac @@ -4836,6 +4836,21 @@ dnl hence CPPFLAGS instead of CFLAGS. [Define if SQLite was compiled with the serialize API] ) ]) + dnl Reject APIs unavailable on the deployment target, even if they link. + CFLAGS="$CFLAGS $CFLAGS_NODIST" + AC_CACHE_CHECK([for usable sqlite3_changes64], + [ac_cv_lib_sqlite3_sqlite3_changes64], [ + AC_LINK_IFELSE([ + AC_LANG_PROGRAM([#include ], [ + (void)sqlite3_changes64(0); + ]) + ], [ac_cv_lib_sqlite3_sqlite3_changes64=yes], + [ac_cv_lib_sqlite3_sqlite3_changes64=no]) + ]) + AS_VAR_IF([ac_cv_lib_sqlite3_sqlite3_changes64], [yes], [ + AC_DEFINE([HAVE_SQLITE3_CHANGES64], [1], + [Define to 1 if you have a usable 'sqlite3_changes64' function.]) + ]) ], [ have_supported_sqlite3=no ]) diff --git a/pyconfig.h.in b/pyconfig.h.in index b0ac8a07019093..d39de31be63173 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -1266,6 +1266,9 @@ /* Define to 1 if you have the 'splice' function. */ #undef HAVE_SPLICE +/* Define to 1 if you have a usable 'sqlite3_changes64' function. */ +#undef HAVE_SQLITE3_CHANGES64 + /* Define to 1 if the system has the type 'ssize_t'. */ #undef HAVE_SSIZE_T From 68de5e29aaf4e8a15f81c2c370bc7a64f5a98f00 Mon Sep 17 00:00:00 2001 From: Charles Machalow Date: Thu, 3 Dec 2026 19:58:33 -0800 Subject: [PATCH 5/5] Swap to _PY naming to make this a private macro that follows the PY naming convention --- Modules/_sqlite/connection.c | 2 +- PC/pyconfig.h | 2 +- configure | 3 ++- configure.ac | 3 ++- pyconfig.h.in | 6 +++--- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 39edd68fa70a88..494c81b05f328e 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -1822,7 +1822,7 @@ pysqlite_connection_get_changes(PyObject *op, void *Py_UNUSED(closure)) if (!pysqlite_check_connection(self)) { return NULL; } -#ifdef HAVE_SQLITE3_CHANGES64 +#ifdef _PY_SQLITE_HAVE_CHANGES64 return PyLong_FromLongLong(sqlite3_changes64(self->db)); #else return PyLong_FromLong(sqlite3_changes(self->db)); diff --git a/PC/pyconfig.h b/PC/pyconfig.h index 5603cd32c7b6d1..86dae4734e77c3 100644 --- a/PC/pyconfig.h +++ b/PC/pyconfig.h @@ -731,7 +731,7 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */ #define HAVE_SOCKET 1 /* Define to 1 if you have a usable 'sqlite3_changes64' function. */ -#define HAVE_SQLITE3_CHANGES64 1 +#define _PY_SQLITE_HAVE_CHANGES64 1 /* Define to 1 if you have the `dup' function. */ #define HAVE_DUP 1 diff --git a/configure b/configure index 17160c470cac20..dd44f3ecaa2b43 100755 --- a/configure +++ b/configure @@ -18355,6 +18355,7 @@ printf "%s\n" "#define PY_SQLITE_HAVE_SERIALIZE 1" >>confdefs.h fi CFLAGS="$CFLAGS $CFLAGS_NODIST" + LDFLAGS="$LDFLAGS $LDFLAGS_NODIST" { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for usable sqlite3_changes64" >&5 printf %s "checking for usable sqlite3_changes64... " >&6; } if test ${ac_cv_lib_sqlite3_sqlite3_changes64+y} @@ -18395,7 +18396,7 @@ printf "%s\n" "$ac_cv_lib_sqlite3_sqlite3_changes64" >&6; } then : -printf "%s\n" "#define HAVE_SQLITE3_CHANGES64 1" >>confdefs.h +printf "%s\n" "#define _PY_SQLITE_HAVE_CHANGES64 1" >>confdefs.h fi diff --git a/configure.ac b/configure.ac index a426314d09c56f..de436d63fdf6c7 100644 --- a/configure.ac +++ b/configure.ac @@ -4838,6 +4838,7 @@ dnl hence CPPFLAGS instead of CFLAGS. ]) dnl Reject APIs unavailable on the deployment target, even if they link. CFLAGS="$CFLAGS $CFLAGS_NODIST" + LDFLAGS="$LDFLAGS $LDFLAGS_NODIST" AC_CACHE_CHECK([for usable sqlite3_changes64], [ac_cv_lib_sqlite3_sqlite3_changes64], [ AC_LINK_IFELSE([ @@ -4848,7 +4849,7 @@ dnl hence CPPFLAGS instead of CFLAGS. [ac_cv_lib_sqlite3_sqlite3_changes64=no]) ]) AS_VAR_IF([ac_cv_lib_sqlite3_sqlite3_changes64], [yes], [ - AC_DEFINE([HAVE_SQLITE3_CHANGES64], [1], + AC_DEFINE([_PY_SQLITE_HAVE_CHANGES64], [1], [Define to 1 if you have a usable 'sqlite3_changes64' function.]) ]) ], [ diff --git a/pyconfig.h.in b/pyconfig.h.in index d39de31be63173..6fda555942d8df 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -1266,9 +1266,6 @@ /* Define to 1 if you have the 'splice' function. */ #undef HAVE_SPLICE -/* Define to 1 if you have a usable 'sqlite3_changes64' function. */ -#undef HAVE_SQLITE3_CHANGES64 - /* Define to 1 if the system has the type 'ssize_t'. */ #undef HAVE_SSIZE_T @@ -2030,6 +2027,9 @@ /* Maximum length in bytes of a thread name */ #undef _PYTHREAD_NAME_MAXLEN +/* Define to 1 if you have a usable 'sqlite3_changes64' function. */ +#undef _PY_SQLITE_HAVE_CHANGES64 + /* Defined if _Complex C type can be used with libffi. */ #undef _Py_FFI_SUPPORT_C_COMPLEX