diff options
Diffstat (limited to 'docs/examples/userguide/language_basics')
22 files changed, 290 insertions, 111 deletions
diff --git a/docs/examples/userguide/language_basics/casting_python.pxd b/docs/examples/userguide/language_basics/casting_python.pxd new file mode 100644 index 000000000..fa3d46030 --- /dev/null +++ b/docs/examples/userguide/language_basics/casting_python.pxd @@ -0,0 +1,2 @@ +cdef extern from *: + ctypedef Py_ssize_t Py_intptr_t diff --git a/docs/examples/userguide/language_basics/casting_python.py b/docs/examples/userguide/language_basics/casting_python.py new file mode 100644 index 000000000..1c02c461c --- /dev/null +++ b/docs/examples/userguide/language_basics/casting_python.py @@ -0,0 +1,22 @@ +from cython.cimports.cpython.ref import PyObject + +def main(): + + python_string = "foo" + + # Note that the variables below are automatically inferred + # as the correct pointer type that is assigned to them. + # They do not need to be typed explicitly. + + ptr = cython.cast(cython.p_void, python_string) + adress_in_c = cython.cast(Py_intptr_t, ptr) + address_from_void = adress_in_c # address_from_void is a python int + + ptr2 = cython.cast(cython.pointer(PyObject), python_string) + address_in_c2 = cython.cast(Py_intptr_t, ptr2) + address_from_PyObject = address_in_c2 # address_from_PyObject is a python int + + assert address_from_void == address_from_PyObject == id(python_string) + + print(cython.cast(object, ptr)) # Prints "foo" + print(cython.cast(object, ptr2)) # prints "foo" diff --git a/docs/examples/userguide/language_basics/casting_python.pyx b/docs/examples/userguide/language_basics/casting_python.pyx index fe84acde2..4cc819ae3 100644 --- a/docs/examples/userguide/language_basics/casting_python.pyx +++ b/docs/examples/userguide/language_basics/casting_python.pyx @@ -1,19 +1,19 @@ -from cpython.ref cimport PyObject
-
-cdef extern from *:
- ctypedef Py_ssize_t Py_intptr_t
-
-python_string = "foo"
-
-cdef void* ptr = <void*>python_string
-cdef Py_intptr_t adress_in_c = <Py_intptr_t>ptr
-address_from_void = adress_in_c # address_from_void is a python int
-
-cdef PyObject* ptr2 = <PyObject*>python_string
-cdef Py_intptr_t address_in_c2 = <Py_intptr_t>ptr2
-address_from_PyObject = address_in_c2 # address_from_PyObject is a python int
-
-assert address_from_void == address_from_PyObject == id(python_string)
-
-print(<object>ptr) # Prints "foo"
-print(<object>ptr2) # prints "foo"
+from cpython.ref cimport PyObject + +cdef extern from *: + ctypedef Py_ssize_t Py_intptr_t + +python_string = "foo" + +cdef void* ptr = <void*>python_string +cdef Py_intptr_t adress_in_c = <Py_intptr_t>ptr +address_from_void = adress_in_c # address_from_void is a python int + +cdef PyObject* ptr2 = <PyObject*>python_string +cdef Py_intptr_t address_in_c2 = <Py_intptr_t>ptr2 +address_from_PyObject = address_in_c2 # address_from_PyObject is a python int + +assert address_from_void == address_from_PyObject == id(python_string) + +print(<object>ptr) # Prints "foo" +print(<object>ptr2) # prints "foo" diff --git a/docs/examples/userguide/language_basics/cdef_block.pyx b/docs/examples/userguide/language_basics/cdef_block.pyx index 4132aeee1..c0c303029 100644 --- a/docs/examples/userguide/language_basics/cdef_block.pyx +++ b/docs/examples/userguide/language_basics/cdef_block.pyx @@ -1,12 +1,12 @@ -from __future__ import print_function
-
-cdef:
- struct Spam:
- int tons
-
- int i
- float a
- Spam *p
-
- void f(Spam *s):
- print(s.tons, "Tons of spam")
+from __future__ import print_function + +cdef: + struct Spam: + int tons + + int i + float a + Spam *p + + void f(Spam *s) except *: + print(s.tons, "Tons of spam") diff --git a/docs/examples/userguide/language_basics/compile_time.pyx b/docs/examples/userguide/language_basics/compile_time.pyx index fcaf75f29..f302dd241 100644 --- a/docs/examples/userguide/language_basics/compile_time.pyx +++ b/docs/examples/userguide/language_basics/compile_time.pyx @@ -1,9 +1,9 @@ -from __future__ import print_function
-
-DEF FavouriteFood = u"spam"
-DEF ArraySize = 42
-DEF OtherArraySize = 2 * ArraySize + 17
-
-cdef int a1[ArraySize]
-cdef int a2[OtherArraySize]
-print("I like", FavouriteFood)
\ No newline at end of file +from __future__ import print_function + +DEF FavouriteFood = u"spam" +DEF ArraySize = 42 +DEF OtherArraySize = 2 * ArraySize + 17 + +cdef int[ArraySize] a1 +cdef int[OtherArraySize] a2 +print("I like", FavouriteFood) diff --git a/docs/examples/userguide/language_basics/struct_union_enum.pyx b/docs/examples/userguide/language_basics/enum.pyx index ccbc28d42..1b5f5d614 100644 --- a/docs/examples/userguide/language_basics/struct_union_enum.pyx +++ b/docs/examples/userguide/language_basics/enum.pyx @@ -1,16 +1,11 @@ -cdef struct Grail:
- int age
- float volume
-
-cdef union Food:
- char *spam
- float *eggs
-
-cdef enum CheeseType:
- cheddar, edam,
- camembert
-
-cdef enum CheeseState:
- hard = 1
- soft = 2
- runny = 3
+cdef enum CheeseType: + cheddar, edam, + camembert + +cdef enum CheeseState: + hard = 1 + soft = 2 + runny = 3 + +print(CheeseType.cheddar) +print(CheeseState.hard) diff --git a/docs/examples/userguide/language_basics/function_pointer.pyx b/docs/examples/userguide/language_basics/function_pointer.pyx new file mode 100644 index 000000000..b345c62b4 --- /dev/null +++ b/docs/examples/userguide/language_basics/function_pointer.pyx @@ -0,0 +1,8 @@ +cdef int(*ptr_add)(int, int) + +cdef int add(int a, int b): + return a + b + +ptr_add = add + +print(ptr_add(1, 3)) diff --git a/docs/examples/userguide/language_basics/function_pointer_struct.pyx b/docs/examples/userguide/language_basics/function_pointer_struct.pyx new file mode 100644 index 000000000..5ef618961 --- /dev/null +++ b/docs/examples/userguide/language_basics/function_pointer_struct.pyx @@ -0,0 +1,9 @@ +cdef struct Bar: + int sum(int a, int b) + +cdef int add(int a, int b): + return a + b + +cdef Bar bar = Bar(add) + +print(bar.sum(1, 2)) diff --git a/docs/examples/userguide/language_basics/kwargs_1.pyx b/docs/examples/userguide/language_basics/kwargs_1.pyx index e5e18c008..1117c967c 100644 --- a/docs/examples/userguide/language_basics/kwargs_1.pyx +++ b/docs/examples/userguide/language_basics/kwargs_1.pyx @@ -1,6 +1,6 @@ -def f(a, b, *args, c, d = 42, e, **kwds):
- ...
-
-
-# We cannot call f with less verbosity than this.
-foo = f(4, "bar", c=68, e=1.0)
+def f(a, b, *args, c, d = 42, e, **kwds): + ... + + +# We cannot call f with less verbosity than this. +foo = f(4, "bar", c=68, e=1.0) diff --git a/docs/examples/userguide/language_basics/kwargs_2.pyx b/docs/examples/userguide/language_basics/kwargs_2.pyx index a2c639ea6..902df694c 100644 --- a/docs/examples/userguide/language_basics/kwargs_2.pyx +++ b/docs/examples/userguide/language_basics/kwargs_2.pyx @@ -1,5 +1,5 @@ -def g(a, b, *, c, d):
- ...
-
-# We cannot call g with less verbosity than this.
-foo = g(4.0, "something", c=68, d="other")
+def g(a, b, *, c, d): + ... + +# We cannot call g with less verbosity than this. +foo = g(4.0, "something", c=68, d="other") diff --git a/docs/examples/userguide/language_basics/open_file.py b/docs/examples/userguide/language_basics/open_file.py new file mode 100644 index 000000000..ad3ae0374 --- /dev/null +++ b/docs/examples/userguide/language_basics/open_file.py @@ -0,0 +1,19 @@ +from cython.cimports.libc.stdio import FILE, fopen +from cython.cimports.libc.stdlib import malloc, free +from cython.cimports.cpython.exc import PyErr_SetFromErrnoWithFilenameObject + +def open_file(): + p = fopen("spam.txt", "r") # The type of "p" is "FILE*", as returned by fopen(). + + if p is cython.NULL: + PyErr_SetFromErrnoWithFilenameObject(OSError, "spam.txt") + ... + + +def allocating_memory(number=10): + # Note that the type of the variable "my_array" is automatically inferred from the assignment. + my_array = cython.cast(p_double, malloc(number * cython.sizeof(double))) + if not my_array: # same as 'is NULL' above + raise MemoryError() + ... + free(my_array) diff --git a/docs/examples/userguide/language_basics/open_file.pyx b/docs/examples/userguide/language_basics/open_file.pyx index 19eac104e..ad45fc8c4 100644 --- a/docs/examples/userguide/language_basics/open_file.pyx +++ b/docs/examples/userguide/language_basics/open_file.pyx @@ -1,18 +1,18 @@ -from libc.stdio cimport FILE, fopen
-from libc.stdlib cimport malloc, free
-from cpython.exc cimport PyErr_SetFromErrnoWithFilenameObject
-
-def open_file():
- cdef FILE* p
- p = fopen("spam.txt", "r")
- if p is NULL:
- PyErr_SetFromErrnoWithFilenameObject(OSError, "spam.txt")
- ...
-
-
-def allocating_memory(number=10):
- cdef double *my_array = <double *> malloc(number * sizeof(double))
- if not my_array: # same as 'is NULL' above
- raise MemoryError()
- ...
- free(my_array)
+from libc.stdio cimport FILE, fopen +from libc.stdlib cimport malloc, free +from cpython.exc cimport PyErr_SetFromErrnoWithFilenameObject + +def open_file(): + cdef FILE* p + p = fopen("spam.txt", "r") + if p is NULL: + PyErr_SetFromErrnoWithFilenameObject(OSError, "spam.txt") + ... + + +def allocating_memory(number=10): + cdef double *my_array = <double *> malloc(number * sizeof(double)) + if not my_array: # same as 'is NULL' above + raise MemoryError() + ... + free(my_array) diff --git a/docs/examples/userguide/language_basics/optional_subclassing.py b/docs/examples/userguide/language_basics/optional_subclassing.py new file mode 100644 index 000000000..480ae100b --- /dev/null +++ b/docs/examples/userguide/language_basics/optional_subclassing.py @@ -0,0 +1,19 @@ +from __future__ import print_function + +@cython.cclass +class A: + @cython.cfunc + def foo(self): + print("A") + +@cython.cclass +class B(A): + @cython.cfunc + def foo(self, x=None): + print("B", x) + +@cython.cclass +class C(B): + @cython.ccall + def foo(self, x=True, k:cython.int = 3): + print("C", x, k) diff --git a/docs/examples/userguide/language_basics/optional_subclassing.pyx b/docs/examples/userguide/language_basics/optional_subclassing.pyx index f655cadf3..b2a3d4dec 100644 --- a/docs/examples/userguide/language_basics/optional_subclassing.pyx +++ b/docs/examples/userguide/language_basics/optional_subclassing.pyx @@ -1,13 +1,19 @@ -from __future__ import print_function
-
-cdef class A:
- cdef foo(self):
- print("A")
-
-cdef class B(A):
- cdef foo(self, x=None):
- print("B", x)
-
-cdef class C(B):
- cpdef foo(self, x=True, int k=3):
- print("C", x, k)
+from __future__ import print_function + + +cdef class A: + + cdef foo(self): + print("A") + + +cdef class B(A): + + cdef foo(self, x=None): + print("B", x) + + +cdef class C(B): + + cpdef foo(self, x=True, int k=3): + print("C", x, k) diff --git a/docs/examples/userguide/language_basics/override.py b/docs/examples/userguide/language_basics/override.py new file mode 100644 index 000000000..f9e0be83f --- /dev/null +++ b/docs/examples/userguide/language_basics/override.py @@ -0,0 +1,17 @@ +from __future__ import print_function + +@cython.cclass +class A: + @cython.cfunc + def foo(self): + print("A") + +@cython.cclass +class B(A): + @cython.ccall + def foo(self): + print("B") + +class C(B): # NOTE: no cclass decorator + def foo(self): + print("C") diff --git a/docs/examples/userguide/language_basics/override.pyx b/docs/examples/userguide/language_basics/override.pyx index 873a7ec6e..1a7ceefb7 100644 --- a/docs/examples/userguide/language_basics/override.pyx +++ b/docs/examples/userguide/language_basics/override.pyx @@ -1,13 +1,17 @@ -from __future__ import print_function
-
-cdef class A:
- cdef foo(self):
- print("A")
-
-cdef class B(A):
- cpdef foo(self):
- print("B")
-
-class C(B): # NOTE: not cdef class
- def foo(self):
- print("C")
+from __future__ import print_function + + +cdef class A: + + cdef foo(self): + print("A") + + +cdef class B(A): + + cpdef foo(self): + print("B") + +class C(B): # NOTE: not cdef class + def foo(self): + print("C") diff --git a/docs/examples/userguide/language_basics/parameter_refcount.py b/docs/examples/userguide/language_basics/parameter_refcount.py new file mode 100644 index 000000000..2b25915ba --- /dev/null +++ b/docs/examples/userguide/language_basics/parameter_refcount.py @@ -0,0 +1,23 @@ +from __future__ import print_function + +from cython.cimports.cpython.ref import PyObject + +import sys + +python_dict = {"abc": 123} +python_dict_refcount = sys.getrefcount(python_dict) + +@cython.cfunc +def owned_reference(obj: object): + refcount = sys.getrefcount(python_dict) + print('Inside owned_reference: {refcount}'.format(refcount=refcount)) + +@cython.cfunc +def borrowed_reference(obj: cython.pointer(PyObject)): + refcount = obj.ob_refcnt + print('Inside borrowed_reference: {refcount}'.format(refcount=refcount)) + +def main(): + print('Initial refcount: {refcount}'.format(refcount=python_dict_refcount)) + owned_reference(python_dict) + borrowed_reference(cython.cast(cython.pointer(PyObject), python_dict)) diff --git a/docs/examples/userguide/language_basics/parameter_refcount.pyx b/docs/examples/userguide/language_basics/parameter_refcount.pyx new file mode 100644 index 000000000..6fe3ffadd --- /dev/null +++ b/docs/examples/userguide/language_basics/parameter_refcount.pyx @@ -0,0 +1,23 @@ +from __future__ import print_function + +from cpython.ref cimport PyObject + +import sys + +python_dict = {"abc": 123} +python_dict_refcount = sys.getrefcount(python_dict) + + +cdef owned_reference(object obj): + refcount = sys.getrefcount(python_dict) + print('Inside owned_reference: {refcount}'.format(refcount=refcount)) + + +cdef borrowed_reference(PyObject * obj): + refcount = obj.ob_refcnt + print('Inside borrowed_reference: {refcount}'.format(refcount=refcount)) + + +print('Initial refcount: {refcount}'.format(refcount=python_dict_refcount)) +owned_reference(python_dict) +borrowed_reference(<PyObject *>python_dict) diff --git a/docs/examples/userguide/language_basics/struct.py b/docs/examples/userguide/language_basics/struct.py new file mode 100644 index 000000000..32b6b252a --- /dev/null +++ b/docs/examples/userguide/language_basics/struct.py @@ -0,0 +1,7 @@ +Grail = cython.struct( + age=cython.int, + volume=cython.float) + +def main(): + grail: Grail = Grail(5, 3.0) + print(grail.age, grail.volume) diff --git a/docs/examples/userguide/language_basics/struct.pyx b/docs/examples/userguide/language_basics/struct.pyx new file mode 100644 index 000000000..3ef79172b --- /dev/null +++ b/docs/examples/userguide/language_basics/struct.pyx @@ -0,0 +1,7 @@ +cdef struct Grail: + int age + float volume + +def main(): + cdef Grail grail = Grail(5, 3.0) + print(grail.age, grail.volume) diff --git a/docs/examples/userguide/language_basics/union.py b/docs/examples/userguide/language_basics/union.py new file mode 100644 index 000000000..efcda358b --- /dev/null +++ b/docs/examples/userguide/language_basics/union.py @@ -0,0 +1,9 @@ +Food = cython.union( + spam=cython.p_char, + eggs=cython.p_float) + +def main(): + arr: cython.p_float = [1.0, 2.0] + spam: Food = Food(spam='b') + eggs: Food = Food(eggs=arr) + print(spam.spam, eggs.eggs[0]) diff --git a/docs/examples/userguide/language_basics/union.pyx b/docs/examples/userguide/language_basics/union.pyx new file mode 100644 index 000000000..e05f63fcc --- /dev/null +++ b/docs/examples/userguide/language_basics/union.pyx @@ -0,0 +1,9 @@ +cdef union Food: + char *spam + float *eggs + +def main(): + cdef float *arr = [1.0, 2.0] + cdef Food spam = Food(spam='b') + cdef Food eggs = Food(eggs=arr) + print(spam.spam, eggs.eggs[0]) |