File: python_destructor_exception_runme.py

package info (click to toggle)
renderdoc 1.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 79,584 kB
  • sloc: cpp: 491,671; ansic: 285,823; python: 12,617; java: 11,345; cs: 7,181; makefile: 6,703; yacc: 5,682; ruby: 4,648; perl: 3,461; php: 2,119; sh: 2,068; lisp: 1,835; tcl: 1,068; ml: 747; xml: 137
file content (66 lines) | stat: -rw-r--r-- 2,000 bytes parent folder | download | duplicates (6)
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()