Skip to content

Initial fixes to make the debug-toolbar to work with Python 3 using six from django.utils #366

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion debug_toolbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
try:
VERSION = __import__('pkg_resources') \
.get_distribution('django-debug-toolbar').version
except Exception, e:
except Exception:
VERSION = 'unknown'
26 changes: 17 additions & 9 deletions debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
Debug Toolbar middleware
"""
import imp
import thread
try:
from django.utils.six.moves import _thread
except ImportError:
from django.utils.six.moves import _dummy_thread as _thread


from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.utils.encoding import smart_unicode
from django.utils import six
try:
from django.utils.encoding import smart_text
except ImportError:
from django.utils.encoding import smart_unicode as smart_text
from django.utils.importlib import import_module

import debug_toolbar.urls
Expand Down Expand Up @@ -38,7 +46,7 @@ class DebugToolbarMiddleware(object):

@classmethod
def get_current(cls):
return cls.debug_toolbars.get(thread.get_ident())
return cls.debug_toolbars.get(_thread.get_ident())

def __init__(self):
self._urlconfs = {}
Expand Down Expand Up @@ -76,7 +84,7 @@ def process_request(self, request):
__traceback_hide__ = True
if self.show_toolbar(request):
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
if isinstance(urlconf, basestring):
if isinstance(urlconf, six.string_types):
urlconf = import_module(getattr(request, 'urlconf', settings.ROOT_URLCONF))

if urlconf not in self._urlconfs:
Expand All @@ -98,11 +106,11 @@ def process_request(self, request):
toolbar = DebugToolbar(request)
for panel in toolbar.panels:
panel.process_request(request)
self.__class__.debug_toolbars[thread.get_ident()] = toolbar
self.__class__.debug_toolbars[_thread.get_ident()] = toolbar

def process_view(self, request, view_func, view_args, view_kwargs):
__traceback_hide__ = True
toolbar = self.__class__.debug_toolbars.get(thread.get_ident())
toolbar = self.__class__.debug_toolbars.get(_thread.get_ident())
if not toolbar:
return
result = None
Expand All @@ -114,7 +122,7 @@ def process_view(self, request, view_func, view_args, view_kwargs):

def process_response(self, request, response):
__traceback_hide__ = True
ident = thread.get_ident()
ident = _thread.get_ident()
toolbar = self.__class__.debug_toolbars.get(ident)
if not toolbar or request.is_ajax():
return response
Expand All @@ -134,9 +142,9 @@ def process_response(self, request, response):
for panel in toolbar.panels:
panel.process_response(request, response)
response.content = replace_insensitive(
smart_unicode(response.content),
smart_text(response.content),
self.tag,
smart_unicode(toolbar.render_toolbar() + self.tag))
smart_text(toolbar.render_toolbar() + self.tag))
if response.get('Content-Length', None):
response['Content-Length'] = len(response.content)
del self.__class__.debug_toolbars[ident]
Expand Down
3 changes: 2 additions & 1 deletion debug_toolbar/panels/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.core.cache.backends.base import BaseCache
from django.dispatch import Signal
from django.template import Node
from django.utils import six
from django.utils.datastructures import SortedDict
from django.utils.translation import ugettext_lazy as _, ungettext

Expand Down Expand Up @@ -166,7 +167,7 @@ def _store_call_info(self, sender, name=None, time_taken=0,
else:
self.hits += 1
elif name == 'get_many':
for key, value in return_value.iteritems():
for key, value in six.iteritems(return_value):
if value is None:
self.misses += 1
else:
Expand Down
8 changes: 4 additions & 4 deletions debug_toolbar/panels/profiling.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import division

from django.utils import six
from django.utils.translation import ugettext_lazy as _
from django.utils.safestring import mark_safe
from debug_toolbar.panels import DebugPanel
Expand All @@ -10,8 +11,7 @@
except ImportError:
DJ_PROFILE_USE_LINE_PROFILER = False


from cStringIO import StringIO
from django.utils.six.moves import StringIO
import cProfile
from pstats import Stats
from colorsys import hsv_to_rgb
Expand All @@ -23,7 +23,7 @@ class DjangoDebugToolbarStats(Stats):

def get_root_func(self):
if self.__root is None:
for func, (cc, nc, tt, ct, callers) in self.stats.iteritems():
for func, (cc, nc, tt, ct, callers) in six.iteritems(self.stats):
if len(callers) == 0:
self.__root = func
break
Expand Down Expand Up @@ -80,7 +80,7 @@ def subfuncs(self):
i = 0
h, s, v = self.hsv
count = len(self.statobj.all_callees[self.func])
for func, stats in self.statobj.all_callees[self.func].iteritems():
for func, stats in six.iteritems(self.statobj.all_callees[self.func]):
i += 1
h1 = h + (i / count) / (self.depth + 1)
if stats[3] == 0:
Expand Down
3 changes: 2 additions & 1 deletion debug_toolbar/panels/request_vars.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.core.urlresolvers import resolve
from django.http import Http404
from django.utils import six
from django.utils.translation import ugettext_lazy as _

from debug_toolbar.panels import DebugPanel
Expand Down Expand Up @@ -53,5 +54,5 @@ def process_response(self, request, response):
if hasattr(self.request, 'session'):
self.record_stats({
'session': [(k, self.request.session.get(k))
for k in self.request.session.iterkeys()]
for k in six.iterkeys(self.request.session)]
})
3 changes: 2 additions & 1 deletion debug_toolbar/panels/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import uuid

from django.db.backends import BaseDatabaseWrapper
from django.utils import six
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _, ungettext_lazy as __

Expand Down Expand Up @@ -134,7 +135,7 @@ def process_response(self, request, response):
if self._queries:
width_ratio_tally = 0
factor = int(256.0 / (len(self._databases) * 2.5))
for n, db in enumerate(self._databases.itervalues()):
for n, db in enumerate(six.itervalues(self._databases)):
rgb = [0, 0, 0]
color = n % 3
rgb[color] = 256 - n / 3 * factor
Expand Down
6 changes: 3 additions & 3 deletions debug_toolbar/templates/debug_toolbar/panels/cache.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ <h3>{% trans "Commands" %}</h3>
<table>
<thead>
<tr>
{% for name in counts.iterkeys %}
{% for name in counts.keys %}
<th>{{ name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for value in counts.itervalues %}
{% for value in counts.values %}
<td>{{ value }}</td>
{% endfor %}
</tr>
Expand Down Expand Up @@ -66,4 +66,4 @@ <h3>{% trans "Calls" %}</h3>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endif %}
2 changes: 1 addition & 1 deletion debug_toolbar/templates/debug_toolbar/panels/headers.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</tr>
</thead>
<tbody>
{% for key, value in headers.iteritems %}
{% for key, value in headers.items %}
<tr class="{% cycle 'djDebugOdd' 'djDebugEven' %}">
<td>{{ key|escape }}</td>
<td>{{ value|escape }}</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h4>{% blocktrans count templates|length as template_count %}Template{% plural %
<h4>{% blocktrans count context_processors|length as context_processors_count %}Context processor{% plural %}Context processors{% endblocktrans %}</h4>
{% if context_processors %}
<dl>
{% for key, value in context_processors.iteritems %}
{% for key, value in context_processors.items %}
<dt><strong>{{ key|escape }}</strong></dt>
<dd>
<div class="djTemplateShowContextDiv"><a class="djTemplateShowContext"><span class="toggleArrow">&#x25B6;</span> {% trans 'Toggle Context' %}</a></div>
Expand Down
2 changes: 1 addition & 1 deletion debug_toolbar/templates/debug_toolbar/panels/versions.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</tr>
</thead>
<tbody>
{% for package, version in versions.iteritems %}
{% for package, version in versions.items %}
<tr class="{% cycle 'djDebugOdd' 'djDebugEven' %}">
<td>{{ package }}</td>
<td>{{ version }}</td>
Expand Down
4 changes: 2 additions & 2 deletions debug_toolbar/toolbar/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def render_toolbar(self):
"""
context = self.template_context.copy()
context.update({
'panels': self.panels,
'panels': list(self.panels) # [panel for panel in self.panels]
})

