Registry / database / cymem

cymem

JSON →
library2.0.13pypypi✓ verified 47d ago

cymem is a Python library that provides efficient memory-management helpers for Cython. It simplifies tying C-level memory allocations (via `calloc`/`free`) to the lifecycle of Python objects, automatically freeing memory when the owning Python object is garbage collected. The core component is `cymem.Pool`, a thin wrapper around `calloc`. Currently at version 2.0.13, it maintains a regular release cadence, often aligning with new Python version support and performance enhancements like free-threading.

database
pip install cymem
Install & Compatibility
Where this runs
tested against v2.0.13 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
4/5 runs
4/5 runs
py 3.11
4/5 runs
4/5 runs
py 3.12
4/5 runs
4/5 runs
py 3.13
4/5 runs
4/5 runs
py 3.9
4/5 runs
4/5 runs
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

about
from cymem import about
from cymem import Pool

This example demonstrates how to allocate C-level memory using `cymem.Pool` within a Cython `.pyx` file. The `Pool` object handles the deallocation of all memory allocated through it when the `Pool` instance itself is garbage collected, simplifying memory management in Cython extensions.

from cymem.cymem cimport Pool from libc.stdlib cimport sizeof def main(): cdef Pool mem = Pool() cdef int* data1 = <int*>mem.alloc(10, sizeof(int)) cdef float* data2 = <float*>mem.alloc(12, sizeof(float)) # Use data1 and data2 data1[0] = 100 data2[0] = 3.14 print(f"Data1 at index 0: {data1[0]}") print(f"Data2 at index 0: {data2[0]}") # Memory is automatically freed when 'mem' (the Pool object) is garbage collected. # No explicit free() calls are needed for memory allocated via Pool. # To run this, you would typically compile it with Cython: # cython -3 --inplace your_module.pyx # Then import and call main() from Python: # import your_module # your_module.main()
Debug
Known issues
gotchaWhen `cymem.Pool` is used with CPython 3.13+ free-threaded builds (PEP 703), operations like `alloc()`, `free()`, and `realloc()` are thread-safe. However, reading the internal state (e.g., `addresses` dict) without explicit critical sections is not thread-safe. Users are also responsible for synchronizing access to the *contents* of the allocated memory across threads.
fix
Ensure critical sections are used for internal state access, and apply fine-grained locks or other synchronization primitives when sharing access to memory contents across threads. Do not rely on coarse-grained locks on the `Pool` instance itself for memory content synchronization.
affects: >=2.0.12
breakingIncompatibility between `cymem`'s compiled C++ files and the installed Cython version can lead to errors like `ValueError: cymem.cymem.Pool has the wrong size, try recompiling.` This often happens when Cython's internal structures change.
fix
Ensure your installed Cython version is compatible with the `cymem` version. If encountering this error, try recompiling your Cython project (if you're compiling `cymem` from source or an older wheel) or updating `cymem` to a version with wheels pre-built against a compatible Cython release. It's recommended to update `pip`, `setuptools`, and `wheel` before installing to ensure the latest compatible binary wheels are used.
affects: <2.0.5 (older versions were more susceptible, but can still occur with mismatched builds).
gotcha`cymem.Pool` simplifies memory deallocation by tying it to the Python object's lifecycle. However, users must still ensure that no raw C pointers obtained from the pool outlive the Python object that owns the `Pool` instance. If the `Pool` object is garbage collected, all its managed memory is freed, invalidating any lingering pointers.
fix
Carefully manage the lifetime of your `cdef` Python objects that hold `cymem.Pool` instances. Ensure that any C pointers derived from the pool are only accessed while the owning Python object and its associated `Pool` are still alive and in scope.
affects: All versions
gotchaAttempting to use Cython-specific `cimport` syntax (e.g., `from cymem.cymem cimport Pool`) directly in a `.py` file executed by a standard Python interpreter will result in a `SyntaxError`. Cython's `cimport` is exclusively for defining types and functions within Cython source files (`.pyx`) that will be compiled, not for Python scripts.
fix
If you are developing a Cython extension, ensure your source file is named `.pyx` and is compiled with Cython. If you intend to use `cymem` from Python, you typically import a *compiled* module that uses `cymem` internally, or `cymem`'s public Python API if available, using standard Python `import` statements. Do not use `cimport` in `.py` files.
affects: All versions
breakingAttempting to use Cython's `cimport` statement directly in a standard Python (`.py`) file will result in a `SyntaxError`. `cimport` is a Cython-specific keyword valid only in Cython (`.pyx`) source files, which must be compiled into a Python-loadable extension before they can be imported into Python.
fix
Use `cimport` exclusively within Cython (`.pyx`) files. To use `cymem.Pool` from a Python (`.py`) file, import it like a regular Python object (e.g., `from cymem.cymem import Pool`) after the `cymem` library (or your own Cython code that `cimports` `cymem`) has been properly compiled and installed.
affects: All versions
Upgrade
Version history
2.0.13latest on PyPI
Audit
Dependencies
CythonrequiredRequired for compiling and using `cymem` in Cython projects, as `cymem` itself is a Cython library that interacts with C-level memory.
Agent activity
81 hits · last 30 days
node
12
seranking-bot
4
ahrefsbot
3
Amazon
1
amazonbot
1
Resources