File: cpp_operators.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (383 lines) | stat: -rw-r--r-- 11,498 bytes parent folder | download | duplicates (4)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# mode: run
# tag: cpp, werror

from __future__ import division

from cython cimport typeof

cimport cython.operator
from cython.operator cimport typeid, dereference as deref

from libc.string cimport const_char
from libcpp cimport bool


cdef out(s, result_type=None):
    print '%s [%s]' % (s.decode('ascii'), result_type)


cdef iout(int s, result_type=None):
    print '%s [%s]' % (s, result_type)


cdef extern from "cpp_operators_helper.h" nogil:
    cdef cppclass TestOps:

        const_char* operator+() except +
        const_char* operator-() except +
        const_char* operator*() except +
        const_char* operator~() except +
        const_char* operator!() except +

        # FIXME: using 'except +' here leads to wrong calls ???
        const_char* operator++()
        const_char* operator--()
        const_char* operator++(int)
        const_char* operator--(int)

        const_char* operator+(int) except +
        const_char* operator+(int,const TestOps&) except +
        const_char* operator-(int) except +
        const_char* operator-(int,const TestOps&) except +
        const_char* operator*(int) except +
        # deliberately omitted operator* to test case where only defined outside class
        const_char* operator/(int) except +
        const_char* operator/(int,const TestOps&) except +
        const_char* operator%(int) except +
        const_char* operator%(int,const TestOps&) except +

        const_char* operator|(int) except +
        const_char* operator|(int,const TestOps&) except +
        const_char* operator&(int) except +
        const_char* operator&(int,const TestOps&) except +
        const_char* operator^(int) except +
        const_char* operator^(int,const TestOps&) except +
        const_char* operator,(int) except +
        const_char* operator,(int,const TestOps&) except +

        const_char* operator<<(int) except +
        const_char* operator<<(int,const TestOps&) except +
        const_char* operator>>(int) except +
        const_char* operator>>(int,const TestOps&) except +

        # FIXME: using 'except +' here leads to invalid C++ code ???
        const_char* operator==(int)
        const_char* operator!=(int)
        const_char* operator>=(int)
        const_char* operator<=(int)
        const_char* operator>(int)
        const_char* operator<(int)

        const_char* operator[](int) except +
        const_char* operator()(int) except +

    # Defining the operator outside the class does work
    # but doesn't help when importing from pxd files
    # (they don't get imported)
    const_char* operator+(float,const TestOps&) except +
    # deliberately omitted operator- to test case where only defined in class
    const_char* operator*(float,const TestOps&) except +
    const_char* operator/(float,const TestOps&) except +
    const_char* operator%(float,const TestOps&) except +

    const_char* operator|(float,const TestOps&) except +
    const_char* operator&(float,const TestOps&) except +
    const_char* operator^(float,const TestOps&) except +
    const_char* operator,(float,const TestOps&) except +

    const_char* operator<<(float,const TestOps&) except +
    const_char* operator>>(float,const TestOps&) except +

    cdef cppclass RefTestOps:

        int& operator+() except +
        int& operator-() except +
        int& operator*() except +
        int& operator~() except +
        int& operator!() except +

        int& operator++() except +
        int& operator--() except +
        int& operator++(int) except +
        int& operator--(int) except +

        int& operator+(int) except +
        int& operator+(int,const TestOps&) except +
        int& operator-(int) except +
        int& operator-(int,const TestOps&) except +
        int& operator*(int) except +
        # deliberately omitted operator* to test case where only defined outside class
        int& operator/(int) except +
        int& operator/(int,const TestOps&) except +
        int& operator%(int) except +
        int& operator%(int,const TestOps&) except +

        int& operator|(int) except +
        int& operator|(int,const TestOps&) except +
        int& operator&(int) except +
        int& operator&(int,const TestOps&) except +
        int& operator^(int) except +
        int& operator^(int,const TestOps&) except +
        int& operator,(int) except +
        int& operator,(int,const TestOps&) except +

        int& operator<<(int) except +
        int& operator<<(int,const TestOps&) except +
        int& operator>>(int) except +
        int& operator>>(int,const TestOps&) except +

        int& operator==(int) except +
        int& operator!=(int) except +
        int& operator>=(int) except +
        int& operator<=(int) except +
        int& operator>(int) except +
        int& operator<(int) except +

        int& operator[](int) except +
        int& operator()(int) except +

    cdef cppclass TruthClass:
        TruthClass()
        TruthClass(bool)
        bool operator bool()
        bool value


