Commit 1310d1eb authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2332 from gabrieldemarmiesse/test_cdef_classes_4

Added tests for cdef_classes.rst. Part 4.
parents 36dfbbca 01f48e9e
# cython: nonecheck=True
# ^^^ Turns on nonecheck globally
import cython
cdef class MyClass:
pass
# Turn off nonecheck locally for the function
@cython.nonecheck(False)
def func():
cdef MyClass obj = None
try:
# Turn nonecheck on again for a block
with cython.nonecheck(True):
print obj.myfunc() # Raises exception
except AttributeError:
pass
print(obj.myfunc()) # Hope for a crash!
...@@ -95,26 +95,9 @@ Some notes on our new implementation of ``evaluate``: ...@@ -95,26 +95,9 @@ Some notes on our new implementation of ``evaluate``:
There is a *compiler directive* ``nonecheck`` which turns on checks There is a *compiler directive* ``nonecheck`` which turns on checks
for this, at the cost of decreased speed. Here's how compiler directives for this, at the cost of decreased speed. Here's how compiler directives
are used to dynamically switch on or off ``nonecheck``:: are used to dynamically switch on or off ``nonecheck``:
#cython: nonecheck=True
# ^^^ Turns on nonecheck globally
import cython
# Turn off nonecheck locally for the function
@cython.nonecheck(False)
def func():
cdef MyClass obj = None
try:
# Turn nonecheck on again for a block
with cython.nonecheck(True):
print obj.myfunc() # Raises exception
except AttributeError:
pass
print obj.myfunc() # Hope for a crash!
.. literalinclude:: ../../examples/tutorial/cdef_classes/nonecheck.pyx
Attributes in cdef classes behave differently from attributes in regular classes: Attributes in cdef classes behave differently from attributes in regular classes:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment