Re: How to fix this code

Thomas Arendsen Hein <[email protected]>
Newsgroups gmane.comp.bug-tracking.roundup.devel
Message-ID <[email protected]>
* John P. Rouillard <[email protected]> [20190406 03:24]:
> I managed to crash roundup in this section of code at line 1724 of
> roundup/hyperdb.py:
>
>    def __getattr__(self, name):
>         if name in self.__dict__:
>             return self.__dict__[name]
>         try:
>             return self.cl.get(self.nodeid, name)
>         except KeyError as value:
>             # we trap this but re-raise it as AttributeError - all other
>             # exceptions should pass through untrapped
>             pass
>         # nope, no such attribute
>         raise AttributeError(str(value))
>
> I am getting an:
>
>    UnboundLocalError: local variable 'value' referenced before assignment
>
> when I pass an invalid property/name to the function. My guess is
> value's scope is only in the scope of the exception code and the raise
> is outside the scope.
>
> >From the comments it looks like it should be:
>
>    def __getattr__(self, name):
>         if name in self.__dict__:
>             return self.__dict__[name]
>         try:
>             return self.cl.get(self.nodeid, name)
>         except KeyError as value:
>             # we trap this but re-raise it as AttributeError - all other
>             # exceptions should pass through untrapped
> 	    # nope, no such attribute							   raise AttributeError(str(value))
>         pass
>
> but I am not sure. Ideas?

Same as in __setattr__, the pass is no longer needed, when the
except section contains actual code:

    def __getattr__(self, name):
        if name in self.__dict__:
            return self.__dict__[name]
        try:
            return self.cl.get(self.nodeid, name)
        except KeyError as value:
            # we trap this but re-raise it as AttributeError - all other
            # exceptions should pass through untrapped
            raise AttributeError(str(value))


I suggest adding the same comment in __setattr__:

    def __setattr__(self, name, value):
        try:
            return self.cl.set(self.nodeid, **{name: value})
        except KeyError as value:
            # we trap this but re-raise it as AttributeError - all other
            # exceptions should pass through untrapped
            raise AttributeError(str(value))


Regards,
Thomas

-- 
Thomas Arendsen Hein <[email protected]> - OpenPGP key: 0x5BB3F5195816791A
https://blogs.intevation.de/thomas/         - https://intevation.de/~thomas/
Intevation GmbH, Neuer Graben 17, 49074 Osnabrueck - AG Osnabrueck, HR B 18998
Geschaeftsfuehrer: Frank Koormann, Bernhard Reiter, Dr. Jan-Oliver Wagner
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.