8000 bpo-45138: Expand traced SQL statements in `sqlite3` trace callback by erlend-aasland · Pull Request #28240 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-45138: Expand traced SQL statements in sqlite3 trace callback #28240

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

Merged
merged 28 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a018384
Use expanded SQL when tracing using v2 API
Sep 1, 2021
0d59fd9
Fall back to unexpanded sql if expanded sql is too long
Sep 8, 2021
654b89f
Remove unneeded indent
Sep 8, 2021
d7dcbd4
Add NEWS
Sep 8, 2021
bc71d99
Revert spurious print traceback change
Sep 8, 2021
e22f58d
Merge branch 'main' into sqlite-bound-trace
Sep 14, 2021
d603841
Merge branch 'main' into sqlite-bound-trace
Sep 15, 2021
b3c7cd7
Merge branch 'main' into sqlite-bound-trace
Oct 6, 2021
b91fc81
Chain exceptions if multiple exceptions happen
Oct 6, 2021
3fa1dfa
Merge branch 'main' into sqlite-bound-trace
Oct 19, 2021
bc1ac3a
Merge branch 'main' into sqlite-bound-trace
Nov 1, 2021
5683a9a
Merge branch 'main' into sqlite-bound-trace
Nov 2, 2021
7e8d2be
Merge branch 'main' into sqlite-bound-trace
Nov 3, 2021
ec19cf7
Improve coverage
Nov 3, 2021
b3940df
Merge branch 'main' into sqlite-bound-trace
Nov 10, 2021
f773a85
Expand test
Nov 10, 2021
a83a945
We can't rely on sqlite_schema being present
Nov 10, 2021
fae8d98
Merge branch 'main' into sqlite-bound-trace
Nov 29, 2021
b016c0c
8000 Adapt tests to new decorator
Nov 29, 2021
391c698
Address review
Nov 29, 2021
8c5faad
Merge branch 'main' into sqlite-bound-trace
Jan 4, 2022
a1c1424
Merge branch 'main' into sqlite-bound-trace
Jan 18, 2022
6ea2605
Merge branch 'main' into sqlite-bound-trace
Jan 20, 2022
24ef8a9
Merge branch 'main' into sqlite-bound-trace
Jan 22, 2022
4d9c9ec
Merge branch 'main' into sqlite-bound-trace
Feb 26, 2022
beff1d5
Update Lib/test/test_sqlite3/test_hooks.py
Mar 3, 2022
9920131
PEP 8
Mar 3, 2022
f086604
Address review: clean up namespace
Mar 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve coverage
  • Loading branch information
Erlend E. Aasland committed Nov 3, 2021
commit ec19cf7ee23ee119b386924e5b257449a9bd7fa6
14 changes: 14 additions & 0 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ def managed_connect(*args, in_mem=False, **kwargs):
unlink(TESTFN)


def memory_database():
cx = sqlite.connect(":memory:")
return contextlib.closing(cx)


@contextlib.contextmanager
def cx_limit(cx, category=sqlite.SQLITE_LIMIT_LENGTH, limit=128):
try:
_prev = cx.setlimit(category, limit)
yield limit
finally:
cx.setlimit(category, _prev)


class ModuleTests(unittest.TestCase):
def test_api_level(self):
self.assertEqual(sqlite.apilevel, "2.0",
Expand Down
40 changes: 30 additions & 10 deletions Lib/test/test_sqlite3/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,25 @@
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.

import contextlib
import sqlite3 as sqlite
import unittest

from test.support.os_helper import TESTFN, unlink
from .test_dbapi import memory_database, cx_limit
from .test_userfunctions import with_tracebacks


@contextlib.contextmanager
def check_trace(self, cx, expected):
try:
traced = []
cx.set_trace_callback(lambda stmt: traced.append(stmt))
yield
finally:
self.assertEqual(traced, expected)


class CollationTests(unittest.TestCase):
def test_create_collation_not_string(self):
con = sqlite.connect(":memory:")
Expand Down Expand Up @@ -292,12 +305,6 @@ def trace(statement):
@unittest.skipIf(sqlite.sqlite_version_info < (3, 14, 0),
"Requires SQLite 3.14.0 or newer")
def test_trace_expanded_sql(self):
cx = sqlite.connect(":memory:")
traced = []
cx.set_trace_callback(lambda stmt: traced.append(stmt))
with cx:
cx.execute("create table t(t)")
cx.executemany("insert into t values(?)", ((v,) for v in range(3)))
expected = [
"create table t(t)",
"BEGIN ",
Expand All @@ -306,13 +313,26 @@ def test_trace_expanded_sql(self):
"insert into t values(2)",
"COMMIT",
]
self.assertEqual(traced, expected)
with memory_database() as cx, check_trace(self, cx, expected):
with cx:
cx.execute("create table t(t)")
cx.executemany("insert into t values(?)", ((v,) for v in range(3)))

@with_tracebacks([
"DataError",
"Expanded SQL string exceeds the maximum string length"
], traceback=False)
def test_trace_too_much_expanded_sql(self):
query = "select * from sqlite_schema where type=?"
with memory_database() as cx, cx_limit(cx) as lim:
with check_trace(self, cx, [query]):
cx.execute(query, ("a"*lim,))

@with_tracebacks(["ZeroDivisionError", "5/0"])
def test_trace_bad_handler(self):
cx = sqlite.connect(":memory:")
cx.set_trace_callback(lambda stmt: 5/0)
cx.execute("select 1")
with memory_database() as cx:
cx.set_trace_callback(lambda stmt: 5/0)
cx.execute("select 1")


if __name__ == "__main__":
Expand Down
0