Skip to content

Commit

Permalink
Fix unicode handling in str, unicode, and repr
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Shah committed Dec 16, 2013
1 parent cccf1d5 commit 26c08e4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/richenum/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ def __init__(self, canonical_name, display_name, *args, **kwargs):
self.canonical_name = canonical_name
self.display_name = display_name

def __repr__(self):
return "<%s: %s ('%s')>" % (
self.__class__.__name__,
self.canonical_name.decode('utf-8', 'replace').encode('ascii', 'replace'),
unicode(self.display_name).encode('ascii', 'replace'))

def __unicode__(self):
return unicode(self.display_name)

def __repr__(self):
return unicode("%s - canonical_name: '%s' display_name: '%s'" % (self.__class__.__name__,
self.canonical_name,
self.display_name))

def __str__(self):
return self.display_name
return unicode(self).encode('utf-8', 'xmlcharrefreplace')

def __hash__(self):
return hash(self.canonical_name)
Expand Down Expand Up @@ -107,11 +108,11 @@ def __init__(self, index, canonical_name, display_name, *args, **kwargs):
self.index = index

def __repr__(self):
return unicode("%s - idx: %d canonical_name: '%s' display_name: '%s'"
% (self.__class__.__name__,
self.index,
self.canonical_name,
self.display_name))
return "<%s #%s: %s ('%s')>" % (
self.__class__.__name__,
self.index,
self.canonical_name.decode('utf-8', 'replace').encode('ascii', 'replace'),
unicode(self.display_name).encode('ascii', 'replace'))

def __cmp__(self, other):
if other is None:
Expand Down
7 changes: 7 additions & 0 deletions tests/richenum/test_ordered_rich_enums.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import copy
import unittest2 as unittest

Expand Down Expand Up @@ -102,3 +103,9 @@ def test_equality_by_index_and_type(self):
coffee_copy = copy.deepcopy(Breakfast.COFFEE)
self.assertFalse(coffee_copy is Breakfast.COFFEE)
self.assertEqual(Breakfast.COFFEE, coffee_copy)

def test_unicode_handling(self):
poop_oatmeal = BreakfastEnumValue(3, 'oatmeal💩', u'Oatmeal💩')
self.assertEqual(repr(poop_oatmeal), "<BreakfastEnumValue #3: oatmeal? ('Oatmeal?')>")
self.assertEqual(str(poop_oatmeal), "Oatmeal💩")
self.assertEqual(unicode(poop_oatmeal), u"Oatmeal💩")
7 changes: 7 additions & 0 deletions tests/richenum/test_rich_enums.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import copy
import unittest2 as unittest

Expand Down Expand Up @@ -125,3 +126,9 @@ def test_equality_by_canonical_name_and_type(self):
okra_copy = copy.deepcopy(Vegetable.OKRA)
self.assertFalse(okra_copy is Vegetable.OKRA)
self.assertEqual(Vegetable.OKRA, okra_copy)

def test_unicode_handling(self):
poop_okra = VegetableEnumValue('gross', 'okra💩', u'Okra💩')
self.assertEqual(repr(poop_okra), "<VegetableEnumValue: okra? ('Okra?')>")
self.assertEqual(str(poop_okra), "Okra💩")
self.assertEqual(unicode(poop_okra), u"Okra💩")

0 comments on commit 26c08e4

Please sign in to comment.