|
Subject: Re: On __findattr__, __getattr__ and __getattribute__ Newsgroups: gmane.comp.lang.jython.devel Date: 2008-06-27 23:05:27 GMT (1 year, 1 week, 15 hours and 57 minutes ago)
>
> On a related/unrelated note skimming the code of PyDictionary it seems
> that the semantics of the python level __getitem__ (dict__getitem__) and
> __finditem__ for it got out of sync, __finditem__ will not respect
> __missing__. I may be confused though.
>
ah, this is amended by PyDefaultDict.java (and the normal Derived
behavior for the subclasses) which has a __finditem__
that calls __getitem__, of course this one is breaking the interface
because of the KeyError vs null issue.
I suppose a good thing to know would be how many people have written
their own subclasses of PyObject or PyXXX
overridding __finditem__/__getitem__. I suppose they would have to
rework them anyway with the next Jython version.
Because given the various constraints and the current tangle one
possible approach and compromise would be to simply
make both __finditem__ and __getitem__ final, and base them on a new
interface, some other method
that would be allowed to either return null or raise KeyError/IndexError
whatever is most convenient, efficient.
Sketching:
public final __finditem__(key) {
try {
return do_find_item(key) // just making a name up
} catch (PyException exc) {
if (Py.matchException(exc,Py.LookupError))
return null;
throw exc;
}
}
public final __getitem__(key) {
res = do_find_item(key)
if (res == null) {
lookupError(key) // overridable
}
return res
}
so one would have the option to have a null returning do_find_item and
override lookupError possibly,
or simply raise KeyError/IndexError directly if that's more convenient.
The exception catching in __finditem__ would probably have no influence
in the common cases/fast path.
With this approach I think the code in *Derived could be much simpler,
and PyDictionary do_find_item would
still simply be table.get(key).
May be missing something though, is kind of bed time around here now.
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
|
|
|