Skip to content
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

[sqlite3] Improve backup error handling #87431

Closed
erlend-aasland opened this issue Feb 19, 2021 · 10 comments
Closed

[sqlite3] Improve backup error handling #87431

erlend-aasland opened this issue Feb 19, 2021 · 10 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@erlend-aasland
Copy link
Contributor

BPO 43265
Nosy @berkerpeksag, @erlend-aasland
PRs
  • bpo-43265: Improve sqlite3.Connection.backup error handling #24586
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-04-14.11:46:52.881>
    created_at = <Date 2021-02-19.09:10:02.141>
    labels = ['type-feature', 'library', '3.10']
    title = '[sqlite3] Improve backup error handling'
    updated_at = <Date 2021-04-14.11:46:52.880>
    user = 'https://github.com/erlend-aasland'

    bugs.python.org fields:

    activity = <Date 2021-04-14.11:46:52.880>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-04-14.11:46:52.881>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2021-02-19.09:10:02.141>
    creator = 'erlendaasland'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43265
    keywords = ['patch']
    message_count = 10.0
    messages = ['387297', '387329', '387343', '387357', '387416', '387418', '387419', '387421', '391065', '391067']
    nosy_count = 2.0
    nosy_names = ['berker.peksag', 'erlendaasland']
    pr_nums = ['24586']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue43265'
    versions = ['Python 3.10']

    @erlend-aasland
    Copy link
    Contributor Author

    There are some issues with the error handling in pysqlite_connection_backup_impl():

    1. ValueError is returned if the target connection equals source connection. Should be OperationalError, IMHO.

    2. The aforementioned check is already performed by sqlite3_backup_init(), so we should just let SQLite take care of it and let _pysqlite_seterror() set the error if sqlite3_backup_init() returns NULL. This will also take care of 1.

    3. The following comment seems to be wrong; errors are set on the connection object, not on the backup handle:
      /* We cannot use _pysqlite_seterror() here because the backup APIs do
      not set the error status on the connection object, but rather on
      the backup handle. */

    After sqlite3_backup_finish(), we can just check the return code, and call _pysqlite_seterror() on the connection and return NULL. The mentioned comment can be removed.

    Resolving these issues will save 18 lines of code, and make the backup function easier to maintain.

    Berker?

    @erlend-aasland erlend-aasland added 3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 19, 2021
    @berkerpeksag
    Copy link
    Member

    I'm not sure about 1) because if target == source it means a user error. OperationalError is usually used for non-user errors.

    1. sounds good to me if that part is already covered by tests and they passed.

    @erlend-aasland
    Copy link
    Contributor Author

    I'm not sure about 1) because if target == source it means a user error. OperationalError is usually used for non-user errors.

    Yes, my bad. ProgrammingError would be better.
    target == source results in SQLITE_ERROR, BTW.

    I'll throw up a PR for 3., then.

    @erlend-aasland erlend-aasland changed the title Improve sqlite3 backup error handling [sqlite3] Improve backup error handling Feb 19, 2021
    @erlend-aasland erlend-aasland changed the title Improve sqlite3 backup error handling [sqlite3] Improve backup error handling Feb 19, 2021
    @erlend-aasland
    Copy link
    Contributor Author

    The unit test suite shows one case of improved "exception text". I'd say it's an improvement.

    $ ./python.exe  # with python/cpython#68774 applied
    >>> import sqlite3
    >>> c1 = sqlite3.connect(":memory:")
    >>> c2 = sqlite3.connect(":memory:")
    >>> c1.backup(c2, name="non-existing")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    sqlite3.OperationalError: unknown database non-existing
    
    $ python3.10  # latest alpha from python.org
    >>> import sqlite3
    >>> c1 = sqlite3.connect(":memory:")
    >>> c2 = sqlite3.connect(":memory:")
    >>> c1.backup(c2, name="non-existing")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    sqlite3.OperationalError: SQL logic error

    @berkerpeksag
    Copy link
    Member

    I'd even consider a cryptic error message a minor usability bug and backport it to at least 3.10.

    @erlend-aasland
    Copy link
    Contributor Author

    3.9, I presume? Yeah, I guess so.

    @erlend-aasland
    Copy link
    Contributor Author

    Regarding 1: After thinking about it, I see nothing wrong with raising ValueError error. No need to change current behaviour.

    @berkerpeksag
    Copy link
    Member

    3.9, I presume?

    Yes, I was going too fast :)

    @berkerpeksag
    Copy link
    Member

    New changeset c1ae741 by Erlend Egeberg Aasland in branch 'master':
    bpo-43265: Improve sqlite3.Connection.backup error handling (GH-24586)
    c1ae741

    @berkerpeksag
    Copy link
    Member

    Thank you.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants