all Python 3sqlite3function
sqlite3.enable_callback_tracebacks()
utility functions
Turns on traceback printing to stderr when your function called from SQL (create_function, an aggregate, a converter) fails. Off by default.
Syntax
sqlite3.enable_callback_tracebacks(flag, /)
Without it, an error inside a user-defined function turns into a faceless OperationalError 'user-defined function raised exception', and the real cause — ZeroDivisionError, KeyError — is lost. After enable_callback_tracebacks(True) the original exception goes to sys.unraisablehook and is printed to stderr, yet the query still fails with the same OperationalError. The flag is global for the whole process.
Examples
# Python 3.11+
import sqlite3
import sys
conn = sqlite3.connect(':memory:')
conn.create_function('half', 1, lambda x: x / 0)
try:
conn.execute('SELECT half(4)')
except sqlite3.OperationalError as e:
print(e) # → user-defined function raised exception
sqlite3.enable_callback_tracebacks(True)
seen = []
sys.unraisablehook = lambda u: seen.append(type(u.exc_value).__name__)
try:
conn.execute('SELECT half(4)')
except sqlite3.OperationalError:
print(seen) # → ['ZeroDivisionError'] — настоящая причина
conn.close()
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy