[Zope-Coders] Keyword Indexes misbehaving?

Casey Duncan casey@zope.com
Mon, 3 Mar 2003 10:47:00 -0500


Yup, this is a bug. It currently does an evil hasattr(newKeywords,=20
'capitalize') to sniff for strings, which are then wrapped in a tuple. Ot=
her=20
types aren't tested.

However it looks like the old code would fail silently in index_object si=
nce=20
it tries to do a for loop over the keywords there, but there its wrapped =
in a=20
try: except TypeError:. I have a feeling that exception block is a bit of=
 a=20
Rube Goldberg special designed to "just make the damn thing work".

So, although this would fix the exception, it unfortuately changes the=20
arguably odd semantics which seem to be:

1. If its a string, wrap it in a tuple and index it.
2. If its a list or tuple or other sequence, just index it.
3. If its something else, fail silently.

I fear that the code you suggested breaks number 2, since it rewraps "oth=
er=20
sequences" into a tuple. I don't know how many apps depend on this behavi=
or.

So it would probably be best to wrap the for loop in a try: except: and e=
ither=20
return an empty list if it can't be iterated or wrap the value in tuple a=
nd=20
return it. The latter subtly changes semantics, but its more consistent I=
=20
think. For 2.6.2 its probably better to return the empty list in that cas=
e.=20
Maybe we "fix" that for 2.7.

Thoughts anyone?

-Casey

On Sunday 02 March 2003 10:29 am, Sidnei da Silva wrote:
> Apparently, Casey's fix for bug #828 caused a new bug :(
> =20
>=20
http://cvs.zope.org/Zope/lib/python/Products/PluginIndexes/KeywordIndex/K=
eywordIndex.py.diff?r1=3D1.11&r2=3D1.11.6.1
>=20
> The CMFCollector has a field that is being indexed as a keyword index, =
but=20
the
> method being indexed returns a single integer. It fails on the 'for' ad=
ded=20
by
> Casey with a 'loop over non-sequence'.=20
>=20
> Im not sure if the KeywordIndex is supposed to index only string keywor=
ds,=20
in
> which case CMFCollector needs to be fixed, or if its supposed to index =
lists=20
of
> any value, in which case i suggest changing the method to:
>=20
>     def _get_object_keywords(self,obj):
> =09newKeywords =3D getattr(obj, self.id, ())
>         if callable(newKeywords):
>             newKeywords =3D newKeywords()
> =09if type(newKeywords) not in [TupleType, ListType]:
>             newKeywords =3D (newKeywords, )
> =09# Uniqueify keywords                                                =
    =09    =20
>   unique =3D {}
> =09for k in newKeywords:
> =09        unique[k] =3D None
> =09newKeywords =3D unique.keys()
>         return newKeywords
>=20
> []'s
> --=20
> Sidnei da Silva (dreamcatcher) <sidnei@x3ng.com.br>
> X3ng Web Technology <http://www.x3ng.com.br>
>=20
>=20
>=20
>=20
> _______________________________________________
> Zope-Coders mailing list
> Zope-Coders@zope.org
> http://mail.zope.org/mailman/listinfo/zope-coders
>=20