10/06/2010

Pydevのデバッガーがエラーになる件

EclipseでPythonを触ってみようとして、はまったのでメモしておく

python 3.1
pydev 1.6.2
実行環境は別途落としてきた3.1を利用。

debug実行時にソースにダブルバイトのコメントが存在した場合、
  1. #あ  
  2. n=1  
  3. print("hoge")  


エラーは以下

pydev debugger: starting
Traceback (most recent call last):
File "C:\util\ide\eclipse36\eclipse\plugins\org.python.pydev.debug_1.6.3.2010100513\pysrc\pydevd.py", line 1147, in
debugger.run(setup['file'], None, None)
File "C:\util\ide\eclipse36\eclipse\plugins\org.python.pydev.debug_1.6.3.2010100513\pysrc\pydevd.py", line 925, in run
line = stream.readline() #Should not raise an exception even if there are no more contents
UnicodeDecodeError: 'cp932' codec can't decode bytes in position 55-56: illegal multibyte sequence

encodeはutf-8に設定してるのだが?pydevd.pyのエラー箇所をみてる。
  1. if not IS_PY3K:  
  2.     execfile(file, globals, locals) #execute the script  
  3. else:  
  4.     stream = open(file)  
  5.     try:  
  6.         encoding = None  
  7.         #Get encoding!  
  8.         for i in range(2):  
  9.             line = stream.readline() #Should not raise an exception even if there are no more contents  
  10.             #Must be a comment line  
  11.             if line.strip().startswith('#'):  
  12.                 #Don't import re if there's no chance that there's an encoding in the line  
  13.                 if 'coding' in line:  
  14.                     import re  
  15.                     p = re.search(r"coding[:=]\s*([-\w.]+)", line)  
  16.                     if p:  
  17.                         encoding = p.group(1)  
  18.                         break  
  19.     finally:  
  20.         stream.close()  


の line = stream.readline()でエラー。
その上で、cp932でstream = open(file)してしまってるためっぽい。
openしてcodingを調べたいのに、cp932で開いてエラーになる

ソース先頭2行目までに、# coding: utf-8
があれば、stream = open(file, encoding='utf-8')のとこだけの変更でよいが、
encoding='utf-8'もいれといた。

  1. if not IS_PY3K:  
  2.     execfile(file, globals, locals) #execute the script  
  3. else:  
  4.     #stream = open(file) #2010.10.6 cahnge  
  5.     stream = open(file, encoding='utf-8')  
  6.     try:  
  7.         #encoding = None #2010.10.6 cahnge  
  8.         encoding='utf-8'  
  9.         #Get encoding!  
  10.         for i in range(2):  
  11.             line = stream.readline() #Should not raise an exception even if there are no more contents  
  12.             #Must be a comment line  
  13.             if line.strip().startswith('#'):  
  14.                 #Don't import re if there's no chance that there's an encoding in the line  
  15.                 if 'coding' in line:  
  16.                     import re  
  17.                     p = re.search(r"coding[:=]\s*([-\w.]+)", line)  
  18.                     if p:  
  19.                         encoding = p.group(1)  
  20.                         break  
  21.     finally:  
  22.         stream.close()  
  23.   
  24.     if encoding:  
  25.         stream = open(file, encoding=encoding)  
  26.     else:  
  27.         stream = open(file)  
  28.     try:  
  29.         contents = stream.read()  
  30.     finally:  
  31.         stream.close()  


他にもっと、ちゃんとしたやり方がある筈なのだが、初心者なのでよくわからん。
動くようになったから、とりあえずこれでいいや。


2011-01-16 追記
バグトラッカに報告された模様、有難いことです。
http://suzumizaki.blog6.fc2.com/blog-entry-157.html
私も世の中に、貢献できるような人間になりたい。。