-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
fix: enable tracebacks for "user"/custom sqlite functions #5383
Open
ThinkChaos
wants to merge
3
commits into
beetbox:master
Choose a base branch
from
ThinkChaos:fix/sqlite-func-exceptions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,7 @@ name: Test | |
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- master | ||
|
||
env: | ||
PY_COLORS: 1 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ run-name: Lint code | |
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- master | ||
|
||
env: | ||
PYTHON_VERSION: 3.8 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,12 @@ | |
import os | ||
import re | ||
import sqlite3 | ||
import sys | ||
import threading | ||
import time | ||
from abc import ABC | ||
from collections import defaultdict | ||
from sqlite3 import Connection | ||
from sqlite3 import Connection, sqlite_version_info | ||
from types import TracebackType | ||
from typing import ( | ||
Any, | ||
|
@@ -73,6 +74,16 @@ | |
""" | ||
|
||
|
||
class DBCustomFunctionError(Exception): | ||
"""A sqlite function registered by beets failed.""" | ||
|
||
def __init__(self): | ||
super().__init__( | ||
"beets defined SQLite function failed; " | ||
"see the other errors above for details" | ||
) | ||
|
||
|
||
class FormattedMapping(Mapping[str, str]): | ||
"""A `dict`-like formatted view of a model. | ||
|
||
|
@@ -286,7 +297,7 @@ | |
"""The flex field SQLite table name. | ||
""" | ||
|
||
_fields: Dict[str, types.Type] = {} | ||
"""A mapping indicating available "fixed" fields on this type. The | ||
keys are field names and the values are `Type` objects. | ||
""" | ||
|
@@ -296,7 +307,7 @@ | |
terms. | ||
""" | ||
|
||
_types: Dict[str, types.Type] = {} | ||
"""Optional Types for non-fixed (i.e., flexible and computed) fields. | ||
""" | ||
|
||
|
@@ -305,7 +316,7 @@ | |
are subclasses of `Sort`. | ||
""" | ||
|
||
_queries: Dict[str, Type[FieldQuery]] = {} | ||
"""Named queries that use a field-like `name:value` syntax but which | ||
do not relate to any specific field. | ||
""" | ||
|
@@ -324,7 +335,7 @@ | |
@cached_classproperty | ||
def _relation(cls) -> type[Model]: | ||
"""The model that this model is closely related to.""" | ||
return cls | ||
|
||
@cached_classproperty | ||
def relation_join(cls) -> str: | ||
|
@@ -439,7 +450,7 @@ | |
# Essential field accessors. | ||
|
||
@classmethod | ||
def _type(cls, key) -> types.Type: | ||
"""Get the type of a field, a `Type` instance. | ||
|
||
If the field has no explicit type, it is given the base `Type`, | ||
|
@@ -730,16 +741,16 @@ | |
cls, | ||
field, | ||
pattern, | ||
query_cls: Type[FieldQuery] = MatchQuery, | ||
) -> FieldQuery: | ||
"""Get a `FieldQuery` for this model.""" | ||
return query_cls(field, pattern, field in cls._fields) | ||
|
||
@classmethod | ||
def all_fields_query( | ||
cls: Type["Model"], | ||
pats: Mapping, | ||
query_cls: Type[FieldQuery] = MatchQuery, | ||
): | ||
"""Get a query that matches many fields with different patterns. | ||
|
||
|
@@ -765,7 +776,7 @@ | |
def __init__( | ||
self, | ||
model_class: Type[AnyModel], | ||
rows: List[Mapping], | ||
db: "Database", | ||
flex_rows, | ||
query: Optional[Query] = None, | ||
|
@@ -970,6 +981,12 @@ | |
self._mutated = False | ||
self.db._db_lock.release() | ||
|
||
if ( | ||
isinstance(exc_value, sqlite3.OperationalError) | ||
and exc_value.args[0] == "user-defined function raised exception" | ||
): | ||
raise DBCustomFunctionError() | ||
|
||
def query(self, statement: str, subvals: Sequence = ()) -> List: | ||
"""Execute an SQL statement with substitution values and return | ||
a list of rows from the database. | ||
|
@@ -1028,6 +1045,10 @@ | |
"sqlite3 must be compiled with multi-threading support" | ||
) | ||
|
||
# Print tracebacks for exceptions in user defined functions | ||
# See also `self.add_functions` and `DBCustomFunctionError`. | ||
sqlite3.enable_callback_tracebacks(True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you reckon we should only do this if the logging level is |
||
|
||
self.path = path | ||
self.timeout = timeout | ||
|
||
|
@@ -1123,9 +1144,14 @@ | |
|
||
return bytestring | ||
|
||
conn.create_function("regexp", 2, regexp) | ||
conn.create_function("unidecode", 1, unidecode) | ||
conn.create_function("bytelower", 1, bytelower) | ||
deterministic = {} | ||
if sys.version_info >= (3, 8) and sqlite_version_info >= (3, 8, 3): | ||
# Let sqlite make extra optimizations | ||
deterministic["deterministic"] = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using create_function = conn.create_function
...
create_function = partial(create_function, deterministic=True)
create_function("regexp", 2, regexp)
... |
||
|
||
conn.create_function("regexp", 2, regexp, **deterministic) | ||
conn.create_function("unidecode", 1, unidecode, **deterministic) | ||
conn.create_function("bytelower", 1, bytelower, **deterministic) | ||
|
||
def _close(self): | ||
"""Close the all connections to the underlying SQLite database | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Were these two changes in github workflows intended?