aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/pysidetest/enum_test.py
blob: 83283453043661810af1143c2843e017022799a4 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import os
import sys
import unittest

from pathlib import Path
sys.path.append(os.fspath(Path(__file__).resolve().parents[1]))
from init_paths import init_test_paths
init_test_paths(True)

from PySide6.QtCore import Qt
from testbinding import Enum1, TestObjectWithoutNamespace

import dis


class ListConnectionTest(unittest.TestCase):

    def testEnumVisibility(self):
        self.assertEqual(Enum1.Option1, 1)
        self.assertEqual(Enum1.Option2, 2)
        self.assertEqual(TestObjectWithoutNamespace.Enum2.Option3, 3)
        self.assertEqual(TestObjectWithoutNamespace.Enum2.Option4, 4)

    def testFlagComparisonOperators(self):  # PYSIDE-1696, compare to self
        f1 = Qt.AlignHCenter | Qt.AlignBottom
        f2 = Qt.AlignHCenter | Qt.AlignBottom
        self.assertTrue(f1 == f1)
        self.assertTrue(f1 <= f1)
        self.assertTrue(f1 >= f1)
        self.assertFalse(f1 != f1)
        self.assertFalse(f1 < f1)
        self.assertFalse(f1 > f1)

        self.assertTrue(f1 == f2)
        self.assertTrue(f1 <= f2)
        self.assertTrue(f1 >= f2)
        self.assertFalse(f1 != f2)
        self.assertFalse(f1 < f2)
        self.assertFalse(f1 > f2)

        self.assertTrue(Qt.AlignHCenter < Qt.AlignBottom)
        self.assertFalse(Qt.AlignHCenter > Qt.AlignBottom)
        self.assertFalse(Qt.AlignBottom < Qt.AlignHCenter)
        self.assertTrue(Qt.AlignBottom > Qt.AlignHCenter)


# PYSIDE-1735: We are testing that opcodes do what they are supposed to do.
#              This is needed in the PyEnum forgiveness mode where we need
#              to introspect the code if an Enum was called with no args.
class InvestigateOpcodesTest(unittest.TestCase):

    def probe_function1(self):
        x = Qt.Alignment

    def probe_function2(self):
        x = Qt.Alignment()

    @staticmethod
    def read_code(func, **kw):
        return list(instr[:3] for instr in dis.Bytecode(func, **kw))

    @staticmethod
    def get_sizes(func, **kw):
        ops = list((instr.opname, instr.offset) for instr in dis.Bytecode(func, **kw))
        res = []
        for idx in range(1, len(ops)):
            res.append((ops[idx - 1][0], ops[idx][1] - ops[idx - 1][1]))
        return sorted(res, key=lambda x: (x[1], x[0]))

    _sin = sys.implementation.name

    @unittest.skipIf(hasattr(sys.flags, "nogil"), f"{_sin} has different opcodes")
    def testByteCode(self):
        import dis
        # opname, opcode, arg
        result_1 = [('LOAD_GLOBAL', 116, 0),
                    ('LOAD_ATTR',   106, 1),
                    ('STORE_FAST',  125, 1),
                    ('LOAD_CONST',  100, 0),
                    ('RETURN_VALUE', 83, None)]

        result_2 = [('LOAD_GLOBAL', 116, 0),
                    ('LOAD_METHOD', 160, 1),
                    ('CALL_METHOD', 161, 0),
                    ('STORE_FAST',  125, 1),
                    ('LOAD_CONST',  100, 0),
                    ('RETURN_VALUE', 83, None)]

        if sys.version_info[:2] <= (3, 6):

            result_2 = [('LOAD_GLOBAL',   116, 0),
                        ('LOAD_ATTR',     106, 1),
                        ('CALL_FUNCTION', 131, 0),
                        ('STORE_FAST',    125, 1),
                        ('LOAD_CONST',    100, 0),
                        ('RETURN_VALUE',   83, None)]

        if sys.version_info[:2] == (3, 11):
            # Note: Python 3.11 is a bit more complex because it can optimize itself.
            # Opcodes are a bit different, and a hidden second code object is used.
            # We investigate this a bit, because we want to be warned when things change.
            QUICKENING_WARMUP_DELAY = 8

            result_1 = [('RESUME',      151, 0),
                        ('LOAD_GLOBAL', 116, 0),
                        ('LOAD_ATTR',   106, 1),
                        ('STORE_FAST',  125, 1),
                        ('LOAD_CONST',  100, 0),
                        ('RETURN_VALUE', 83, None)]

            result_2 = [('RESUME',      151, 0),
                        ('LOAD_GLOBAL', 116, 1),
                        ('LOAD_ATTR',   106, 1),
                        ('PRECALL',     166, 0),
                        ('CALL',        171, 0),
                        ('STORE_FAST',  125, 1),
                        ('LOAD_CONST',  100, 0),
                        ('RETURN_VALUE', 83, None)]

            sizes_2 = [('LOAD_CONST',   2),
                       ('RESUME',       2),
                       ('STORE_FAST',   2),
                       ('PRECALL',      4),
                       ('CALL',        10),
                       ('LOAD_ATTR',   10),
                       ('LOAD_GLOBAL', 12)]

            self.assertEqual(self.read_code(self.probe_function2, adaptive=True), result_2)
            self.assertEqual(self.get_sizes(self.probe_function2, adaptive=True), sizes_2)

            @staticmethod
            def code_quicken(f, times):
                # running the code triggers acceleration after some runs.
                for _ in range(times):
                    f()

            code_quicken(self.probe_function2, QUICKENING_WARMUP_DELAY - 1)
            self.assertEqual(self.read_code(self.probe_function2, adaptive=True), result_2)
            self.assertEqual(self.get_sizes(self.probe_function2, adaptive=True), sizes_2)

            result_3 = [('RESUME_QUICK',       150, 0),
                        ('LOAD_GLOBAL_MODULE',  55, 1),
                        ('LOAD_ATTR_ADAPTIVE',  39, 1),
                        ('PRECALL_ADAPTIVE',    64, 0),
                        ('CALL_ADAPTIVE',       22, 0),
                        ('STORE_FAST',         125, 1),
                        ('LOAD_CONST',         100, 0),
                        ('RETURN_VALUE',        83, None)]

            sizes_3 = [('LOAD_CONST',          2),
                       ('RESUME_QUICK',        2),
                       ('STORE_FAST',          2),
                       ('PRECALL_ADAPTIVE',    4),
                       ('CALL_ADAPTIVE',      10),
                       ('LOAD_ATTR_ADAPTIVE', 10),
                       ('LOAD_GLOBAL_MODULE', 12)]

            code_quicken(self.probe_function2, 1)
            self.assertEqual(self.read_code(self.probe_function2, adaptive=True), result_3)
            self.assertEqual(self.get_sizes(self.probe_function2, adaptive=True), sizes_3)

        if sys.version_info[:2] >= (3, 12):

            result_1 = [('RESUME', 151, 0),
                        ('LOAD_GLOBAL', 116, 0),
                        ('LOAD_ATTR', 106, 2),
                        ('STORE_FAST', 125, 1),
                        ('RETURN_CONST', 121, 0)]

            result_2 = [('RESUME', 151, 0),
                        ('LOAD_GLOBAL', 116, 1),
                        ('LOAD_ATTR', 106, 2),
                        ('CALL', 171, 0),
                        ('STORE_FAST', 125, 1),
                        ('RETURN_CONST', 121, 0)]

        self.assertEqual(self.read_code(self.probe_function1), result_1)
        self.assertEqual(self.read_code(self.probe_function2), result_2)


if __name__ == '__main__':
    unittest.main()