Aug 29, 2008 - IronPython 2.0B4とVisual StudioでPythonスクリプトをデバッグ
下の記事で、2.0B4のipy.exeに-Dオプションを指定すると、Visual Studio 2008(SP1で確認)でPythonのスクリプトをちゃんとデバッグできたと書きましたが、 これは誤りで、2.0A6のときに気づいた問題は改善されていませんでしたorz。
これは何を言っているのかというと、PythonのスクリプトファイルをVisual StudioのIDEに表示して、ブレークポイントを設定したりステップ実行すると、現在の停止位置が何故か1行ずれてしまうという問題が起こります。
IronPython本体を追跡して原因を調査したところ、スクリプトの先頭行に「# coding: エンコード名」を記述すると上記の問題が発生することが分かりました。
と、いうわけで、↓が修正パッチになります。
diff -r -c -P IronPython/Runtime/PythonContext.cs.orig IronPython/Runtime/PythonContext.cs
*** IronPython/Runtime/PythonContext.cs.orig Fri Jul 25 11:38:12 2008
--- IronPython/Runtime/PythonContext.cs Thu Aug 28 08:51:37 2008
***************
*** 13,18 ****
--- 13,20 ----
*
* ***************************************************************************/
+ #define DEBUGGER_WORKAROUND
+
using System;
using System.Collections;
using System.Collections.Generic;
***************
*** 544,549 ****
--- 546,554 ----
if (encoding == null)
throw new IOException("unknown encoding type");
+ #if DEBUGGER_WORKAROUND
+ stream.Seek(start_position, SeekOrigin.Begin);
+ #else
if (!gotEncoding) {
// if we didn't get an encoding seek back to the beginning...
stream.Seek(start_position, SeekOrigin.Begin);
***************
*** 552,557 ****
--- 557,563 ----
// buffering doesn't throw us off)
stream.Seek(bytesRead, SeekOrigin.Begin);
}
+ #endif
// re-read w/ the correct encoding type...
return new StreamReader(stream, encoding);
これでちゃんと停止中の行にハイライトが表示されるようになったので、修正方法は多分これでよいのでしょう。 呼び出し元に復帰してからの処理をデバッグしていないので、本当に正しいのかどうか、半分ぐらいしか自身がありません。