cpp_template_subclasses.pyx 3.74 KB
Newer Older
1 2
# mode: run
# tag: cpp, werror
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

from cython.operator import dereference as deref
from libcpp.pair cimport pair
from libcpp.vector cimport vector

cdef extern from "cpp_template_subclasses_helper.h":
    cdef cppclass Base:
        char* name()

    cdef cppclass A[A1](Base):
        A1 funcA(A1)

    cdef cppclass B[B1, B2](A[B2]):
        pair[B1, B2] funcB(B1, B2)

    cdef cppclass C[C1](B[long, C1]):
        C1 funcC(C1)

    cdef cppclass D[D1](C[pair[D1, D1]]):
        pass

    cdef cppclass E(D[double]):
        pass

def testA(x):
    """
    >>> testA(10)
    10.0
    """
    cdef Base *base
    cdef A[double] *a = NULL
    try:
        a = new A[double]()
        base = a
        assert base.name() == b"A", base.name()
        return a.funcA(x)
    finally:
        del a

def testB(x, y):
    """
    >>> testB(1, 2)
    >>> testB(1, 1.5)
    """
    cdef Base *base
    cdef A[double] *a
    cdef B[long, double] *b = NULL
    try:
        base = a = b = new B[long, double]()
        assert base.name() == b"B", base.name()
        assert a.funcA(y) == y
        assert <object>b.funcB(x, y) == (x, y)
    finally:
        del b

def testC(x, y):
    """
    >>> testC(37, [1, 37])
    >>> testC(25, [1, 5, 25])
    >>> testC(105, [1, 3, 5, 7, 15, 21, 35, 105])
    """
    cdef Base *base
    cdef A[vector[long]] *a
    cdef B[long, vector[long]] *b
    cdef C[vector[long]] *c = NULL
    try:
        base = a = b = c = new C[vector[long]]()
        assert base.name() == b"C", base.name()
        assert <object>a.funcA(y) == y
        assert <object>b.funcB(x, y) == (x, y)
        assert <object>c.funcC(y) == y
    finally:
        del c

def testD(x, y):
    """
    >>> testD(1, 1.0)
    >>> testD(2, 0.5)
    >>> testD(4, 0.25)
    """
    cdef Base *base
    cdef A[pair[double, double]] *a
    cdef B[long, pair[double, double]] *b
    cdef C[pair[double, double]] *c
    cdef D[double] *d = NULL
    try:
        base = a = b = c = d = new D[double]()
        assert base.name() == b"D", base.name()
        assert <object>a.funcA((y, y)) == (y, y)
        assert <object>b.funcB(x, (y, y + 1)) == (x, (y, y + 1))
        assert <object>c.funcC((y, y)) == (y, y)
    finally:
        del d

def testE(x, y):
    """
    >>> testD(1, 1.0)
    >>> testD(2, 0.5)
    >>> testD(4, 0.25)
    """
    cdef Base *base
    cdef A[pair[double, double]] *a
    cdef B[long, pair[double, double]] *b
    cdef C[pair[double, double]] *c
    cdef D[double] *d
    cdef E *e = NULL
    try:
        base = a = b = c = d = e = new E()
        assert base.name() == b"E", base.name()
        assert <object>a.funcA((y, y)) == (y, y)
        assert <object>b.funcB(x, (y, y + 1)) == (x, (y, y + 1))
        assert <object>c.funcC((y, y)) == (y, y)
    finally:
        del e

118 119 120 121 122 123 124 125 126 127

cdef public pair[int, double] public_return_pair(a, b) except *:
  return pair[int, double](a, b)

def test_GH1599(a, b):
  """
  >>> test_GH1599(1, 2)
  (1, 2.0)
  """
  return public_return_pair(a, b)
128 129 130 131 132 133 134 135 136 137 138 139 140


# Related to GH Issue #1852.

cdef cppclass Callback[T]:#(UntypedCallback):
    pass

cdef cppclass MyClass[O]:
    void Invoke(Callback[O]*)

cdef cppclass MySubclass[T](MyClass[T]):
    void Invoke(Callback[T]* callback):
      pass
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


cdef cppclass Getter[T]:
    T get(bint fire) except *:
        if fire:
            raise RuntimeError
        else:
           raise NotImplementedError

cdef cppclass GetInt(Getter[int]):
    int get(bint fire) except *:
        if fire:
            raise RuntimeError
        else:
            return 389

def test_subclass_exception_values(bint fire):
    """
    >>> test_subclass_exception_values(False)
    389
    >>> test_subclass_exception_values(True)
    Traceback (most recent call last):
    ...
    RuntimeError
    """
    cdef GetInt getter
    return getter.get(fire)