Meadow+Python-modeでPythonを書いている。

問題

漢字を使ったプログラムで、C-c C-c(=バッファ内容を実行)とすると、
UnicodeDecodeErrorが出ていた。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

mes = u'漢字'
print mes
  • sitecustomize.pyでsetdefaultencoding('utf-8')としている。
  • ソースファイルが、SJISの時は問題が出なかったが、utf-8にすると出た。
  • バッファ内容がpythonに渡されるときのencodeが正しくないと思われる。

解決方法

.emacsに追加(あるいは修正)

(defadvice py-execute-region (around my-py-execute-region)
  "back to the original buffer when py-execute-region finished."
  (if (get-buffer "*Python Output*")
      (kill-buffer "*Python Output*")
    nil)
  (let* ((coding-system-for-write buffer-file-coding-system))
    ad-do-it
    )
  (shrink-window-if-larger-than-buffer)
  (other-window -1)
  )
(ad-enable-advice 'py-execute-region 'around 'my-py-execute-region)
(ad-activate 'py-execute-region)

確認状況

  • 上のテストプログラムは、動くようになった。
  • 上のテストプログラムは、SJISEUC-JPに変更しても動いた。
  • C-c C-c以外は、試していない。
  • Meadowでしか試していない。

参考

python-mode on emacs 環境向上計画

  • 上の.emacsのコードは、ここのものの一部を修正したものである。