From 39d9e0fc4304d2497b946580064fb658d02db1b6 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sat, 12 Oct 2013 10:10:22 +0200 Subject: [PATCH] improve error handling in malloc tutorial example --- docs/src/tutorial/memory_allocation.rst | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/src/tutorial/memory_allocation.rst b/docs/src/tutorial/memory_allocation.rst index c0df674..c995d0e 100644 --- a/docs/src/tutorial/memory_allocation.rst +++ b/docs/src/tutorial/memory_allocation.rst @@ -73,24 +73,28 @@ to a Python object to leverage the Python runtime's memory management, e.g.:: cdef class SomeMemory: - + cdef double* data - - def __init__(self, number): + + def __cinit__(self, number): # allocate some memory (filled with random data) - self.data = malloc(number * sizeof(double)) - if self.data == NULL: + self.data = PyMem_Malloc(number * sizeof(double)) + if not self.data: raise MemoryError() - + def resize(self, new_number): # Allocates new_number * sizeof(double) bytes, # preserving the contents and making a best-effort to # re-use the original data location. - self.data = realloc(self.data, new_number * sizeof(double)) - + mem = PyMem_Realloc(self.data, new_number * sizeof(double)) + if not mem: + raise MemoryError() + # Only overwrite the pointer if the memory was really reallocated. + # On error (mem is NULL), the originally memory has not been freed. + self.data = mem + def __dealloc__(self, number): - if self.data != NULL: - free(self.data) + PyMem_Free(self.data) # no-op if self.data is NULL It should be noted that Cython has special support for (multi-dimensional) arrays of simple types via NumPy and memory views which are more full featured -- 2.7.4