cdef cppclass TruthSubClass(TruthClass):
    pass


def test_unops():
    """
    >>> test_unops()
    unary + [const_char *]
    unary - [const_char *]
    unary ~ [const_char *]
    unary * [const_char *]
    unary ! [const_char *]
    """
    cdef TestOps* t = new TestOps()
    out(+t[0], typeof(+t[0]))
    out(-t[0], typeof(-t[0]))
    out(~t[0], typeof(~t[0]))
    x = deref(t[0])
    out(x, typeof(x))
    out(not t[0], typeof(not t[0]))
    del t

def test_incdec():
    """
    >>> test_incdec()
    unary ++ [const_char *]
    unary -- [const_char *]
    post ++ [const_char *]
    post -- [const_char *]
    """
    cdef TestOps* t = new TestOps()
    a = cython.operator.preincrement(t[0])
    out(a, typeof(a))
    b = cython.operator.predecrement(t[0])
    out(b, typeof(b))
    c = cython.operator.postincrement(t[0])
    out(c, typeof(c))
    d = cython.operator.postdecrement(t[0])
    out(d, typeof(d))
    del t

def test_binop():
    """
    >>> test_binop()
    binary + [const_char *]
    binary - [const_char *]
    binary * [const_char *]
    binary / [const_char *]
    binary % [const_char *]
    binary & [const_char *]
    binary | [const_char *]
    binary ^ [const_char *]
    binary << [const_char *]
    binary >> [const_char *]
    binary COMMA [const_char *]
    """
    cdef TestOps* t = new TestOps()
    out(t[0] + 1, typeof(t[0] + 1))
    out(t[0] - 1, typeof(t[0] - 1))
    out(t[0] * 1, typeof(t[0] * 1))
    out(t[0] / 1, typeof(t[0] / 1))
    out(t[0] % 1, typeof(t[0] % 1))

    out(t[0] & 1, typeof(t[0] & 1))
    out(t[0] | 1, typeof(t[0] | 1))
    out(t[0] ^ 1, typeof(t[0] ^ 1))

    out(t[0] << 1, typeof(t[0] << 1))
    out(t[0] >> 1, typeof(t[0] >> 1))

    x = cython.operator.comma(t[0], 1)
    out(x, typeof(x))
    del t

def test_nonmember_binop():
    """
    >>> test_nonmember_binop()
    nonmember binary + [const_char *]
    nonmember binary - [const_char *]
    nonmember binary / [const_char *]
    nonmember binary % [const_char *]
    nonmember binary & [const_char *]
    nonmember binary | [const_char *]
    nonmember binary ^ [const_char *]
    nonmember binary << [const_char *]
    nonmember binary >> [const_char *]
    nonmember binary COMMA [const_char *]
    nonmember binary2 + [const_char *]
    nonmember binary2 * [const_char *]
    nonmember binary2 / [const_char *]
    nonmember binary2 % [const_char *]
    nonmember binary2 & [const_char *]
    nonmember binary2 | [const_char *]
    nonmember binary2 ^ [const_char *]
    nonmember binary2 << [const_char *]
    nonmember binary2 >> [const_char *]
    nonmember binary2 COMMA [const_char *]
    """

    cdef TestOps* t = new TestOps()
    out(1 + t[0], typeof(1 + t[0]))
    out(1 - t[0], typeof(1 - t[0]))
    # * deliberately omitted
    out(1 / t[0], typeof(1 / t[0]))
    out(1 % t[0], typeof(1 % t[0]))
    out(1 & t[0], typeof(1 & t[0]))
    out(1 | t[0], typeof(1 | t[0]))
    out(1 ^ t[0], typeof(1 ^ t[0]))
    out(1 << t[0], typeof(1 << t[0]))
    out(1 >> t[0], typeof(1 >> t[0]))

    x = cython.operator.comma(1, t[0])
    out(x, typeof(x))

    # now test float operators defined outside class
    out(1. + t[0], typeof(1. + t[0]))
    # operator - deliberately omitted
    out(1. * t[0], typeof(1. * t[0]))
    out(1. / t[0], typeof(1. / t[0]))
    out(1. % t[0], typeof(1. % t[0]))
    out(1. & t[0], typeof(1. & t[0]))
    out(1. | t[0], typeof(1. | t[0]))
    out(1. ^ t[0], typeof(1. ^ t[0]))
    out(1. << t[0], typeof(1. << t[0]))
    out(1. >> t[0], typeof(1. >> t[0]))

    # for some reason we need a cdef here - not sure this is quite right
    y = cython.operator.comma(1., t[0])
    out(y, typeof(y))
    del t

