[Python-Dev] Context manager lifetime rules Was: Re: Adding bytes.frombuffer() constructor to PEP 467

INADA Naoki songofacandy at gmail.com
Fri Jan 6 07:27:30 EST 2017


>> > with memoryview(x) as m:
>> >      b = bytes(m)
>> >      # or b = m.tobytes()
>>
>> Thinking more about this, and after looking at my own code in asyncpg
>> and uvloop, I'm now in favor of adding bytes.frombuffer() with the
>> proposed signature: ``bytes.frombuffer(byteslike, length=-1, offset=0)``
>>
>> Inada-san is right, the memoryview should be explicitly released, but
>> few do that. Instead, a lot of people simply rely on CPython refcounting
>> semantics, which will cause the temporary memoryview be GCed asap.  That
>> won't work so flawlessly in PyPy and will cause hard to understand bugs.
>
> Did you noticed, that we're stirring in the same kind of soup lately?
> (apart from my person, who's not that deep in the details)
>
> Given above code snippet, my issue is caused from "m" surviving *too* long
> after the context manager block ended, resulting in a delayed release of the
> memoryview, which in turn interferes with subsequent code.

In case of memoryview, it detaches x.  Like closed file object, it
doesn't hold any resource
other than memory used for the object itself.

>
> Simple minded, as I am, I would expect, that "m" is actively removed from
> local context as soon as the context manager block ends. I would even argue,
> that this is part of the contract, that the context manager offers here.

In case of general context manager, there are some use cases which needs "m"
after context ended.
For example, https://docs.python.org/3.5/library/unittest.html#unittest.TestCase.assertRaises


More information about the Python-Dev mailing list