Google App EngineのTextPropertyの文字コードの扱い

Python覚えたてなうえ、いろいろ試行錯誤中なので間違ってるかもしれません。気軽にコメントください。

  • ちょっと戸惑ったこと。
x.name = db.Text('Shift_JISの文字列',encoding='shift_jis')
x.put()
....
# shift_jisでDBに登録したTextPropertyを読み込むと、
x = ...get()
x.name # <- こいつは内部エンコーディング(例えば# -*- coding: utf-8 -*- なら utf-8)にエンコードしてある。
  • ちょっと変な仕様に思えること。
Hoge.name = db.Text('ほぼShift_JISの文字列...',encoding='shift_jis')

#文字列の内容によってはたまに以下のエラーがでる
# UnicodeDecodeError: 'shift_jis' codec can't decode bytes in position 9-10: illegal multibyte sequence

unicode関数やdecode関数でいうところのerrorsが、db.Textにないのがちょっと変な仕様な気がする。
これで回避した。

str = 'ほぼShift_JISの文字列...'.decode('shift_jis','ignore').encode('shift_jis','ignore')
Hoge.name = db.Text(str, encoding='shift_jis')