Skip to content

Commit

Permalink
Attempts UTF-8 decoding on UnicodeError.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamteem committed May 28, 2010
1 parent 9621adf commit 20b7251
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions redisco/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

import redis

class Client(object):
Expand Down
6 changes: 5 additions & 1 deletion redisco/models/attributes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: UTF-8 -*-
"""
Defines the fields that can be added to redisco models.
"""
Expand Down Expand Up @@ -67,7 +68,10 @@ def typecast_for_read(self, value):

def typecast_for_storage(self, value):
"""Typecasts the value for storing to Redis."""
return unicode(value)
try:
return unicode(value)
except UnicodeError:
return unicode(value.decode('utf-8'))

def value_type(self):
return unicode
Expand Down
7 changes: 5 additions & 2 deletions redisco/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,10 @@ def _write(self, _new=False):
if callable(v):
v = v()
if v:
h[index] = str(v)
try:
h[index] = unicode(v)
except UnicodeError:
h[index] = unicode(v.decode('utf-8'))
del self.db[self.key()]
if h:
self.db.hmset(self.key(), h)
Expand Down Expand Up @@ -489,7 +492,7 @@ def _tuple_for_index_key_attr_zset(self, att, val, sval):
(self._key[att], self._index_key_for_attr_val(att, sval)))

def _index_key_for_attr_val(self, att, val):
return self._key[att][_encode_key(str(val))]
return self._key[att][_encode_key(val)]

##################
# Python methods #
Expand Down
3 changes: 3 additions & 0 deletions redisco/models/modelset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
Handles the queries.
"""
from attributes import IntegerField, DateTimeField
import redisco
from redisco.containers import SortedSet, Set, List, NonPersistentList
Expand Down
5 changes: 4 additions & 1 deletion redisco/models/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import base64

def _encode_key(s):
return base64.b64encode(s).replace("\n", "")
try:
return base64.b64encode(str(s)).replace("\n", "")
except UnicodeError, e:
return base64.b64encode(s.encode('utf-8')).replace("\n", "")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
import os

version = '0.1.dev17'
version = '0.1.dev18'

try:
from setuptools import setup
Expand Down
14 changes: 14 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import time
from threading import Thread
import base64
Expand Down Expand Up @@ -54,6 +55,19 @@ def test_save(self):
jejomar = Person.objects.get_by_id('2')
self.assertEqual(None, jejomar.last_name)

def test_unicode(self):
p = Person(first_name="Niña", last_name="Jose")
self.assert_(p.save())
g = Person.objects.create(first_name="Granny", last_name="Goose")
self.assert_(g)

p = Person.objects.filter(first_name="Niña").first()
self.assert_(p)
self.assertEqual("Niña Jose", p.full_name())




def test_update(self):
person1 = Person(first_name="Granny", last_name="Goose")
person1.save()
Expand Down

0 comments on commit 20b7251

Please sign in to comment.