Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
D
Django-Redis-Cache
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shared
Django-Redis-Cache
Commits
be20ec70
Commit
be20ec70
authored
Jun 12, 2013
by
Carl Meyer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle __str__/__unicode__ correctly for Python 2/3.
parent
eb17f1bd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
6 deletions
+24
-6
cache.py
redis_cache/cache.py
+4
-6
compat.py
redis_cache/compat.py
+20
-0
No files found.
redis_cache/cache.py
View file @
be20ec70
...
...
@@ -2,7 +2,7 @@ from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
from
django.core.exceptions
import
ImproperlyConfigured
from
django.utils
import
importlib
from
django.utils.datastructures
import
SortedDict
from
.compat
import
smart_text
,
smart_bytes
from
.compat
import
smart_text
,
smart_bytes
,
python_2_unicode_compatible
try
:
import
cPickle
as
pickle
...
...
@@ -18,6 +18,7 @@ from redis.connection import UnixDomainSocketConnection, Connection
from
redis.connection
import
DefaultParser
@
python_2_unicode_compatible
class
CacheKey
(
object
):
"""
A stub string class that we can use to check if a key was created already.
...
...
@@ -29,13 +30,10 @@ class CacheKey(object):
return
self
.
_key
==
other
def
__str__
(
self
):
return
s
elf
.
__unicode__
(
)
return
s
mart_text
(
self
.
_key
)
def
__repr__
(
self
):
return
self
.
__unicode__
()
def
__unicode__
(
self
):
return
smart_text
(
self
.
_key
)
return
repr
(
self
.
_key
)
def
__hash__
(
self
):
return
hash
(
self
.
_key
)
...
...
redis_cache/compat.py
View file @
be20ec70
import
sys
PY3
=
(
sys
.
version_info
>=
(
3
,))
try
:
# Django 1.5+
from
django.utils.encoding
import
smart_text
,
smart_bytes
...
...
@@ -6,3 +10,19 @@ except ImportError:
from
django.utils.encoding
import
smart_unicode
,
smart_str
smart_text
=
smart_unicode
smart_bytes
=
smart_str
def
python_2_unicode_compatible
(
klass
):
"""
A decorator that defines __unicode__ and __str__ methods under Python 2.
Under Python 3 it does nothing.
To support Python 2 and 3 with a single code base, define a __str__ method
returning text and apply this decorator to the class.
Backported from Django 1.5+.
"""
if
not
PY3
:
klass
.
__unicode__
=
klass
.
__str__
klass
.
__str__
=
lambda
self
:
self
.
__unicode__
()
.
encode
(
'utf-8'
)
return
klass
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment