[issue7045] utf-8 encoding error

Ezio Melotti report at bugs.python.org
Sat Oct 3 12:22:01 CEST 2009


Ezio Melotti <ezio.melotti at gmail.com> added the comment:

I can't reproduce it either on Ubuntu 9.04 32-bit. I tried both from the
terminal and from the file, using Py3.2a0.

As Martin said, the fact that in narrow builds of Python the codepoints
outside the BMP are represented with two surrogate pairs is a known
"issue". This is how UTF-16 works, even if it has some problematic
side-effects.
In your example 'line[0]' is not equal to 'first' because line[0] is the
codepoint of the first surrogate and 'first' is a scalar value that
represents the SHAVIAN LETTER TOT (U+010451).

Regarding the traceback you pasted in the first post, have you used
print('𐑑') or print(line[0])?

This is what I get using line[0]:
>>> line = '𐑑𐑧𐑕𐑑𐑦𐑙'
>>> first = '𐑑'
>>> print(line[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'utf-8' codec can't encode character '\ud801' in
position 0: surrogates not allowed

In this case you are getting an error because lone surrogates are
invalid and they can't be encoded. If you use line[:2] instead it works
because it takes both the surrogates:

>>> print(line[0:2])
𐑑
>>> first == line[0:2]
True

If you really got that error with print('𐑛'), then #3297 could be related.

Can you also try this and see what it prints?
>>> import sys
>>> sys.maxunicode

----------
nosy: +ezio.melotti
priority:  -> normal

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7045>
_______________________________________


More information about the Python-bugs-list mailing list