2011-11-14 33 views
1

を持っていない私は、次のようなエラーがスローされたPythonスクリプトを継承していますはAttributeError:「int型オブジェクトが属性「エンコード」

root  : ERROR Unexpected exception encountered in application 'ImagesForWeb' 
Traceback (most recent call last): 
    File "build/bdist.linux-i686/egg/columbiancommon/scaffolding/consoleapp.py", line 169, in run_safe 
    self.run(**kwargs) 
    File "/var/scripts/ImagesForWeb/imagesforweb.py", line 102, in run 
    gallery = columbiancommon.EllingtonPhotoGallery(configobj = self.cmgr) 
    File "build/bdist.linux-i686/egg/columbiancommon/ellington/photogallery.py", line 51, in __init__ 
    self.Reload() 
    File "build/bdist.linux-i686/egg/columbiancommon/ellington/photogallery.py", line 128, in Reload 
    self.SetStatus(self.status) 
    File "build/bdist.linux-i686/egg/columbiancommon/ellington/photogallery.py", line 68, in SetStatus 
    self.SetControl("status", [self.status.encode('utf-8', 'replace')]) 
AttributeError: 'int' object has no attribute 'encode' 

私は、Pythonにかなり新たなんだとかなりのデバッグを開発していませんまだこの問題を解決する方法を知っているスキル。

ここで、上記のエラーにrefrencedさphotogallery.pyからコードスニペットです:これはscaffolding.py内にあるSetControl方法である

def SetStatus(self, status): 
    """ 
    Sets the publication status of this photo gallery. 

    Expects to be set to an integer constant, shortcuts include:: 

     EllingtonPhotoGallery.LIVE 
     EllingtonPhotoGallery.DRAFT 
     EllingtonPhotoGallery.DELETED 
     EllingtonPhotoGallery.UNREVIEWED 

    """ 
    if(not isinstance(status, int)): 
     raise EllingtonMechanizeException("Unexpected status value. Please use a status constant.") 
    self.status = status 
    self.SetControl("status", [self.status.encode('utf-8', 'replace')]) 

は、私はそれがラインだと信じ

def SetControl(self, control_name, control_value_unclean): 
    """ 
    Raw access to the mechanize method of setting controls to specific values. 

    **WARNING** Do not use this unless you have a really good reason to do so-- `EllingtonMechanizeScaffolding.SetControlToValue` 
    and `EllingtonMechanizeScaffolding.SetControlToValueSafe` are much more elegant solutions. 

    :Parameters: 
     - `control_name`: The name of the control you're trying to assign a value to. 
     - `control_value_unclean`: The value to assign to said control. Either a boolean value, 

    """ 
    self.browser[control_name] = control_value_unclean 
    return True 

ことエラーを投げているself.SetControl("status", [self.status.encode('utf-8', 'replace')]) と言っていますが、なぜエラーが発生しているのかわかりません。コードは6ヶ月前に継承されて以来、私の最後には変更されていません。

ご協力いただければ幸いです。

+2

EllingtonとDjangoはPython 2.xのみであるため、これはPython 3.xではありません。 –

+1

このコードは6ヶ月間は変更されていない可能性は非常に低いようです。 'self.status = status'と' self.status'が異常な動作をするような奇妙なプロパティや '__ {g、s} etattr__'ハックを保存すると、ここに表示されているコードは例外をスローするか' .encode'を試みます'int'のインスタンスであるオブジェクトです。 – delnan

+0

そして、self.statusがunicodeオブジェクトであっても、self.status.encode( 'utf-8'、 'replace')の 'replace'は非常に悪い 'unicode'オブジェクトを隠蔽するので悪いです。 1つは対になっていないサロゲートを含む。 –

答えて

7

最初intのインスタンスであることをstatusを主張して、あなたはそれがunicode方法だから、それはありませんencode方法を、使用してみてください。整数を文字列に変換する場合は、unicode(self.status)を使用します。それで、encodeを使うことができますが、そうしない方がいいでしょう。

+0

チップをありがとう、私はそれを試してみましょう。また、なぜあなたは「そうすべきではないでしょう」と言うのですか? – bigmike7801

+0

@ bigmike7801:SetControlメソッドを見て、それが何を期待しているか見てきましたか? –

+0

上記の質問にSetControlのコードを追加しましたが、その問題を引き起こしている可能性のあるものは表示されません。 – bigmike7801

0

repr()機能を使用します。この関数は、unicode、utf、nullおよびintを扱うことができます。この関数の良い部分は、Unicode(例えばencode('utf-8')のような)やアスキー文字列を文字列に変換するときに、この関数が値を失わないことです。また、この関数は表現のサイズ制限を提供するので、オブジェクトに柔軟性があります。

関連する問題