[Cython] [cython-users] malloc vs PyMem_malloc

Stefan Behnel stefan_ml at behnel.de
Sun Dec 11 17:51:59 CET 2011


mark florisson, 11.12.2011 01:02:
> On 10 December 2011 22:00, Stefan Behnel wrote:
>> mark florisson, 10.12.2011 21:44:
>>> On 10 December 2011 20:39, mark florisson wrote:
>>>> On 10 December 2011 19:16, Robert Bradshaw wrote:
>>>>> On this note, a useful pattern is
>>>>>
>>>>> try:
>>>>>     x = malloc(...)
>>>>> finally:
>>>>>     free(x)
>>>>>
>>>>> It could be nice to encapsulate this in a context manager.
>>
>> Absolutely.
>>
>>>> I think I'd prefer variable-sized arrays that would always get
>>>> deallocated on exit of the function
>>
>> Why? A context manager is much clearer
>
> That is highly subjective, I think it would be harder to read and
> introduce more code blocks and nesting.
>
>> and gives users total control over
>> the lifetime of the memory.
>
> Yes, but very often you don't need it. And if Cython would support
> declarations in blocks you'd get it for free. Supporting that
> (disregarding the difficulties of that) would also be helpful in
> identifying the scope and privatization rules in parallel blocks.
>
> The thing is that a context manager would be very Cython-specific

Not at all. It's the One Way To Do It in Python.

Stefan


More information about the cython-devel mailing list