55
66from django .conf import settings
77from django .template import Node
8- from django .utils import simplejson
98from django .utils .encoding import force_unicode , smart_str
109
1110from debug_toolbar .utils import ms_from_timedelta , tidy_stacktrace , \
1211 get_template_info , get_stack
1312from debug_toolbar .utils .compat .db import connections
1413
14+ try :
15+ import json
16+ except ImportError : # python < 2.6
17+ from django .utils import simplejson as json
18+
1519try :
1620 from hashlib import sha1
17- except ImportError :
21+ except ImportError : # python < 2.5
1822 from django .utils .hashcompat import sha_constructor as sha1
1923
2024# TODO:This should be set in the toolbar loader as a default and panels should
@@ -87,12 +91,25 @@ def _quote_params(self, params):
8791 for key , value in params .iteritems ())
8892 return map (self ._quote_expr , params )
8993
94+ def _decode (self , param ):
95+ try :
96+ return force_unicode (param , strings_only = True )
97+ except UnicodeDecodeError :
98+ return '(encoded string)'
99+
90100 def execute (self , sql , params = ()):
91- __traceback_hide__ = True
92101 start = datetime .now ()
93102 try :
94103 return self .cursor .execute (sql , params )
95104 finally :
105+ # FIXME: Sometimes connections which are not in the connections
106+ # dict are used (for example in test database destroying).
107+ # The code below (at least get_transaction_id(alias) needs to have
108+ # the connection in the connections dict. It would be good to
109+ # not have this requirement at all, but for now lets just skip
110+ # these connections.
111+ if self .db .alias not in connections :
112+ return
96113 stop = datetime .now ()
97114 duration = ms_from_timedelta (stop - start )
98115 enable_stacktraces = getattr (settings ,
@@ -103,10 +120,8 @@ def execute(self, sql, params=()):
103120 stacktrace = []
104121 _params = ''
105122 try :
106- _params = simplejson .dumps (
107- [force_unicode (x , strings_only = True ) for x in params ]
108- )
109- except TypeError :
123+ _params = json .dumps (map (self ._decode , params ))
124+ except Exception :
110125 pass # object not JSON serializable
111126
112127 template_info = None
@@ -124,7 +139,7 @@ def execute(self, sql, params=()):
124139 del cur_frame
125140
126141 alias = getattr (self .db , 'alias' , 'default' )
127- conn = connections [ alias ] .connection
142+ conn = self . db .connection
128143 # HACK: avoid imports
129144 if conn :
130145 engine = conn .__class__ .__module__ .split ('.' , 1 )[0 ]
@@ -148,11 +163,17 @@ def execute(self, sql, params=()):
148163 }
149164
150165 if engine == 'psycopg2' :
151- from psycopg2 .extensions import TRANSACTION_STATUS_INERROR
166+ # If an erroneous query was ran on the connection, it might
167+ # be in a state where checking isolation_level raises an
168+ # exception.
169+ try :
170+ iso_level = conn .isolation_level
171+ except conn .InternalError :
172+ iso_level = 'unknown'
152173 params .update ({
153174 'trans_id' : self .logger .get_transaction_id (alias ),
154175 'trans_status' : conn .get_transaction_status (),
155- 'iso_level' : conn . isolation_level if not conn . get_transaction_status () == TRANSACTION_STATUS_INERROR else "" ,
176+ 'iso_level' : iso_level ,
156177 'encoding' : conn .encoding ,
157178 })
158179
0 commit comments