[Python-Dev] another dict crasher

Michael Hudson mwh@python.net
04 Jun 2001 20:01:10 +0100


"Tim Peters" <tim.one@home.com> writes:

> >> However, then an adversarial user can construct a case that never
> >> terminates.
> 
> > I seem to have done this - it was odd, though - it only loops when I
> > bump the dict to fairly enormous preportions for reasons I don't
> > really (want to) understand.
> 
> Pass it on.  I deliberately "started over" via a recursive call instead of a
> goto so that an offending program would eventually die with a stack fault
> instead of just running forever.  So if you're seeing something run forever,
> it may be a different problem.

I left it running overnight, and it terminated!  (with a KeyError).  I
can't say I really understand what's going on, but I'm in Exam Hell at
the moment (for the last time!  Yippee!), so don't have any spare
cycles to think about it hard.

Anyway, this is what I was running:

dict = {}

# let's force dict to malloc its table
for i in range(1,10000):
    dict[i] = i

hashcode = 0

class Machiavelli2:
    def __eq__(self, other):
        global hashcode
        d2 = dict.copy()
        dict.clear()
        hashcode += 1
        for k,v in d2.items():
            dict[k] = v
        return 1
    def __hash__(self):
        return hashcode

dict[Machiavelli2()] = Machiavelli2()

print dict[Machiavelli2()]

If you thought my last test case was contrived, I look forward to you
finding adjectives for this one...

Cheers,
M.

-- 
  (ps: don't feed the lawyers: they just lose their fear of humans)
                                         -- Peter Wood, comp.lang.lisp