![](https://secure.gravatar.com/avatar/e608007d31f82b93631e21d5921aef67.jpg?s=120&d=mm&r=g)
I find myself sometimes, when writing IO Code from C, wanting to pass memory that I have allocated internally and filled with data, to Python without copying it into a string object. To this end, I have locally created a (2.x) method called PyBuffer_FromMemoryAndDestructor(), which is the same as PyBuffer_FromMemory() except that it will call a provided destructor function with an optional arg, to release the memory address given, when no longer in use. First of all, I'd futilely like to suggest this change for 2.x. The existing PyBuffer_FromMemory() provides no lifetime management. Second, the ByBuffer object doesn't support the new Py_buffer interface, so you can't really use this then, like putting a memoryview around it. This is a fixable bug, otoh. Thirdly, in py3k I think the situation is different. There you would (probably, correct me if I'm wrong) emulate the old PyBuffer_FromMemory with a combination of the new PyBuffer_FromContiguous and a PyMemoryView_FromBuffer(). But this also does not allow any lifetime magement of the external memory. So, for py3k, I'd actually like to extend the Memoryview object, and provide something like PyMemoryView_FromExternal() that takes an optional pointer to a "void destructor(void *arg, void *ptr)) and an (void *arg), to be called when the buffer is released. K
![](https://secure.gravatar.com/avatar/db5f70d2f2520ef725839f046bdc32fb.jpg?s=120&d=mm&r=g)
On Wed, 27 Oct 2010 12:02:11 +0800 Kristján Valur Jónsson <kristjan@ccpgames.com> wrote:
First of all, I'd futilely like to suggest this change for 2.x. The existing PyBuffer_FromMemory() provides no lifetime management.
By "futilely" you mean you know it won't be accepted, since 2.x is in bug fixes-only mode? :)
So, for py3k, I'd actually like to extend the Memoryview object, and provide something like PyMemoryView_FromExternal() that takes an optional pointer to a "void destructor(void *arg, void *ptr)) and an (void *arg), to be called when the buffer is released.
Sounds reasonable to me. Regards Antoine.
![](https://secure.gravatar.com/avatar/e608007d31f82b93631e21d5921aef67.jpg?s=120&d=mm&r=g)
Looking better at this, I don't think it is such a The MemoryView is designed to be a wrapper around the Py_buffer interface. Slicing it, for example, creates a new memoryview based on the same underlying object. Having the MemoryView get its data from two different places would be very hacky, I think. What is needed, I think, is a basic ExternalMemory C api object with a buffer interface that does what I describe. This exists in 2.7 (the BufferObject) but with the shortcomings I mentioned. But as far as I know, there is no similar object in py3k. K -----Original Message----- From: python-ideas-bounces+kristjan=ccpgames.com@python.org [mailto:python-ideas-bounces+kristjan=ccpgames.com@python.org] On Behalf Of Antoine Pitrou Sent: Wednesday, October 27, 2010 18:48 To: python-ideas@python.org Subject: Re: [Python-ideas] ExternalMemory
So, for py3k, I'd actually like to extend the Memoryview object, and provide something like PyMemoryView_FromExternal() that takes an optional pointer to a "void destructor(void *arg, void *ptr)) and an (void *arg), to be called when the buffer is released.
Sounds reasonable to me. Regards Antoine. _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
participants (2)
-
Antoine Pitrou
-
Kristján Valur Jónsson