Dear list,

I'm experiencing a strange encoding error when using 'paster shell' with
Pylons 0.9.7. In my SQLAlchemy objects I define my own __repr__() method to
an ORM-mapped class. Observe:

class VirtualUser(MyOrm):
    def __repr__(self):
        return u"VirtualUser (#%s): Login=%s Email=%s Comment=%s" % \
            (self.id, self.login, self.email, self.comment)

Now when in 'paster shell' I try to get this object by just typing its name:

========================================================
In [48]: user
Out[48]: <type 'unicode'>
VirtualUser (#22): Login=some-user-name [email protected]
Comment=a-cömment-with-speciäl-chäräcters Domain=example.com
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)

/home/chaas/projekte/ispwebadmin/<ipython console> in <module>()

/usr/lib/pymodules/python2.5/IPython/Prompts.pyc in __call__(self, arg)
    549
    550             # and now call a possibly user-defined print mechanism
--> 551             manipulated_val = self.display(arg)
    552
    553             # user display hooks can change the variable to be
stored in

/usr/lib/pymodules/python2.5/IPython/Prompts.pyc in _display(self, arg)
    575             return IPython.generics.result_display(arg)
    576         except TryNext:
--> 577             return self.shell.hooks.result_display(arg)
    578
    579     # Assign the default display method:

/usr/lib/pymodules/python2.5/IPython/hooks.pyc in __call__(self, *args, **kw)
    139             #print "prio",prio,"cmd",cmd #dbg
    140             try:
--> 141                 ret = cmd(*args, **kw)
    142                 return ret
    143             except ipapi.TryNext, exc:

/usr/lib/pymodules/python2.5/IPython/hooks.pyc in result_display(self, arg)
    169
    170     if self.rc.pprint:
--> 171         out = pformat(arg)
    172         if '\n' in out:
    173             # So that multi-line strings line up with the left
column of

/usr/lib/python2.5/pprint.pyc in pformat(self, object)
    109     def pformat(self, object):
    110         sio = _StringIO()
--> 111         self._format(object, sio, 0, 0, {}, 0)
    112         return sio.getvalue()
    113

/usr/lib/python2.5/pprint.pyc in _format(self, object, stream, indent,
allowance, context, level)
    127             self._readable = False
    128             return
--> 129         rep = self._repr(object, context, level - 1)
    130         typ = _type(object)
    131         sepLines = _len(rep) > (self._width - 1 - indent - allowance)

/usr/lib/python2.5/pprint.pyc in _repr(self, object, context, level)
    193     def _repr(self, object, context, level):
    194         repr, readable, recursive = self.format(object, context.copy(),
--> 195                                                 self._depth, level)
    196         if not readable:
    197             self._readable = False

/usr/lib/python2.5/pprint.pyc in format(self, object, context, maxlevels,
level)
    205         and whether the object represents a recursive construct.
    206         """
--> 207         return _safe_repr(object, context, maxlevels, level)
    208
    209

/usr/lib/python2.5/pprint.pyc in _safe_repr(object, context, maxlevels, level)
    290         return format % _commajoin(components), readable, recursive
    291
--> 292     rep = repr(object)
    293     return rep, (rep and not rep.startswith('<')), False
    294

UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in
position 120: ordinal not in range(128)
========================================================

So apparently my __repr__() is called but right after that I get the
UnicodeEncodeError exception. The funny thing is that I get the expected
result by calling user.__repr__() but not when calling repr(user):

========================================================
In [7]: user.__repr__()
Out[7]: u'VirtualUser (#22): Login=some-user-name [email protected]
Comment=a-cömment-with-speciäl-chäräcters Domain=example.com'

In [8]: repr(user)
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)

/home/chaas/projekte/ispwebadmin/<ipython console> in <module>()

UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in
position 120: ordinal not in range(128)
========================================================

Does anyone have an explanation why foo.__repr__() and repr(foo) makes such
a difference here? Hints welcome.

Cheers
 Christoph

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to