2012-12-31 17:17:57 -05:00
|
|
|
import nilmdb
|
|
|
|
from nilmdb.utils.printf import *
|
|
|
|
|
|
|
|
import nose
|
|
|
|
from nose.tools import *
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
|
2013-01-05 15:00:34 -05:00
|
|
|
from testutil.helpers import *
|
2012-12-31 17:17:57 -05:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import cStringIO
|
2013-01-05 15:05:42 -05:00
|
|
|
import gc
|
2012-12-31 17:17:57 -05:00
|
|
|
|
2013-01-18 16:55:51 -05:00
|
|
|
import inspect
|
|
|
|
|
2012-12-31 17:17:57 -05:00
|
|
|
err = cStringIO.StringIO()
|
|
|
|
|
|
|
|
@nilmdb.utils.must_close(errorfile = err)
|
|
|
|
class Foo:
|
2013-01-18 16:55:51 -05:00
|
|
|
def __init__(self, arg):
|
|
|
|
fprintf(err, "Init %s\n", arg)
|
2012-12-31 17:17:57 -05:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
fprintf(err, "Deleting\n")
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
fprintf(err, "Closing\n")
|
|
|
|
|
2013-01-05 15:05:42 -05:00
|
|
|
@nilmdb.utils.must_close(errorfile = err, wrap_verify = True)
|
2013-01-02 00:00:05 -05:00
|
|
|
class Bar:
|
2013-01-05 15:05:42 -05:00
|
|
|
def __init__(self):
|
|
|
|
fprintf(err, "Init\n")
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
fprintf(err, "Deleting\n")
|
|
|
|
|
2013-03-01 16:30:22 -05:00
|
|
|
@classmethod
|
|
|
|
def baz(self):
|
|
|
|
fprintf(err, "Baz\n")
|
|
|
|
|
2013-01-05 15:05:42 -05:00
|
|
|
def close(self):
|
|
|
|
fprintf(err, "Closing\n")
|
|
|
|
|
2013-01-18 16:55:51 -05:00
|
|
|
def blah(self, arg):
|
|
|
|
fprintf(err, "Blah %s\n", arg)
|
2013-01-05 15:05:42 -05:00
|
|
|
|
|
|
|
@nilmdb.utils.must_close(errorfile = err)
|
|
|
|
class Baz:
|
2013-01-02 00:00:05 -05:00
|
|
|
pass
|
|
|
|
|
2012-12-31 17:17:57 -05:00
|
|
|
class TestMustClose(object):
|
|
|
|
def test(self):
|
|
|
|
|
|
|
|
# Note: this test might fail if the Python interpreter doesn't
|
|
|
|
# garbage collect the object (and call its __del__ function)
|
|
|
|
# right after a "del x".
|
|
|
|
|
2013-01-05 15:05:42 -05:00
|
|
|
# Trigger error
|
|
|
|
err.truncate()
|
2013-01-18 16:55:51 -05:00
|
|
|
x = Foo("hi")
|
|
|
|
# Verify that the arg spec was maintained
|
2013-01-18 17:20:48 -05:00
|
|
|
eq_(inspect.getargspec(x.__init__),
|
|
|
|
inspect.ArgSpec(args = ['self', 'arg'],
|
|
|
|
varargs = None, keywords = None, defaults = None))
|
2012-12-31 17:17:57 -05:00
|
|
|
del x
|
2013-01-05 15:05:42 -05:00
|
|
|
gc.collect()
|
2012-12-31 17:17:57 -05:00
|
|
|
eq_(err.getvalue(),
|
2013-01-18 16:55:51 -05:00
|
|
|
"Init hi\n"
|
2012-12-31 17:17:57 -05:00
|
|
|
"error: Foo.close() wasn't called!\n"
|
|
|
|
"Deleting\n")
|
|
|
|
|
2013-01-05 15:05:42 -05:00
|
|
|
# No error
|
2012-12-31 17:17:57 -05:00
|
|
|
err.truncate(0)
|
2013-01-18 16:55:51 -05:00
|
|
|
y = Foo("bye")
|
2012-12-31 17:17:57 -05:00
|
|
|
y.close()
|
|
|
|
del y
|
2013-01-05 15:05:42 -05:00
|
|
|
gc.collect()
|
2012-12-31 17:17:57 -05:00
|
|
|
eq_(err.getvalue(),
|
2013-01-18 16:55:51 -05:00
|
|
|
"Init bye\n"
|
2012-12-31 17:17:57 -05:00
|
|
|
"Closing\n"
|
|
|
|
"Deleting\n")
|
2013-01-02 00:00:05 -05:00
|
|
|
|
2013-01-18 17:20:48 -05:00
|
|
|
# Verify function calls when wrap_verify is True
|
2013-01-02 00:00:05 -05:00
|
|
|
err.truncate(0)
|
|
|
|
z = Bar()
|
2013-01-18 17:20:48 -05:00
|
|
|
eq_(inspect.getargspec(z.blah),
|
|
|
|
inspect.ArgSpec(args = ['self', 'arg'],
|
|
|
|
varargs = None, keywords = None, defaults = None))
|
2013-01-18 16:55:51 -05:00
|
|
|
z.blah("boo")
|
2013-01-02 00:00:05 -05:00
|
|
|
z.close()
|
2013-01-18 17:20:48 -05:00
|
|
|
with assert_raises(AssertionError) as e:
|
|
|
|
z.blah("hello")
|
|
|
|
in_("called <function blah at 0x", str(e.exception))
|
|
|
|
in_("> after close", str(e.exception))
|
2013-01-05 15:05:42 -05:00
|
|
|
# Since the most recent assertion references 'z',
|
|
|
|
# we need to raise another assertion here so that
|
|
|
|
# 'z' will get properly deleted.
|
|
|
|
with assert_raises(AssertionError):
|
|
|
|
raise AssertionError()
|
2013-01-02 00:00:05 -05:00
|
|
|
del z
|
2013-01-05 15:05:42 -05:00
|
|
|
gc.collect()
|
|
|
|
eq_(err.getvalue(),
|
|
|
|
"Init\n"
|
2013-01-18 16:55:51 -05:00
|
|
|
"Blah boo\n"
|
2013-01-05 15:05:42 -05:00
|
|
|
"Closing\n"
|
|
|
|
"Deleting\n")
|
|
|
|
|
|
|
|
# Class with missing methods
|
|
|
|
err.truncate(0)
|
|
|
|
w = Baz()
|
|
|
|
w.close()
|
|
|
|
del w
|
2013-01-02 00:00:05 -05:00
|
|
|
eq_(err.getvalue(), "")
|
2013-01-05 15:05:42 -05:00
|
|
|
|