[Tutor] 2to3 conversion

Steven D'Aprano steve at pearwood.info
Fri Jul 1 15:02:31 CEST 2011


Zubin Mithra wrote:
> Hey everyone,
> 
> I was running 2to3 on a particular file and I got the following traceback(
> http://paste.pocoo.org/show/223468/).

For short amounts of text, such as a traceback, please don't use a paste 
bin, just copy it into your post.

Some people are reading mail at a time or place where they might not 
have web access, or from somewhere that blocks access to the paste bin, 
but they can still read mail.

In your case, the error is:


Traceback (most recent call last):
   File "setup.py", line 86, in <module>
     util.run_2to3([i])
   File "/usr/lib/python3.0/distutils/util.py", line 572, in run_2to3
     r.refactor(files, write=True)
   File "/usr/lib/python3.0/lib2to3/refactor.py", line 196, in refactor
     self.refactor_file(dir_or_file, write, doctests_only)
   File "/usr/lib/python3.0/lib2to3/refactor.py", line 224, in refactor_file
     input = f.read() + "\n" # Silence certain parse errors
   File "/usr/lib/python3.0/io.py", line 1728, in read
     decoder.decode(self.buffer.read(), final=True))
   File "/usr/lib/python3.0/io.py", line 1299, in decode
     output = self.decoder.decode(input, final=final)
   File "/usr/lib/python3.0/codecs.py", line 300, in decode
     (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 232-234: 
invalid data


which looks like a bug in the 2to3 script.

This is not really a tutor question -- this list is for learning Python, 
not general Python related questions. In future, you might have better 
responses on the main python list, <python-list at python.org>.

But my guess is that the source file you are trying to convert contains 
an encoded character that doesn't exist in UTF-8. Try opening the 
original file (not the copy on the paste bin) and extracting those three 
bytes, and see what they are:


fp = open('distutils.util.py', 'r')
text = fp.read(234)
fp.close()
n = len(text.split('\n'))
s = text[232:235]
print("Bad bytes %r on line %d" % (s, n))



-- 
Steven



More information about the Tutor mailing list