CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cython

Python compiler that transforms Python code into C/C++ extensions for high-performance computing.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

core-language.mddocs/

Core Language Features

Cython's core language constructs, type system, decorators, and compiler directives that extend Python with static typing and C-level performance. These features enable seamless integration between Python and C code while maintaining Python's syntax and readability.

Capabilities

Type System and Declarations

Core functions for declaring variables with C types, casting between types, and introspecting type information.

def declare(t=None, value=_Unspecified, **kwds):
    """Declare a variable with a specific Cython type.
    
    Args:
        t: The type to declare (int, double, etc.)
        value: Optional initial value
        **kwds: Additional type parameters
    
    Returns:
        The declared variable with proper type
    """

def cast(t, *args, **kwargs):
    """Cast a value to a specific type.
    
    Args:
        t: Target type for casting
        *args: Values to cast
        **kwargs: Additional casting options (typecheck, etc.)
    
    Returns:
        The value cast to the target type
    """

def sizeof(arg):
    """Get the size in bytes of a type or object.
    
    Args:
        arg: Type or object to measure
    
    Returns:
        Size in bytes (returns 1 in pure Python mode)
    """

def typeof(arg):
    """Get the type name of an object.
    
    Args:
        arg: Object to inspect
    
    Returns:
        String representation of the object's type
    """

def address(arg):
    """Get the memory address of an object as a pointer.
    
    Args:
        arg: Object to get address of
    
    Returns:
        Pointer to the object
    """

Function Decorators

Decorators that modify function behavior for performance and C integration.

def locals(**arg_types):
    """Declare local variable types for a function.
    
    Args:
        **arg_types: Mapping of variable names to types
    
    Returns:
        Decorator function
    """

def cfunc(f):
    """Mark a function as a C function (cdef).
    
    Args:
        f: Function to mark as C function
    
    Returns:
        The function marked for C compilation
    """

def ccall(f):
    """Mark a function as callable from C code.
    
    Args:
        f: Function to mark as C callable
    
    Returns:
        The function marked as C callable
    """

def inline(f, *args, **kwds):
    """Inline C code or mark function for inlining.
    
    Args:
        f: Function to inline or C code string
        *args: Additional arguments for inline compilation
        **kwds: Keyword arguments for compilation
    
    Returns:
        Inlined function or compiled inline code result
    """

def compile(f):
    """Runtime compile a function with Cython.
    
    Args:
        f: Function to compile at runtime
    
    Returns:
        RuntimeCompiledFunction instance
    """

Compiler Directives

Directives that control Cython compiler behavior and optimizations.

def boundscheck(flag):
    """Control array bounds checking.
    
    Args:
        flag: Boolean to enable/disable bounds checking
    
    Returns:
        Decorator/context manager for bounds checking control
    """

def wraparound(flag):
    """Control negative array indexing.
    
    Args:
        flag: Boolean to enable/disable wraparound indexing
    
    Returns:
        Decorator/context manager for wraparound control
    """

def cdivision(flag):
    """Use C division semantics instead of Python division.
    
    Args:
        flag: Boolean to enable/disable C division
    
    Returns:
        Decorator/context manager for division control
    """

def profile(flag):
    """Enable profiling support for functions.
    
    Args:
        flag: Boolean to enable/disable profiling
    
    Returns:
        Decorator for profiling control
    """

def linetrace(flag):
    """Enable line tracing for debugging.
    
    Args:
        flag: Boolean to enable/disable line tracing
    
    Returns:
        Decorator for line tracing control
    """

def exceptval(value=None, check=True):
    """Set exception return value for C functions.
    
    Args:
        value: Value to return on exception
        check: Whether to check for exceptions
    
    Returns:
        Decorator for exception handling control
    """

GIL Control

Context managers and decorators for controlling Python's Global Interpreter Lock.

class _nogil:
    """Context manager and decorator for releasing the GIL."""
    
    def __call__(self, x):
        """Use as decorator to mark function as nogil.
        
        Args:
            x: Function to mark as nogil
        
        Returns:
            The function marked for nogil execution
        """
    
    def __enter__(self):
        """Enter nogil context."""
    
    def __exit__(self, exc_class, exc, tb):
        """Exit nogil context.
        
        Returns:
            True if no exception occurred
        """

nogil = _nogil()
gil = _nogil()  # Re-acquire GIL context manager
with_gil = _nogil()  # Decorator ensuring function runs with GIL

Memory Operations

Functions for low-level memory operations and C-style arithmetic.

def cdiv(a, b):
    """Perform C-style integer division.
    
    Args:
        a: Dividend
        b: Divisor
    
    Returns:
        Result of C-style division
    """

def cmod(a, b):
    """Perform C-style modulo operation.
    
    Args:
        a: Dividend  
        b: Divisor
    
    Returns:
        Result of C-style modulo
    """

Threading Support

Classes for thread synchronization and critical sections.

class critical_section:
    """Context manager and decorator for thread-safe critical sections."""
    
    def __init__(self, arg0, arg1=None):
        """Initialize critical section.
        
        Args:
            arg0: Lock object or function to protect
            arg1: Optional second lock object
        """
    
    def __call__(self, *args, **kwds):
        """Use as decorator."""
    
    def __enter__(self):
        """Enter critical section."""
    
    def __exit__(self, exc_class, exc, tb):
        """Exit critical section."""

class pymutex:
    """Python threading mutex implementation."""
    
    def __init__(self):
        """Initialize mutex using threading.Lock."""
    
    def acquire(self):
        """Acquire the mutex lock."""
    
    def release(self):
        """Release the mutex lock."""
    
    def __enter__(self):
        """Context manager entry."""
    
    def __exit__(self, exc_type, exc_value, traceback):
        """Context manager exit."""

pythread_type_lock = pymutex  # Alias for pymutex

Usage Examples

Basic Type Declarations

import cython

# Declare variables with specific types
x = cython.declare(cython.int, 5)
y = cython.declare(cython.double, 3.14)

# Cast between types
result = cython.cast(cython.long, x * y)

# Get type information
print(cython.typeof(result))  # Prints type name
print(cython.sizeof(cython.int))  # Prints size in bytes

Function Optimization

import cython

@cython.cfunc
@cython.locals(x=cython.int, y=cython.int)
def fast_add(x, y):
    return x + y

@cython.boundscheck(False)
@cython.wraparound(False)
def fast_array_sum(arr):
    total = 0
    for i in range(len(arr)):
        total += arr[i]
    return total

GIL Management

import cython

@cython.nogil
def cpu_intensive_task():
    # This function releases the GIL
    result = 0
    for i in range(1000000):
        result += i * i
    return result

def mixed_function():
    with cython.nogil:
        # CPU-intensive work without GIL
        result = cpu_intensive_task()
    
    # Python operations with GIL
    print(f"Result: {result}")

Install with Tessl CLI

npx tessl i tessl/pypi-cython

docs

build-system.md

c-cpp-interoperability.md

command-line-tools.md

core-language.md

debugging-profiling.md

import-system.md

index.md

ipython-integration.md

tile.json