File: owned_arg_refs.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 (46 lines) | stat: -rw-r--r-- 1,180 bytes parent folder | download | duplicates (11)
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

cdef class Owner:
    cdef object x

cdef call_me_with_owner(Owner owner, x):
    owner.x = "def" # overwrite external reference
    return x        # crashes if x is not owned by function or caller

def test_ext_type_attr():
    """
    >>> test_ext_type_attr()
    'abc5'
    """
    owner = Owner()
    owner.x = ''.join("abc%d" % 5) # non-interned object
    return call_me_with_owner(owner, owner.x)


cdef void call_me_without_gil(Owner owner, x) with gil:
    owner.x = "def" # overwrite external reference
    print x         # crashes if x is not owned by function or caller

def test_ext_type_attr_nogil():
    """
    >>> test_ext_type_attr_nogil()
    abc5
    """
    owner = Owner()
    owner.x = ''.join("abc%d" % 5) # non-interned object
    with nogil:
        call_me_without_gil(owner, owner.x)


# the following isn't dangerous as long as index access uses temps

cdef call_me_with_list(list l, x):
    l[:] = [(1,2), (3,4)] # overwrite external reference
    return x              # crashes if x is not owned by function or caller

def test_index():
    """
    >>> test_index()
    [3, 4]
    """
    l = [[1,2],[3,4]]
    return call_me_with_list(l, l[1])