Fix repr unicode handling
Python2 requires utf-8 encoded bytestring Python3 requires unicode string
This commit is contained in:
parent
f497adcabd
commit
f05878d90f
2 changed files with 18 additions and 3 deletions
|
@ -606,6 +606,11 @@ class TestPhotoObject(TestPhotos):
|
|||
"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')
|
||||
def test_photo_object_create_attribute(self, _):
|
||||
"""
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
"""
|
||||
Base object supporting the storage of custom fields as attributes
|
||||
"""
|
||||
|
||||
from __future__ import unicode_literals
|
||||
import sys
|
||||
|
||||
class TroveboxObject(object):
|
||||
""" Base object supporting the storage of custom fields as attributes """
|
||||
_type = "None"
|
||||
|
@ -41,11 +45,17 @@ class TroveboxObject(object):
|
|||
|
||||
def __repr__(self):
|
||||
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:
|
||||
return "<%s id='%s'>" % (self.__class__.__name__, self.id)
|
||||
value = "<%s id='%s'>" % (self.__class__.__name__, self.id)
|
||||
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):
|
||||
""" Returns this object's attributes """
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue