farcepest / MySQLdb1

MySQL database connector for Python (legacy version)
https://sourceforge.net/projects/mysql-python/
667 stars 318 forks source link

Encode message passed to warn #122

Closed dundee closed 7 years ago

dundee commented 7 years ago

Funcion warn expects first argument to be str only. If unicode containing some non-ascii symbols is passed, exception is raised.

>>> from warnings import warn
>>> warn(u'řčš')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python2.7/warnings.py", line 29, in _show_warning
    file.write(formatwarning(message, category, filename, lineno, line))
  File "/usr/lib/python2.7/warnings.py", line 38, in formatwarning
    s =  "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

>>> warn(u'řčš'.encode('utf-8'))
/usr/local/bin/bpython:2: UserWarning: řčš

Plain ascii strings are not affected by this fix:

>>> warn('aaa'.encode('utf-8'))
/usr/local/bin/bpython:2: UserWarning: aaa