3
3
import json
4
4
from time import time
5
5
6
+ import django .test .testcases
6
7
from django .utils .encoding import force_str
7
8
8
9
from debug_toolbar import settings as dt_settings
@@ -31,10 +32,15 @@ class SQLQueryTriggered(Exception):
31
32
"""Thrown when template panel triggers a query"""
32
33
33
34
34
- def wrap_cursor (connection , panel ):
35
+ def wrap_cursor (connection ):
36
+ # If running a Django SimpleTestCase, which isn't allowed to access the database,
37
+ # don't perform any monkey patching.
38
+ if isinstance (connection .cursor , django .test .testcases ._DatabaseFailure ):
39
+ return
35
40
if not hasattr (connection , "_djdt_cursor" ):
36
41
connection ._djdt_cursor = connection .cursor
37
42
connection ._djdt_chunked_cursor = connection .chunked_cursor
43
+ connection ._djdt_logger = None
38
44
39
45
def cursor (* args , ** kwargs ):
40
46
# Per the DB API cursor() does not accept any arguments. There's
@@ -43,48 +49,33 @@ def cursor(*args, **kwargs):
43
49
# See:
44
50
# https://github.com/jazzband/django-debug-toolbar/pull/615
45
51
# https://github.com/jazzband/django-debug-toolbar/pull/896
52
+ logger = connection ._djdt_logger
53
+ cursor = connection ._djdt_cursor (* args , ** kwargs )
54
+ if logger is None :
55
+ return cursor
46
56
if allow_sql .get ():
47
57
wrapper = NormalCursorWrapper
48
58
else :
49
59
wrapper = ExceptionCursorWrapper
50
- return wrapper (connection . _djdt_cursor ( * args , ** kwargs ), connection , panel )
60
+ return wrapper (cursor , connection , logger )
51
61
52
62
def chunked_cursor (* args , ** kwargs ):
53
63
# prevent double wrapping
54
64
# solves https://github.com/jazzband/django-debug-toolbar/issues/1239
65
+ logger = connection ._djdt_logger
55
66
cursor = connection ._djdt_chunked_cursor (* args , ** kwargs )
56
- if not isinstance (cursor , BaseCursorWrapper ):
67
+ if logger is not None and not isinstance (cursor , BaseCursorWrapper ):
57
68
if allow_sql .get ():
58
69
wrapper = NormalCursorWrapper
59
70
else :
60
71
wrapper = ExceptionCursorWrapper
61
- return wrapper (cursor , connection , panel )
72
+ return wrapper (cursor , connection , logger )
62
73
return cursor
63
74
64
75
connection .cursor = cursor
65
76
connection .chunked_cursor = chunked_cursor
66
77
67
78
68
- def unwrap_cursor (connection ):
69
- if hasattr (connection , "_djdt_cursor" ):
70
- # Sometimes the cursor()/chunked_cursor() methods of the DatabaseWrapper
71
- # instance are already monkey patched before wrap_cursor() is called. (In
72
- # particular, Django's SimpleTestCase monkey patches those methods for any
73
- # disallowed databases to raise an exception if they are accessed.) Thus only
74
- # delete our monkey patch if the method we saved is the same as the class
75
- # method. Otherwise, restore the prior monkey patch from our saved method.
76
- if connection ._djdt_cursor == connection .__class__ .cursor :
77
- del connection .cursor
78
- else :
79
- connection .cursor = connection ._djdt_cursor
80
- del connection ._djdt_cursor
81
- if connection ._djdt_chunked_cursor == connection .__class__ .chunked_cursor :
82
- del connection .chunked_cursor
83
- else :
84
- connection .chunked_cursor = connection ._djdt_chunked_cursor
85
- del connection ._djdt_chunked_cursor
86
-
87
-
88
79
class BaseCursorWrapper :
89
80
pass
90
81
0 commit comments