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

bpo-44859: Improve error handling in sqlite3 and change some errors #27654

Merged
merged 2 commits into from Aug 8, 2021

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Aug 7, 2021

  • MemoryError is now raised instead of sqlite3.Warning when
    memory is not enough for encoding a statement to UTF-8
    in Connection.call() and Cursor.execute().
  • UnicodEncodeError is now raised instead of sqlite3.Warning when
    the statement contains surrogate characters
    in Connection.__call__() and Cursor.execute().
  • TypeError is now raised instead of ValueError for non-string
    script argument in Cursor.executescript().
  • ValueError is now raised for script containing the null
    character instead of truncating it in Cursor.executescript().
  • Correctly handle exceptions raised when getting boolean value
    of the result of the progress handler.
  • Add may tests covering different exceptional cases.

https://bugs.python.org/issue44859

* MemoryError is now raised instead of sqlite3.Warning when
  memory is not enough for encoding a statement to UTF-8
  in Connection.__call__() and Cursor.execute().
* UnicodEncodeError is now raised instead of sqlite3.Warning when
  the statement contains surrogate characters
  in Connection.__call__() and Cursor.execute().
* TypeError is now raised instead of ValueError for non-string
  script argument in Cursor.executescript().
* ValueError is now raised for script containing the null
  character instead of truncating it in Cursor.executescript().
* Correctly handle exceptions raised when getting boolean value
  of the result of the progress handler.
* Add may tests covering different exceptional cases.
Copy link
Contributor

@erlend-aasland erlend-aasland left a comment

LGTM, in general. Region coverage for util.c is now above 80%; sqlite3 coverage is getting better and better.

I left some minor comments.

Modules/_sqlite/connection.c Outdated Show resolved Hide resolved
}
} else {
PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
sql_len = strlen(sql_script);

This comment has been minimized.

@erlend-aasland

erlend-aasland Aug 7, 2021
Contributor

What about adding a custom AC converter that can extract the string length, so we don't have to iterate over the string again? Is it worth it?

This comment has been minimized.

@serhiy-storchaka

serhiy-storchaka Aug 7, 2021
Author Member

I think it is not worth. The code is simpler now (some checks were delegated to Argument Clinic), and it I prefer simplicity at the cost of additional strlen(). Custom converter would make it more complex than the original code. strlen() should be called in any case to check for embedded null characters. Now it is called 2 times instead of 1 time. The overhead is not too large.

sql_len = strlen(sql_script);
int max_length = sqlite3_limit(self->connection->db,
SQLITE_LIMIT_LENGTH, -1);
if (sql_len >= (unsigned)max_length) {

This comment has been minimized.

@erlend-aasland

erlend-aasland Aug 7, 2021
Contributor

Suggested change
if (sql_len >= (unsigned)max_length) {
if (sql_len >= (size_t)max_length) {

This comment has been minimized.

@serhiy-storchaka

serhiy-storchaka Aug 7, 2021
Author Member

How does it convert: int -> unsigned -> size_t or int -> ssize_t -> size_t? The standard specifies what the intermediate conversion the compiler should used, but without looking into it I cannot say that it will be the correct one.

If convert explicitly to unsigned, the conversion sequence will be unambiguous: int -> unsigned -> size_t.

@erlend-aasland
Copy link
Contributor

@erlend-aasland erlend-aasland commented Aug 7, 2021

BTW, that's a neat enhancement of the with_traceback decorator!

@erlend-aasland
Copy link
Contributor

@erlend-aasland erlend-aasland commented Aug 7, 2021

We should consider changing some more errors, as discussed in #27642 (comment), #27645 (comment), and #27645 (comment)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
@serhiy-storchaka serhiy-storchaka merged commit 0eec627 into python:main Aug 8, 2021
13 checks passed
13 checks passed
@github-actions
Docs
Details
@github-actions
Check for source changes
Details
@github-actions
Check if generated files are up to date
Details
@github-actions
Windows (x86)
Details
@github-actions
Windows (x64)
Details
@github-actions
macOS
Details
@github-actions
Ubuntu
Details
@github-actions
Ubuntu SSL tests with OpenSSL
Details
@github-actions
Address sanitizer Address sanitizer
Details
Azure Pipelines PR #20210807.27 succeeded
Details
@travis-ci
Travis CI - Pull Request Build Passed
Details
@bedevere-bot
bedevere/issue-number Issue number 44859 found
Details
@bedevere-bot
bedevere/news News entry found in Misc/NEWS.d
@serhiy-storchaka serhiy-storchaka deleted the serhiy-storchaka:sqlite-errors branch Aug 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants