8000 Handle psycopg2's string composition by bmdavi3 · Pull Request #170 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

Handle psycopg2's string composition #170

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 3 commits into from Nov 15, 2018
Merged
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
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

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
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


import psycopg2.sql
Copy link
Member

Choose a reason for hiding this comment

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

I think this line is not intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha. Correct, will update.


try:
from django.urls import resolve
except ImportError:
Expand Down Expand Up @@ -203,6 +216,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
0