Cython has moved to github.
cython-devel
view tests/run/nonecheck.pyx @ 1622:4f0327bdebc9
test for temp allocation bug in call
| author | Robert Bradshaw <robertwb@math.washington.edu> |
|---|---|
| date | Sat Jan 17 01:25:34 2009 -0800 (3 years ago) |
| parents | 421a3edf1abf |
| children | 82d312a9f1fc |
line source
1 __doc__ = u"""
2 Tests accessing attributes of extension type variables
3 set to None
5 >>> obj = MyClass(2, 3)
6 >>> getattr_(obj)
7 2
8 >>> getattr_(None)
9 Traceback (most recent call last):
10 ...
11 AttributeError: 'NoneType' object has no attribute 'a'
13 >>> setattr_(obj)
14 >>> getattr_(obj)
15 10
16 >>> setattr_(None)
17 Traceback (most recent call last):
18 ...
19 AttributeError: 'NoneType' object has no attribute 'a'
23 >>> obj = MyClass(2, 3)
24 >>> checking(obj)
25 2
26 2
27 >>> checking(None)
28 var is None
30 >>> check_and_assign(obj)
31 Traceback (most recent call last):
32 ...
33 AttributeError: 'NoneType' object has no attribute 'a'
35 >>> check_buffer_get(None)
36 Traceback (most recent call last):
37 ...
38 TypeError: 'NoneType' object is unsubscriptable
40 >>> check_buffer_set(None)
41 Traceback (most recent call last):
42 ...
43 TypeError: 'NoneType' object is unsubscriptable
45 """
47 cimport cython
49 cdef class MyClass:
50 cdef int a, b
51 def __init__(self, a, b):
52 self.a = a
53 self.b = b
55 @cython.nonecheck(True)
56 def getattr_(MyClass var):
57 print var.a
59 @cython.nonecheck(True)
60 def setattr_(MyClass var):
61 var.a = 10
63 def some():
64 return MyClass(4, 5)
66 @cython.nonecheck(True)
67 def checking(MyClass var):
68 state = (var is None)
69 if not state:
70 print var.a
71 if var is not None:
72 print var.a
73 else:
74 print u"var is None"
76 @cython.nonecheck(True)
77 def check_and_assign(MyClass var):
78 if var is not None:
79 print var.a
80 var = None
81 print var.a
83 @cython.nonecheck(True)
84 def check_buffer_get(object[int] buf):
85 return buf[0]
87 @cython.nonecheck(True)
88 def check_buffer_set(object[int] buf):
89 buf[0] = 1
