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


Groups > comp.lang.python > #67691

Re: how to get bytes from bytearray without copying

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'python,': 0.02; 'from:charset:iso-8859-2': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'api': 0.11; 'python': 0.11; 'def': 0.12; 'bullet': 0.16; 'ptr': 0.16; 'read-only.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'proxy': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'code': 0.31; 'assert': 0.31; 'ctypes': 0.31; 'class': 0.32; 'url:python': 0.33; 'subject:from': 0.34; 'could': 0.34; 'object,': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'ian': 0.60; 'worry': 0.60; 'url:3': 0.61; 'real': 0.63; 'charset:iso-8859-2': 0.64; 'skip:r 30': 0.69; 'apart': 0.72; 'subject:get': 0.81; 'bite': 0.84; 'doable': 0.84; 'received:hr': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Juraj Ivančić <juraj.ivancic@gmail.com>
Subject Re: how to get bytes from bytearray without copying
Date Tue, 04 Mar 2014 16:23:32 +0100
References <lf0h3g$m7r$1@ger.gmane.org> <CALwzidncPvJS_aPJENre2HfOvV_PoY8mSV4PGA+i8FcwYPFc=A@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-2; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 78-0-246-204.adsl.net.t-com.hr
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0
In-Reply-To <CALwzidncPvJS_aPJENre2HfOvV_PoY8mSV4PGA+i8FcwYPFc=A@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7722.1393946633.18130.python-list@python.org> (permalink)
Lines 29
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1393946633 news.xs4all.nl 2859 [2001:888:2000:d::a6]:53319
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:67691

Show key headers only | View raw


On 3.3.2014. 2:27, Ian Kelly wrote:

> Python 3.3 has a C API function to create a memoryview for a char*,
> that can be made read-only.
>
>      http://docs.python.org/3/c-api/memoryview.html#PyMemoryView_FromMemory
>
> I don't see a way to do what you want in pure Python, apart from
> perhaps writing an elaborate proxy class that would just be a poor
> man's memoryview.  Or you could bite the bullet and copy everything
> once at the start to create a bytes object, and then never have to
> worry about it again.

Just for reference, it is doable in pure Python, with ctypes help:

pydll = ctypes.cdll.LoadLibrary("python{}{}".format(
     sys.version_info.major, sys.version_info.minor))

def ro_memoryview_from_bytearray(buffer):
     assert isinstance(buffer, bytearray)
     ptr = ctypes.c_char_p(pydll.PyByteArray_AsString(
         ctypes.py_object(buffer)))
     mv_id = pydll.PyMemoryView_FromMemory(ptr, len(buffer), 0)
     return ctypes.cast(mv_id, py_object).value

Note that this is just the jist, in real code I added safeguards to 
prevent misuse of the (temporary) memoryview.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: how to get bytes from bytearray without copying Juraj Ivančić <juraj.ivancic@gmail.com> - 2014-03-04 16:23 +0100

csiph-web