Joel Hedlund <yoh...@ifm.liu.se> writes: > I'm having a very hard time explaining why this snippet *sometimes* > raises KeyError: > > snippet: >> print type(self.pool) >> for frag in self.pool.keys(): >> if frag is fragment_info: >> print "the fragment_info *is* in the pool", hash(frag), >> hash(fragment_info), hash(frag) == hash(fragment_info), frag == >> fragment_info, frag in self.pool, frag in self.pool.keys() >> try: >> renderer_index = self.pool.pop(fragment_info) >> except KeyError: >> print "Glorious KeyError!" >> for frag in self.pool.keys(): >> if frag is fragment_info: >> print "the fragment_info *is* in the pool", hash(frag), >> hash(fragment_info), hash(frag) == hash(fragment_info), frag == >> fragment_info, frag in self.pool, frag in self.pool.keys() >> raise > > > output: >> <type 'dict'> >> the fragment_info *is* in the pool 987212075 987212075 True True False True >> Glorious KeyError! >> the fragment_info *is* in the pool 987212075 987212075 True True False True >> Traceback (most recent call last): >> File "/home/yohell/workspace/missy/core/gui.py", line 92, in process_job >> renderer_index = self.pool.pop(fragment_info) >> KeyError: <core.gui.FragmentInfo object at 0x8fc906c> > > This snippet is part of a much larger gtk program, and the problem > only from time to time, predominantly when the cpu is under heavy load > and this method gets called a lot. If I didn't know better I'd say > it's a bug in python's dict implementation, but I do know better, so I > know it's far more likely that I've made a mistake somewhere. I'll be > damned if I can figure out what and where though. I've reproduced this > bug (?) with python-2.5.2 on Ubuntu 8.10 and python-2.5.1 on WinXP. > > I would very much like an explanation to this that does not involve > threads, because I haven't made any that I'm aware of. I can't even > understand how this could happen. How do I even debug this? > > Please help, I feel like I've taken crazy pills here! > /Joel Hedlund
It could happen if two object x and y are such that hash(x) != hash(y) but x == y, breaking the requirement on hash values. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list