[Spambayes] Corpus.Message.__getattr__ can't be correct can it?

Tim Stone - Four Stones Expressions tim at fourstonesExpressions.com
Fri Jan 17 14:03:44 EST 2003

1/17/2003 1:29:15 PM, Just van Rossum <just at letterror.com> wrote:

>Richie Hindle wrote:
>> > In Corpus.Message, __getattr__ is defined as
>> > 
>> >     def __getattr__(self, attributeName):
>> >         '''On-demand loading of the message text.'''
>> > 
>> >         if attributeName in ('hdrtxt', 'payload'):
>> >             self.load()
>> >         return getattr(self, attributeName)
>> > 
>> > This has to be an infloop, right?
>> It should probably be:
>>     return self.__dict__[attributeName]
>> so that it raises an exception when something goes wrong. [ ... ]
>Neither makes sense (unless I'm missing some magic context): __getattr__
>is only called if the attr isn't found the normal way, which means it's
>for sure not in self.__dict__.

It's not an infloop if self.load() sets the attributes hdrtxt and payload, AND 
attributeName is in ('hdrtxt', 'payload').  Obviously both of these conditions 
are not being met.  self.load() ends up calling self.setSubstance, which does 
nothing if the message substance cannot be split into header text and payload.  
This is an error.  setSubstance should look like:

    def setSubstance(self, sub):
        '''set this message substance'''

        bodyRE = re.compile(r"\r?\n(\r?\n)(.*)", re.DOTALL+re.MULTILINE)
        bmatch = bodyRE.search(sub)
        if bmatch:
            self.payload = bmatch.group(2)
            self.hdrtxt = sub[:bmatch.start(2)]
            self.payload = sub  #we don't have valid headers, only payload
            self.hdrtxt = ''

and __getattr__ should look like:

    def __getattr__(self, attributeName):
        '''On-demand loading of the message text.'''

        if attributeName in ('hdrtxt', 'payload'):
            # will recurse if load does not set hdrtxt or payload
            return getattr(self, attributeName)
            # we should never get here.  if we do, some attribute is missing
            # and we don't know what to do about it
            raise AttributeError, attributeName

Unfortunately, I don't have cvs access to fix this at the moment.
>Spambayes mailing list
>Spambayes at python.org

c'est moi - TimS

More information about the Spambayes mailing list