514 Commits
Author SHA1 Message Date
Jelle Zijlstra 656af533dc gh-159739: Keep sqlite3 cursor metadata and factories valid during queries (#160403)
A cursor exposed its description tuple before filling in all the columns, so an allocation failure or a reader on a free-threaded build could encounter an unset tuple entry. An allocation failure could also clear lastrowid and crash the next execute. Build new metadata before replacing the old values and synchronize reads and writes. Keep a strong reference to each row or text factory while it is called so replacing the factory during a fetch cannot destroy an active callable.
2026-10-24 13:46:15 -07:00
Jelle Zijlstra 4df47d8e60 gh-159739: Keep SQLite list parameters alive while binding (#160192)
When a SQL parameter sequence is an exact list, sqlite3 retrieved an element as a borrowed reference before taking ownership. On free-threaded builds another thread can replace it in between, so binding can use a freed object and crash. Fetch a strong reference atomically. Backport to 3.15 and 3.14.
2026-10-22 21:50:43 +00:00
cfd6cf7154 gh-157407: Use _PyUnicode_AsUTF8NoNUL for AC 's' and 'z' converters (#158210)
* gh-157407: Use _PyUnicode_AsUTF8NoNUL for AC 's' and 'z' converters

The code was doing the same as the helper added in GH-111957 but inline.
That resulted in significantly more `.text` for most functions. Refactor
the Arguemnt Clinic converters code to deduplicate a bit while keeping
generated code close to original.

On my Linux x64 box this reduces `.text` by ~6200 bytes in a clang
release build (no PGO or LTO). Performance is flat.

The helper was already exported for `_sqlite3`, update the comment that
it is used in AC generated code as well.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Update Include/internal/pycore_unicodeobject.h

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>

* Remove NEWS, add period

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
2026-10-05 17:03:27 +00:00
Serhiy StorchakaandClaude Opus 5 5d1ab456ba gh-157470: Add structured version info for sqlite3 (GH-157471)
Add constants sqlite3.SQLITE_VERSION and sqlite3.SQLITE_VERSION_INFO
which provide information about the version of the SQLite library that
was used for building the module.

Make sqlite3.sqlite_version_info a named tuple.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-29 15:48:39 +03:00
Pieter EendebakandClaude Opus 5 1c4fa4f98e gh-157407: Argument Clinic: parse positional-only defining_class methods without _PyArg_Parser (#157483)
* gh-136681: Drop _PyArg_Parser for positional-only defining-class methods

A method that takes a defining class and whose other parameters are all
positional-only (e.g. decimal.Decimal.from_float) was routed through
Argument Clinic's generic keyword path: it emitted a static _PyArg_Parser,
an (empty) keyword tuple and an _PyArg_UnpackKeywords() call, even though
it accepts no keyword arguments.

Such methods now go through parse_pos_only(): the wrapper still uses the
mandatory METH_METHOD|METH_FASTCALL|METH_KEYWORDS convention (the only one
that delivers the defining class), but it rejects keywords with
_PyArg_NoKwnames() and reads positional arguments directly, with no
parser, keyword tuple or argsbuf.

This converts 135 methods across 19 clinic headers, removing the static
_PyArg_Parser, the keyword arrays/tuples and the per-call
_PyArg_UnpackKeywords() + parser_init() overhead for each.

_PyArg_NoKwnames() is now exported (PyAPI_FUNC) so generated code in
shared extension modules can use it, like _PyArg_NoKeywords().

Error messages for these methods now match ordinary positional-only
functions ("expected ... arguments" / "takes no keyword arguments"
instead of the _PyArg_UnpackKeywords phrasing); affected stdlib tests
are updated accordingly.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* gh-157407: Rename NEWS entry to the correct issue number

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-27 21:31:22 +00:00
Serhiy Storchaka 6b5d4143f3 gh-113318: Better implementation of @getter and @setter in Argument Clinic (GH-156066)
The value of a @setter is now a parameter, which can use a converter,
and a @getter can define a return converter.  Declaring the value is
optional.

The accessors of an attribute are now collected in a Property, so the
entry of PyGetSetDef is identified by the Python name instead of the C
basename, and is composed without the help of the preprocessor unless
some accessor is compiled conditionally.
2026-09-19 18:48:39 +03:00
Victor Stinner 86c93a5b84 gh-155742: Use PyBytesWriter in _Py_strhex_impl() (#157734)
Replace soft deprecated PyBytes_FromStringAndSize() with
PyBytesWriter.

Replace also PyBytes_FromStringAndSize(NULL, 0) with
Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) in other functions.

Replace also PyBytes_FromStringAndSize(NULL, 0) with
PyBytes_FromStringAndSize("", 0) in _Py_GetConstant_Init().
2026-09-18 14:09:09 +02:00
cui fliter 65e149a15f gh-155781: Hold strong references to sqlite3 converters (#155784) 2026-08-21 20:42:29 +05:30
Stan Ulbrych 033dc43bcf gh-155952: Fix <unrepresentable> Argument Clinic defaults in several signatures (#155954) 2026-08-21 10:03:51 +01:00
Steve Stagg 8e96dd6f25 gh-152817: Prevent deletion of sqlite3 cursor.row_factory attr, missed from: gh-149738 (GH-152818) 2026-08-21 07:58:49 +00:00
Serhiy Storchaka 53760b3f8d gh-156101: Fix sqlite3 Cursor.arraysize on a failed assignment (GH-156105)
PyLong_AsUInt32() stores 0 in the target on error, so the attribute was
clobbered when the assigned value was too large.
2026-08-21 10:12:58 +03:00
Serhiy Storchaka daebcac92d gh-156100: Fix crashes in the sqlite3 Connection.autocommit setter (GH-156104)
Deleting the attribute crashed, and setting it to an integer which does not
fit in C long reported success with OverflowError set.
2026-08-21 10:04:25 +03:00
Serhiy Storchaka 915970ce9d gh-113318: Fix @getter and @setter in Argument Clinic (GH-155778)
Fix generating an accessor in a preprocessor conditional block.
Reject the accessors of the same attribute with different C basenames and
the same accessor defined twice.
Reject deletion of the attribute, which crashed the setter, unless the new
directive @deleter is applied to it.
2026-08-18 12:35:22 +00:00
Victor Stinner 079dd5ee39 gh-155742: Get PyBytes_AS_STRING() as const char* (#155751)
Use "const char*" type instead of "char*" to store
PyBytes_AS_STRING(): treat bytes objects as immutable.
2026-08-14 12:00:45 +00:00
Jiseok CHOIandSerhiy Storchaka c72ea530c3 gh-150449: Support negative steps in sqlite3.Blob slices (GH-150450)
Reading or writing a slice with a negative step computed a negative length
for sqlite3_blob_read() and sqlite3_blob_write(), so it failed instead of
returning or storing the selected bytes.

Compute the contiguous region which covers all selected bytes, and index it
with a size_t cursor, so that a step of any sign and magnitude works.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2026-08-14 09:25:38 +03:00
Serhiy Storchaka b758ff4869 gh-155702: Fix sqlite3.Blob slice assignment with a step (GH-155703)
It patched the bytes object read from the blob, which for a single byte
is an immortal singleton, so that the value of that byte was changed in
the whole process.
2026-08-14 07:38:45 +03:00
tonghuaroot (童话) b704b647b2 gh-153695: Fix sqlite3.Row.__hash__ with an unhashable value (#153696) 2026-07-14 22:23:58 +00:00
Jiseok CHOI fc9c4db130 gh-150913: Fix sqlite3.Blob validation for empty slice assignment (GH-150915)
ass_subscript_slice() returned early when the computed slice length
was zero, bypassing validation performed for non-empty slices.
2026-06-04 16:41:47 +03:00
Sepehr Rasouli 60fdb3192b gh-149738: Fix segmentation fault bug in sqllite3 (#149754)
Deleting the `row_factory` or `text_factory` attribute is no longer allowed.
2026-06-02 11:07:08 +02:00
Serhiy Storchaka 0466560b31 gh-150285: Fix too long docstrings in the sqlite3 module (GH-150290) 2026-05-24 15:02:58 +03:00
Bénédikt Tran aa6680775d gh-146090: fix memory management of internal sqlite3 callback contexts (#146569) 2026-03-29 14:21:37 +02:00
Karolina SurmaandVictor Stinner 1887a95f51 gh-128341: Use _Py_ABI_SLOT in stdlib modules (#145770)
Rename from _Py_INTERNAL_ABI_SLOT to _Py_ABI_SLOT
and define the macro using _PyABIInfo_DEFAULT.

Use the ABI slot in stdlib extension modules to enable running
a check of ABI version compatibility.

_tkinter, _tracemalloc and readline don't use the slots, hence they need
explicit handling.

Co-authored-by: Victor Stinner <vstinner@python.org>
2026-03-24 17:47:55 +00:00
A.Ibrahim 06b0920f12 gh-142787: Handle empty sqlite3 blob slices (#142824) 2026-02-26 23:40:25 +00:00
Thomas Kluyver 74c1f4145e gh-144377: Clean up sqlite3 Connection's list of weakrefs to Cursor objects (#144378) 2026-02-03 13:36:12 +01:00
Bénédikt Tran 7f6c16a956 gh-142830: prevent some crashes when mutating sqlite3 callbacks (#143245) 2026-01-01 11:55:05 +01:00
Prithviraj Chaudhuri b1558b6d3e gh-140042: Removing unsafe call to sqlite3_shutdown (GH-141690) 2025-11-20 15:35:22 +01:00
Bénédikt Tran 27acaf1cb7 gh-139327: consolidate sqlite3_finalize and sqlite3_reset usages (GH-139329) 2025-10-15 15:18:07 +02:00
Victor Stinner c4e7d245d6 gh-138342: Move _PyObject_VisitType() to the internal C API (#139734) 2025-10-08 12:10:58 +02:00
Bénédikt Tran d0a3eff9d6 gh-139327: fix some reference leaks in sqlite3 error branches (#139328) 2025-10-01 11:10:12 +02:00
Bénédikt Tran bc172ee830 gh-139283: correctly handle size limit in cursor.fetchmany() (#139296)
Passing a negative or zero size to `cursor.fetchmany()` made it fetch all rows
instead of none.

While this could be considered a security vulnerability, it was decided to treat
this issue as a regular bug as passing a non-sanitized *size* value in the first
place is not recommended.
2025-09-30 11:18:55 +02:00
Victor Stinner 243d599a05 gh-129813, PEP 782: Use PyBytesWriter in _sqlite (#138956)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public
PyBytesWriter API.
2025-09-18 14:58:35 +02:00
chiri 8eb106240f gh-138736: Fix sqlite3.Connection.blobopen 'row' parameter type and naming (#138738)
The 'row' parameter represents a ROWID. Clarify this in docs and docstrings.
2025-09-17 15:51:14 +00:00
Peter Bierma 4f6ecd10c2 gh-138342: Use a common utility for visiting an object's type (GH-138343)
Add `_PyObject_VisitType` in place of `tp_traverse` functions that only visit the object's type.
2025-09-01 16:20:33 +00:00
Adam Turner 918e3ba6c0 GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
b1056c2a44 gh-135607: remove null checking of weakref list in dealloc of extension modules and objects (#135614)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
2025-06-30 11:14:31 +00:00
Petr ViktorinandTan Long e7a3c20b92 gh-133390: Support SQL keyword completion for sqlite3 CLI (GH-133393) (GH-135292)
Co-authored-by: Tan Long <tanloong@foxmail.com>
2025-06-12 16:28:30 +02:00
Petr Viktorin 24069fbca8 Revert "gh-133390: Support SQL keyword completion for sqlite3 CLI (#133393)" temporarily (GH-135232)
This reverts commit 62b3d2d443,
which broke buildbots
2025-06-07 08:56:43 +00:00
Tan Long 62b3d2d443 gh-133390: Support SQL keyword completion for sqlite3 CLI (#133393) 2025-06-06 16:52:41 +02:00
Serhiy Storchaka dcf93c4c93 gh-133595: Clean up sqlite3.Connection APIs (GH-133605)
* All parameters of sqlite3.connect() except "database" are now keyword-only.
* The first three parameters of methods create_function() and
  create_aggregate() are now positional-only.
* The first parameter of methods set_authorizer(), set_progress_handler()
  and set_trace_callback() is now positional-only.
2025-05-08 15:42:00 +03:00
8614f86b71 gh-131525: Cache the result of tuple_hash (#131529)
* gh-131525: Cache the result of tuple_hash

* Fix debug builds

* Add blurb

* Fix formatting

* Pre-compute empty tuple singleton

* Mostly set the cache within tuple_alloc

* Fixes for TSAN

* Pre-compute empty tuple singleton

* Fix for 32-bit platforms

* Assert that op != NULL in _PyTuple_RESET_HASH_CACHE

* Use FT_ATOMIC_STORE_SSIZE_RELAXED macro

* Update Include/internal/pycore_tuple.h

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>

* Fix alignment

* atomic load

* Update Objects/tupleobject.c

Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>

---------

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>
2025-03-27 09:57:06 -04:00
Mark Shannon a45f25361d GH-131238: More refactoring of core header files (GH-131351)
Adds new pycore_stats.h header file to help break dependencies involving the pycore_code.h header.
2025-03-17 14:41:05 +00:00
Victor Stinner 9d759b63d8 gh-111178: Change Argument Clinic signature for METH_O (#130682)
Use "PyObject*" for METH_O functions to fix an undefined behavior.
2025-03-11 16:33:36 +01:00
Erlend E. Aasland 3b366a4a4b gh-129928: Rework sqlite3 error helpers (#129929)
Add a helper for raising DB-API compatible exceptions based on the
result code of SQLite C APIs. Some APIs do not store the error indicator
on the database pointer, so we need to be able to deduce the DB-API
compatible exception directly from the error code.

- rename _pysqlite_seterror() as set_error_from_db()
- introduce set_error_from_code()
2025-02-11 07:49:25 +00:00
Erlend E. Aasland 3a2e7aacf6 gh-129928: Raise more accurate exception for incorrect sqlite3 UDF creation (#129941)
Consistently raise ProgrammingError if the user tries to create an UDF
with an invalid number of parameters.
2025-02-11 08:26:01 +01:00
Erlend E. Aasland 7e6ee50b6b gh-129603: Don't segfault if sqlite3.Row description is None (#129604) 2025-02-10 00:27:28 +01:00
Victor StinnerandErlend E. Aasland 218f205f20 gh-129354: Use PyErr_FormatUnraisable() function (#129524)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Update test_sqlite3 tests.

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
2025-02-03 12:10:18 +00:00
Bénédikt Tran 881984b41a gh-111178: fix UBSan failures in Modules/_sqlite (GH-129087)
* fix UBSan failures for `pysqlite_Blob`
* fix UBSan failures for `pysqlite_Connection`
* fix UBSan failures for `pysqlite_Cursor`
* fix UBSan failures for `pysqlite_PrepareProtocol`
* fix UBSan failures for `pysqlite_Row`
* fix UBSan failures for `pysqlite_Statement`

* suppress unused return values
2025-01-31 14:33:30 +01:00
Victor Stinner 3bebe46d34 gh-128911: Add PyImport_ImportModuleAttr() function (#128912)
Add PyImport_ImportModuleAttr() and
PyImport_ImportModuleAttrString() functions.

* Add unit tests.
* Replace _PyImport_GetModuleAttr()
  with PyImport_ImportModuleAttr().
* Replace _PyImport_GetModuleAttrString()
  with PyImport_ImportModuleAttrString().
* Remove "pycore_import.h" includes, no longer needed.
2025-01-30 11:17:29 +00:00
Erlend E. Aasland 379ab856f5 gh-129346: Handle allocation errors for SQLite aggregate context (#129347) 2025-01-27 18:16:19 +01:00
Erlend E. Aasland 537296cdcd gh-111178: Generate correct signature for most self converters (#128447) 2025-01-20 12:40:18 +01:00