Skip to content
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

Add more context to warnings emitted from Django when unpickling objects #381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import random
import re
import socket
import warnings
from collections import OrderedDict

from django.conf import settings
Expand Down Expand Up @@ -208,7 +209,7 @@ def get(self, key, default=None, version=None, client=None):
if value is None:
return default

return self.decode(value)
return self.decode(value, key)

def persist(self, key, version=None, client=None):
if client is None:
Expand Down Expand Up @@ -304,9 +305,11 @@ def clear(self, client=None):
except _main_exceptions as e:
raise ConnectionInterrupted(connection=client, parent=e)

def decode(self, value):
def decode(self, value, key):
"""
Decode the given value.

Key is used to provide better context to warnings.
"""
try:
value = int(value)
Expand All @@ -316,7 +319,18 @@ def decode(self, value):
except CompressorError:
# Handle little values, chosen to be not compressed
pass
value = self._serializer.loads(value)

with warnings.catch_warnings(record=True) as caught_warnings:
value = self._serializer.loads(value)

for warning in caught_warnings:
warnings.warn_explicit(
warning.category("{} (cache key: {})".format(str(warning.message), key)),
warning.category,
warning.filename,
warning.lineno,
)

return value

def encode(self, value):
Expand Down Expand Up @@ -356,7 +370,7 @@ def get_many(self, keys, version=None, client=None):
for key, value in zip(map_keys, results):
if value is None:
continue
recovered_data[map_keys[key]] = self.decode(value)
recovered_data[map_keys[key]] = self.decode(value, key)
return recovered_data

def set_many(self, data, timeout=DEFAULT_TIMEOUT, version=None, client=None):
Expand Down