[Zope-Coders] ZCTextIndex too sensitive
Casey Duncan
casey@zope.com
Thu, 12 Jun 2003 13:59:07 -0400
On Thursday 12 June 2003 12:53 pm, Sidnei da Silva wrote:
> Howdy folks,
>=20
> Im trying to finish NZO alpha, and Im having lots of trouble with
> ZCTextIndex being too sensitive to errors. Heres what's happening:
[snip]
> Line 157 of ZCTextIndex is:=20
>=20
> ----------------------------------------------------------------------
> if callable(text):
> text =3D text()
> ----------------------------------------------------------------------
>=20
> So, how come 'text' is callable but doesnt have __call__? Weird.=20
It must be trying to index an ExtensionClass instance.=20
callable(ExtensionClass instance) always returns true 8^(
> I
> think there is some code on other indexes that deal with this, but I
> havent looked around yet (a grep for safe_callable may do it). Looking
> at the PluggableIndexes/TextIndex/TextIndex.py, I can see that it does
> a more safer (from my pov) approach, which is putting a try/except
> around the call and catching AttributeError and KeyError.
>=20
> Now my question: How do we fix this?=20
>=20
> a. Putting a try/except
> b. Using 'safe_callable'
> c. ???
I might suggest using safe_callable, except it would add a dependency to=20
DocumentTemplate, which feels very wrong to me. The try/except is also wr=
ong=20
because you don't know if the exception was the result of calling the thi=
ng=20
(in which case it should propagate) or a result of it not being callable=20
(which usually raises TypeError AFAIK).
The right answer might be to add an abstracted function that can be impor=
ted=20
from ZCatalog or PlugInIndexes.common. Something like:
def dataToIndex(obj, name):
...get attribute name from obj...
...find out if it should be called and call it...
return data
Unfortunately it will be tricky to do this in a way that is compatible wi=
th=20
Python2.1 and 2.2 (a requirement), works with extension classes (ditto), =
does=20
not mask exceptions and generally does not make everyone queasy (optional=
=20
;^). On the bright side, all indexes could use this one function making t=
hing=20
potentially more consistent.
> PS: May I merge my other patch to KeywordIndexes with the
> modifications suggested by Casey[1]?
ok by me ;^) Just so long as the tests pass.
-Casey