Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71132 > unrolled thread

how to create an external string when binding C to Python?

Started bySimon <simonhf@gmail.com>
First post2014-05-08 16:10 -0700
Last post2014-05-08 19:25 -0600
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  how to create an external string when binding C to Python? Simon <simonhf@gmail.com> - 2014-05-08 16:10 -0700
    Re: how to create an external string when binding C to Python? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-05-09 00:46 +0100
    Re: how to create an external string when binding C to Python? Ian Kelly <ian.g.kelly@gmail.com> - 2014-05-08 19:25 -0600

#71132 — how to create an external string when binding C to Python?

FromSimon <simonhf@gmail.com>
Date2014-05-08 16:10 -0700
Subjecthow to create an external string when binding C to Python?
Message-ID<3afe7539-e3e9-43d6-ab58-1f4aa7895b46@googlegroups.com>
I'd like to make a C memory buffer available inside Python via the Python C/API without copying that memory into Python. How to do this? I've read [1] but it's not clear that this functionality exists. In javascript it's possible using String::NewExternal() [2].

"Creates a new external string using the ASCII data defined in the given resource.

When the external string is no longer live on V8's heap the resource will be disposed by calling its Dispose method. The caller of this function should not otherwise delete or modify the resource. Neither should the underlying buffer be deallocated or modified except through the destructor of the external string resource."

[1] https://docs.python.org/2/c-api/buffer.html
[2] http://izs.me/v8-docs/classv8_1_1String.html#a07c47bf675b802c550984fa24511a589

[toc] | [next] | [standalone]


#71136

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-05-09 00:46 +0100
Message-ID<mailman.9802.1399592834.18130.python-list@python.org>
In reply to#71132
On 09/05/2014 00:10, Simon wrote:
> I'd like to make a C memory buffer available inside Python via the Python C/API without copying that memory into Python. How to do this? I've read [1] but it's not clear that this functionality exists. In javascript it's possible using String::NewExternal() [2].
>
> "Creates a new external string using the ASCII data defined in the given resource.
>
> When the external string is no longer live on V8's heap the resource will be disposed by calling its Dispose method. The caller of this function should not otherwise delete or modify the resource. Neither should the underlying buffer be deallocated or modified except through the destructor of the external string resource."
>
> [1] https://docs.python.org/2/c-api/buffer.html
> [2] http://izs.me/v8-docs/classv8_1_1String.html#a07c47bf675b802c550984fa24511a589
>

Does this help 
http://jakevdp.github.io/blog/2014/05/05/introduction-to-the-python-buffer-protocol/ 
?

Also note that the Python 3 buffer protocol differs from that of Python 
2, so make sure that you check out PEP 3118 that's referenced in the 
above link.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#71144

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-05-08 19:25 -0600
Message-ID<mailman.9807.1399598784.18130.python-list@python.org>
In reply to#71132
On Thu, May 8, 2014 at 5:10 PM, Simon <simonhf@gmail.com> wrote:
> I'd like to make a C memory buffer available inside Python via the Python C/API without copying that memory into Python. How to do this? I've read [1] but it's not clear that this functionality exists. In javascript it's possible using String::NewExternal() [2].
>
> "Creates a new external string using the ASCII data defined in the given resource.
>
> When the external string is no longer live on V8's heap the resource will be disposed by calling its Dispose method. The caller of this function should not otherwise delete or modify the resource. Neither should the underlying buffer be deallocated or modified except through the destructor of the external string resource."
>
> [1] https://docs.python.org/2/c-api/buffer.html
> [2] http://izs.me/v8-docs/classv8_1_1String.html#a07c47bf675b802c550984fa24511a589

In 3.3 or greater you can create a memoryview directly from a char*
using this this call:
https://docs.python.org/3/c-api/memoryview.html#c.PyMemoryView_FromMemory

Otherwise, you can do it using the buffer protocol but it takes a
little more work than just a function call.  You'll need to define in
C an extension type that will wrap your C buffer and that implements
the buffer protocol, and then objects of that type can be used as the
basis for memoryviews (or other buffer consumers).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web