Fix repr unicode handling

Python2 requires utf-8 encoded bytestring
Python3 requires unicode string
This commit is contained in:
sneakypete81 2014-01-31 19:34:01 +00:00
parent f497adcabd
commit f05878d90f
2 changed files with 18 additions and 3 deletions

View file

@ -606,6 +606,11 @@ class TestPhotoObject(TestPhotos):
"name": "Test Name"}) "name": "Test Name"})
self.assertEqual(repr(photo), "<Photo name='Test Name'>") self.assertEqual(repr(photo), "<Photo name='Test Name'>")
def test_photo_object_repr_with_unicode_id(self):
""" Ensure that a unicode id is correctly represented """
photo = trovebox.objects.photo.Photo(self.client, {"id": "\xfcmlaut"})
self.assertIn(repr(photo), [b"<Photo id='\xc3\xbcmlaut'>", "<Photo id='\xfcmlaut'>"])
@mock.patch.object(trovebox.Trovebox, 'post') @mock.patch.object(trovebox.Trovebox, 'post')
def test_photo_object_create_attribute(self, _): def test_photo_object_create_attribute(self, _):
""" """

View file

@ -1,6 +1,10 @@
""" """
Base object supporting the storage of custom fields as attributes Base object supporting the storage of custom fields as attributes
""" """
from __future__ import unicode_literals
import sys
class TroveboxObject(object): class TroveboxObject(object):
""" Base object supporting the storage of custom fields as attributes """ """ Base object supporting the storage of custom fields as attributes """
_type = "None" _type = "None"
@ -41,11 +45,17 @@ class TroveboxObject(object):
def __repr__(self): def __repr__(self):
if self.name is not None: if self.name is not None:
return "<%s name='%s'>" % (self.__class__.__name__, self.name) value = "<%s name='%s'>" % (self.__class__.__name__, self.name)
elif self.id is not None: elif self.id is not None:
return "<%s id='%s'>" % (self.__class__.__name__, self.id) value = "<%s id='%s'>" % (self.__class__.__name__, self.id)
else: else:
return "<%s>" % (self.__class__.__name__) value = "<%s>" % (self.__class__.__name__)
# Python2 requires a bytestring
if sys.version < '3':
return value.encode('utf-8')
else: # pragma: no cover
return value
def get_fields(self): def get_fields(self):
""" Returns this object's attributes """ """ Returns this object's attributes """