Skip to content
Merged
Changes from 2 commits
Commits
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
12 changes: 12 additions & 0 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
from django import VERSION as DJANGO_VERSION
from django.core import signals

try:
import psycopg2.sql

def sql_to_string(sql):
if isinstance(sql, psycopg2.sql.SQL):
return sql.string
return sql
except ImportError:
def sql_to_string(sql):
return sql
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer the full try/except/else blocks so there's less in the try:

try:
    import psycopg2.sql
except ImportError:
    def sql_to_string(sql):
        return sql
else:
    # Handle psycopg2 sql object strings (or some such comment)
    def sql_to_string(sql):
        if isinstance(sql, psycopg2.sql.SQL):
            return sql.string
        return sql

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine, just a function declaration whose errors are caught


try:
from django.urls import resolve
except ImportError:
Expand Down Expand Up @@ -203,6 +214,7 @@ def format_sql(sql, params):
# convert sql with named parameters to sql with unnamed parameters
conv = _FormatConverter(params)
if params:
sql = sql_to_string(sql)
sql = sql % conv
params = conv.params
else:
Expand Down