Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug __repr__ting an XmlModel on Python3 (doesn't define unicode) #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions djxml/xmlmodels/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
from django.core.exceptions import (ObjectDoesNotExist, FieldError,
MultipleObjectsReturned,)
from django.db.models.base import subclass_exception
try:
from django.utils.encoding import (
smart_bytes as smart_str, force_text as force_unicode)
except ImportError:
from django.utils.encoding import smart_str, force_unicode
from django.utils.encoding import smart_str, force_text
from django.utils.encoding import python_2_unicode_compatible

from .signals import xmlclass_prepared
from .options import Options, DEFAULT_NAMES
Expand Down Expand Up @@ -130,7 +127,7 @@ def _prepare(cls):

xmlclass_prepared.send(sender=cls)


@python_2_unicode_compatible
@six.add_metaclass(XmlModelBase)
class XmlModel(object):

Expand Down Expand Up @@ -218,14 +215,14 @@ def create_from_file(cls, xml_file):

def __repr__(self):
try:
u = unicode(self)
u = smart_str(self)
except (UnicodeEncodeError, UnicodeDecodeError):
u = '[Bad Unicode data]'
return smart_str(u'<%s: %s>' % (self.__class__.__name__, u))

def __str__(self):
if hasattr(self, '__unicode__'):
return force_unicode(self).encode('utf-8')
return force_text(self).encode('utf-8')
return '%s object' % self.__class__.__name__

def __eq__(self, other):
Expand Down