summaryrefslogtreecommitdiff
path: root/Examples/test-suite/python/python_nondynamic_runme.py
blob: 524f4d1c4ce1310d674aa640f1bfc2eb98432e9b (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import python_nondynamic

def is_python_modern():
    """Return True if SWIG is generating Python code using -modern. Works only if %python_nondynamic has been used."""
    return hasattr(python_nondynamic, "_swig_setattr_nondynamic_class_variable")

def debug_print(s):
    show_debug = False
    if show_debug:
        print(s)

aa = python_nondynamic.A()

aa.a = 1
aa.b = 2

# Check values are really set
if python_nondynamic.retrieve_A_a(aa) != 1: raise RuntimeError("a not set correctly")
if python_nondynamic.retrieve_A_b(aa) != 2: raise RuntimeError("b not set correctly")

try:
    aa.c = 2
    raise RuntimeError("A is not static")
except AttributeError as e:
    debug_print(e)
    pass

class PurePythonClass(object):
    def __init__(self):
        self.variables = dict()
    def __set__(self, name, value):
        self.variables[name] = value
    pass

class B(python_nondynamic.A):
    c = 4
    cc = PurePythonClass()
    cc.nnn = "new attrib"

    def __init__(self):
        python_nondynamic.A.__init__(self)
        pass
    pass

bb = B()
bb.a = 4
bb.b = 5
# Check values are really set
if python_nondynamic.retrieve_A_a(bb) != 4: raise RuntimeError("a not set correctly")
if python_nondynamic.retrieve_A_b(bb) != 5: raise RuntimeError("b not set correctly")

try:
    bb.c = 3
    raise RuntimeError("B.c class variable messes up nondynamic-ness of B bb.c={} B.c={}".format(bb.c, B.c))
except AttributeError as e:
    debug_print(e)
    pass

try:
    bb.d = 2
    raise RuntimeError("B is not static")
except AttributeError as e:
    debug_print(e)
    pass

cc = python_nondynamic.C()
cc.d = 3

# An inconsistency between builtin and (non-builtin/modern).
# Class variables cannot be set on builtin types, like other Python builtins, eg list.classvar=111 fails
if python_nondynamic.is_python_builtin():
    try:
        python_nondynamic.C.classvar = 111
        raise RuntimeError("C should not allow static variables to be added when using builtin")
    except AttributeError as e:
        debug_print(e)
        pass
else:
    python_nondynamic.C.classvar = 111

if is_python_modern() and not python_nondynamic.is_python_builtin():
    # Not working with builtin or non-modern :(
    try:
        B.a = 10
        raise RuntimeError("B should not allow adding a class variable by setting it as an instance variable")
    except AttributeError as e:
        debug_print(e)
        pass

    try:
        python_nondynamic.A.a = 10
        raise RuntimeError("A should not allow adding a class variable by setting it as an instance variable")
    except AttributeError as e:
        debug_print(e)
        pass

if not python_nondynamic.is_python_builtin():
    try:
        bb.cc = 3
        raise RuntimeError("B.cc class variable messes up nondynamic-ness of B bb.cc={} B.cc={}".format(bb.cc, B.cc))
    except AttributeError as e:
        debug_print(e)
        pass