Skip to content

Commit 285ddc6

Browse files
mergify[bot]brettlangdonmabdinur
authored
fix(tracing): fix error when using sqlite3 backup method (backport #4246) (#4250)
* fix(tracing): fix error when using sqlite3 backup method (#4246) ## Description SQlite3 does an explicit type check on the `target` of `backup()` if the target is also a patched connection we need to unwrap it before calling the original `backup` method. Note: this does not add a span/tracing to `backup` ## Checklist - [ ] Title must conform to [conventional commit](https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional). - [ ] Add additional sections for `feat` and `fix` pull requests. - [ ] Ensure tests are passing for affected code. - [ ] [Library documentation](https://github.com/DataDog/dd-trace-py/tree/1.x/docs) and/or [Datadog's documentation site](https://github.com/DataDog/documentation/) is updated. Link to doc PR in description. ## Relevant issue(s) Fixes #4241 ## Testing strategy Added a test case/call to `backup` to ensure when the target is also a traced connection that there is no error. ## Reviewer Checklist - [x] Title is accurate. - [x] Description motivates each change. - [x] No unnecessary changes were introduced in this PR. - [x] PR cannot be broken up into smaller PRs. - [x] Avoid breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Tests provided or description of manual testing performed is included in the code or PR. - [x] Release note has been added for fixes and features, or else `changelog/no-changelog` label added. - [x] All relevant GitHub issues are correctly linked. - [x] Backports are identified and tagged with Mergifyio. - [ ] Add to milestone. (cherry picked from commit 82b1521) # Conflicts: # tests/contrib/sqlite3/test_sqlite3.py * Update tests/contrib/sqlite3/test_sqlite3.py * Update tests/contrib/sqlite3/test_sqlite3.py Co-authored-by: Brett Langdon <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Munir Abdinur <[email protected]>
1 parent 57e7e07 commit 285ddc6

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

ddtrace/contrib/sqlite3/patch.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sqlite3
33
import sqlite3.dbapi2
4+
import sys
45

56
from ddtrace import config
67
from ddtrace.vendor import wrapt
@@ -75,3 +76,13 @@ def __init__(self, conn, pin=None, cursor_cls=None):
7576
def execute(self, *args, **kwargs):
7677
# sqlite has a few extra sugar functions
7778
return self.cursor().execute(*args, **kwargs)
79+
80+
# backup was added in Python 3.7
81+
if sys.version_info >= (3, 7, 0):
82+
83+
def backup(self, target, *args, **kwargs):
84+
# sqlite3 checks the type of `target`, it cannot be a wrapped connection
85+
# https://github.com/python/cpython/blob/4652093e1b816b78e9a585d671a807ce66427417/Modules/_sqlite/connection.c#L1897-L1899
86+
if isinstance(target, TracedConnection):
87+
target = target.__wrapped__
88+
return self.__wrapped__.backup(target, *args, **kwargs)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
sqlite3: fix error when using ``connection.backup`` method.

tests/contrib/sqlite3/test_sqlite3.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sqlite3
2+
import sys
23
import time
34
from typing import TYPE_CHECKING
45

@@ -371,3 +372,12 @@ def test_iterator_usage(patched_conn):
371372
"""Ensure sqlite3 patched cursors can be used as iterators."""
372373
rows = next(patched_conn.execute("select 1"))
373374
assert len(rows) == 1
375+
376+
377+
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Connection.backup was added in Python 3.7")
378+
def test_backup(patched_conn):
379+
"""Ensure sqlite3 patched connections backup function can be used"""
380+
destination = sqlite3.connect(":memory:")
381+
382+
with destination:
383+
patched_conn.backup(destination, pages=1)

0 commit comments

Comments
 (0)