return render_to_string('debug_toolbar/base.html', context)
Expand Down Expand Up @@ -93,7 +93,7 @@ def load_panel_classes():
panel_module, panel_classname = panel_path[:dot], panel_path[dot + 1:]
try:
mod = import_module(panel_module)
except ImportError, e:
except ImportError as e:
raise ImproperlyConfigured(
'Error importing debug panel %s: "%s"' %
(panel_module, e))
Expand Down
12 changes: 8 additions & 4 deletions debug_toolbar/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import inspect
import os.path
import django
import SocketServer
try:
import socketserver
except ImportError: # python 2.x
import SocketServer as socketserver
import sys

from django.conf import settings
from django.views.debug import linebreak_iter
from django.utils import six
from django.utils.html import escape
from django.utils.safestring import mark_safe

# Figure out some paths
django_path = os.path.realpath(os.path.dirname(django.__file__))
socketserver_path = os.path.realpath(os.path.dirname(SocketServer.__file__))
socketserver_path = os.path.realpath(os.path.dirname(socketserver.__file__))


def ms_from_timedelta(td):
Expand All @@ -28,7 +32,7 @@ def tidy_stacktrace(stack):
"""
Clean up stacktrace and remove all entries that:
1. Are part of Django (except contrib apps)
2. Are part of SocketServer (used by Django's dev server)
2. Are part of socketserver (used by Django's dev server)
3. Are the last entry (which is part of our stacktracing code)

``stack`` should be a list of frame tuples from ``inspect.stack()``
Expand Down Expand Up @@ -56,7 +60,7 @@ def render_stacktrace(trace):
stacktrace = []
for frame in trace:
params = map(escape, frame[0].rsplit(os.path.sep, 1) + list(frame[1:]))
params_dict = dict((unicode(idx), v) for idx, v in enumerate(params))
params_dict = dict((six.text_type(idx), v) for idx, v in enumerate(params))
try:
stacktrace.append(u'<span class="path">%(0)s/</span>'
u'<span class="file">%(1)s</span>'
Expand Down
3 changes: 2 additions & 1 deletion debug_toolbar/utils/sqlparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class SQLParseError(Exception):


# Setup namespace
from django.utils import six
from debug_toolbar.utils.sqlparse import engine
from debug_toolbar.utils.sqlparse import filters
from debug_toolbar.utils.sqlparse import formatter
Expand Down Expand Up @@ -52,4 +53,4 @@ def split(sql):
"""
stack = engine.FilterStack()
stack.split_statements = True
return [unicode(stmt) for stmt in stack.run(sql)]
return [six.text_type(stmt) for stmt in stack.run(sql)]
4 changes: 3 additions & 1 deletion debug_toolbar/utils/sqlparse/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import re

