You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

49 lines
989 B

  1. import nilmdb
  2. from nilmdb.utils.printf import *
  3. import nose
  4. from nose.tools import *
  5. from nose.tools import assert_raises
  6. from test_helpers import *
  7. import sys
  8. import cStringIO
  9. err = cStringIO.StringIO()
  10. @nilmdb.utils.must_close(errorfile = err)
  11. class Foo:
  12. def __init__(self):
  13. fprintf(err, "Init\n")
  14. def __del__(self):
  15. fprintf(err, "Deleting\n")
  16. def close(self):
  17. fprintf(err, "Closing\n")
  18. class TestMustClose(object):
  19. def test(self):
  20. # Note: this test might fail if the Python interpreter doesn't
  21. # garbage collect the object (and call its __del__ function)
  22. # right after a "del x".
  23. x = Foo()
  24. del x
  25. eq_(err.getvalue(),
  26. "Init\n"
  27. "error: Foo.close() wasn't called!\n"
  28. "Deleting\n")
  29. err.truncate(0)
  30. y = Foo()
  31. y.close()
  32. del y
  33. eq_(err.getvalue(),
  34. "Init\n"
  35. "Closing\n"
  36. "Deleting\n")