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 unicode error when trying to escape binary data #1107

Merged
merged 7 commits into from Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions debug_toolbar/panels/sql/tracking.py
Expand Up @@ -6,7 +6,7 @@
from time import time

from django.utils import six
from django.utils.encoding import force_text
from django.utils.encoding import DjangoUnicodeDecodeError, force_text

from debug_toolbar import settings as dt_settings
from debug_toolbar.utils import get_stack, get_template_info, tidy_stacktrace
Expand Down Expand Up @@ -84,7 +84,12 @@ def __init__(self, cursor, db, logger):

def _quote_expr(self, element):
if isinstance(element, six.string_types):
return "'%s'" % force_text(element).replace("'", "''")
try:
return "'%s'" % force_text(element).replace("'", "''")
except DjangoUnicodeDecodeError:
return repr(element)
elif isinstance(element, six.binary_type):
dbowd marked this conversation as resolved.
Show resolved Hide resolved
return '(binary data)'
else:
return repr(element)

Expand Down
12 changes: 12 additions & 0 deletions tests/panels/test_sql.py
Expand Up @@ -122,6 +122,18 @@ def test_param_conversion(self):
'["2017-12-22 16:07:01"]'
))

@unittest.skipUnless(connection.vendor not in ('sqlite', 'postgresql'), '')
dbowd marked this conversation as resolved.
Show resolved Hide resolved
def test_binary_param_force_text(self):
self.assertEqual(len(self.panel._queries), 0)

with connection.cursor() as cursor:
cursor.execute("SELECT * FROM auth_user WHERE username = %s", [b'\xff'])

self.assertEqual(len(self.panel._queries), 1)
dbowd marked this conversation as resolved.
Show resolved Hide resolved

self.panel.process_response(self.request, self.response)
self.panel.generate_stats(self.request, self.response)
dbowd marked this conversation as resolved.
Show resolved Hide resolved

@unittest.skipUnless(connection.vendor != 'sqlite',
'Test invalid for SQLite')
def test_raw_query_param_conversion(self):
Expand Down