|
|
@@ -9,6 +9,7 @@ from testutil.helpers import * |
|
|
|
|
|
|
|
import sys |
|
|
|
import cStringIO |
|
|
|
import gc |
|
|
|
|
|
|
|
err = cStringIO.StringIO() |
|
|
|
|
|
|
@@ -23,8 +24,22 @@ class Foo: |
|
|
|
def close(self): |
|
|
|
fprintf(err, "Closing\n") |
|
|
|
|
|
|
|
@nilmdb.utils.must_close(errorfile = err) |
|
|
|
@nilmdb.utils.must_close(errorfile = err, wrap_verify = True) |
|
|
|
class Bar: |
|
|
|
def __init__(self): |
|
|
|
fprintf(err, "Init\n") |
|
|
|
|
|
|
|
def __del__(self): |
|
|
|
fprintf(err, "Deleting\n") |
|
|
|
|
|
|
|
def close(self): |
|
|
|
fprintf(err, "Closing\n") |
|
|
|
|
|
|
|
def blah(self): |
|
|
|
fprintf(err, "Blah\n") |
|
|
|
|
|
|
|
@nilmdb.utils.must_close(errorfile = err) |
|
|
|
class Baz: |
|
|
|
pass |
|
|
|
|
|
|
|
class TestMustClose(object): |
|
|
@@ -34,26 +49,51 @@ class TestMustClose(object): |
|
|
|
# garbage collect the object (and call its __del__ function) |
|
|
|
# right after a "del x". |
|
|
|
|
|
|
|
# Trigger error |
|
|
|
err.truncate() |
|
|
|
x = Foo() |
|
|
|
del x |
|
|
|
gc.collect() |
|
|
|
eq_(err.getvalue(), |
|
|
|
"Init\n" |
|
|
|
"error: Foo.close() wasn't called!\n" |
|
|
|
"Deleting\n") |
|
|
|
|
|
|
|
# No error |
|
|
|
err.truncate(0) |
|
|
|
|
|
|
|
y = Foo() |
|
|
|
y.close() |
|
|
|
del y |
|
|
|
gc.collect() |
|
|
|
eq_(err.getvalue(), |
|
|
|
"Init\n" |
|
|
|
"Closing\n" |
|
|
|
"Deleting\n") |
|
|
|
|
|
|
|
# Verify errors |
|
|
|
err.truncate(0) |
|
|
|
|
|
|
|
z = Bar() |
|
|
|
z.blah() |
|
|
|
z.close() |
|
|
|
with assert_raises(AssertionError): |
|
|
|
z.blah() |
|
|
|
# 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() |
|
|
|
del z |
|
|
|
gc.collect() |
|
|
|
eq_(err.getvalue(), |
|
|
|
"Init\n" |
|
|
|
"Blah\n" |
|
|
|
"Closing\n" |
|
|
|
"Deleting\n") |
|
|
|
|
|
|
|
# Class with missing methods |
|
|
|
err.truncate(0) |
|
|
|
w = Baz() |
|
|
|
w.close() |
|
|
|
del w |
|
|
|
eq_(err.getvalue(), "") |
|
|
|
|