Fix bug updating WeakKeyDictionary during iteration

This is an attempt to fix the bug reported in the mailing list thread "BUG: RuntimeError: dictionary changed size during iteration".