from django.utils import six

from debug_toolbar.utils.sqlparse import tokens as T
from debug_toolbar.utils.sqlparse import sql

Expand Down Expand Up @@ -131,7 +133,7 @@ def __init__(self, width=2, char=' ', line_width=None):
def _get_offset(self, token):
all_ = list(self._curr_stmt.flatten())
idx = all_.index(token)
raw = ''.join(unicode(x) for x in all_[:idx + 1])
raw = ''.join(six.text_type(x) for x in all_[:idx + 1])
line = raw.splitlines()[-1]
# Now take current offset into account and return relative offset.
full_offset = len(line) - len(self.char * (self.width * self.indent))
Expand Down
8 changes: 5 additions & 3 deletions debug_toolbar/utils/sqlparse/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import re

from django.utils import six

from debug_toolbar.utils.sqlparse import tokens
from debug_toolbar.utils.sqlparse.keywords import KEYWORDS, KEYWORDS_COMMON

Expand Down Expand Up @@ -79,7 +81,7 @@ def _process_state(cls, unprocessed, processed, state):

try:
rex = re.compile(tdef[0], rflags).match
except Exception, err:
except Exception as err:
raise ValueError(("uncompilable regex %r in state"
" %r of %r: %s"
% (tdef[0], state, cls, err)))
Expand All @@ -92,7 +94,7 @@ def _process_state(cls, unprocessed, processed, state):
new_state = None
else:
tdef2 = tdef[2]
if isinstance(tdef2, str):
if isinstance(tdef2, bytes):
# an existing state
if tdef2 == '#pop':
new_state = -1
Expand Down Expand Up @@ -222,7 +224,7 @@ def get_tokens(self, text, unfiltered=False):
Also preprocess the text, i.e. expand tabs and strip it if
wanted and applies registered filters.
"""
if not isinstance(text, unicode):
if not isinstance(text, six.text_type):
if self.encoding == 'guess':
try:
text = text.decode('utf-8')
Expand Down
Loading