File: memoryview_in_subclasses.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 (58 lines) | stat: -rw-r--r-- 1,111 bytes parent folder | download | duplicates (9)
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
"""
Test for memory leaks when adding more memory view attributes in subtypes.
"""

import gc

from cython.view cimport array


def count_memoryviews():
    gc.collect()
    return sum([1 if 'memoryview' in str(type(o)) else 0
                for o in gc.get_objects()])


def run_test(cls, num_iters):
    orig_count = count_memoryviews()
    def f():
        x = cls(1024)
    for i in range(num_iters):
        f()
    return count_memoryviews() - orig_count


cdef class BaseType:
    """
    >>> run_test(BaseType, 10)
    0
    """
    cdef double[:] buffer

    def __cinit__(self, n):
        self.buffer = array((n,), sizeof(double), 'd')


cdef class Subtype(BaseType):
    """
    >>> run_test(Subtype, 10)
    0
    """
    cdef double[:] buffer2

    def __cinit__(self, n):
        self.buffer2 = array((n,), sizeof(double), 'd')


cdef class SubtypeWithUserDealloc(BaseType):
    """
    >>> run_test(SubtypeWithUserDealloc, 10)
    0
    """
    cdef double[:] buffer2

    def __cinit__(self, n):
        self.buffer2 = array((n,), sizeof(double), 'd')

    def __dealloc__(self):
        pass