From e806603a6e4ac3d488a0cadb1e962507a6f76fc1 Mon Sep 17 00:00:00 2001 From: Anssi Kääriäinen Date: Thu, 5 Jul 2012 14:06:21 +0300 Subject: Fixed issue #230 -- Avoid queries in aborted transactions On PostgreSQL when the transaction is in aborted status, checking connection.isolation_level will result in an error. --- debug_toolbar/utils/tracking/db.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'debug_toolbar/utils/tracking/db.py') diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py index 4d87090..01b4331 100644 --- a/debug_toolbar/utils/tracking/db.py +++ b/debug_toolbar/utils/tracking/db.py @@ -146,10 +146,17 @@ class NormalCursorWrapper(object): } if engine == 'psycopg2': + # If an erroneous query was ran on the connection, it might + # be in a state where checking isolation_level raises an + # exception. + try: + iso_level = conn.isolation_level + except conn.InternalError: + iso_level = 'unknown' params.update({ 'trans_id': self.logger.get_transaction_id(alias), 'trans_status': conn.get_transaction_status(), - 'iso_level': conn.isolation_level, + 'iso_level': iso_level, 'encoding': conn.encoding, }) -- cgit v1.2.3 From c1f7b3a273600b94dc433902c0a480dc8874d26a Mon Sep 17 00:00:00 2001 From: Anssi Kääriäinen Date: Mon, 27 Aug 2012 15:29:20 +0300 Subject: Made usable connections with alias not in db.connections --- debug_toolbar/utils/tracking/db.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'debug_toolbar/utils/tracking/db.py') diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py index 01b4331..2c82920 100644 --- a/debug_toolbar/utils/tracking/db.py +++ b/debug_toolbar/utils/tracking/db.py @@ -88,6 +88,14 @@ class NormalCursorWrapper(object): try: return self.cursor.execute(sql, params) finally: + # FIXME: Sometimes connections which are not in the connections + # dict are used (for example in test database destroying). + # The code below (at least get_transaction_id(alias) needs to have + # the connection in the connections dict. It would be good to + # not have this requirement at all, but for now lets just skip + # these connections. + if self.db.alias not in connections: + return stop = datetime.now() duration = ms_from_timedelta(stop - start) enable_stacktraces = getattr(settings, @@ -119,7 +127,7 @@ class NormalCursorWrapper(object): del cur_frame alias = getattr(self.db, 'alias', 'default') - conn = connections[alias].connection + conn = self.db.connection # HACK: avoid imports if conn: engine = conn.__class__.__module__.split('.', 1)[0] -- cgit v1.2.3 From d4971605cc0f82315805c44ea8346f609b1f07bb Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sun, 30 Dec 2012 17:52:56 -0500 Subject: Use the built-in json module on python >= 2.6. `django.utils.simplejson` is pending deprecation as of django 1.5 and will be removed in 1.7. --- debug_toolbar/utils/tracking/db.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'debug_toolbar/utils/tracking/db.py') diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py index 0dc22a6..d898b31 100644 --- a/debug_toolbar/utils/tracking/db.py +++ b/debug_toolbar/utils/tracking/db.py @@ -5,16 +5,20 @@ from threading import local from django.conf import settings from django.template import Node -from django.utils import simplejson from django.utils.encoding import force_unicode, smart_str from debug_toolbar.utils import ms_from_timedelta, tidy_stacktrace, \ get_template_info, get_stack from debug_toolbar.utils.compat.db import connections +try: + import json +except ImportError: # python < 2.6 + from django.utils import simplejson as json + try: from hashlib import sha1 -except ImportError: +except ImportError: # python < 2.5 from django.utils.hashcompat import sha_constructor as sha1 # TODO:This should be set in the toolbar loader as a default and panels should @@ -103,7 +107,7 @@ class NormalCursorWrapper(object): stacktrace = [] _params = '' try: - _params = simplejson.dumps( + _params = json.dumps( [force_unicode(x, strings_only=True) for x in params] ) except TypeError: -- cgit v1.2.3 From 090acab49e07a09c678a510a812a97724bdaf2a5 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 20 Feb 2013 13:42:06 -0800 Subject: Improvements for decoding params for recording --- debug_toolbar/utils/tracking/db.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'debug_toolbar/utils/tracking/db.py') diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py index d898b31..db9974e 100644 --- a/debug_toolbar/utils/tracking/db.py +++ b/debug_toolbar/utils/tracking/db.py @@ -91,8 +91,12 @@ class NormalCursorWrapper(object): for key, value in params.iteritems()) return map(self._quote_expr, params) + def _decode(self, param): + if isinstance(param, unicode): + return param.decode('utf-8', 'ignore') + return param + def execute(self, sql, params=()): - __traceback_hide__ = True start = datetime.now() try: return self.cursor.execute(sql, params) @@ -107,10 +111,8 @@ class NormalCursorWrapper(object): stacktrace = [] _params = '' try: - _params = json.dumps( - [force_unicode(x, strings_only=True) for x in params] - ) - except TypeError: + _params = json.dumps(map(self._decode, params)) + except Exception: pass # object not JSON serializable template_info = None -- cgit v1.2.3 From 8d0245d51bf3dda12a89dd1b5b5e5bad69959519 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 20 Feb 2013 13:44:10 -0800 Subject: Improve how we decode strings --- debug_toolbar/utils/tracking/db.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'debug_toolbar/utils/tracking/db.py') diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py index db9974e..b675227 100644 --- a/debug_toolbar/utils/tracking/db.py +++ b/debug_toolbar/utils/tracking/db.py @@ -92,9 +92,10 @@ class NormalCursorWrapper(object): return map(self._quote_expr, params) def _decode(self, param): - if isinstance(param, unicode): - return param.decode('utf-8', 'ignore') - return param + try: + return force_unicode(param, strings_only=True) + except UnicodeDecodeError: + return '(encoded string)' def execute(self, sql, params=()): start = datetime.now() -- cgit v1.2.3