/Lib/test/crashers/loosing_mro_ref.py

http://unladen-swallow.googlecode.com/ · Python · 35 lines · 27 code · 1 blank · 7 comment · 0 complexity · a578779515ab3178baaff89e5269806e MD5 · raw file

  1. """
  2. There is a way to put keys of any type in a type's dictionary.
  3. I think this allows various kinds of crashes, but so far I have only
  4. found a convoluted attack of _PyType_Lookup(), which uses the mro of the
  5. type without holding a strong reference to it. Probably works with
  6. super.__getattribute__() too, which uses the same kind of code.
  7. """
  8. class MyKey(object):
  9. def __hash__(self):
  10. return hash('mykey')
  11. def __cmp__(self, other):
  12. # the following line decrefs the previous X.__mro__
  13. X.__bases__ = (Base2,)
  14. # trash all tuples of length 3, to make sure that the items of
  15. # the previous X.__mro__ are really garbage
  16. z = []
  17. for i in range(1000):
  18. z.append((i, None, None))
  19. return -1
  20. class Base(object):
  21. mykey = 'from Base'
  22. class Base2(object):
  23. mykey = 'from Base2'
  24. # you can't add a non-string key to X.__dict__, but it can be
  25. # there from the beginning :-)
  26. X = type('X', (Base,), {MyKey(): 5})
  27. print X.mykey
  28. # I get a segfault, or a slightly wrong assertion error in a debug build.