Skip to content

User.full_name doesn't handle non-ASCII (in Python 2?) #1043

@nmlorg

Description

@nmlorg

Steps to reproduce

updater = ext.Updater(token=settings.telegram_token())
def F(bot, update):
  user = update.effective_user
  print repr(user.first_name), repr(user.last_name)
  print '%s %s' % (user.first_name, user.last_name)
  print user.full_name

updater.dispatcher.add_handler(ext.MessageHandler(0, F))
updater.start_polling()
updater.idle()

Expected behaviour

u'Dan\u2022iel' u'Reed'
Dan•iel Reed
Dan•iel Reed

Actual behaviour

u'Dan\u2022iel' u'Reed'
Dan•iel Reed
ERROR dispatcher.py:301] An uncaught error was raised while processing the update
Traceback (most recent call last):
  File "local/lib/python2.7/site-packages/telegram/ext/dispatcher.py", line 279, in process_update
    handler.handle_update(update, self)
  File "local/lib/python2.7/site-packages/telegram/ext/messagehandler.py", line 169, in handle_update
    return self.callback(dispatcher.bot, update, **optional_args)
  File "<stdin>", line 5, in F
  File "local/lib/python2.7/site-packages/telegram/user.py", line 91, in full_name
    return '{} {}'.format(self.first_name, self.last_name)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2022' in position 3: ordinal not in range(128)

Configuration

Operating System:

Version of Python, python-telegram-bot & dependencies:

python-telegram-bot 10.0.1
certifi 2018.01.18
future 0.16.0
Python 2.7.14 (default, Sep 23 2017, 22:06:14)  [GCC 7.2.0]

I'm a little rushed, but this is works for me:

    @property
    def full_name(self):
        """
        :obj:`str`: Convenience property. The user's :attr:`first_name`, followed by (if available)
            :attr:`last_name`.

        """
        if self.last_name:
!           return u'{} {}'.format(self.first_name, self.last_name)
        return self.first_name

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions