summaryrefslogtreecommitdiff
path: root/Examples/test-suite/python/python_destructor_exception_runme.py
blob: ee71ab33b967ed6009f64a39074c77d7f87cc134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import python_destructor_exception
from StringIO import StringIO
import sys

def error_function():
    python_destructor_exception.ClassWithThrowingDestructor().GetBlah()

def runtest():
    attributeErrorOccurred = False
    try:
        error_function()
    except AttributeError, e:
        attributeErrorOccurred = True
    return attributeErrorOccurred

def test1():
    stderr_saved = sys.stderr
    buffer = StringIO()
    attributeErrorOccurred = False
    try:
        # Suppress stderr while making this call to suppress the output shown by PyErr_WriteUnraisable
        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

# See issue #559, #560, #573 - In Python 3.5, test2() call to the generator 'gen' was
# resulting in the following (not for -builtin where there is no call to SWIG_Python_CallFunctor
# as SwigPyObject_dealloc is not used):
#
# StopIteration
#
# During handling of the above exception, another exception occurred:
# ...
# SystemError: <built-in function delete_VectorInt> returned a result with an error set

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")

# These two tests are different are two different ways to recreate essentially the same problem
# reported by Python 3.5 that an exception was already set when destroying a wrapped object
test1()
test2()