[Zope-dev] Keyword Indexes causing Keyerrors

Itamar Shtull-Trauring itamar@maxnm.com
Thu, 21 Dec 2000 12:27:35 +0200


Here's what was causing my KeyErrors, and how I solved it, and why I think
KeywordIndexes broke. I have a keyword index on the "categories" property of
my CatalogAware object. "categories" is a list.

    def manage_removeItems(self, ids=[]):
        """ Remove listed items from this category """
        for id in ids:
            ob = getattr(self.Items, id)
            categories = ob.categories # <-- this is the problem
            categories.remove(self.uid)
            ob.manage_changeProperties(categories=categories)
            ob.reindex_object()

I solved the problem by switching the problem line to say "categories =
ob.categories[:]".

What does this mean? I was mutating the same list that was attached to the
object. Doing this caused the Keyword Index to break. I assume the reason
then is that the index somewhere stored a reference to the "categories"
property, not a copy. This means that when I mutated ob.categories, I was
also mutating the contents of the index, causing it to get screwed up. The
solution would be that keyword indexes store a *copy* of the property they
are indexing, not a reference to it.

If I'm not mistaken, that means changing line 42 in UnKeywordIndex.py (2.2.4
version) from:

	unindex[i] = kws

to:
	unindex[i] = kws[:]

Although use of deepcopy may be a better idea?

-- 
Email: itamar(at)shtull-trauring.org
Web:   http://itamarst.org