Glossary¶
- Extension type¶
“Extension type” can refer to either a Cython class defined with
cdefclass
or@cclass
,or more generally to any Python type that is ultimately implemented as anative C struct (including the built-in types likeint ordict).- Dynamic allocation or Heap allocation¶
A C variable allocated with
malloc
(in C) ornew
(in C++) isallocated dynamically/heap allocated.Its lifetime is until the user deletes it explicitly (withfree
in C ordel
in C++).This can happen in a different function than the allocation.- Global Interpreter Lock or GIL¶
A lock inside the Python interpreter to ensure that only one Python thread is run at once.This lock is purely to ensure that race conditions do not corrupt internal Python state.Python objects cannot be manipulated unless the GIL is held.It is most relevant to Cython when writing code that should be run in parallel. If you arenot aiming to write parallel code then there is usually no benefit to releasing the GIL inCython. You should not use the GIL as a general locking mechanism in your code since manyoperations on Python objects can lead to it being released and to control being passed toanother thread. Also see theCPython project’s glossary entry.
- pointer¶
Apointer is a variable that stores the address of another variable(i.e. direct address of the memory location). They allow fordynamic memory allocation and deallocation. They can be used to builddynamic data structures.Read more.
- Python object¶
When using Python, the contents of every variable is a Python object(including Cython extension types). Key features of Python objects are thatthey are passedby reference and that their lifetime ismanaged automaticallyso that they are destroyed when no more references exist to them.In Cython, they are distinct from C types, which are passedby value and whoselifetime is managed depending on whether they are allocated on the stack or heap.To explicitly declare a Python object variable in Cython use
cdefobjectabc
.Internally in C, they are referred to asPyObject*
.- Stack allocation¶
A C variable declared within a function as
cdefSomeTypea
is said to be allocated on the stack.It exists for the duration of the function only.- Typed memoryview¶
A useful Cython type for getting quick access to blocks of memory.A memoryview alone does not actually own any memory.However, it can be initialized with a Python object that supports thebuffer protocol (typically “array” types, for example a Numpy array).The memoryview keeps a reference to that Python object aliveand provides quick access to the memory without needing to gothrough the Python API of the object and its
__getitem__()
/__setitem__()
methods.For more information, seeTyped Memoryviews.