def test_cmp():
    """
    >>> test_cmp()
    binary == [const_char *]
    binary != [const_char *]
    binary >= [const_char *]
    binary > [const_char *]
    binary <= [const_char *]
    binary < [const_char *]
    """
    cdef TestOps* t = new TestOps()
    out(t[0] == 1, typeof(t[0] == 1))
    out(t[0] != 1, typeof(t[0] != 1))
    out(t[0] >= 1, typeof(t[0] >= 1))
    out(t[0] > 1, typeof(t[0] > 1))
    out(t[0] <= 1, typeof(t[0] <= 1))
    out(t[0] < 1, typeof(t[0] < 1))
    del t


def test_index_call():
    """
    >>> test_index_call()
    binary [] [const_char *]
    binary () [const_char *]
    """
    cdef TestOps* t = new TestOps()
    out(t[0][100], typeof(t[0][100]))
    out(t[0](100), typeof(t[0](100)))
    del t


def test_index_assignment():
    """
    >>> test_index_assignment()
    0 [int &]
    123 [int [&]]
    """
    cdef RefTestOps* t = new RefTestOps()
    iout(t[0][100], typeof(t[0][100]))
    t[0][99] = 123
    iout(t[0](100), typeof(t[0](100)))
    del t


def test_bool_op():
    """
    >>> test_bool_op()
    """
    cdef TruthClass yes = TruthClass(True)
    cdef TruthClass no = TruthClass(False)
    if yes:
        pass
    else:
        assert False
    if no:
        assert False

def test_bool_cond():
    """
    >>> test_bool_cond()
    """
    assert (TruthClass(False) or TruthClass(False)).value == False
    assert (TruthClass(False) or TruthClass(True)).value == True
    assert (TruthClass(True) or TruthClass(False)).value == True
    assert (TruthClass(True) or TruthClass(True)).value == True

    assert (TruthClass(False) and TruthClass(False)).value == False
    assert (TruthClass(False) and TruthClass(True)).value == False
    assert (TruthClass(True) and TruthClass(False)).value == False
    assert (TruthClass(True) and TruthClass(True)).value == True


ctypedef int* int_ptr

def test_typeid_op():
    """
    >>> test_typeid_op()
    """
    cdef TruthClass* test_1 = new TruthClass()
    cdef TruthSubClass* test_2 = new TruthSubClass()
    cdef TruthClass* test_3 = <TruthClass*> test_2
    cdef TruthClass* test_4 = <TruthClass*> 0

    assert typeid(TruthClass).name()
    assert typeid(test_1).name()
    assert typeid(TruthClass) == typeid(deref(test_1))

    assert typeid(TruthSubClass).name()
    assert typeid(test_2).name()
    assert typeid(TruthSubClass) == typeid(deref(test_2))
    assert typeid(TruthSubClass) == typeid(deref(test_3))
    assert typeid(TruthClass) != typeid(deref(test_3))

    assert typeid(TruthClass).name()
    assert typeid(test_3).name()
    assert typeid(TruthSubClass).name()
    assert typeid(deref(test_2)).name()
    assert typeid(int_ptr).name()

    try:
        typeid(deref(test_4))
        assert False
    except TypeError:
        assert True

    del test_1, test_2