Re: How to fix this code
"John P. Rouillard" <[email protected]>
| Newsgroups | gmane.comp.bug-tracking.roundup.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi Thomas: In message <[email protected]>, Thomas Arendsen Hein writes: >* 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): ... >> 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): ... >> 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)) > Done and ... >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)) Done. Checkin: 5a9159ad773f Thanks for the help. -- -- rouilj John Rouillard =========================================================================== My employers don't acknowledge my existence much less my opinions.