import python_destructor_exception
import sys
if sys.version_info[0:2] < (3, 0):
import StringIO as io
else:
import io
def error_function():
python_destructor_exception.ClassWithThrowingDestructor().GetBlah()
def runtest():
attributeErrorOccurred = False
try:
error_function()
except AttributeError:
attributeErrorOccurred = True
return attributeErrorOccurred
def test1():
stderr_saved = sys.stderr
buffer = io.StringIO()
attributeErrorOccurred = False
try:
sys.stderr = buffer
attributeErrorOccurred = runtest()
finally:
sys.stderr.flush()
sys.stderr = stderr_saved
if not attributeErrorOccurred:
raise RuntimeError("attributeErrorOccurred failed")
if not buffer.getvalue().count("I am the ClassWithThrowingDestructor dtor doing bad things") >= 1:
raise RuntimeError("ClassWithThrowingDestructor dtor doing bad things failed")
class VectorHolder(object):
def __init__(self, v):
self.v = v
def gen(self):
for e in self.v:
yield e
def addup():
sum = 0
for i in VectorHolder(python_destructor_exception.VectorInt([1, 2, 3])).gen():
sum = sum + i
return sum
def test2():
sum = addup()
if sum != 6:
raise RuntimeError("Sum is incorrect")
test1()
test2()