Removing a persistent local utility

By Roel Bruggink | On Jul 29, 2009
Removing a utility without getting attribute errors.

What is the problem?

Trying to remove a local persistent utility using unregisterUtility results in errors like "AttributeError: type object 'IFavoriteStorage' has no attribute '__iro__' ". And above all: no one seems to know how to fix it "properly". Comments are more than welcome!

Solution

There are two parts to the solution: there is the definition of the utility and the utility itself.

1. Unregister the utility (remove defition):

portal.getSiteManager().unregisterUtility(utility, IFavoriteStorage)

2. Delete the object (remove utility itself):

del my_utility

Example

So add or edit Extensions/Install.py and add something like this:

from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import log 
from zope.component import getUtility
from fourdigits.favorites.interfaces import IFavoriteStorage
import transaction

def uninstall(portal, reinstall=False):
    # Remove utilities when really uninstalled
    if not reinstall:
        my_utility = getUtility(IMyFirstUtility)
        portal.getSiteManager().unregisterUtility(my_utility, IFavoriteStorage)
        del my_utility
        transaction.commit()
        log("Uninstalled fourdigits.favorites")
	
    return "Ran all uninstall steps."