File: nonecheck.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 (192 lines) | stat: -rw-r--r-- 4,589 bytes parent folder | download | duplicates (10)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
__doc__ = u"""
Tests accessing attributes of extension type variables
set to None
"""

cimport cython

cdef class MyClass:
    cdef int a, b
    def __init__(self, a, b):
        self.a = a
        self.b = b

@cython.nonecheck(True)
def getattr_(MyClass var):
    """
    >>> obj = MyClass(2, 3)
    >>> getattr_(obj)
    2
    >>> getattr_(None)
    Traceback (most recent call last):
    AttributeError: 'NoneType' object has no attribute 'a'
    >>> setattr_(obj)
    >>> getattr_(obj)
    10
    """
    print var.a

@cython.nonecheck(True)
def setattr_(MyClass var):
    """
    >>> obj = MyClass(2, 3)
    >>> setattr_(obj)
    >>> setattr_(None)
    Traceback (most recent call last):
    AttributeError: 'NoneType' object has no attribute 'a'
    """
    var.a = 10

@cython.nonecheck(True)
def getattr_nogil(MyClass var):
    """
    >>> getattr_nogil(None)
    Traceback (most recent call last):
    AttributeError: 'NoneType' object has no attribute 'a'
    """
    with nogil:
        var.a

@cython.nonecheck(True)
def setattr_nogil(MyClass var):
    """
    >>> setattr_nogil(None)
    Traceback (most recent call last):
    AttributeError: 'NoneType' object has no attribute 'a'
    """
    with nogil:
        var.a = 1

def some():
    return MyClass(4, 5)

@cython.nonecheck(True)
def checking(MyClass var):
    """
    >>> obj = MyClass(2, 3)
    >>> checking(obj)
    2
    2
    >>> checking(None)
    var is None
    """
    state = (var is None)
    if not state:
        print var.a
    if var is not None:
        print var.a
    else:
        print u"var is None"

@cython.nonecheck(True)
def check_and_assign(MyClass var):
    """
    >>> obj = MyClass(2, 3)
    >>> check_and_assign(obj)
    Traceback (most recent call last):
    AttributeError: 'NoneType' object has no attribute 'a'
    """
    if var is not None:
        print var.a
        var = None
        print var.a

@cython.nonecheck(True)
def check_buffer_get(object[int] buf):
    """
    >>> check_buffer_get(None)
    Traceback (most recent call last):
    TypeError: 'NoneType' object is not subscriptable
    """
    return buf[0]

@cython.nonecheck(True)
def check_buffer_set(object[int] buf):
    """
    >>> check_buffer_set(None)
    Traceback (most recent call last):
    TypeError: 'NoneType' object is not subscriptable
    """
    buf[0] = 1

@cython.nonecheck(True)
def test_memslice_get(double[:] buf):
    """
    >>> test_memslice_get(None)
    Traceback (most recent call last):
    TypeError: Cannot index None memoryview slice
    """
    return buf[0]

@cython.nonecheck(True)
def test_memslice_set(double[:] buf):
    """
    >>> test_memslice_set(None)
    Traceback (most recent call last):
    TypeError: Cannot index None memoryview slice
    """
    buf[0] = 1.0

@cython.nonecheck(True)
def test_memslice_copy(double[:] buf):
    """
    >>> test_memslice_copy(None)
    Traceback (most recent call last):
    AttributeError: Cannot access 'copy' attribute of None memoryview slice
    """
    cdef double[:] copy = buf.copy()

@cython.nonecheck(True)
def test_memslice_transpose(double[:] buf):
    """
    >>> test_memslice_transpose(None)
    Traceback (most recent call last):
    AttributeError: Cannot transpose None memoryview slice
    """
    cdef double[:] T = buf.T

@cython.nonecheck(True)
def test_memslice_shape(double[:] buf):
    """
    >>> test_memslice_shape(None)
    Traceback (most recent call last):
    AttributeError: Cannot access 'shape' attribute of None memoryview slice
    """
    cdef Py_ssize_t extent = buf.shape[0]

@cython.nonecheck(True)
def test_memslice_slice(double[:] buf):
    """
    >>> test_memslice_slice(None)
    Traceback (most recent call last):
    TypeError: Cannot slice None memoryview slice
    """
    cdef double[:] sliced = buf[1:]

@cython.nonecheck(True)
def test_memslice_slice2(double[:] buf):
    """
    Should this raise an error? It may not slice at all.
    >>> test_memslice_slice(None)
    Traceback (most recent call last):
    TypeError: Cannot slice None memoryview slice
    """
    cdef double[:] sliced = buf[:]

@cython.nonecheck(True)
def test_memslice_slice_assign(double[:] buf):
    """
    >>> test_memslice_slice_assign(None)
    Traceback (most recent call last):
    TypeError: Cannot assign to None memoryview slice
    """
    buf[...] = 2

@cython.nonecheck(True)
def test_memslice_slice_assign2(double[:] buf):
    """
    >>> test_memslice_slice_assign2(None)
    Traceback (most recent call last):
    TypeError: Cannot slice None memoryview slice
    """
    buf[:] = buf[::-1]