File: cpp_temp_assignment.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, 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 (98 lines) | stat: -rw-r--r-- 2,902 bytes parent folder | download
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
# tag: cpp,cpp11
# mode: compile 
# tag: no-cpp-locals
# TODO cpp_locals works fine with the standard library that comes with gcc11
# but not with gcc8. Therefore disable the test for now

cdef extern from *:
    """
    class NoAssignIterator {
        public:
            explicit NoAssignIterator(int pos) : pos_(pos) {}
            NoAssignIterator(NoAssignIterator&) = delete;
            NoAssignIterator(NoAssignIterator&&) {}
            NoAssignIterator& operator=(NoAssignIterator&) = delete;
            NoAssignIterator& operator=(NoAssignIterator&&) { return *this; }
            // Default constructor of temp variable is needed by Cython
            // as of 3.0a6.
            NoAssignIterator() : pos_(0) {}
            int operator*() {
                return pos_;
            }
            NoAssignIterator operator++() {
                return NoAssignIterator(pos_ + 1);
            }
            int operator!=(NoAssignIterator other) {
                return pos_ != other.pos_;
            }
            int pos_;
    };
    class NoAssign {
        public:
            NoAssign() {}
            NoAssign(NoAssign&) = delete;
            NoAssign(NoAssign&&) {}
            NoAssign& operator=(NoAssign&) = delete;
            NoAssign& operator=(NoAssign&&) { return *this; }
            void func() {}
            NoAssignIterator begin() {
                return NoAssignIterator(0);
            }
            NoAssignIterator end() {
                return NoAssignIterator(2);
            }
    };

    NoAssign get_NoAssign_Py() {
        return NoAssign();
    }
    NoAssign get_NoAssign_Cpp() {
        return NoAssign();
    }

    """
    cdef cppclass NoAssignIterator:
        int operator*()
        NoAssignIterator operator++()
        int operator!=(NoAssignIterator)

    cdef cppclass NoAssign:
        void func()
        NoAssignIterator begin()
        NoAssignIterator end()

    # might raise Python exception (thus needs a temp)
    NoAssign get_NoAssign_Py() except *
    # might raise C++ exception (thus needs a temp)
    NoAssign get_NoAssign_Cpp() except +

cdef internal_cpp_func(NoAssign arg):
    pass

def test_call_to_function():
    # will fail to compile if move constructors aren't used
    internal_cpp_func(get_NoAssign_Py())
    internal_cpp_func(get_NoAssign_Cpp())

def test_assignment_to_name():
    # will fail if move constructors aren't used
    cdef NoAssign value
    value = get_NoAssign_Py()
    value = get_NoAssign_Cpp()

def test_assignment_to_scope():
    cdef NoAssign value
    value = get_NoAssign_Py()
    value = get_NoAssign_Cpp()
    def inner():
        value.func()

cdef class AssignToClassAttr:
    cdef NoAssign attr
    def __init__(self):
        self.attr = get_NoAssign_Py()
        self.attr = get_NoAssign_Cpp()

def test_generator_cpp_iterator_as_temp():
    for i in get_NoAssign_Py():
        yield i