|
|
@@ -11,12 +11,14 @@ import sys |
|
|
|
import cStringIO |
|
|
|
import gc |
|
|
|
|
|
|
|
import inspect |
|
|
|
|
|
|
|
err = cStringIO.StringIO() |
|
|
|
|
|
|
|
@nilmdb.utils.must_close(errorfile = err) |
|
|
|
class Foo: |
|
|
|
def __init__(self): |
|
|
|
fprintf(err, "Init\n") |
|
|
|
def __init__(self, arg): |
|
|
|
fprintf(err, "Init %s\n", arg) |
|
|
|
|
|
|
|
def __del__(self): |
|
|
|
fprintf(err, "Deleting\n") |
|
|
@@ -35,8 +37,8 @@ class Bar: |
|
|
|
def close(self): |
|
|
|
fprintf(err, "Closing\n") |
|
|
|
|
|
|
|
def blah(self): |
|
|
|
fprintf(err, "Blah\n") |
|
|
|
def blah(self, arg): |
|
|
|
fprintf(err, "Blah %s\n", arg) |
|
|
|
|
|
|
|
@nilmdb.utils.must_close(errorfile = err) |
|
|
|
class Baz: |
|
|
@@ -51,29 +53,35 @@ class TestMustClose(object): |
|
|
|
|
|
|
|
# Trigger error |
|
|
|
err.truncate() |
|
|
|
x = Foo() |
|
|
|
x = Foo("hi") |
|
|
|
# Verify that the arg spec was maintained |
|
|
|
(args, varargs, keywords, defaults) = inspect.getargspec(x.__init__) |
|
|
|
eq_(args, ['self', 'arg']) |
|
|
|
eq_(None, varargs) |
|
|
|
eq_(None, keywords) |
|
|
|
eq_(None, defaults) |
|
|
|
del x |
|
|
|
gc.collect() |
|
|
|
eq_(err.getvalue(), |
|
|
|
"Init\n" |
|
|
|
"Init hi\n" |
|
|
|
"error: Foo.close() wasn't called!\n" |
|
|
|
"Deleting\n") |
|
|
|
|
|
|
|
# No error |
|
|
|
err.truncate(0) |
|
|
|
y = Foo() |
|
|
|
y = Foo("bye") |
|
|
|
y.close() |
|
|
|
del y |
|
|
|
gc.collect() |
|
|
|
eq_(err.getvalue(), |
|
|
|
"Init\n" |
|
|
|
"Init bye\n" |
|
|
|
"Closing\n" |
|
|
|
"Deleting\n") |
|
|
|
|
|
|
|
# Verify errors |
|
|
|
err.truncate(0) |
|
|
|
z = Bar() |
|
|
|
z.blah() |
|
|
|
z.blah("boo") |
|
|
|
z.close() |
|
|
|
with assert_raises(AssertionError): |
|
|
|
z.blah() |
|
|
@@ -86,7 +94,7 @@ class TestMustClose(object): |
|
|
|
gc.collect() |
|
|
|
eq_(err.getvalue(), |
|
|
|
"Init\n" |
|
|
|
"Blah\n" |
|
|
|
"Blah boo\n" |
|
|
|
"Closing\n" |
|
|
|
"Deleting\n") |
|
|
|
|
|
|
|