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

fix(tracing): fix error when using sqlite3 backup method (backport #4246) #4250

Merged
merged 11 commits into from
Oct 6, 2022
11 changes: 11 additions & 0 deletions ddtrace/contrib/sqlite3/patch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sqlite3
import sqlite3.dbapi2
import sys

from ddtrace import config
from ddtrace.vendor import wrapt
Expand Down Expand Up @@ -75,3 +76,13 @@ def __init__(self, conn, pin=None, cursor_cls=None):
def execute(self, *args, **kwargs):
# sqlite has a few extra sugar functions
return self.cursor().execute(*args, **kwargs)

# backup was added in Python 3.7
if sys.version_info >= (3, 7, 0):

def backup(self, target, *args, **kwargs):
# sqlite3 checks the type of `target`, it cannot be a wrapped connection
# https://github.com/python/cpython/blob/4652093e1b816b78e9a585d671a807ce66427417/Modules/_sqlite/connection.c#L1897-L1899
if isinstance(target, TracedConnection):
target = target.__wrapped__
return self.__wrapped__.backup(target, *args, **kwargs)
4 changes: 4 additions & 0 deletions releasenotes/notes/fix-sqlite3-backup-bf92c698567237b9.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
sqlite3: fix error when using ``connection.backup`` method.
19 changes: 19 additions & 0 deletions tests/contrib/sqlite3/test_sqlite3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sqlite3
import sys
import time

import pytest
Expand Down Expand Up @@ -351,3 +352,21 @@ def test_context_manager(self):
cursor.fetchall()
spans = self.get_spans()
assert len(spans) == 1
<<<<<<< HEAD
=======
brettlangdon marked this conversation as resolved.
Show resolved Hide resolved


def test_iterator_usage(patched_conn):
"""Ensure sqlite3 patched cursors can be used as iterators."""
rows = next(patched_conn.execute("select 1"))
assert len(rows) == 1


@pytest.mark.skipif(sys.version_info < (3, 7), reason="Connection.backup was added in Python 3.7")
def test_backup(patched_conn):
"""Ensure sqlite3 patched connections backup function can be used"""
destination = sqlite3.connect(":memory:")

with destination:
patched_conn.backup(destination, pages=1)
>>>>>>> 82b15216 (fix(tracing): fix error when using sqlite3 backup method (#4246))
brettlangdon marked this conversation as resolved.
Show resolved Hide resolved