Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'subject:Python': 0.06; 'exists.': 0.07; 'method.': 0.07; 'modified': 0.07; 'modify': 0.07; 'string': 0.09; 'ascii': 0.09; 'caller': 0.09; 'implements': 0.09; 'subject:create': 0.09; 'subject:string': 0.09; 'python': 0.11; '[2].': 0.16; 'deallocated': 0.16; 'heap': 0.16; 'resource.': 0.16; 'subject:when': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'memory': 0.22; 'otherwise,': 0.22; 'this?': 0.23; '(or': 0.24; "i've": 0.25; 'define': 0.26; 'extension': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'external': 0.29; '[1]': 0.29; '[2]': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:python': 0.33; 'call.': 0.33; 'copying': 0.34; "i'd": 0.34; 'except': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'possible': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'clear': 0.37; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'resource': 0.38; 'little': 0.38; 'delete': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'new': 0.61; 'url:3': 0.61; "you'll": 0.62; 'more': 0.64; 'url:me': 0.69; 'protocol,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=/p3gn/YUEpFQyvoc4JWPskhIpxyReSgS66/bNZ6iHG8=; b=FUZ4Rg6feQcS21JYuyGgTsDrxsDMgh74+wMV73JmfbZoR8M+PdtDYQlgDtxVB7BX8J +LRB93/5maQl0yus2yrL0ANSrMQk43BwcvJDPjOnZp4mYJ1eCbUm9bFM4mLQE6wrBatn YeL3hvriEr8DqYVj8WeEWcg/i9Cl2w099GxBa5dg9HI7cO835ugMvIdCP2jlcfgumgxo +KTUr9ETJk4Bk82UFymFf8DgGohJUndwjDLFa4gpd0UA3DL4Tx09uI/HQeHnsv9QaV3G AYLZ7BLLQzT/lHPC3I0ocCMql+CVB9evOQhn6rcP6QPkvFPR1ttpkzt34+FjlwpcIg/y S4VQ== X-Received: by 10.66.179.111 with SMTP id df15mr4889931pac.52.1399598775866; Thu, 08 May 2014 18:26:15 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <3afe7539-e3e9-43d6-ab58-1f4aa7895b46@googlegroups.com> References: <3afe7539-e3e9-43d6-ab58-1f4aa7895b46@googlegroups.com> From: Ian Kelly Date: Thu, 8 May 2014 19:25:35 -0600 Subject: Re: how to create an external string when binding C to Python? To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399598784 news.xs4all.nl 2832 [2001:888:2000:d::a6]:48592 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71144 On Thu, May 8, 2014 at 5:10 PM, 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 po= ssible 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 sho= uld not otherwise delete or modify the resource. Neither should the underly= ing 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#a07c47bf675b802c550984fa= 24511a589 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).