Skip to content
Snippets Groups Projects
Unverified Commit c9986be7 authored by Erlend Egeberg Aasland's avatar Erlend Egeberg Aasland Committed by GitHub
Browse files

[3.11] gh-95273: Improve documented return values and exceptions raised for...

[3.11] gh-95273: Improve documented return values and exceptions raised for sqlite3 class methods (GH-95530) (#95673)

Co-authored-by: default avatarEzio Melotti <ezio.melotti@gmail.com>
Co-authored-by: default avatarAlex Waygood <Alex.Waygood@Gmail.com>
(cherry picked from commit 12d92c73)

Co-authored-by: default avatarErlend Egeberg Aasland <erlend.aasland@protonmail.com>
parent 6d834414
Branches
Tags
No related merge requests found
...@@ -285,15 +285,14 @@ Module functions and constants ...@@ -285,15 +285,14 @@ Module functions and constants
in RAM instead of on disk. in RAM instead of on disk.
:type database: :term:`path-like object` :type database: :term:`path-like object`
:param timeout: :param float timeout:
How many seconds the connection should wait before raising How many seconds the connection should wait before raising
an exception, if the database is locked by another connection. an exception, if the database is locked by another connection.
If another connection opens a transaction to modify the database, If another connection opens a transaction to modify the database,
it will be locked until that transaction is committed. it will be locked until that transaction is committed.
Default five seconds. Default five seconds.
:type timeout: float
:param detect_types: :param int detect_types:
Control whether and how data types not Control whether and how data types not
:ref:`natively supported by SQLite <sqlite3-types>` :ref:`natively supported by SQLite <sqlite3-types>`
are looked up to be converted to Python types, are looked up to be converted to Python types,
...@@ -306,7 +305,6 @@ Module functions and constants ...@@ -306,7 +305,6 @@ Module functions and constants
even when the *detect_types* parameter is set; :class:`str` will be even when the *detect_types* parameter is set; :class:`str` will be
returned instead. returned instead.
By default (``0``), type detection is disabled. By default (``0``), type detection is disabled.
:type detect_types: int
:param isolation_level: :param isolation_level:
The :attr:`~Connection.isolation_level` of the connection, The :attr:`~Connection.isolation_level` of the connection,
...@@ -316,25 +314,22 @@ Module functions and constants ...@@ -316,25 +314,22 @@ Module functions and constants
See :ref:`sqlite3-controlling-transactions` for more. See :ref:`sqlite3-controlling-transactions` for more.
:type isolation_level: str | None :type isolation_level: str | None
:param check_same_thread: :param bool check_same_thread:
If ``True`` (default), only the creating thread may use the connection. If ``True`` (default), only the creating thread may use the connection.
If ``False``, the connection may be shared across multiple threads; If ``False``, the connection may be shared across multiple threads;
if so, write operations should be serialized by the user to avoid data if so, write operations should be serialized by the user to avoid data
corruption. corruption.
:type check_same_thread: bool
:param factory: :param Connection factory:
A custom subclass of :class:`Connection` to create the connection with, A custom subclass of :class:`Connection` to create the connection with,
if not the default :class:`Connection` class. if not the default :class:`Connection` class.
:type factory: :class:`Connection`
:param cached_statements: :param int cached_statements:
The number of statements that ``sqlite3`` The number of statements that ``sqlite3``
should internally cache for this connection, to avoid parsing overhead. should internally cache for this connection, to avoid parsing overhead.
By default, 128 statements. By default, 128 statements.
:type cached_statements: int
:param uri: :param bool uri:
If set to ``True``, *database* is interpreted as a If set to ``True``, *database* is interpreted as a
:abbr:`URI (Uniform Resource Identifier)` with a file path :abbr:`URI (Uniform Resource Identifier)` with a file path
and an optional query string. and an optional query string.
...@@ -342,7 +337,6 @@ Module functions and constants ...@@ -342,7 +337,6 @@ Module functions and constants
and the path can be relative or absolute. and the path can be relative or absolute.
The query string allows passing parameters to SQLite, The query string allows passing parameters to SQLite,
enabling various :ref:`sqlite3-uri-tricks`. enabling various :ref:`sqlite3-uri-tricks`.
:type uri: bool
:rtype: Connection :rtype: Connection
...@@ -477,28 +471,23 @@ Connection objects ...@@ -477,28 +471,23 @@ Connection objects
Open a :class:`Blob` handle to an existing Open a :class:`Blob` handle to an existing
:abbr:`BLOB (Binary Large OBject)`. :abbr:`BLOB (Binary Large OBject)`.
:param table: :param str table:
The name of the table where the blob is located. The name of the table where the blob is located.
:type table: str
:param column: :param str column:
The name of the column where the blob is located. The name of the column where the blob is located.
:type column: str
:param row: :param str row:
The name of the row where the blob is located. The name of the row where the blob is located.
:type row: str
:param readonly: :param bool readonly:
Set to ``True`` if the blob should be opened without write Set to ``True`` if the blob should be opened without write
permissions. permissions.
Defaults to ``False``. Defaults to ``False``.
:type readonly: bool
:param name: :param str name:
The name of the database where the blob is located. The name of the database where the blob is located.
Defaults to ``"main"``. Defaults to ``"main"``.
:type name: str
:raises OperationalError: :raises OperationalError:
When trying to open a blob in a ``WITHOUT ROWID`` table. When trying to open a blob in a ``WITHOUT ROWID`` table.
...@@ -551,14 +540,12 @@ Connection objects ...@@ -551,14 +540,12 @@ Connection objects
Create or remove a user-defined SQL function. Create or remove a user-defined SQL function.
:param name: :param str name:
The name of the SQL function. The name of the SQL function.
:type name: str
:param narg: :param int narg:
The number of arguments the SQL function can accept. The number of arguments the SQL function can accept.
If ``-1``, it may take any number of arguments. If ``-1``, it may take any number of arguments.
:type narg: int
:param func: :param func:
A callable that is called when the SQL function is invoked. A callable that is called when the SQL function is invoked.
...@@ -567,11 +554,10 @@ Connection objects ...@@ -567,11 +554,10 @@ Connection objects
Set to ``None`` to remove an existing SQL function. Set to ``None`` to remove an existing SQL function.
:type func: :term:`callback` | None :type func: :term:`callback` | None
:param deterministic: :param bool deterministic:
If ``True``, the created SQL function is marked as If ``True``, the created SQL function is marked as
`deterministic <https://sqlite.org/deterministic.html>`_, `deterministic <https://sqlite.org/deterministic.html>`_,
which allows SQLite to perform additional optimizations. which allows SQLite to perform additional optimizations.
:type deterministic: bool
:raises NotSupportedError: :raises NotSupportedError:
If *deterministic* is used with SQLite versions older than 3.8.3. If *deterministic* is used with SQLite versions older than 3.8.3.
...@@ -588,14 +574,12 @@ Connection objects ...@@ -588,14 +574,12 @@ Connection objects
Create or remove a user-defined SQL aggregate function. Create or remove a user-defined SQL aggregate function.
:param name: :param str name:
The name of the SQL aggregate function. The name of the SQL aggregate function.
:type name: str
:param n_arg: :param int n_arg:
The number of arguments the SQL aggregate function can accept. The number of arguments the SQL aggregate function can accept.
If ``-1``, it may take any number of arguments. If ``-1``, it may take any number of arguments.
:type n_arg: int
:param aggregate_class: :param aggregate_class:
A class must implement the following methods: A class must implement the following methods:
...@@ -619,14 +603,12 @@ Connection objects ...@@ -619,14 +603,12 @@ Connection objects
Create or remove a user-defined aggregate window function. Create or remove a user-defined aggregate window function.
:param name: :param str name:
The name of the SQL aggregate window function to create or remove. The name of the SQL aggregate window function to create or remove.
:type name: str
:param num_params: :param int num_params:
The number of arguments the SQL aggregate window function can accept. The number of arguments the SQL aggregate window function can accept.
If ``-1``, it may take any number of arguments. If ``-1``, it may take any number of arguments.
:type num_params: int
:param aggregate_class: :param aggregate_class:
A class that must implement the following methods: A class that must implement the following methods:
...@@ -852,16 +834,14 @@ Connection objects ...@@ -852,16 +834,14 @@ Connection objects
Works even if the database is being accessed by other clients Works even if the database is being accessed by other clients
or concurrently by the same connection. or concurrently by the same connection.
:param target: :param Connection target:
The database connection to save the backup to. The database connection to save the backup to.
:type target: Connection
:param pages: :param int pages:
The number of pages to copy at a time. The number of pages to copy at a time.
If equal to or less than ``0``, If equal to or less than ``0``,
the entire database is copied in a single step. the entire database is copied in a single step.
Defaults to ``-1``. Defaults to ``-1``.
:type pages: int
:param progress: :param progress:
If set to a callable, it is invoked with three integer arguments for If set to a callable, it is invoked with three integer arguments for
...@@ -872,18 +852,16 @@ Connection objects ...@@ -872,18 +852,16 @@ Connection objects
Defaults to ``None``. Defaults to ``None``.
:type progress: :term:`callback` | None :type progress: :term:`callback` | None
:param name: :param str name:
The name of the database to back up. The name of the database to back up.
Either ``"main"`` (the default) for the main database, Either ``"main"`` (the default) for the main database,
``"temp"`` for the temporary database, ``"temp"`` for the temporary database,
or the name of a custom database as attached using the or the name of a custom database as attached using the
``ATTACH DATABASE`` SQL statement. ``ATTACH DATABASE`` SQL statement.
:type name: str
:param sleep: :param float sleep:
The number of seconds to sleep between successive attempts The number of seconds to sleep between successive attempts
to back up remaining pages. to back up remaining pages.
:type sleep: float
Example 1, copy an existing database into another:: Example 1, copy an existing database into another::
...@@ -909,11 +887,17 @@ Connection objects ...@@ -909,11 +887,17 @@ Connection objects
.. versionadded:: 3.7 .. versionadded:: 3.7
.. method:: getlimit(category, /) .. method:: getlimit(category, /)
Get a connection runtime limit. *category* is the limit category to be Get a connection runtime limit.
queried.
:param int category:
The `SQLite limit category`_ to be queried.
:rtype: int
:raises ProgrammingError:
If *category* is not recognised by the underlying SQLite library.
Example, query the maximum length of an SQL statement:: Example, query the maximum length of an SQL statement::
...@@ -927,14 +911,23 @@ Connection objects ...@@ -927,14 +911,23 @@ Connection objects
.. method:: setlimit(category, limit, /) .. method:: setlimit(category, limit, /)
Set a connection runtime limit. *category* is the limit category to be Set a connection runtime limit.
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 Attempts to increase a limit above its hard upper bound are silently
truncated to the hard upper bound. Regardless of whether or not the limit truncated to the hard upper bound. Regardless of whether or not the limit
was changed, the prior value of the limit is returned. was changed, the prior value of the limit is returned.
:param int category:
The `SQLite limit category`_ to be set.
:param int limit:
The value of the new limit.
If negative, the current limit is unchanged.
:rtype: int
:raises ProgrammingError:
If *category* is not recognised by the underlying SQLite library.
Example, limit the number of attached databases to 1:: Example, limit the number of attached databases to 1::
import sqlite3 import sqlite3
...@@ -943,6 +936,8 @@ Connection objects ...@@ -943,6 +936,8 @@ Connection objects
.. versionadded:: 3.11 .. versionadded:: 3.11
.. _SQLite limit category: https://www.sqlite.org/c3ref/c_limit_attached.html
.. method:: serialize(*, name="main") .. method:: serialize(*, name="main")
...@@ -952,8 +947,11 @@ Connection objects ...@@ -952,8 +947,11 @@ Connection objects
serialization is the same sequence of bytes which would be written to serialization is the same sequence of bytes which would be written to
disk if that database were backed up to disk. disk if that database were backed up to disk.
*name* is the database to be serialized, and defaults to the main :param str name:
database. The database name to be serialized.
Defaults to ``"main"``.
:rtype: bytes
.. note:: .. note::
...@@ -969,12 +967,24 @@ Connection objects ...@@ -969,12 +967,24 @@ Connection objects
:class:`Connection`. :class:`Connection`.
This method causes the database connection to disconnect from database This method causes the database connection to disconnect from database
*name*, and reopen *name* as an in-memory database based on the *name*, and reopen *name* as an in-memory database based on the
serialization contained in *data*. Deserialization will raise serialization contained in *data*.
:exc:`OperationalError` if the database connection is currently involved
in a read transaction or a backup operation. :exc:`OverflowError` will be :param bytes data:
raised if ``len(data)`` is larger than ``2**63 - 1``, and A serialized database.
:exc:`DatabaseError` will be raised if *data* does not contain a valid
SQLite database. :param str name:
The database name to deserialize into.
Defaults to ``"main"``.
:raises OperationalError:
If the database connection is currently involved in a read
transaction or a backup operation.
:raises DatabaseError:
If *data* does not contain a valid SQLite database.
:raises OverflowError:
If :func:`len(data) <len>` is larger than ``2**63 - 1``.
.. note:: .. note::
...@@ -1071,13 +1081,13 @@ Cursor objects ...@@ -1071,13 +1081,13 @@ Cursor objects
.. method:: fetchone() .. method:: fetchone()
Fetch the next row of a query result set as a :class:`tuple`. Return the next row of a query result set as a :class:`tuple`.
Return ``None`` if no more data is available. Return ``None`` if no more data is available.
.. method:: fetchmany(size=cursor.arraysize) .. method:: fetchmany(size=cursor.arraysize)
Fetch the next set of rows of a query result as a :class:`list`. Return the next set of rows of a query result as a :class:`list`.
Return an empty list if no more rows are available. Return an empty list if no more rows are available.
The number of rows to fetch per call is specified by the *size* parameter. The number of rows to fetch per call is specified by the *size* parameter.
...@@ -1093,7 +1103,7 @@ Cursor objects ...@@ -1093,7 +1103,7 @@ Cursor objects
.. method:: fetchall() .. method:: fetchall()
Fetch all (remaining) rows of a query result as a :class:`list`. Return all (remaining) rows of a query result as a :class:`list`.
Return an empty list if no rows are available. Return an empty list if no rows are available.
Note that the :attr:`arraysize` attribute can affect the performance of Note that the :attr:`arraysize` attribute can affect the performance of
this operation. this operation.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment