Skip to content

Replace OrderedDict #1625

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

Merged
merged 3 commits into from
May 19, 2022
Merged
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
3 changes: 1 addition & 2 deletions debug_toolbar/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def verified_data(self):

@classmethod
def sign(cls, data):
items = sorted(data.items(), key=lambda item: item[0])
return signing.Signer(salt=cls.salt).sign(
json.dumps({key: force_str(value) for key, value in items})
json.dumps({key: force_str(value) for key, value in data.items()})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit worried about this. I thought that maybe the string was hashed and that changing the order of keys may lead to invalid hashes but this doesn't seem to be the case since we're not hashing, only signing and unsigning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another consideration is that when the signing code was originally added, DjDT still supported Python 3.6, which although in practice maintained dict insertion order, did not guarantee it. So to guarantee that the tests were correct, it was probably necessary to sort the data before signing at that point in time (no longer necessary now that Python 3.7 is the minimum supported version).

)
37 changes: 17 additions & 20 deletions debug_toolbar/panels/cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import inspect
import sys
import time
from collections import OrderedDict

try:
from django.utils.connection import ConnectionProxy
Expand Down Expand Up @@ -168,25 +167,23 @@ def __init__(self, *args, **kwargs):
self.hits = 0
self.misses = 0
self.calls = []
self.counts = OrderedDict(
(
("add", 0),
("get", 0),
("set", 0),
("get_or_set", 0),
("touch", 0),
("delete", 0),
("clear", 0),
("get_many", 0),
("set_many", 0),
("delete_many", 0),
("has_key", 0),
("incr", 0),
("decr", 0),
("incr_version", 0),
("decr_version", 0),
)
)
self.counts = {
"add": 0,
"get": 0,
"set": 0,
"get_or_set": 0,
"touch": 0,
"delete": 0,
"clear": 0,
"get_many": 0,
"set_many": 0,
"delete_many": 0,
"has_key": 0,
"incr": 0,
"decr": 0,
"incr_version": 0,
"decr_version": 0,
}
cache_called.connect(self._store_call_info)

def _store_call_info(
Expand Down
14 changes: 5 additions & 9 deletions debug_toolbar/panels/headers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections import OrderedDict

from django.utils.translation import gettext_lazy as _

from debug_toolbar.panels import Panel
Expand Down Expand Up @@ -36,21 +34,19 @@ class HeadersPanel(Panel):

def process_request(self, request):
wsgi_env = list(sorted(request.META.items()))
self.request_headers = OrderedDict(
(unmangle(k), v) for (k, v) in wsgi_env if is_http_header(k)
)
self.request_headers = {
unmangle(k): v for (k, v) in wsgi_env if is_http_header(k)
}
if "Cookie" in self.request_headers:
self.request_headers["Cookie"] = "=> see Request panel"
self.environ = OrderedDict(
(k, v) for (k, v) in wsgi_env if k in self.ENVIRON_FILTER
)
self.environ = {k: v for (k, v) in wsgi_env if k in self.ENVIRON_FILTER}
self.record_stats(
{"request_headers": self.request_headers, "environ": self.environ}
)
return super().process_request(request)

def generate_stats(self, request, response):
self.response_headers = OrderedDict(sorted(response.items()))
self.response_headers = dict(sorted(response.items()))
self.record_stats({"response_headers": self.response_headers})


Expand Down
3 changes: 1 addition & 2 deletions debug_toolbar/panels/history/panel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from collections import OrderedDict

from django.http.request import RawPostDataException
from django.template.loader import render_to_string
Expand Down Expand Up @@ -87,7 +86,7 @@ def content(self):

Fetch every store for the toolbar and include it in the template.
"""
stores = OrderedDict()
stores = {}
for id, toolbar in reversed(self.toolbar._store.items()):
stores[id] = {
"toolbar": toolbar,
Expand Down
10 changes: 1 addition & 9 deletions debug_toolbar/panels/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections import OrderedDict

from django.conf import settings
from django.utils.translation import gettext_lazy as _
from django.views.debug import get_default_exception_reporter_filter
Expand All @@ -22,10 +20,4 @@ def title(self):
return _("Settings from %s") % settings.SETTINGS_MODULE

def generate_stats(self, request, response):
self.record_stats(
{
"settings": OrderedDict(
sorted(get_safe_settings().items(), key=lambda s: s[0])
)
}
)
self.record_stats({"settings": dict(sorted(get_safe_settings().items()))})
2 changes: 1 addition & 1 deletion debug_toolbar/panels/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def signals(self):

def generate_stats(self, request, response):
signals = []
for name, signal in sorted(self.signals.items(), key=lambda x: x[0]):
for name, signal in sorted(self.signals.items()):
receivers = []
for receiver in signal.receivers:
receiver = receiver[1]
Expand Down
3 changes: 1 addition & 2 deletions debug_toolbar/panels/staticfiles.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
from os.path import join, normpath

from django.conf import settings
Expand Down Expand Up @@ -137,7 +136,7 @@ def get_staticfiles_finders(self):
of relative and file system paths which that finder was able
to find.
"""
finders_mapping = OrderedDict()
finders_mapping = {}
for finder in finders.get_finders():
try:
for path, finder_storage in finder.list([]):
Expand Down
3 changes: 1 addition & 2 deletions debug_toolbar/panels/templates/panel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
from contextlib import contextmanager
from os.path import normpath
from pprint import pformat, saferepr
Expand Down Expand Up @@ -39,7 +38,7 @@ def _request_context_bind_template(self, template):
self.template = template
# Set context processors according to the template engine's settings.
processors = template.engine.template_context_processors + self._processors
self.context_processors = OrderedDict()
self.context_processors = {}
updates = {}
for processor in processors:
name = f"{processor.__module__}.{processor.__name__}"
Expand Down
3 changes: 3 additions & 0 deletions debug_toolbar/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def __init__(self, request, get_response):
if panel.enabled:
get_response = panel.process_request
self.process_request = get_response
# Use OrderedDict for the _panels attribute so that items can be efficiently
# removed using FIFO order in the DebugToolbar.store() method. The .popitem()
# method of Python's built-in dict only supports LIFO removal.
self._panels = OrderedDict()
while panels:
panel = panels.pop()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

SIGNATURE = "-WiogJKyy4E8Om00CrFSy0T6XHObwBa6Zb46u-vmeYE"

DATA = {"value": "foo", "date": datetime(2020, 1, 1, tzinfo=timezone.utc)}
DATA = {"date": datetime(2020, 1, 1, tzinfo=timezone.utc), "value": "foo"}
SIGNED_DATA = f'{{"date": "2020-01-01 00:00:00+00:00", "value": "foo"}}:{SIGNATURE}'


Expand Down