File: inherited_final_method.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: 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 (75 lines) | stat: -rw-r--r-- 1,313 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
# mode: run
# tag: exttype, final

cimport cython

cdef class TopBase:
    cdef method(self):
        return None


cdef class BaseClass(TopBase):
    """
    >>> obj = BaseClass()
    >>> obj.call_base()
    'base'
    """
    cdef base(self):
        return '@base'

    cdef method(self):
        return 'base'

    def call_base(self):
        return self.method()


@cython.final
cdef class ChildClass(BaseClass):
    """
    >>> obj = ChildClass()
    >>> obj.call_base()
    'child'
    >>> obj.call_child()
    'child'
    """
    cdef child(self):
        return '@child'

    cdef method(self):
        return 'child'

    def call_child(self):
        # original bug: this requires a proper cast for self
        return self.method()


def test_BaseClass():
    """
    >>> test_BaseClass()
    '@base'
    'base'
    """
    cdef BaseClass obj = BaseClass()
    print(repr(obj.base()))
    print(repr(obj.method()))


def test_ChildClass():
    """
    >>> test_ChildClass()
    '@base'
    '@child'
    'child'
    '@base'
    '@child'
    'child'
    """
    cdef ChildClass obj = ChildClass()
    print(repr(obj.base()))
    print(repr(obj.child()))
    print(repr(obj.method()))
    cdef BaseClass bobj = obj
    print(repr(obj.base()))
    print(repr(obj.child()))
    print(repr(bobj.method()))